Ejemplo n.º 1
0
def onmessagesent(info):
    print("\t- [onmessagesent] => " + str(info.getPayload()))


def oncommand(info):
    print("- [oncommand] => " + info.getTag() + " => " +
          str(info.getPayload()))


def onsettingsupdated(info):
    print("- [onsettingsupdated] => " + info.getTag() + " => " +
          info.getPayload())


iotc.on("ConnectionStatus", onconnect)
iotc.on("MessageSent", onmessagesent)
iotc.on("Command", oncommand)
iotc.on("SettingsUpdated", onsettingsupdated)

# 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) + ", \
Ejemplo n.º 2
0
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()