ESP8266MOD

少し分かってきたのでアプリ含めたキットなんぞも面白そうだなと。2千円なので思わずポッチた。アグリカルチャーへの応用もテーマの一つだから。YOUTUBEでArduinoで検索すると世界のアイディアは面白い。
●感想
ケーブルが固い。今どきこんな固いケーブルありか。説明書はWordで作成されたものがCDにある。まあ、このクラスのものってマニアックなんだと思うけど、IoTでAuduinoかなんかでやってる輩は世界中にいると思えば市場性はある。いい視点だ。感心!

http://www.handsontec.com/pdf_learn/esp8266-V10.pdf

●シリアル通信
ドライバはCDに入っているので簡単に入った。ESP32のようにTera Termで115200bpsで接続したが、文字化けで読めない・・・。検索すると「起動時のシリアルポートの速度は 74880bps 」とある。Teraにはないボーレート。Arduinoのシリアルモニタで74880にしてキットのリセットボタンで以下の文字が見えた。しかし最後の行が化けている。

ets Jan 8 2013,rst cause:2, boot mode:(3,7)

load 0x40100000, len 27728, room 16
tail 0
chksum 0x2a
load 0x3ffe8000, len 2124, room 8
tail 4
chksum 0x07
load 0x3ffe8850, len 9276, room 4
tail 8
chksum 0xba
csum 0xba
rf[112] : 00
rf[113] : 00
rf[114] : 00

SD⸮hJ⸮F⸮

最後化けたのでシリアルモニタのボーレートを115200bpsに変えたらコマンドを受け付けるようになった。2度手間必要だ。しかしATコマンドを送信しても何だか返信がErrorばかりだ。ここが本題ではないのでLチカしてみるか。

/**
 * lightsensor link ESP8266:    VCC - 3.3v    GND - GND    SCL - D1    SDA - D2  
 * 1602 LCD link ESP8266:    VCC - 5V    GND - GND    SCL - D1    SDA - D2 
 * DHT11 link ESP8266:    VCC - 3.3V    GND - GND    OUT - D4
 * BH1750FVI link ESP8266:    VCC - 3.3V    GND - GND    DO-D7    AO-AO
 
 * Note:
 * You should modify the wifi account: WIFI_SSID   WIFI_PWD
 * Update ThingSpeak API: WIFI_PWD below first
*/

int cnt = 0;

void setup(){
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(2,OUTPUT);
  cnt = 0;
}

void loop(){
  Serial.print("------->");
  Serial.print(cnt);
  Serial.println();
  digitalWrite(2,0);
  delay(1000);
  digitalWrite(2,1);
  delay(1000);
  cnt ++;
}

2番ピンってフラッシュ書き込み時に高速で点滅を繰り返すLEDだが・・・。
In our example in chapter 5 on blinking the blue LED, the blue LED in connected to GPIO2
書き込みがESP32に比べると遅いのは転送スピードを15200にしてるからか?
色々調べることができた。HIGHとLOWもESP32とか他のデジタルアウトと違う。まあ、直接出力電圧をLEDにか引っ張り込むかで違うかな。この辺も調べよう。Lチカだけでもこれだけ気にすることありだった。

Arduino Tips (debug)

・最初はデバッグモードを有効にしておくと、どこでつまずいているのか分かりやすい。
・やりかたは”Enable debugging“の通りに。(settings.h で、#define ENABLE_UHS_DEBUGGING 1 に書き代えて保存)

 

ありゃりゃ遅れてるぞ!

AWSでFree RTOSにESP32追加
アマゾン「AWS IoT」は何が衝撃的なのか
MQTTでIoT(MMQTTとは何か)

何だか別の視点でというか・・・。電子屋やソフト屋の視点だけでは世の中の変革について行けない。作るのは電子工作(枝葉末節:半田付け・プログラム)だけど創る視点で市場を見る眼が必要だ(毎回言ってるような)。

【閑話休題】

小学校からプログラミング不要は思い違いだったようだ。
最初の言語が拙いものでもいい。if文が分かれば後は何とかなる。
後は子供たちのレベルで動くものや遊びを表現して行ける。子供の集中力は凄まじいものがあるから大いに期待できる。

誰でもある程度まで行ける時代(21世紀)に来た。
AuduinoやPythonがそれを可能にし始めている。

ESP32 LEDをWIFIで制御

なんかいい感じになってきた。LEDを増やしWEBデザインも押しボタンにしてみた。少しは頭使わないとね。
Auduinoのコンパイルエラーで理由が分からないのあるな。今のところコツは、無視して強硬!!

ブラウザからボタン・クリックでで、ピン5とピン14に接続したLEDを点灯や滅灯させる制御。このリストの次で説明のものは何故かiPhone5だと動かなかった。シリアルモニタに出力される GETの引数が化ける・・・。iPadも mac も問題ない。以下は全機種で制御できている。

/**
 WiFi Web Server LED Blink
*/
 
#include <WiFi.h>

const char* ssid     = "wx03-bc7caa";
const char* password = "50547acdc2730";

const char html[] = 
"<!DOCTYPE html><html lang='ja'><head><meta charset='UTF-8'>\
<style text='text/css'>\
.btn{display  :inline-block;  font-size :49pt;   text-align : center;   border : 2px solid #000066;}\
.btn:hover {  color  : #000066;  background    : #ffffff; }</style>\
<title>WiFi_LED Test</title></head>\
<body><p>LED Test</p>\
<form method='get'>\
<input type ='submit' name='ron' value = '赤ON  ' class ='btn' /><br><br>\
<input type='submit' name='rof' value='赤OFF' class ='btn'/><br><br><br>\
<input type='submit' name='gon' value='緑ON  '  class ='btn'/><br><br>\
<input type='submit' name='gof' value='緑OFF' class ='btn'/><br>\
</form></body></html>";


WiFiServer server(80);

const int led5 = 5;
const int led14 = 14;

void setup()
{
    Serial.begin(115200);
    pinMode(led5, OUTPUT);      // set the LED pin mode
    pinMode(led14, OUTPUT);
    delay(10);

    // We start by connecting to a WiFi network

    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected.");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    
    server.begin();

}

int value = 0;

void loop(){
 WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("<h1>Click <a href=\"/H\">here</a> to turn the LED on pin 5 on.</h1>");
            client.print("<h1>Click <a href=\"/L\">here</a> to turn the LED on pin 5 off.</h1><br>");
            client.print("<h1>Click <a href=\"/M\">here</a> to turn the LED on pin 14 on.</h1>");
            client.print("<h1>Click <a href=\"/N\">here</a> to turn the LED on pin 14 off.</h1><br>");           

            //client.print(html);
            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          digitalWrite(led5, HIGH);               // GET /H turns the LED on
         }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(led5, LOW);                // GET /L turns the LED off
          }
        if (currentLine.endsWith("GET /M")) {
            digitalWrite(led14, HIGH);
        }
        if (currentLine.endsWith("GET /N")) {
          digitalWrite(led14, LOW);
        }        
      
      }
    }
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
}

 

/**
 WiFi Web Server LED Blink
*/
 
#include <WiFi.h>

// mobile routerのもの
const char* ssid     = "wx03-xxxx";
const char* password = "xxxxxxxxx";

const char html[] = 
"<!DOCTYPE html><html lang='ja'><head><meta charset='UTF-8'>\
<style>input {margin:8px;width:80px;}\
div {font-size:16pt;color:red;text-align:center;width:400px;border:groove 40px orange;}</style>\
<title>WiFi_LED Test</title></head>\
<body><p>LED Test</p>\
<form method='get'>\
<input type='submit' name='ron' value='赤ON' /><br>\
<input type='submit' name='rof' value='赤OFF' /><br>\
<input type='submit' name='gon' value='緑ON' /><br>\
<input type='submit' name='gof' value='緑OFF' /><br>\
</form></body></html>";


WiFiServer server(80);

const int led5 = 5;
const int led14 = 14;

void setup()
{
    Serial.begin(115200);
    pinMode(led5, OUTPUT);      // set the LED pin mode
    pinMode(led14, OUTPUT);
    delay(10);

    // We start by connecting to a WiFi network

    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println("");
    Serial.println("WiFi connected.");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    
    server.begin();

}

int value = 0;

void loop(){
 WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("New Client.");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.print(html);
            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          } else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        if (currentLine.endsWith("GET /?gon")) {
          digitalWrite(led5, HIGH); 
         }
        if (currentLine.endsWith("GET /?gof")) {
          digitalWrite(led5, LOW);
          }
        if (currentLine.endsWith("GET /?ron")) {
            digitalWrite(led14, HIGH);
        }
        if (currentLine.endsWith("GET /?rof")) {
          digitalWrite(led14, LOW);
        }        
      
      }
    }
    // close the connection:
    client.stop();
    Serial.println("Client Disconnected.");
  }
}

●考察
macでうまく書き込めた。これでキット単独で動作していることになる(電池でも動くということ)。
別PCのWindows10のUSBアダプタにつないで動作確認。キットの電源ONの赤色LEDは点いている。スマホからアクセスすると動作しない・・・。
アダプタはACアダプタ付きだからポートのパワーは問題ないはずだが。見るとアダプタの電源が入っていないじゃないか。テラタームでキットからの情報は表示されている。ブラウンアウトでリセットを繰り返していた。思ったよりというかキットの起動電流は少なくないというのは本当だ。