Paso 5: El código
Copie y pegue este código en el IDE de Arduino, asegúrese de que ya se instale la biblioteca de la Junta de linkit uno
Abra el Bloc de notas que tiene la variable y la clave token. y en este código.
Después de todo, subir
//Put your variable key in "idvariable" //and put your token key in "token" #include <LTask.h> #include <LWiFi.h> #include <LWiFiClient.h> #define WIFI_AP "YourWiFiName" #define WIFI_PASSWORD "WiFiPassword" #define WIFI_AUTH LWIFI_WPA // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP according to you WiFi AP configuration LWiFiClient client; String idvariable = "YourVariableKey"; String token = "YourTokenKey"; void setup() { // Open serial communications and wait for port to open: LWiFi.begin(); Serial.begin(115200); Serial.println("connecting..."); while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD))) { delay(1000); } } void loop() { int value = analogRead(A0); save_value(String(value)); } void save_value(String value) { // if you get a connection, report back via serial: int num=0; String var = "{\"value\":"+ String(value) + "}"; num = var.length(); delay(10000); if(client.connect("things.ubidots.com", 80)) { Serial.println("connected"); // New lines according to ubidots support: client.println("POST /api/v1.6/variables/"+idvariable+"/values HTTP/1.1"); Serial.println("POST /api/v1.6/variables/"+idvariable+"/values HTTP/1.1"); client.println("Content-Type: application/json"); Serial.println("Content-Type: application/json"); client.println("Content-Length: "+String(num)); Serial.println("Content-Length: "+String(num)); client.println("X-Auth-Token: "+token); Serial.println("X-Auth-Token: "+token); client.println("Host: things.ubidots.com\n"); Serial.println("Host: things.ubidots.com\n"); client.print(var); Serial.print(var+"\n"); } else { // if you didn't get a connection to the server: Serial.println("connection failed"); } if (!client.connected()) { Serial.println(); Serial.println("disconnecting."); client.stop(); } if (client.available()) { char c = client.read(); Serial.print(c); } client.flush(); client.stop(); }