Example #1
0
        humidity = 100
    elif humidity < 0:
        humidity = 0

    return temperatureF / 100.0, pressure / 100.0, humidity


## End BME

deviceId = "17f83c9b-e31c-4e11-8b9c-1b89ee99e714"
scopeId = "0ne00062CC3"
deviceKey = "V4sxmvey5dA5PBNqtCatp5uUZn/JwppsfIfZXdusocc="

iotc = iotc.Device(scopeId, deviceKey, deviceId,
                   IOTConnectType.IOTC_CONNECT_SYMM_KEY)
iotc.setLogLevel(IOTLogLevel.IOTC_LOGGING_API_ONLY)

gCanSend = False
gCounter = 0


def onconnect(info):
    global gCanSend
    print("- [onconnect] => status:" + str(info.getStatusCode()))
    if info.getStatusCode() == 0:
        if iotc.isConnected():
            gCanSend = True


def onmessagesent(info):
    print("\t- [onmessagesent] => " + str(info.getPayload()))
Example #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()