Paso 6: Enviar datos del Sensor a Azure
Instalar bibliotecas de API de Azure
Instalar la API de Azure y otras bibliotecas requisitos ejecutando los siguientes comandos en una ventana de terminal en su Pi.
$ sudo apt-get install gcc cmake uuid-dev libssl-dev $ wget sourceware.org:/pub/libffi/libffi-3.2.1.tar.gz $ tar -zvxf libffi-3.2.1.tar.gz $ cd libffi-3.2.1/ $ ./configure $ sudo make install $ sudo ldconfig $ cd ~ $ sudo apt-get install python-pip $ sudo pip install requests[security] $ sudo pip install certifi urllib3[secure] pyopenssl ndg-httpsclient pyasn1 azure $ sudo apt-get install python-openssl
Llamar a la API de Azure de código Python
Este código ha sido editado para importar la biblioteca azul y una llamada a la sbs.send_event al final de cada bucle.
import osimport globimport timeimport threadimport socketfrom datetime import datetimefrom azure.servicebus import ServiceBusService os.system('modprobe w1-gpio')os.system('modprobe w1-therm')base_dir = '/sys/bus/w1/devices/'device1_folder = glob.glob(base_dir + '28*')[0]device2_folder = glob.glob(base_dir + '28*')[1]device1_file = device1_folder + '/w1_slave'device2_file = device2_folder + '/w1_slave'name_space = 'sensordemo-ns'key_name = 'RootManageSharedAccessKey'key_value = 'nmoamu9fHRphGQodT/J7SBXfmLGYfVsrUDIZXxm+hMc='def read_temp_raw(dfile): f = open(dfile, 'r') lines = f.readlines() f.close() return lines def read_temp(dfile): lines = read_temp_raw(dfile) while lines[0].strip()[-3:] != 'YES': time.sleep(0.2) lines = read_temp_raw(dfile) equals_pos = lines[1].find('t=') if equals_pos != -1: temp_string = lines[1][equals_pos+2:] temp_c = float(temp_string) / 1000.0 return temp_chost = socket.gethostname() while True: try: temp1 = read_temp(device1_file) temp2 = read_temp(device2_file) body = '{\"DeviceId\": \"' + host + '\" ' now = datetime.now() body += ', \"rowid\":' + now.strftime('%Y%m%d%H%M%S') body += ', \"Time\":\"' + now.strftime('%Y/%m/%d %H:%M:%S') + '\"' body += ', \"Temp1\":' + str(temp1) body += ', \"Temp2\":' + str(temp2) + '}' print body sbs = ServiceBusService(service_namespace=name_space,shared_access_key_name=key_name, shared_access_key_value=key_value) hubStatus = sbs.send_event('sensordemohub',body) print "Send Status:", repr(hubStatus) time.sleep(5) except Exception as e: print "Exception - ",repr(e)