模組簡介
參考ESP8266(wiki) https://en.wikipedia.org/wiki/ESP8266
ESP-01 module pinout
The pinout is as follows for the common ESP-01 module:
1. VCC, Voltage (+3.3 V; can handle up to 3.6 V)
2. GND, Ground (0 V)
3. RX, Receive data bit X
4. TX, Transmit data bit X
5. CH_PD, Chip power-down
6. RST, Reset
7. GPIO 0, General-purpose input/output No. 0
8. GPIO 2, General-purpose input/output No. 2
電路圖
範例說明
利用epy上的ADC量測Light感測器,epy透過UART介面與ESP01 WiFi模組連接。將資料傳送到http://dweet.io/的雲端。
dweet.io免費的雲端平臺,不需要註冊帳戶獲取 ID或KEY。
發送資料格式:https://dweet.io/dweet/for/my-thing-name?hello=world
my-thing-name 是自己起的地址名稱;hello=world 鍵值對數據
用網頁打開獲取資料:https://dweet.io/get/dweets/for/my-thing-name
https://dweet.io/get/latest/dweet/for/my-thing-name
http://dweet.io/follow/my-thing-name
程式列表
Python - ESP01.py
"""
ESP01.py
"""
import utime
DEBUG = True
class ESP01:
def __init__(self, port):
self.port = port
"""
* Name: sendData
* Description: this Function regulates how the AT Commands will ge sent to the ESP8266.
*
* Params: command - the AT Command to send
* - timeout - the time to wait for a response
* - debug - print to Serial window?(true = yes, false = no)
*
* Returns: The response from the esp8266 (if there is a reponse)
"""
def sendData(self,command,timeout,debug):
response = ""
self.port.write(command)
utime.sleep_ms(timeout)
response = self.port.read() # 從串口讀取數據
if(debug):
#if the "debug" variable value is TRUE, print the response on the Serial monitor.
# cmd = response.decode('utf-8');
# print("received:",cmd) # 列印接收到的資料
print("received:"+str(response) ) # 列印接收到的資料
return response #return the String response.
"""
* 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(self,SSID,PASSWORD):
print("connecting to wifi")
self.sendData("AT\r\n",200,DEBUG)
str1 = self.sendData("AT+RST\r\n",1000,False)
if(DEBUG):
print("received:",str1) # 列印接收到的資料
print("received:{}".format(str1)) # 列印接收到的資料
self.sendData("AT+GMR\r\n",1000,DEBUG)
CWJAP_CMD="AT+CWJAP=\"" + SSID + "\",\"" + PASSWORD + "\"\r\n"
self.sendData(CWJAP_CMD,25000,DEBUG)
utime.sleep_ms(3000)
self.sendData("AT+CWMODE=1\r\n", 1500, DEBUG) #set the ESP8266 WiFi mode to station mode.
utime.sleep_ms(1000)
self.sendData("AT+CIFSR\r\n", 1500, DEBUG) #Show IP Address, and the MAC Address.
utime.sleep_ms(1000)
Python - ePy-Lite_ESP01_dweet.py
"""
ePy-Lite_ESP01_dweet.py
EPY_Lite ESP01
-----------------
P2(UART0_RxD) TxD
P3(UART0_TxD) RxD
3V3 VCC
GND GND
"""
from machine import Switch #獲取按鍵KEY庫
from machine import ADC
from machine import Pin
from machine import UART
from machine import LED
import utime
import ESP01
SSID = "MYAP" # 填入要連線的WiFi熱點名稱
PASSWORD = "19760106" # Wi-Fi熱點密碼
HOST = "dweet.io" # 伺服器網址,不可動
PORT = 80
thing_name = "EPY_dweet"
hello = "Light"
str1 = ""
DEBUG = True
ledR = None
# Start Function
if __name__ == '__main__':
ledR = LED('ledy')
light = ADC(Pin.board.AIN0)
KeyA = Switch('keya') #創建按鍵A
wifi_uart=UART(0,115200)
wifi = ESP01.ESP01(wifi_uart)
wifi.InitWifiModule(SSID,PASSWORD)
print("--- Start ---")
light_val=0
try:
while True:
light_val = light.read()
ledR.on()
#https://dweet.io/dweet/for/my-thing-name?hello=world
request_str = "POST /dweet/for/" + thing_name + "?" + hello + "=" + str(light_val) + "\r\n" \
+ "Host: www.dweet.io\r\n" \
+ "Connection: close\r\n\r\n"
request_lenth = str(len(request_str)) # 計算訊息的字串總長度
CIPSEND_CMD="AT+CIPSEND=" + request_lenth + "\r\n"
if(DEBUG):
print(CIPSEND_CMD) # 顯示此訊息內容進行debug
print(request_str) # 顯示此訊息內容進行debug
CIPSTART_CMD="AT+CIPSTART=\"TCP\",\"" + HOST + "\"," + str(PORT) + "\r\n"
wifi.sendData(CIPSTART_CMD,15000,DEBUG)
data = wifi.sendData(CIPSEND_CMD, 1000, DEBUG)
if 'ERROR' in data: # 使用in運算子檢查
print('find \"ERROR\"')
if '>' in data: # 使用in運算子檢查
print('find \">\"')
else:
print('not find \">\"')
# while (data.find(">")<0):
# utime.sleep_ms(100)
# if(wifi_uart.any()): # 當串口有可讀數據時
# data = wifi_uart.read()
wifi.sendData(request_str, 1000, DEBUG)
ledR.off()
if (KeyA.value()) == True: #Press A Key
break
utime.sleep_ms(5000)
finally:
pass
light.deinit()
wifi_uart.deinit()
# KeyA.deinit()
#https://dweet.io/get/dweets/for/EPY_dweet
#https://dweet.io/get/latest/dweet/for/EPY_dweet
#http://dweet.io/follow/EPY_dweet
執行結果