CloudとIoTの結合

繋がってしまえばなんとでも。まあ自分のレンタルサーバーでも仕組みはできるけど、利用するという観点も必要だろうな。何でも自分でやりたいのは分かるけどそうでなく有りものを組み合わせて大きなシステムを構築も魅力だと思わないか。大きなことってところに引かれる。

IFTTT

http://blog.mlkcca.com/iot/twitter-led/

 

KKHMF BME280 ESP8266でプログラム

やっと到着した。ソースのサンプル発見。スイッチサイエンスの基板でとあるが基本は変わらないはず。ラズベリーパイでなく今回はこちらで行うことにしてみる。

Copyright (c) 2015, Embedded Adventures
All rights reserved.

Contact us at source [at] embeddedadventures.com
www.embeddedadventures.com
 
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
 
- Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
 
- Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.
 
- Neither the name of Embedded Adventures nor the names of its contributors
  may be used to endorse or promote products derived from this software
  without specific prior written permission.
  
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
THE POSSIBILITY OF SUCH DAMAGE.
 
*/
 
// BME280 MOD-1022 weather multi-sensor Arduino demo
// Written originally by Embedded Adventures
 
#include <BME280_MOD-1022.h>
 
#include <Wire.h>
 
// Arduino needs this to pring pretty numbers
 
void printFormattedFloat(float x, uint8_t precision) {
char buffer[10];
 
  dtostrf(x, 7, precision, buffer);
  Serial.print(buffer);
 
}
 
 
// print out the measurements
 
void printCompensatedMeasurements(void) {
 
float temp, humidity,  pressure, pressureMoreAccurate;
double tempMostAccurate, humidityMostAccurate, pressureMostAccurate;
char buffer[80];
 
  temp      = BME280.getTemperature();
  humidity  = BME280.getHumidity();
  pressure  = BME280.getPressure();
   
  pressureMoreAccurate = BME280.getPressureMoreAccurate();  // t_fine already calculated from getTemperaure() above
   
  tempMostAccurate     = BME280.getTemperatureMostAccurate();
  humidityMostAccurate = BME280.getHumidityMostAccurate();
  pressureMostAccurate = BME280.getPressureMostAccurate();
  Serial.println("                Good  Better    Best");
  Serial.print("Temperature  ");
  printFormattedFloat(temp, 2);
  Serial.print("         ");
  printFormattedFloat(tempMostAccurate, 2);
  Serial.println();
   
  Serial.print("Humidity     ");
  printFormattedFloat(humidity, 2);
  Serial.print("         ");
  printFormattedFloat(humidityMostAccurate, 2);
  Serial.println();
 
  Serial.print("Pressure     ");
  printFormattedFloat(pressure, 2);
  Serial.print(" ");
  printFormattedFloat(pressureMoreAccurate, 2);
  Serial.print(" ");
  printFormattedFloat(pressureMostAccurate, 2);
  Serial.println();
}
 
 
// setup wire and serial
 
void setup()
{
  Wire.begin();     // Wire.begin(sda, scl)
  pinMode(12, OUTPUT);
  Serial.begin(115200);
}
 
// main loop
 
void loop()
{
 
  uint8_t chipID;
   
  Serial.println();
  Serial.println();
  Serial.println("Welcome to the BME280 MOD-1022 weather multi-sensor test sketch!");
  Serial.println("Embedded Adventures (www.embeddedadventures.com)");
  chipID = BME280.readChipId();
   
  // find the chip ID out just for fun
  Serial.print("ChipID = 0x");
  Serial.println(chipID, HEX);
   
  
  // need to read the NVM compensation parameters
  BME280.readCompensationParams();
   
  // Need to turn on 1x oversampling, default is os_skipped, which means it doesn't measure anything
  BME280.writeOversamplingPressure(os1x);  // 1x over sampling (ie, just one sample)
  BME280.writeOversamplingTemperature(os1x);
  BME280.writeOversamplingHumidity(os1x);
   
  // example of a forced sample.  After taking the measurement the chip goes back to sleep
  BME280.writeMode(smForced);
  while (BME280.isMeasuring()) {
    Serial.println("Measuring...");
    delay(50);
  }
  Serial.println("Done!");
   
  // read out the data - must do this before calling the getxxxxx routines
  BME280.readMeasurements();
  Serial.print("Temp=");
  Serial.println(BME280.getTemperature());  // must get temp first
  Serial.print("Humidity=");
  Serial.println(BME280.getHumidity());
  Serial.print("Pressure=");
  Serial.println(BME280.getPressure());
  Serial.print("PressureMoreAccurate=");
  Serial.println(BME280.getPressureMoreAccurate());  // use int64 calculcations
  Serial.print("TempMostAccurate=");
  Serial.println(BME280.getTemperatureMostAccurate());  // use double calculations
  Serial.print("HumidityMostAccurate=");
  Serial.println(BME280.getHumidityMostAccurate()); // use double calculations
  Serial.print("PressureMostAccurate=");
  Serial.println(BME280.getPressureMostAccurate()); // use double calculations
   
  // Example for "indoor navigation"
  // We'll switch into normal mode for regular automatic samples
   
  BME280.writeStandbyTime(tsb_0p5ms);        // tsb = 0.5ms
  BME280.writeFilterCoefficient(fc_16);      // IIR Filter coefficient 16
  BME280.writeOversamplingPressure(os16x);    // pressure x16
  BME280.writeOversamplingTemperature(os2x);  // temperature x2
  BME280.writeOversamplingHumidity(os1x);     // humidity x1
   
  BME280.writeMode(smNormal);
    
  while (1) {
    digitalWrite(12, HIGH);
    while (BME280.isMeasuring()) {
 
    }
     
    // read out the data - must do this before calling the getxxxxx routines
    BME280.readMeasurements();
    printCompensatedMeasurements();
     
    delay(1000);
    Serial.println();
 
    digitalWrite(12, LOW);
    delay(1000);
  }
}

 

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 に書き代えて保存)