Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. import os
  2. import glob
  3. import random
  4. import time
  5. import sys
  6. import paho.mqtt.client as mqtt
  7.  
  8. # Using the Python Device SDK for IoT Hub:
  9. #   https://github.com/Azure/azure-iot-sdk-python
  10. # The sample connects to a device-specific MQTT endpoint on your IoT Hub.
  11. import iothub_client
  12. # pylint: disable=E0611
  13. from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult
  14. from iothub_client import IoTHubMessage, IoTHubMessageDispositionResult, IoTHubError, DeviceMethodReturnValue
  15.  
  16. # The device connection string to authenticate the device with your IoT hub.
  17. # Using the Azure CLI:
  18. # az iot hub device-identity show-connection-string --hub-name {YourIoTHubName} --device-id MyNodeDevice --output table
  19. CONNECTION_STRING = "HostName=tonystestiothub.azure-devices.net;DeviceId=SYO_Raspi;SharedAccessKey=if75jIezBHn7fqyjtIPrgGHH5NcHJvmS7Hx4BaV49Ws="
  20. # Using the MQTT protocol.
  21. PROTOCOL = IoTHubTransportProvider.MQTT
  22. MESSAGE_TIMEOUT = 10000
  23.  
  24. os.system('modprobe w1-gpio')
  25. os.system('modprobe w1-therm')
  26.  
  27. base_dir = '/sys/bus/w1/devices/'
  28. device_folder = glob.glob(base_dir + '28*')[0]
  29. device_file = device_folder + '/w1_slave'
  30.  
  31. def read_temp_raw():
  32.     f = open(device_file, 'r')
  33.     lines = f.readlines()
  34.     f.close()
  35.     return lines
  36.  
  37. def read_temp():
  38.     lines = read_temp_raw()
  39.     while lines[0].strip()[-3:] != 'YES':
  40.         time.sleep(0.2)
  41.         lines = read_temp_raw()
  42.     equals_pos = lines[1].find('t=')
  43.     if equals_pos != -1:
  44.         temp_string = lines[1][equals_pos+2:]
  45.         temp_c = float(temp_string) / 1000.0
  46.         temp_f = temp_c * 9.0 / 5.0 + 32.0
  47.         return temp_c
  48.    
  49. def on_publish(client,userdata,result):            
  50.     print("data published \n")
  51.  
  52.  
  53. def send_confirmation_callback(message, result, user_context):
  54.     print ( "IoT Hub responded to message with status: %s" % (result) )
  55.  
  56. client= mqtt.Client("Tony's publisher")                          
  57. client.on_publish = on_publish
  58. client.connect("193.167.167.59",1883)
  59.  
  60. iotHubClient = IoTHubClient(CONNECTION_STRING, PROTOCOL)
  61.    
  62. while True:
  63.     print(read_temp())
  64.     # Lähetetään mosquitto brokerille
  65.     client.publish("SYO_temperature",'Tony sensor: ' + str(read_temp()))
  66.     # Lähetetään IoT hubille
  67.     message = IoTHubMessage('Tony sensor: ' + str(read_temp()))
  68.     iotHubClient.send_event_async(message, send_confirmation_callback, None)                  
  69.     time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement