○準備
①「ESP8266でIoT - 開発環境のセットアップ」の手順で開発環境をセットアップします。
②「Arduino+ubidotsでセンサー・データをリアルタイムで可視(グラフ)化する」の「①ubidotsの準備」の手順でubidotsのデータソース(Source)、変数(Variable)を作成します。
○ライブラリのインストール
ubidotsのライブラリをインストールするには、次のように操作します。
①UbidotsMicroESP8266 library(ubidots-nodemcu-master.zip)をダウンロードします。
②Arudino IDEを起動します。
③[スケッチ]→[ライブラリをインクルード]→[.ZIP形式のライブラリをインクルード]を選択します。
④ダウンロードしたZIPファイル「ubidots-nodemcu-master.zip」を選択し、[開く]をクリックします。
○プログラム(スケッチ)
※プログラムを書込む際には、GPIO0をLowにします。
・1つのデータの送信
ubidotsに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 "UbidotsMicroESP8266.h" | |
#define ID "<variable ID>" // Put here your Ubidots variable ID | |
#define TOKEN "<TOKEN>" // Put here your Ubidots TOKEN | |
#define SSID "<ssid>" | |
#define PASS "<password>" | |
Ubidots client(TOKEN); | |
void setup(){ | |
Serial.begin(115200); | |
delay(10); | |
client.wifiConnection(SSID,PASS); | |
} | |
void loop(){ | |
float value = analogRead(A0); | |
client.add(ID, value); | |
client.sendAll(); | |
} |
・複数のデータの送信
ubidotsに複数のデータ(最大3つ)を送信するプログラムは、次のように書きます。
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 "UbidotsMicroESP8266.h" | |
#define ID1 "<variable ID1>" // Put here your Ubidots variable ID | |
#define ID2 "<variable ID2>" | |
#define ID3 "<variable ID3>" | |
#define TOKEN "<TOKEN>" // Put here your Ubidots TOKEN | |
#define SSID "<ssid>" | |
#define PASS "<password>" | |
Ubidots client(TOKEN); | |
void setup(){ | |
Serial.begin(115200); | |
delay(10); | |
client.wifiConnection(SSID, PASS); | |
} | |
void loop(){ | |
float value1 = analogRead(A0); | |
float value2 = analogRead(A1); | |
float value3 = analogRead(A2); | |
client.add(ID1, value1); | |
client.add(ID2, value2); | |
client.add(ID3, value3); | |
client.sendAll(); | |
} |
・データの取得
ubidotsからデータを取得するプログラムは、次のように書きます。
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 "UbidotsMicroESP8266.h" | |
#define ID "<variable ID>" // Put here your Ubidots variable ID | |
#define TOKEN "<TOKEN>" // Put here your Ubidots TOKEN | |
#define SSID "<ssid>" | |
#define PASS "<password>" | |
Ubidots client(TOKEN); | |
void setup() { | |
Serial.begin(115200); | |
delay(10); | |
client.wifiConnection(SSID,PASS); | |
} | |
void loop() { | |
float value = client.getValue(ID); | |
Serial.println(value); | |
} |
<variable ID>にはvariable IDを、<TOKEN>にはubidotsのTOKENを、<ssid>にはWiFiのSSIDを、<password>にはWiFiのパスワードを入力します。
0 件のコメント:
コメントを投稿