ubidotsのデータソース(Source)、変数(Variable)の作成方法については、以下記事を参照してください。
・Arduino+ubidotsでセンサー・データをリアルタイムで可視(グラフ)化する
This file contains 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 <ArduinoJson.h> | |
int ERRLED =11; | |
String SSID = "<SSID>"; | |
String PASS = "<Password>"; | |
String TOKEN = "<Token>"; | |
String SERVER = "things.ubidots.com"; | |
int PORT = 80; | |
String ID = "<Variable ID>"; | |
String uri= "/api/v1.6/variables/" + ID + "/values"; | |
void setup() { | |
pinMode(ERRLED, OUTPUT); | |
Serial.begin(115200); | |
Serial.println("AT"); | |
if (!Serial.find("OK")) digitalWrite(ERRLED, HIGH); | |
// Connect Wifi | |
Serial.println("AT+CWJAP=\"" + SSID + "\",\"" + PASS + "\""); | |
delay(10000); | |
if (!Serial.find("OK")) digitalWrite(ERRLED, HIGH); | |
// Open TCP connection to the host: | |
Serial.println("AT+CIPSTART=\"TCP\",\"" + SERVER + "\"," + PORT); | |
delay(50); | |
if (!Serial.find("OK")) digitalWrite(ERRLED, HIGH); | |
} | |
void loop() { | |
float val = analogRead(A0); | |
// Reserve memory space | |
StaticJsonBuffer<200> jsonBuffer; | |
// Build object tree in memory | |
JsonObject& dat = jsonBuffer.createObject(); | |
dat["value"] = val; | |
String value; | |
dat.printTo(value); | |
value += "\r\n"; | |
int contentlength = value.length(); | |
// Construct our HTTP call | |
String httpPacket = "POST " + uri + " HTTP/1.1\r\nHost: " + SERVER + "\r\nX-Auth-Token: "+ TOKEN +"\r\nContent-Length: " + contentlength + " \r\nContent-Type: application/json\r\n\r\n" + value +"\r\n"; | |
int length = httpPacket.length(); | |
// Send our message length | |
Serial.print("AT+CIPSEND="); | |
Serial.println(length); | |
delay(10); // Wait a little for the ESP to respond | |
// check if the ESP is running well | |
if (!Serial.find(">")) digitalWrite(ERRLED, HIGH); | |
// Send our http request | |
Serial.print(httpPacket); | |
delay(10); // Wait a little for the ESP to respond | |
// check if the ESP is running wel | |
if (!Serial.find("SEND OK\r\n")) digitalWrite(ERRLED, HIGH); | |
delay(10000); // wait 10 seconds before updating | |
} |
<SSID>には、WiFiのSSIDを、<Password>にはWiFiのパスワードを入力します。
<Token>にはTokenを、<Variable ID>にはVariable IDを入力します。
Tokenは、ubidotsのメニューのユーザー名をクリックし、「API Credentials」を選択すると、確認できます。

ubidotsには、ESP8266のライブラリが用意されていますが、そのライブラリは、まったく使い物になりませんでした。
0 件のコメント:
コメントを投稿