2020年9月5日 星期六

WiFi模組通訊實作Part3 – IFTTT雲端平臺

 

 WiFi模組通訊實作Part3 – IFTTT雲端平臺

範例說明

IFTTT的意思是If This Then That的縮寫,這篇將會透過 IFTTT LINE Notify,實現「觸發某件事,就自動發送 LINE 訊息」的功能。

 

IFTTT提供的網址格式如下
https://maker.ifttt.com/trigger/
事件名稱/with/key/驗證碼

 

發送資料格式:

GET /trigger/事件名稱/with/key/驗證碼 HTTP/1.1

Host: maker.ifttt.com

Connection: close

 

 

 

程式列表

 

"""

 ePy_ESP01_ifttt.py

 

 EPY           ESP01

 -----------------

 3V3           VCC

 P8(UART1_TxD) RxD

 P9(UART1_RxD) TxD

 GND           GND

 

"""

 

from machine import KEY  #獲取按鍵KEY

from machine import ADC

from machine import PIN

from machine import UART

from machine import LED

import utime

 

SSID = "MYAP"            # 填入要連線的WiFi熱點名稱

PASSWORD = "19760106"    # Wi-Fi熱點密碼

HOST = "maker.ifttt.com"        # 伺服器網址,不可動

PORT = 80

event = "IFTTT_LINE"

key = "xxxxxxxxxxxxxxxx"

str1 = ""

DEBUG = True

ledR = None

 

"""

* 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(command,timeout,debug):

  response = ""

  uar_1.write(command)

  utime.sleep_ms(timeout)

  response = uar_1.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) # 列印接收到的資料

  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():

  print("connecting to wifi")

  sendData("AT\r\n",200,DEBUG)

 

  str1 = sendData("AT+RST\r\n",1000,False)

  if(DEBUG):

    print("received:",str1) # 列印接收到的資料

    # print("received:{}".format(str1)) # 列印接收到的資料

 

  sendData("AT+GMR\r\n",1000,DEBUG)

 

  CWJAP_CMD="AT+CWJAP=\"" + SSID + "\",\"" + PASSWORD + "\"\r\n"

  sendData(CWJAP_CMD,25000,DEBUG)

  utime.sleep_ms(3000)

 

  sendData("AT+CWMODE=1\r\n", 1500, DEBUG)  #set the ESP8266 WiFi mode to station mode.

  utime.sleep_ms(1000)

 

  sendData("AT+CIFSR\r\n", 1500, DEBUG)  #Show IP Address, and the MAC Address.

  utime.sleep_ms(1000)

 

# Start Function

if __name__ == '__main__':

  ledR = LED(LED.LED1)

  light = ADC(4)

  uar_1=UART(1,115200)

  key_c = KEY(KEY.KEYC)    #創建按鍵C

  key_d = KEY(KEY.KEYD)    #創建按鍵D

 

  InitWifiModule()

  print("--- Start ---")

 

  light_val=0

 

  try:

    while True:

      light_val = light.read()

 

      if key_d.value() == 0:      #如果按鍵C被按下

        ledR.on()

        #https://maker.ifttt.com/trigger/{event}/with/key/{key}?value1=[1]&value2=[2]

        request_str = "GET /trigger/" + event + "/with/key/" + key + "?value1=" + str(light_val) + " HTTP/1.1\r\n" \

                    + "Host: " + HOST + "\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"

        sendData(CIPSTART_CMD,5000,DEBUG)

 

        data = sendData(CIPSEND_CMD, 2000, 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(uar_1.any()): # 當串口有可讀數據時

            # data = uar_1.read()

        sendData(request_str, 1000, DEBUG)

        ledR.off()

        utime.sleep_ms(5000)

 

      if key_c.value() == 0:      #如果按鍵C被按下

        break

 

  finally:

    pass

 

light.deinit()

uar_1.deinit()

key_c.deinit()

key_d.deinit()

 

執行結果

 

沒有留言:

張貼留言