ubidotsのライブラリをベースに、dweet.ioのライブラリを書いてみました。
使用には、ArduinoJson libraryが必要になります。
○ライブラリのインストール
dweet.ioのライブラリをインストールするには、次のように操作します。
※「ESP8266でIoT - 開発環境のセットアップ」の手順で開発環境をセットアップ済のこととします。
①dweetESP8266 library(dweet-esp-master.zip)をダウンロードします。
②Arduino IDEを起動します。
③[スケッチ]→[ライブラリをインクルード]→[.ZIP形式のライブラリをインクルード]を選択します。
④ダウンロードしたZIPファイル「dweet-esp-master.zip」を選択し、[開く]をクリックします。
○プログラム(スケッチ)
※プログラムを書込む際には、GPIO0をLowにします。
・1つのデータの送信
dweet.ioに1つのデータを送信するプログラムは、次のように書きます。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include "dweetESP8266.h" | |
#define THIG_NAME "Your_thing_neme_here" // Put here your thing name | |
#define WIFISSID "ssid" | |
#define PASSWORD "password" | |
dweet client; | |
void setup(){ | |
Serial.begin(115200); | |
delay(10); | |
client.wifiConnection(WIFISSID, PASSWORD); | |
} | |
void loop(){ | |
String val = String(analogRead(A0)); | |
client.add(key, val); // specifies the args of type "String" | |
client.sendAll(THIG_NAME); | |
} |
dweet.ioのデータはキーと値の組み合わせで送信します。キー(Key)、値(ValまたはValue)は、String型で指定してください。 たとえば、キー「action」、値「on」のデータを送信するには「client.add(”action”, "on")」と指定します。数値の場合、String型に変換して指定します。
・複数データの送信
・データの取得
getDweet関数の第2引数keyには、取得したいデータのキーを指定します。たとえば、キー「action」のデータを取得するには、「getDweet(THIG_NAME, "action")」と指定します。
dweet.ioに複数のデータ(最大5つ)を送信するプログラムは、次のように書きます。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "dweetESP8266.h" | |
#define THIG_NAME "Your_thing_neme_here" // Put here your thing name | |
#define WIFISSID "ssid" | |
#define PASSWORD "password" | |
dweet client; | |
void setup(){ | |
Serial.begin(115200); | |
delay(10); | |
client.wifiConnection(WIFISSID, PASSWORD); | |
} | |
void loop(){ | |
String value1 = String(analogRead(A0)); | |
String value2 = String(analogRead(A1)); | |
String value3 = String(analogRead(A2)); | |
client.add(key1, value1); // specifies the args of type "String" | |
client.add(key2, value2); | |
client.add(key3, value3); | |
client.sendAll(THIG_NAME); | |
} |
dweet.ioからデータを取得するプログラムは、次のように書きます。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "dweetESP8266.h" | |
#define THIG_NAME "Your_thing_neme_here" // Put here your thing name | |
#define WIFISSID "ssid" | |
#define PASSWORD "password" | |
dweet client; | |
void setup() { | |
Serial.begin(115200); | |
delay(10); | |
client.wifiConnection(WIFISSID, PASSWORD); | |
} | |
void loop() { | |
// specifies the args of type "String" | |
String value = client.getDweet(THIG_NAME, key); | |
Serial.println(value); | |
} |
0 件のコメント:
コメントを投稿