Example #1
0
def iothub_client_sample_run():
    try:
        client = iothub_client_init()

        if client.protocol == IoTHubTransportProvider.MQTT:
            print("IoTHubClient is reporting state")
            reported_state = "{\"newState\":\"standBy\"}"
            client.send_reported_state(reported_state, len(reported_state),
                                       send_reported_state_callback,
                                       SEND_REPORTED_STATE_CONTEXT)

        if not config.SIMULATED_DATA:
            sensor = SenseHat()
            print 'PI SenseHat Object Created'
        else:
            # sensor = BME280(address = config.I2C_ADDRESS)
            sensor = BME280SensorSimulator()

        telemetry.send_telemetry_data(parse_iot_hub_name(), EVENT_SUCCESS,
                                      "IoT hub connection is established")
        while True:
            global MESSAGE_COUNT, MESSAGE_SWITCH
            if MESSAGE_SWITCH:
                # send a few messages every minute
                print("IoTHubClient sending %d messages" % MESSAGE_COUNT)
                temperature = sensor.get_temperature()
                humidity = sensor.get_humidity()
                msg_txt_formatted = MSG_TXT % (temperature, humidity)
                print(msg_txt_formatted)
                message = IoTHubMessage(msg_txt_formatted)
                # optional: assign ids
                message.message_id = "message_%d" % MESSAGE_COUNT
                message.correlation_id = "correlation_%d" % MESSAGE_COUNT
                # optional: assign properties
                prop_map = message.properties()
                prop_map.add(
                    "temperatureAlert",
                    "true" if temperature > TEMPERATURE_ALERT else "false")

                client.send_event_async(message, send_confirmation_callback,
                                        MESSAGE_COUNT)
                print(
                    "IoTHubClient.send_event_async accepted message [%d] for transmission to IoT Hub."
                    % MESSAGE_COUNT)

                status = client.get_send_status()
                print("Send status: %s" % status)
                MESSAGE_COUNT += 1
            time.sleep(config.MESSAGE_TIMESPAN / 1000.0)

    except IoTHubError as iothub_error:
        print("Unexpected error %s from IoTHub" % iothub_error)
        telemetry.send_telemetry_data(
            parse_iot_hub_name(), EVENT_FAILED,
            "Unexpected error %s from IoTHub" % iothub_error)
        return
    except KeyboardInterrupt:
        print("IoTHubClient sample stopped")

    print_last_message_time(client)
Example #2
0
def iothub_client_sample_run():
    try:
        client = iothub_client_init()
        client.send_reported_state(TWIN_PAYLOAD, len(TWIN_PAYLOAD), send_reported_state_callback, SEND_REPORTED_STATE_CONTEXT)
        if client.protocol == IoTHubTransportProvider.MQTT:
            #update_reboottime(client)
            reported_state = "{\"newState\":\"standBy\"}"
            client.send_reported_state(reported_state, len(reported_state), send_reported_state_callback, SEND_REPORTED_STATE_CONTEXT)
            print ( "客户端准备就绪 ")
        if not config.SIMULATED_DATA:
            sensor = BME280(address = config.I2C_ADDRESS)
        else:
            sensor = BME280SensorSimulator()
        while True:
            global MESSAGE_COUNT,MESSAGE_SWITCH
            if MESSAGE_SWITCH:
                # send a few messages every minute
                print ( "IoTHubClient sending %d messages" % MESSAGE_COUNT )
                cpu_percent= psutil.cpu_percent()
                memory = psutil.virtual_memory()
                memorypercent=float(memory.used)/float(memory.total)
                temperature = sensor.read_temperature()
                humidity = sensor.read_humidity()
                msg_txt_formatted = MSG_TXT % (
                    temperature,
                    humidity,                   
                    cpu_percent,
                    memorypercent)
                print (msg_txt_formatted)
                message = IoTHubMessage(msg_txt_formatted)
                # optional: assign ids
                message.message_id = "message_%d" % MESSAGE_COUNT
                message.correlation_id = "correlation_%d" % MESSAGE_COUNT
                # optional: assign properties
                prop_map = message.properties()
                prop_map.add("temperatureAlert", "true" if temperature > TEMPERATURE_ALERT else "false")
                client.send_event_async(message, send_confirmation_callback, MESSAGE_COUNT)
                print ( "IoTHubClient.send_event_async accepted message [%d] for transmission to IoT Hub." % MESSAGE_COUNT )
                status = client.get_send_status()
                print ( "Send status: %s" % status )
                MESSAGE_COUNT += 1
                print ( "" )
            time.sleep(config.MESSAGE_TIMESPAN / 1000.0)
            temperature = sensor.read_temperature()
            if temperature > TEMPERATURE_EMERGENCY :
                if not FAN_SWITCH:
                    fan_on()#测试代码
                    sendTempAlarm(temperature)
            if temperature < TEMPERATURE_EMERGENCY :
                if FAN_SWITCH:
                    fan_off()#测试代码
        
    except IoTHubError as iothub_error:
        print ( "Unexpected error %s from IoTHub" % iothub_error )
        return
    except KeyboardInterrupt:
        #reported_state = "{\"newState\":\"offline\"}"
        #client.send_reported_state(reported_state, len(reported_state), send_reported_state_callback, SEND_REPORTED_STATE_CONTEXT)
        print ( "客户端被终止工作" )

    print_last_message_time(client)
Example #3
0
def iothub_client_sample_run():
    try:
        client = iothub_client_init()

        if client.protocol == IoTHubTransportProvider.MQTT:
            print ( "IoTHubClient is reporting state" )
            reported_state = "{\"newState\":\"standBy\"}"
            client.send_reported_state(reported_state, len(reported_state), send_reported_state_callback, SEND_REPORTED_STATE_CONTEXT)

        if not config.SIMULATED_DATA:
		
            sensor = BME280(address = config.I2C_ADDRESS)
        else:
            sensor = BME280SensorSimulator()

        telemetry.send_telemetry_data(parse_iot_hub_name(), EVENT_SUCCESS, "IoT hub connection is established")
        while True:
            global MESSAGE_COUNT,MESSAGE_SWITCH
            if MESSAGE_SWITCH:
                # send a few messages every minute
                print ( "IoTHubClient sending %d messages" % MESSAGE_COUNT )
		#wlodek start-------------------------
                humidity, temperature = Adafruit_DHT.read_retry(11, 4)
		#temperature = sensor.read_temperature()
                #humidity = sensor.read_humidity()
				#wlodek s**t--------------------------
                soundPin = 7
                GPIO.setup(soundPin, GPIO.IN)
                
                if GPIO.input(soundPin) == GPIO.LOW:
                    soundsensor=0
                else:
                    soundsensor=1
		#------------------------------------
                soundsensorAmplitude = mcp.read_adc(0)	
                #----------------------------------    
                msg_txt_formatted = MSG_TXT % (
                    temperature,
                    humidity)
                print (msg_txt_formatted)
                msg_txtFormat = MSG_TXT_TWO % (
                    soundsensor)
		#---------------------------------------
                msg_txtFormat_three = MSG_TXT_THREE % (
                    soundsensorAmplitude)
                #----------------------------------------	
                message = IoTHubMessage(msg_txt_formatted)
                messageTwo = IoTHubMessage(msg_txtFormat)
		#--------------------------------------------
                messageThree = IoTHubMessage(msg_txtFormat_three)
		#---------------------------------------
                # optional: assign ids
                message.message_id = "message_%d" % MESSAGE_COUNT
                message.correlation_id = "correlation_%d" % MESSAGE_COUNT
                messageTwo.message_id = "message_%d" % MESSAGE_COUNT
                messageTwo.correlation_id = "correlation_%d" % MESSAGE_COUNT
		#--------------------------------------------------
                messageThree.message_id = "message_%d" % MESSAGE_COUNT
                messageThree.correlation_id = "correlation_%d" % MESSAGE_COUNT
		#---------------------------------------------------------
				
                # optional: assign properties
                prop_map = message.properties()
                prop_map.add("temperatureAlert", "true" if temperature > TEMPERATURE_ALERT else "false")

                client.send_event_async(message, send_confirmation_callback, MESSAGE_COUNT)
                print ( "IoTHubClient.send_event_async accepted message [%d] for transmission to IoT Hub." % MESSAGE_COUNT )

                client.send_event_async(messageTwo, send_confirmation_callback, MESSAGE_COUNT)
                print ( "IoTHubClient.send_event_async accepted message [%d] for transmission to IoT Hub." % MESSAGE_COUNT )
                
                status = client.get_send_status()
                print ( "Send status: %s" % status )
                MESSAGE_COUNT += 1
            time.sleep(config.MESSAGE_TIMESPAN / 1000.0)

    except IoTHubError as iothub_error:
        print ( "Unexpected error %s from IoTHub" % iothub_error )
        telemetry.send_telemetry_data(parse_iot_hub_name(), EVENT_FAILED, "Unexpected error %s from IoTHub" % iothub_error)
        return
    except KeyboardInterrupt:
        print ( "IoTHubClient sample stopped" )

    print_last_message_time(client)