Raspberry Pi send data to TinyWebDB
TBD
Raspberry Pi send data to TinyWebDB
TBD
上記のページを参考に進める。
温度センサーの代わりに、6軸センサーのデータを活用。(6軸センサーの例は、参考1)
動作の詳細は未確認コードだが、とりあえず動くので、アップする。
// Sample Arduino Json Web Client // Downloads and parse http://jsonplaceholder.typicode.com/users/1 // // Copyright Benoit Blanchon 2014-2017 // MIT License // // Arduino JSON library // https://bblanchon.github.io/ArduinoJson/ // If you like this project, please add a star! #include <ArduinoJson.h> #include <Arduino.h> #include <M5StickC.h> #include <WiFi.h> #include <WiFiMulti.h> #include <HTTPClient.h> #define WIFI_SSID "uislab003" // ① #define WIFI_PASSWORD "nihao12345" WiFiMulti WiFiMulti; int count = 1; // ③ #define USE_SERIAL Serial WiFiClient client; const char* resource = "http://tinydb.ml/"; // http resource const unsigned long BAUD_RATE = 9600; // serial connection speed const unsigned long HTTP_TIMEOUT = 10000; // max respone time from server const size_t MAX_CONTENT_SIZE = 512; // max size of the HTTP response HTTPClient http; float accX = 0.0F; float accY = 0.0F; float accZ = 0.0F; void setup() { USE_SERIAL.begin(115200); M5.begin(); M5.IMU.Init(); M5.Lcd.setRotation(3); M5.Lcd.setCursor(0, 0, 2); WiFiMulti.addAP(WIFI_SSID,WIFI_PASSWORD); M5.Lcd.print("Connecting"); while(WiFiMulti.run() != WL_CONNECTED) { M5.Lcd.print("."); delay(1000); } M5.Lcd.println(""); M5.Lcd.println("Connected to"); M5.Lcd.println(WiFi.localIP()); USE_SERIAL.print("Connected to "); USE_SERIAL.println(WiFi.localIP()); delay(500); } void loop() { M5.update(); // ⑤ if (M5.BtnA.wasPressed() ) { // ⑥ M5.Lcd.println("Pushed"); sensor_TinyWebDB(); count ++; // ⑧ } delay(100); } void sensor_TinyWebDB() { int httpCode; char tag[32]; char value[128]; // read values from the sensor M5.IMU.getAccelData(&accX,&accY,&accZ); const size_t bufferSize = JSON_ARRAY_SIZE(2) + JSON_OBJECT_SIZE(4); DynamicJsonBuffer jsonBuffer(bufferSize); JsonObject& root = jsonBuffer.createObject(); root["sensor"] = "IMU"; root["accX"] = String(accX); root["accY"] = String(accY); root["accZ"] = String(accZ); root.printTo(value); USE_SERIAL.printf("[TinyWebDB] %sn", value); USE_SERIAL.printf("ESP32 Chip id = %08X\n", ESP.getEfuseMac()); sprintf(tag, "esp32-%06x", ESP.getEfuseMac()); httpCode = TinyWebDBStoreValue(tag, value); // httpCode will be negative on error if(httpCode > 0) { // HTTP header has been send and Server response header has been handled USE_SERIAL.printf("[HTTP] POST... code: %d\n", httpCode); if(httpCode == HTTP_CODE_OK) { TinyWebDBValueStored(); } } else { USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); TinyWebDBWebServiceError(http.errorToString(httpCode).c_str()); } http.end(); delay(10000); } void get_TinyWebDB(const char* tag0) { int httpCode; char tag[32]; char value[128]; httpCode = TinyWebDBGetValue(tag0); // httpCode will be negative on error if(httpCode > 0) { // HTTP header has been send and Server response header has been handled USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); if(httpCode == HTTP_CODE_OK) { String payload = http.getString(); const char * msg = payload.c_str(); USE_SERIAL.println(payload); if (TinyWebDBreadReponseContent(tag, value, msg)){ TinyWebDBGotValue(tag, value); } } } else { USE_SERIAL.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str()); TinyWebDBWebServiceError(http.errorToString(httpCode).c_str()); } http.end(); delay(10000); } int TinyWebDBWebServiceError(const char* message) { } // ---------------------------------------------------------------------------------------- // Wp TinyWebDB API // Action URL Post Parameters Response // Get Value {ServiceURL}/getvalue tag JSON: ["VALUE","{tag}", {value}] // ---------------------------------------------------------------------------------------- int TinyWebDBGetValue(const char* tag) { char url[64]; sprintf(url, "%s%s?tag=%s", resource, "getvalue/", tag); USE_SERIAL.printf("[HTTP] %s\n", url); // configure targed server and url http.begin(url); USE_SERIAL.print("[HTTP] GET...\n"); // start connection and send HTTP header int httpCode = http.GET(); return httpCode; } int TinyWebDBGotValue(const char* tag, const char* value) { USE_SERIAL.printf("[TinyWebDB] %s\n", tag); USE_SERIAL.printf("[TinyWebDB] %s\n", value); return 0; } // ---------------------------------------------------------------------------------------- // Wp TinyWebDB API // Action URL Post Parameters Response // Store A Value {ServiceURL}/storeavalue tag,value JSON: ["STORED", "{tag}", {value}] // ---------------------------------------------------------------------------------------- int TinyWebDBStoreValue(const char* tag, const char* value) { char url[64]; sprintf(url, "%s%s", resource, "storeavalue"); USE_SERIAL.printf("[HTTP] %s\n", url); // POST パラメータ作る char params[128]; sprintf(params, "tag=%s&value=%s", tag, value); USE_SERIAL.printf("[HTTP] POST %s\n", params); // configure targed server and url http.begin(url); // start connection and send HTTP header http.addHeader("Content-Type", "application/x-www-form-urlencoded"); int httpCode = http.POST(params); String payload = http.getString(); //Get the response payload Serial.println(payload); //Print request response payload http.end(); return httpCode; } int TinyWebDBValueStored() { return 0; } // Parse the JSON from the input string and extract the interesting values // Here is the JSON we need to parse // [ // "VALUE", // "LED1", // "on", // ] bool TinyWebDBreadReponseContent(char* tag, char* value, const char* payload) { // Compute optimal size of the JSON buffer according to what we need to parse. // See https://bblanchon.github.io/ArduinoJson/assistant/ const size_t BUFFER_SIZE = JSON_OBJECT_SIZE(3) // the root object has 3 elements + MAX_CONTENT_SIZE; // additional space for strings // Allocate a temporary memory pool DynamicJsonBuffer jsonBuffer(BUFFER_SIZE); // JsonObject& root = jsonBuffer.parseObject(payload); JsonArray& root = jsonBuffer.parseArray(payload); JsonArray& root_ = root; if (!root.success()) { Serial.println("JSON parsing failed!"); return false; } // Here were copy the strings we're interested in strcpy(tag, root_[1]); // "led1" strcpy(value, root_[2]); // "on" return true; } // Pause for a 1 minute void wait() { Serial.println("Wait 60 seconds"); delay(60000); }
WordPressに API機能を追加して、クライドとして利用する方法を試み。
WP-TinyWebDB-APIは、両者の長所を連携し、WordPressをAppInventorのTinyWebDBサービスとして利用するためのAPIを、WordPressのプラグインとして提供したもの。
本章は、WP-TinyWebDB-APIのインストール、Postman を使ってAPIの動作確認する。
公式サイトを利用する
管理パネルの「Plugins」タブ上のpluginを追加してください。
tinywebdb を検索して、インストールしてください。
管理パネルの「Plugins」タブ上のpluginを有効化してください。
Activate the plugin on the “Plugins” tab of the administration panel.
サイトをブラウザーで開くと、普通のWordPressのサイトに見える
追加したAPI機能をみる。(これをTinyWebDBのServiceURLにセットする)
http://tinywebdb.edu2web.com/api
http://tinywebdb.edu2web.com/api/getvalue/?tag=questionsChenLab
ブラウザから取得したデータは、少々見にくい。PostmanというChrome の拡張を使うと、便利。認証、パラメタ引き渡しもできる。
蓄積したデータのグラフ表示する
参考
前回の続き、データの公開するため、shortcodeを利用する方法を学ぶ。
Firebaseという固定ページを作り、次のショットコードを追加してください。
[firebase_login]Please login[/firebase_login]
ページを保存して、ブラウザで開くと、次のような表示になります。
ここで、Firebaseの認証情報を使う。
(事前に、Firebaseのメール/パスワード認証の許可と、ユーザの追加をしてから)
次のショットコードを追加してください。
[firebase_greetings]Only login user see it [/firebase_greetings]
ページを保存して、ブラウザで開くと、次のような表示になります。
次のショットコードを追加してください。
[realtime class=’your-class-name’ collection_name=’users’ document_name=’user001′ ]
これで、1件目の表示はできた。
しかし、全てのデータの一覧表示はどうすれば?
次のようなショットコードは機能しない。
[realtime class=’your-class-name’ collection_name=’users’ ]
ログインできないなど問題発生するときに、エラーを特定するため、エラー表示を追加する。
[firebase_login_error class=”your-class-name”][/firebase_login_error]
わざと、パスワード間違って入力したら、次のエラーを確認できる。
次のショットコードを追加してください。
[firebase_logout]
M5StickCに距離センサ VL53L0Xを接続用 Hat を作ります。
結線の方法はまた調べてないが、結線したら変更は大変だから、ジャパーワイヤ使える実験用のHatも作りました。
AMG8833を利用したThermal Camera を 試作しました。
説明
1. パナソニックのセンサーです。
2. 8 x 8赤外線グリッドアレイ(64ピクセル)。
3. 0℃〜80℃ の 範囲の温度を+ 2.5℃ の精度で測定します。
4. 最大7メートル の距離から人間を検出することができます。
5. 最大フレームレート10Hzで、独自の人感センサーやミニサーマルカメラ作成に最適です。
6. センサーはI2Cを介して通信します。
7. 人の活動を検知して、エアコンや照明を制御。 自動ドアやエレベータで人を検知に最適です。
M5Stackは、320 x 240 TFTカラーディスプレイ、microSDカードスロット、スピーカーを備えたコンパクトで便利な開発モジュールです。ESP32を搭載しているため、Wi-FiおよびBluetooth通信を扱え、Arduino環境での開発が可能です。
最初のソースコードはhkoffer / M5Stack-Thermal-Camera-:AMG8833 8×8 を補完し 24×24 で実現。こちらすべて収める3Dケースのデータがあるので、いつかプリントしたい。
実際利用したのは、m600x の機能アップ版 : https://github.com/m600x/M5Stack-Thermal-Camera
Aliexpressで昨年8月購入した5ドル+送料の激安 ESP32-CAMは放置したまま、冬休み期間テストしてみることに。
ESP32-CAM自体シリアルーUSB通信機能がないので、プログラムを書き込んだりするためにはUSB/TTLシリアルコンバーターが必要になる。300円でAmazonから購入した、Raspberry Pi ラズベリーパイ用の USB-TTLシリアルコンソールのUSB変換COMケーブルモジュールのケーブル を使用した。黒い台はDonkeyCar 車台の不良品を利用。黄色テープも地面にDonkeyCarのトラックを作る際用意したもの。
配線部分を拡大した写真です。青い線は、書き込み時には、ESP32-CAMのIO0とGNDをショートして行う。
USB-TTL | ESP32-CAM |
---|---|
TXD ( 緑 ) | UOR |
RXD ( 白 ) | UOT |
– | ( IO0 – GND ) |
5V | 5V |
GND | GND |
ボードマネージャーを使用してESP32ボードを追加する必要があります。
これを完了すると、Arduino IDEボードマネージャーにESP32ボードのリストが表示されます。このリストからA-Thinker ESP32-CAMボードを選択します。
使用するサンプルスケッチは、CameraWebServerスケッチです。次のようにロードできます。
このスケッチはESPO32-CAMをフル機能のオンラインカメラに変え、顔検出機能と豊富なコントロールを完備しています。これは、ESP32-CAM機能の非常に印象的なデモです。
スケッチを使用する前に、ネットワークに合わせてスケッチを修正し、正しいESP32モジュールを選択する必要があります。
ESP32-CAMは2.4 GHz WiFiネットワークでのみ機能することに注意してください。
スケッチのコンパイルには時間がかかる場合がありますが、これは正常です。完了したら、USBケーブルを取り外し、ジャンパー線を取り外してから、USBケーブルを再接続して、ボードの電源を再びオンにします。
シリアルモニターを開き、ボーレートが115,200 bpsに設定されていることを確認します。次に、ESP32-CAMモジュールのリセットスイッチを押します。
初期化情報に続いて、ボードがネットワークに接続し、IPアドレスを取得したことを示すメッセージが表示されます。IPアドレスは、http://192.168.0.180などのURLの形式になります。
このアドレスをコピーして、Webブラウザーのアドレスバーに貼り付けます。Webブラウザーは、ESP32-CAMが接続されているのと同じネットワーク上にある必要があります。
ブラウザで、いろいろ操作できそう。
これで、カメラのWebページが表示され、いくつかのコントロールが表示されます。
[ストリームの開始]ボタンをクリックして、ビデオをストリーミングします。画面上部のドロップダウンを使用して、ビデオのサイズとフレームレートを変更できます。
一応、顔の登録と認識までできたが、後日また補足する。
ESP32は非常に低いスタンバイ電流で動作できますが、無線の動作時に大量の電流を消費するため、WiFiとBluetoothの両方のパフォーマンスに影響を与える可能性があります。
電源の問題があるかどうかを判断する1つの方法は、ESP32-CAMの起動時にシリアルモニターを観察することです。「ブラウンアウト状態」が検出されたというメッセージが表示された場合は、ESP32-CAMが供給できる電流よりも多くの電流を引き込もうとしている可能性があります。
ESP32-CAM Video Streaming and Face Recognition with Arduino IDE