智聯感測
-
將溫度感測器的數值使用機器人程式自動送到客製化網站
import requests import time import uuid import Adafruit_DHT LOGIN_URL = 'http://drweb.nksh.tp.edu.tw:3000/create/' headers = { 'accept': 'text/html,application/xhtml+xml,application/xml', 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36' } response = requests.get(LOGIN_URL, headers=headers, verify=False) headers['cookie'] = '; '.join([x.name + '=' + x.value for x in response.cookies]) headers['content-type'] = 'application/x-www-form-urlencoded' GPIO_PIN = 4 try: print("--------------") while True: h, t = Adafruit_DHT.read_retry(Adafruit_DHT.DHT11,GPIO_PIN) if h is not None and t is not None: mac_address = uuid.UUID(int = uuid.getnode()).hex[-12:] print(mac_address + ":temp={0:0.1f}".format(t)) payload = { 'value1': 0, 'value2': format(t), 'value3': 0, 'mac_address': mac_address } else: print("fail,please retry") response = requests.post(LOGIN_URL, data=payload, headers=headers, verify=False) headers['cookie'] = '; '.join([x.name + '=' + x.value for x in response.cookies]) time.sleep(60) except KeyboardInterrupt: print('close')