Skip to content

Instantly share code, notes, and snippets.

@AgustinPelaez
Last active September 2, 2015 05:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AgustinPelaez/dff68ce7d717249620b6 to your computer and use it in GitHub Desktop.
Save AgustinPelaez/dff68ce7d717249620b6 to your computer and use it in GitHub Desktop.
Send a collection of values to Ubidots using Liknit One WiFi
/*
Ubidots WiFi client
This sketch reads analog inputs and sends the values to Ubidots
Change the macro WIFI_AP, WIFI_PASSWORD, WIFI_AUTH, URL, TOKEN and VARIABLE_ID accordingly.
based on the Web Client example created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
modified 20 Aug 2014
by MediaTek Inc.
modified 6 Jun 2015
by Ubidots, Inc.
*/
#include <LTask.h>
#include <LWiFi.h>
#include <LWiFiClient.h>
#include <LBattery.h>
#define WIFI_AP "ubidots"
#define WIFI_PASSWORD "123456789"
#define WIFI_AUTH LWIFI_WPA // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.
// Ubidots information
#define URL "things.ubidots.com"
#define TOKEN "your-token" // replace with your Ubidots token generated in your profile tab
#define VARID1 "55e5ff1d76254254e182cc9b" // create a variable in Ubidots and put its ID here (http://app.ubidots.com/ubi/datasources/)
#define VARID2 "55e617e1762542762d69d541"
//#define VARID3 "55dc8a3d76254218aa3e394b"
int counter = 0;
void setup()
{
LTask.begin();
LWiFi.begin();
Serial.begin(9600);
// keep retrying until connected to AP
Serial.println("Connecting to AP");
while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
{
delay(1000);
}
}
// I plan to use this function in the future to reconnect to WiFi if the connection drops:
const char * const wifi_status_str(LWifiStatus ws){
switch(ws){
case LWIFI_STATUS_DISABLED:
return "WiFi module status: disabled";
break;
case LWIFI_STATUS_DISCONNECTED:
return "WiFi module status: disconnected";
break;
case LWIFI_STATUS_CONNECTED:
return "WiFi module status: connected";
break;
}
return "WiFi status: error, no such status";
}
void loop()
{
Serial.println("Connecting to Ubidots...");
LWiFiClient c;
while (!c.connect(URL, 80))
{
Serial.println("Retrying to connect...");
delay(100);
}
Serial.println("Connected!");
while(1){
//LWifiStatus ws = LWiFi.status();
//Serial.println(wifi_status_str(ws));
String payload = "[{\"variable\":\"" VARID1 "\",\"value\":"+ String(analogRead(A0)) + "},{\"variable\":\"" VARID2 "\",\"value\":" + String(analogRead(A1)) + "}]";
String le = String(payload.length());
if(!c.connected()){
while (!c.connect(URL, 80)){
delay(100);
}
Serial.println("Client reconnected!");
}
// Build HTTP POST request
c.print(F("POST /api/v1.6/collections/values/?token="));
c.print(TOKEN);
c.println(F(" HTTP/1.1"));
c.println(F("Content-Type: application/json"));
c.print(F("Content-Length: "));
c.println(le);
c.print(F("Host: "));
c.println(URL);
c.println();
c.println(payload);
int v;
while(c.available()){
v = c.read();
if(v < 0){
Serial.println("No response.");
break;
}
//Serial.print((char)v);
}
delay(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment