WiFi模組通訊實作 – Ubidots物連網平台
範例說明
利用epy讀取HTU21D感測器,epy透過UART介面與ESP01 WiFi模組連接。將資料傳送到ubidots的雲端。
發送資料格式:
|
POST /api/v1.6/devices/Devices
HTTP/1.1 Host: industrial.api.ubidots.com X-Auth-Token: Token Content-Type: application/json Content-Length: param_lenth param_data |
Devices: 為裝置名稱 Token: 為註冊帳戶獲取Token param_lenth: 為param_data資料長度 param_data: 為上傳訊息內容 {"visibility": data} visibility為資料名稱(如溫度、濕度...等) data為資料內容(如28、66.5...等) |
程式列表
Python - ePy-Lite_HTU21D_Ubidots.py
|
""" ePy-Lite_HTU21D_Ubidots.py ePy-Lite HTU21D ----------------- 3V3 VCC P17(SCL0) SCL GND GND P16(SDA0) SDA ePy-Lite ESP01 ----------------- P2(UART0_RxD) TxD P3(UART0_TxD) RxD 3V3 VCC GND GND """ from
machine import Switch #Get button KEY
library from
machine import I2C,
Pin, LED ,UART from
machine import Timer import
utime from
htu21d import HTU21D import
ESP01 BLEOUT
= False DEBUG
= True #
initialise variables delayTime
= 5000 #
in Sekunden SSID
= "MYAP" #
填入要連線的WiFi熱點名稱 PASSWORD
= "
" #
Wi-Fi熱點密碼 HOST
= "industrial.api.ubidots.com" #
伺服器網址,不可動 PORT
= 80 API_URL
= "industrial.api.ubidots.com" # 連線對象URL,不可動 Token
= "BBFF-
" #
資料平台裝置的存取權限碼 Devices
= "epy_iot" visibility1
= "Temperature" visibility2
= "Humidity" ledY
= None KeyADone
= None """ * Name: InitWifiModule * Description: this Function gives the commands that we need to send to
the sendData() function to send it. * * Params: Nothing. * * Returns: Nothing (void). """ def InitWifiModule(SSID,PASSWORD): print("connecting
to wifi") if not
wifi.isconnected(): buffer =
wifi.sendData("AT+CWLAP\r\n",1000,DEBUG) CWJAP_CMD="AT+CWJAP=\"" +
SSID + "\",\"" +
PASSWORD + "\"\r\n" wifi.sendData(CWJAP_CMD,25000,DEBUG) #
utime.sleep_ms(3000) while not
wifi.isconnected(): pass wifi.sendData("AT+CWMODE=1\r\n", 1500,
DEBUG) #set the ESP8266
WiFi mode to station mode. utime.sleep_ms(1000) wifi.sendData("AT+CIFSR\r\n", 1500,
DEBUG) #Show IP Address,
and the MAC Address. utime.sleep_ms(1000) def key_int(): global
KeyADone KeyADone = True def tick3(timer): global
TimerDone,Timer10s,Timer10sDone TimerDone = True Timer10s += 1 if
Timer10s>=100 : Timer10s=0 Timer10sDone = True #
Start Function if
__name__ == '__main__': ledY =
LED('ledy') ledY.off() KeyA =
Switch('keya') #Create button A KeyA.callback(key_int) KeyADone = False timer =
Timer(3,freq
= 10) timer.callback(tick3) Timer10s = 0 Timer10sDone = False TimerDone = False count = 0 # Declaration I2C i2c =
I2C(0,I2C.MASTER,baudrate=10000) #Create
I2C0 Master Mode, Baudrate=10kHz sensor =
HTU21D(i2c) if(BLEOUT): ble =
UART(1,115200) ble.write('AT+MODE_DATA\r\n') wifi_uart=UART(0,115200) wifi =
ESP01.ESP01(wifi_uart) InitWifiModule(SSID,PASSWORD) utime.sleep(2) print("Start
humidity measurement.") while True: if
Timer10sDone == True: ledY.on() Temperature =
sensor.readTemperatureData() Humidity =
sensor.readHumidityData() print("Temperature:%.1f*C,
Humidity:%.1f%%"%(Temperature,Humidity)) if(BLEOUT): ble.write('T
= {}*C, H = {}%\r\n'.format (Temperature,Humidity)) #
上傳訊息內容格式,其中visibility為資料名稱(如溫度、濕度...等),Temperature/Humidity為資料內容(如28、66.5...等) #
{"Temperature":20.76,"Humidity":49.29} param_data = '{"' +
visibility1 + '":' + str(Temperature) + ',"' +
visibility2 + '":' + str(Humidity) + '}' #
param_data = '{"' + visibility1 + '": ' + str(Temperature) + '}' #
定義發送至伺服器的訊息內容 message_str = "POST
/api/v1.6/devices/" +
Devices + "
HTTP/1.1\r\n" \ + "Host:
" +
API_URL + "\r\n"
\ + "X-Auth-Token:
" +
Token + "\r\n"
\ + "Content-Type:
application/json\r\n" \ + "Content-Length:
" + str(len(param_data)) + "\r\n\r\n"
\ +
param_data + "\r\n\r\n"
\ + "Connection:
close\r\n\r\n" wifi.connect(HOST,PORT) wifi.data_update(message_str) #
feeback:
{"humidity":[{"status_code":201}],"temperature":[{"status_code":201}]} ledY.off() Timer10sDone = False if
KeyADone == True: #Press
A Key break if(BLEOUT): ble.deinit() wifi.deinit() i2c.deinit() KeyA.callback(None) print("Exit
humidity measurement.") |