freeboard.io is no more for free :(
What is Dweet.io?It's an easy way to send data (we will call them dweets) through a TCP connection with Dweet.io servers. It basically sends dweets, retrieves dweets and offers a lot of options for us to play with - easy and free.
With an amazing library that does the heavy lifting. Using #include <ESP8266WiFi.h>
, we create a TCP connection with the host dweet.io and send HTTP messages like a search in our normal navigator! PLAY WITH IT
Well Dweet.io stores and collects our data, and Freeboard.io chews that data and put it into a really fancy deskboard! We have a lot of options for managing dweets: gauges, stream video, charts, text labels, etc.
First we need to do the usual stuff, #includes, variable declaration, etc.
// WiFi parameters
const char* ssid = "your wifi name";
const char* password = "your wiffi password";
String thingName="TYPICALSENSORTHING";//thing for grouping all the data together
const char* host = "dweet.io"; //host :) for tcp connection
This is the magical part; we declare two global strings (outside loop or setup). One of them is going to have the names of our variables, and the other one the values of these variables paired by index of the array (e.g., SEIS=6).
String arrayVariableNames[]={"UNO","DOS","TRES","CUATRO","CINCO","SEIS","SIETE","OCHO"};//THIS WAY WE REFER TO EACH VARIABLE AS AN INDEX IN THIS GLOBAL ARRAY.
int arrayVariableValues[]={1,2,3,4,5,6,7,8};
int numberVariables=sizeof(arrayVariableValues)/sizeof(arrayVariableValues[0]);//tells the number of arguments inside each array
Just add or remove arguments in both of them as you need: for example, if you had one of those boring humidity/temperature sensors, I would change it like this:
String arrayVariableNames[]={"humidity","temperature"};//THIS WAY WE REFER TO EACH VARIABLE AS AN INDEX IN THIS GLOBAL ARRAY.
int arrayVariableValues[]={20,50};
int numberVariables=sizeof(arrayVariableValues)/sizeof(arrayVariableValues[0]);//tells the number of arguments inside each array
numberVariables()
calculates the amount of variables for proper indexing - don't worry about that.
We start the serial comms with the PC and send some messages for debugging - typical stuff.
wifi.begin()
the name says it all. It connects to the WiFi SSID we provided with the password we also provided, then it checks status of connections and retries 15 times before giving up (it cannot connect at all if we misspell the SSID or the password).
//////////////////////////////
void setup() {
Serial.begin(9600);
///////////wifi things
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
//initialice and connect to wifi lan
WiFi.begin(ssid, password);
int retries = 0;
while ((WiFi.status() != WL_CONNECTED) && (retries < 15)) {
retries++;
delay(500);
Serial.print(".");
}
if(retries>14){
Serial.println(F("WiFi conection FAILED"));
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println(F("WiFi connected"));
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println(F("======================================================"));
}
Serial.println(F("Setup ready"));
}
LOOP():Play with your things and when you are ready just stick them inside.
void loop() {
arrayVariableValues[0]=25;//this is how you change values of the variables
arrayVariableValues[1]=42;
arrayVariableValues[2]=9;
arrayVariableNames[0]="veinticinco";//this is how you change the name of the variable( veinticinco=25)
dweetdata();//upload it
}
More magical and funny functions we need:dweetdata()
: this manages the TCP connection with the host (dweet.io).
void dweetdata(){//connects TCP,sends dweet,drops connection, prints the server response on the console
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
client.print(GetDweetStringHttpBuilder());
delay(10);//slow doown ...stability stuff
// Read all the lines of the reply from dweet server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
//just display ending conection on the serial port
Serial.println();
Serial.println("closing connection");
}
GetDweetStringBuilder():
this is a plain function that gets our data, converts it into a fancy string so we can use client.println() function and sends the dweet.
String GetDweetStringHttpBuilder() {
int i=0;
String dweetHttpGet="GET /dweet/for/";//initial empty get request
dweetHttpGet=dweetHttpGet+String(thingName)+"?";//start concatenating the thing name (dweet.io)
for(i=0;i<(numberVariables);i++){//concatenate all the variable names and values
if(i==numberVariables-1){
dweetHttpGet=dweetHttpGet + String(arrayVariableNames[i]) + "="+ String( arrayVariableValues[i]);//the lastone doesnt have a "&" at the end
}
else
dweetHttpGet=dweetHttpGet + String(arrayVariableNames[i]) + "="+ String( arrayVariableValues[i]) + "&";
}
dweetHttpGet=dweetHttpGet+" HTTP/1.1\r\n"+
"Host: " +
host +
"\r\n" +
"Connection: close\r\n\r\n";
return dweetHttpGet;//this is our freshly made http string request
}
Happy IOTing! :D
Comments