def onconnect(info): global gCanSend display_status("Connecting...\n=> MS Azure") print("- [onconnect] => status:" + str(info.getStatusCode())) if info.getStatusCode() == 0: if iotc.isConnected(): gCanSend = True
def onconnect(info): global gCanSend global iotc print("- [onconnect] => status:" + str(info.getStatusCode())) if info.getStatusCode() == 0: if iotc.isConnected(): gCanSend = True
def onconnect(info): global gCanSend print("- [onconnect] => status:" + str(info.getStatusCode())) if info.getStatusCode() == 0: if iotc.isConnected(): gCanSend = True rospy.init_node('botctrl', anonymous=True) rospy.Subscriber('cmd_vel', Twist, callback) rospy.Subscriber('stt', String, sttCallback) # spin() simply keeps python from exiting until this node is stopped rospy.spin()
def onconnect(info): global gCanSend #print("- [onconnect] => status:" + str(info.getStatusCode())) mslog.info('[onconnect] => status: ' + str(info.getStatusCode())) if info.getStatusCode() == 0: if iotc.isConnected(): mslog.info('Connected') gCanSend = True else: mslog.info('Not connected?') else: print('Disconnected due to status code (' + str(info.getStatusCode()) + ')') mslog.error('Disconnected due to status code (' + str(info.getStatusCode()) + ')')
# Start by Connecting then Sending iotc.connect() (airtemp, airpressure, airhumidity) = readBME280All(addr=DEVICE) (r, g, b, c, lux, color_temp) = readTCSAll() iotc.sendTelemetry("{ \ \"airtemperature\": " + str(airtemp) + ", \ \"airpressure\": " + str(airpressure) + ", \ \"airhumidity\": " + str(airhumidity) + ", \ \"lux\": " + str(lux) + ", \ \"colortemp\": " + str(color_temp) + ", \ \"green\": " + str(g) + ", \ \"blue\": " + str(b) + ", \ \"clear\": " + str(c) + ", \ \"red\": " + str(r) + "}") starttime = time.time() while iotc.isConnected(): time.sleep(5) iotc.doNext() # do the async work needed to be done for MQTT elapsedtime = time.time() - starttime if elapsedtime > 66: print("Sending telemetry..") ## Collect Telemetry Data (airtemp, airpressure, airhumidity) = readBME280All(addr=DEVICE) (r, g, b, c, lux, color_temp) = readTCSAll() elapsedtime = 0 starttime = time.time() iotc.sendTelemetry("{ \ \"airtemperature\": " + str(airtemp) + ", \ \"airpressure\": " + str(airpressure) + ", \ \"airhumidity\": " + str(airhumidity) + ", \ \"lux\": " + str(lux) + ", \
iotc.connect() # loop over frames from the video file stream while True: data = ser.readline()[:-2] #the last bit gets rid of the new-line chars if data: values = data.decode().split(',') airQuality = values[1].split('=')[1] print(values) # If IoT Central is Connected, then send our Telemetry. if iotc.isConnected(): iotc.doNext() # do the async work needed to be done for MQTT if gCanSend == True: if gCounter % 5 == 0: gCounter = 0 print("Sending telemetry..") iotc.sendTelemetry("{ \ \"temp\": " + str(randint(20, 45)) + ", \ \"airQuality\": " + str(airQuality) + ", \ \"zombieDetected\": " + str(zombieDetected) + ", \ \"accelerometerX\": " + str(randint(2, 15)) + ", \ \"accelerometerY\": " + str(randint(3, 9)) + ", \ \"accelerometerZ\": " + str(randint(1, 4)) + "}") gCounter += 1
def main(): global gCounter global ledState global iotc ## Start Configuration ## Azure IoT Central #region # Set Callback functions iotc.on("ConnectionStatus", onconnect) iotc.on("MessageSent", onmessagesent) iotc.on("Command", oncommand) iotc.on("SettingsUpdated", onsettingsupdated) # Set logging iotc.setLogLevel(IOTLogLevel.IOTC_LOGGING_API_ONLY) #endregion ## Sensor #region try: # Use the BCM pin numbering GPIO.setmode(GPIO.BCM) # Set ledPin to be an output pin GPIO.setup(ledPin, GPIO.OUT) #endregion ## Start Post-Configuration iotc.connect() # Send the device property and ledstate once at the start of the program, so telemetry is shown in IoT Central. if iotc.isConnected() and gCanSend == True: sendDeviceProperty() sendLedState() while iotc.isConnected(): iotc.doNext() # do the async work needed to be done for MQTT if gCanSend == True: # Do this when gCounter is 20. if gCounter % 20 == 0: gCounter = 0 print("Sending telemetry..") # The key in the json that will be sent to IoT Central must be equal to the Name in IoT Central!! # eg. "Temperature" in the json must equal (case sensitive) the Name field "Temperature" in IoT Central # if you want real temp data use function: readTempSensor() iotc.sendTelemetry("{ \ \"Temperature\": " + str(randint(0, 25)) + ", \ \"Pressure\": " + str(randint(850, 1150)) + ", \ \"Humidity\": " + str(randint(0, 100)) + ", \ \"CPUTemperature\": " + str(getCPUtemperature()) + ", \ }") gCounter += 1 except Exception as ex: print("Exception: " + str(ex)) finally: GPIO.cleanup()