2021年8月17日火曜日

ESP-WROOM-02D(ESP8266)を使ってみる1

安価に無線LANモジュールを入手出来ないかと模索中。 Lichee Pi Zeroにつなげたいじゃん。
ESP-WROOM-02D https://akizukidenshi.com/catalog/g/gM-13289/とか安いじゃん @360
開発ボード https://www.digikey.jp/product-detail/ja/espressif-systems/ESP8266-DEVKITC-02D-F/1965-1001-ND/9649768

技適も問題なし。


本当は、今年の3月くらいにやっていたのだけど、なぜかブログにアップするのを忘れていた。
なんだかなー。

準備

用意したもの

※フラッシュメモリの容量は2MB

ドキュメント

書き込みツール

  • windowsはESPFlashDownloadTool
  • pythonツールもある[rsptool.py]

SDKなど

技適のメモ

カスタムファームでの技適問題はスイッチサイエンスさんが確認してくれている
https://mag.switch-science.com/2016/01/20/esp-wroom-02_telec/

なので、

ファーム 技適
The official AT firmware (ESP8266_NONOS_SDK) OK
RTOS (ESP8266_RTOS_SDK) OK
Arduino core for ESP8266 WiFi chip OK
MicroPython ?
FreeRTOS https://github.com/aws/amazon-freertos ?

初期ファームで起動・最新バージョンに書き換え

ready
AT+GMR
AT+GMR
AT version:1.6.2.0(Apr 13 2018 11:10:59)
SDK version:2.2.1(6ab97e9)
compile time:Jun  7 2018 19:34:26
Bin version(Wroom 02):1.6.2
OK

Quick Start Guideの手順書通りにファーム書き換え、windowsが必要になったけどね。。
rsptool.py使えば、linuxでもできる気がするけど試していない。

ready
AT+GMR
AT+GMR
AT version:1.7.4.0(May 11 2020 19:13:04)
SDK version:3.0.4(9532ceb)
compile time:May 27 2020 10:12:22
Bin version(Wroom 02):1.7.4
OK

The official AT firmwareでhttpリクエストしてみる

ATコマンドを駆使してhttpリクエストをやってみる
ATコマンドは
https://www.espressif.com/sites/default/files/documentation/4a-esp8266_at_instruction_set_en.pdf
https://www.espressif.com/sites/default/files/documentation/4b-esp8266_at_command_examples_en.pdf
を参考にした

今勉強中のPythonでやってみる
pyserialを使うので

pip3 install pyserial

が必要。

テスト用プログラム

import serial
import threading
import time
import io

busy_flg = False
rcv_flg = True

def _rcv() :
    global busy_flg
    global rcv_flg
    global sio
    while rcv_flg:
        rawline = sio.readline()
        line = rawline.replace("\r","").replace("\n","")
        if (line == "OK") or (line == "SEND OK") :
#            print("Found \"OK\".")
            busy_flg = False
        elif (line == "FAIL") or (line == "ERROR") :
#            print("Found \"FAIL\" or \"ERROR\".")
            busy_flg = False

        if len(line) > 0 :
            print(line)

def _send(data,nocr=False) :
    global busy_flg
    global sio
    while busy_flg == True :
        time.sleep(1)
        print("*")
    busy_flg = True

    print("snd>"+data)
    if nocr:
        sio.write(data)
    else :
        sio.write(data+"\r\n")
    sio.flush()


ser = serial.Serial('/dev/ttyUSB1',115200,timeout=0.5)
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))

rcv_thread = threading.Thread(target=_rcv)
rcv_thread.daemon = True
rcv_thread.start()

try:
    _send(data = "AT+GMR")

    _send(data = "AT+CWMODE=1")     # Station mode 
    _send(data = "AT+CWLAP")        # Lists Available APs
    _send(data = "AT+CWJAP_CUR=\"[ESSID]\",\"[password]\"")        # Connects to an AP

    _send(data = "AT+CIFSR")        # Gets the local IP address 
    _send(data = "AT+CIPSTA?")  

    _send(data = "AT+CIPSNTPCFG=1,9,\"pool.ntp.org\",\"jp.pool.ntp.org\"") # Sets the Configuration of SNTP 
    _send(data = "AT+CIPSNTPTIME?") # Checks the SNTP Time

    _send(data = "AT+CIPSTART=\"TCP\",\"203.137.163.123\",80")        #protocol,server IP and port
    
    http_body="GET / HTTP/1.0\r\n"
    http_body+="\r\n\r\n"
    http_body_length = len(http_body)

    _send(data = "AT+CIPSEND="+str(http_body_length))
    _send(data = http_body,nocr=True)
    time.sleep(10)

    _send(data = "AT+CIPCLOSE")
    _send(data = "AT+CWQAP")        # Disconnects from the AP 

except KeyboardInterrupt:
    print("Bye.")
    

print("Exit.")
busy_flg = False
rcv_flg = False
rcv_thread.join()
ser.close()

やってること

ATコマンドを順に実行している。エラー処理などは未実装。

  1. バージョン表示
  2. ステーションモード(STA)に変更
  3. アクセスポイント(AP)を検索
  4. APに接続
  5. APからもらったIPアドレスなどを表示
  6. NTPサーバーを設定(“pool.ntp.org”,“jp.pool.ntp.org”)
  7. NTPサーバーから時刻を取得
  8. WEBサーバーに接続(今回は、自分のグローバルIPアドレスを確認できるサービス http://ipaddr.show/ のIPを設定[203.137.163.123])
  9. WEBサーバーにリクエスト送信
  10. 10秒待つ
  11. TCPセッション切断
  12. APから切断

実行結果

adeno@drakorange:~/develop/ESP8266$ python3 test.py 
snd>AT+GMR
AT+GMR
AT version:1.7.4.0(May 11 2020 19:13:04)
SDK version:3.0.4(9532ceb)
compile time:May 27 2020 10:12:22
Bin version(Wroom 02):1.7.4
OK
*
snd>AT+CWMODE=1
AT+CWMODE=1
OK
*
snd>AT+CWLAP
AT+CWLAP
*
*
+CWLAP:(4,"****************",-79,"**:**:**:**:**:**",1,5,0,4,4,7,1)
(省略)
OK
*
snd>AT+CWJAP_CUR="SSID","PASSWORD"
AT+CWJAP_CUR="SSID","PASSWORD"
*
*
*
WIFI CONNECTED
*
*
WIFI GOT IP
OK
*
snd>AT+CIFSR
AT+CIFSR
+CIFSR:STAIP,"192.168.1.19"
+CIFSR:STAMAC,"**:**:**:**:**:**"
OK
*
snd>AT+CIPSTA?
AT+CIPSTA?
+CIPSTA:ip:"192.168.1.19"
+CIPSTA:gateway:"192.168.1.1"
+CIPSTA:netmask:"255.255.255.0"
OK
*
snd>AT+CIPSNTPCFG=1,9,"pool.ntp.org","jp.pool.ntp.org"
AT+CIPSNTPCFG=1,9,"pool.ntp.org","jp.pool.ntp.org"
OK
*
snd>AT+CIPSNTPTIME?
AT+CIPSNTPTIME?
+CIPSNTPTIME:Wed Mar 31 06:19:39 2021
OK
*
snd>AT+CIPSTART="TCP","203.137.163.123",80
AT+CIPSTART="TCP","203.137.163.123",80
CONNECT
OK
*
snd>AT+CIPSEND=20
AT+CIPSEND=20
OK
*
snd>GET / HTTP/1.0



> 
Recv 20 bytes
SEND OK
+IPD,133:HTTP/1.0 200 OK
Date: Tue, 30 Mar 2021 21:19:43 GMT
Content-Length: 16
Content-Type: text/plain; charset=utf-8
***.***.***.***
CLOSED
snd>AT+CIPCLOSE
AT+CIPCLOSE
ERROR
*
snd>AT+CWQAP
Exit.
AT+CWQAP

いい感じ
なんとなく、わかってきた。

0 件のコメント:

コメントを投稿