2021年8月18日水曜日

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

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



official AT firmwareでATコマンドを駆使した簡単なhttpリクエストは成功したので
少し調子に乗って、Arduino core for ESP8266 WiFi chipを使って同じようなことをしてみたい。
初めてArduino使うわ。
なので、Arduino環境の構築から。

Arduino環境構築

この手順に従ってやっていく
https://github.com/esp8266/Arduino#installing-with-boards-manager

参考
https://www.indoorcorgielec.com/resources/arduinoide設定/esp-wroom-02搭載製品
https://keijirotanabe.github.io/blog/2017/02/07/esp8266-Arduino-170207/

Arduino IDE

https://www.arduino.cc/en/software

今回は、ディストリビューションのソフトウェア管理からインストールしちゃう。
apt install

後のVScode連携でハマった(IDEの場所をVSCodeから見つけられない)ので、
公式からインストールすることにした。
https://www.arduino.cc/en/Guide/Linux

ボードマネージャーに追加

https://arduino.esp8266.com/stable/package_esp8266com_index.json

HelloWorld

巷のサンプルのままだけど。

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("Hello World");
  delay(1000);
}


これがArduinoかー。わずか3行でこんなことができちゃうんだ。すごい世界観だ。

VSCode

なにやら、VSCodeの拡張機能があるみたい。すてき。
https://github.com/Microsoft/vscode-arduino
https://qiita.com/narikei/items/847613a8f01a9e1527d7#arduinoに書き込む
https://qiita.com/kamata1729/items/10226444bc89e2533e4f

上記を参考に導入。


Arduinoでhttpリクエストしてみる

Arduinoはサンプルコードがいっぱいある。
参考にテストコードを作成

#include <ESP8266WiFi.h>
#include <time.h>

WiFiClient client;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  testRun();
}

void printScanResult(int networksFound)
{
  Serial.printf("%d network(s) found\r\n", networksFound);
  for (int i = 0; i < networksFound; i++)
  {
    Serial.printf("%d: %s, Ch:%d (%ddBm) %s %s\r\n", 
      i + 1, WiFi.SSID(i).c_str(), WiFi.channel(i), WiFi.RSSI(i), 
      WiFi.encryptionType(i) == ENC_TYPE_NONE ? "open" : "",WiFi.BSSIDstr(i).c_str());
  }
  Serial.printf("End\r\n");
}

void show_WifiStatus(){
  char result[32];
  switch(WiFi.status()){
    case WL_CONNECTED:
      strcpy(result,"connection is established.");
      break;
    case WL_NO_SSID_AVAIL:
      strcpy(result,"SSID cannot be reached.");
      break;
    case WL_CONNECT_FAILED:
      strcpy(result,"connect failed.");
      break;
    case WL_IDLE_STATUS:
      strcpy(result,"idle.");
      break;
    case WL_DISCONNECTED:
      strcpy(result,"disconnect.");
      break;
    default:
      strcpy(result,"unknown.");
      break;
  }
  Serial.printf("Connection status: %s\r\n",result);
}

void testRun(){
  Serial.printf("Scan AP\r\n");
  delay(100);
  WiFi.scanNetworksAsync(printScanResult);
  
  delay(10000);
  const char ssid[] = "************";
  Serial.printf("Connect AP\r\n");
  show_WifiStatus();
  Serial.printf("Connecting to %s\n", ssid);
  WiFi.begin(ssid, "************");
  show_WifiStatus();
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  show_WifiStatus();
  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());
  configTzTime("JST-9", "pool.ntp.org", "jp.pool.ntp.org"); 

  delay(10000);

  time_t t;
  struct tm *tm;
  static const char *wd[7] = {"Sun","Mon","Tue","Wed","Thr","Fri","Sat"};

  t = time(NULL);
  tm = localtime(&t);
  Serial.printf("ESP8266 :  %04d/%02d/%02d(%s) %02d:%02d:%02d\r\n",
        tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
        wd[tm->tm_wday],
        tm->tm_hour, tm->tm_min, tm->tm_sec);

  delay(3000);
  Serial.println("\r\nStarting connection...");
  if (client.connect("httpbin.org", 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /ip HTTP/1.0");
    client.println();

    delay(3000);
    while(client.available()) {
      String line = client.readStringUntil('\r');
      Serial.print(line);
    }

    client.stop();
  }

  WiFi.disconnect();
  delay(3000);
  show_WifiStatus();
}

void loop() {
  delay(500);
}

なんとなく、わかったような。。
久しぶりにCだなぁ。

0 件のコメント:

コメントを投稿