2019年11月10日日曜日

ESP8266 Arduino 続き)

アマゾンで”Aideepen 2個セット ESP8266 ESP-12F CH340G CH340 V2 3.3V USB WeMos D1 Mini WIFI開発ボードD1 Mini NodeMCU IOTボード”を購入しましたが、Serial Monitorがうまく動作しなくて、昨日から四苦八苦していましたが、やっと解決策を見つけました。 (これ、技適マーク無いので、WiFiは使っちゃダメです)。
「ツール」->「ボード」で、LOLIN WeMosがらみの物を指定すると、ちゃんと表示されるようになりました(もっとも、最初に表示されるゴミは相変わらずですが、、、。中国語のバナーかなんかかな?)。
 LOLIN WeMOS, WeMOSで4種類ありますが、取り敢えず、どれでも動いていますが、正しくはメモリの量などでちゃんと選ぶ必要があるのでしょう



早速、ブレッドボード上にDHT11を乗っけてWiFiで定期的に温度湿度を送信できるようにしてみました。 「わーお、なんて簡単なんでしょ。 プルアップ抵抗も何もいらず、あまりにも簡単」


スケッチは、以前作ったもののコピペで、、、。 消費電力を抑えるためにdeepsleep()を使って1時間毎に! ただし、以前deepsleep()では1時間は出来なかったように記憶していたので、ググってみたら、ココが出てきましたが、実際にやるとやはり60分を指定するとうまくゆかないので、30分にすることにしました。 何故60分が出来ないんだろう?

deepsleep()を利用するために、RSTとDPIO16(D0)を接続する必要がありますが、これ接続しておくと、スケッチの書き込みが出来なくなるので、スケッチの書き込みが終了した後にD0とRSTをショートさせる必要があります。

後は、来週以降PPかなんかの容器に入れて、太陽電池とリチウム電池の電源で実際にどの位の期間使えるのか、検証することにします。

 3-4年前に購入した素のESP8266-WROOM2が幾つかあるのですが、うまくプログラムの書き込みが出来なくて、このままでは廃棄かな? 今更メンドクサくなってしまいました。

参考にした記事; 
 WeMos D1 (ESP8266) を動かす
 Get started with Arduino[D1/D1 mini series] 
   ESP8266の真骨頂Deep-Sleepモードの使い方

   ESP-WROOM-02のDeep-sleepはどれだけ寝ていられるのか 



====== Sketch =======

/*
    This sketch establishes a TCP connection to a "quote of the day" service.
    It sends a "hello" message, and then prints received data.
*/

#include 
#include 

#ifndef STASSID
#define STASSID "XXXXXXXX"
#define STAPSK  "YYYYYYYYYYYYY"
#define IDENTIFIER "Garden Center"




//#define IDENTIFIER "Garden East"
//#define IDENTIFIER "Garden West"
#endif
#define DHTPIN 14
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

const char* ssid     = STASSID;
const char* password = STAPSK;
const char* Identifier = IDENTIFIER;

const char* host = "192.168.1.3";
const uint16_t port = 80;

void setup() {
  Serial.begin(115200);
  // We start by connecting to a WiFi network
  /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
     would try to act as both a client and an access-point and could cause
     network-issues with your other WiFi-devices on your WiFi-network. */
  WiFi.mode(WIFI_STA);
  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());
  // dht to start
  dht.begin();
}

void loop() {
  // Serial.print("connecting to ");
  // Serial.print(host);
  // Serial.print(':');
  // Serial.println(port);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    delay(5000);
    return;
  }

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    delay(100);
    return;
  }
    // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C\t");
  //Serial.print(f);
  //Serial.print(" *F\t");
  Serial.print("; Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Heat index: ");
  Serial.print(hic);
  Serial.print(" *C ");
  // Serial.print(hif);
  // Serial.println(" *F");

  // WEB serverにデータをアップロードする。
  String url = "/weather/weather_log.php";
        url += "?location=" + String(Identifier); // ID to identify location
        url += "&temperature="+String(t);
        url += "&humidity=" +String(h);
  Serial.print("Requesting URL: ");
  Serial.println("GET "+url);
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                "Host: " + host + "\r\n" +
                "Connection: close\r\n\r\n");
  delay(50); 
  // Read all the lines of the reply from server and print them to Serial
  Serial.println("Returned message from server: ");
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  client.stop();
  // 30分deepsleepする。
  ESP.deepSleep(30*60*1000*1000, WAKE_RF_DEFAULT); // 60*1000*1000 = 1min, 30 min
  delay(1000); // execute once every 5 minutes, don't flood remote service
}
 







0 件のコメント:

php のインストールの確認

phpって最初のfacebook書くときに使われたみたいで、それなりに歴史のある言語で、私も2006年位から使っていますが、CLIで使う事はあまり無いので、apacheとの連携のトラブル(mod_phpのバージョンの齟齬)などは気になりますが、拡張モジュールのインストールの問題に...