I have used Agustin’s demo code he emailed me to send 10 variables to a Ubidots account.
This code is working pretty good for me.
Next I tried to add switch to the dashboard that would control a Ubidots Variable when it was switches. I tried to integrate the Ubidots GET example code along with my working code below but I get compile errors about having EthernetClient & Ubidots Client in the same code.
I’ve spent hours trying to figure it out myself but with no luck.
I tried you new library that was just uploaded to Git 3 days ago but I can’t get that to work at all.
So I figured it may be best to see if you guys can easily add a GET request to the code that I have working below:
#include <Ethernet.h>
#include <SPI.h>
#include <UbidotsEthernet.h>
#define ID "56dc7b147625422fb6ec085d" // Put here your Ubidots variable ID
#define TOKEN "ZCZxcFGzfyB1mFTztUFnUaj8K0ZIt4" // Put here your Ubidots TOKEN
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 };
EthernetClient client;
String idvariable = "56dbd26276254207a77f17dc"; //
String idvariable_current = "56dbd265762542088bd9353e"; //
String idvariable_power = "56dbd23776254207d8704fcd"; //
String idvariable_soc = "56dbd22476254206ffac0bbc"; //
String idvariable_ce = "56dbd25f762542088bd93536"; //
String idvariable_ttg = "56dbd26976254208af6ba4b3"; //
String idvariable_h2 = "56dbd26a7625420869504855"; //
String idvariable_h4 = "56dbd26776254207a77f17e5"; //
String idvariable_h6 = "56dbd27576254208441ef594"; //
String idvariable_h9 = "56dbd9207625422dff9d9600"; //
String token = "ZCZxcFGzfyB1mFTztUFnUaj8K0ZIt4"; //Omers Account
int Relay = 2;
void setup() {
pinMode(Relay, OUTPUT); //Set's Digital Pin 2 to a Output Pin
digitalWrite(Relay, LOW); // Turns off relay.
// disable SD SPI
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
//Baud is set to 19200 because this is the baud that the BMV-600 transmits at.
Serial.begin(19200);
//Serial.println("BMV Battery Monitor");
//Serial.println("==========================");
delay(1000);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;);
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
}
delay(3000);
}
void loop() {
// main program loop
Serial.flush();
Serial.begin(19200);
while (Serial.available() == 0);
{
char ch = Serial.read();
if(ch == 'V') // Wait for the capital V and ignore any detritus before it. The capital V (Voltage) marks the beginning of the data thats what we actually want.
{
int V = Serial.parseInt(); //Battery Voltage
int I = Serial.parseInt(); //Current in or out of the battery
int P = Serial.parseInt(); //W Instantaneous power
long CE = Serial.parseInt(); //Consumed Energy
unsigned int SOC = Serial.parseInt(); //State of charge
long TTG = Serial.parseInt(); //Time to Go
long AR = Serial.parseInt(); //Alarm relay state
long BMV = Serial.parseInt(); //The 600 part of 600S gets parsed as it is an integer
long FW = Serial.parseInt(); //Firmware version gets parsed as it is an integer
long Ha = Serial.parseInt(); // This parses the "1" integer from "H1" The next line will parse actual the value of H1
long H1 = Serial.parseInt(); //Depth of Deepest Discharge mAh
long Hb = Serial.parseInt(); // This parses the "2" integer from "H2" The next line will parse the actual value of H2 etc.
long H2 = Serial.parseInt(); //Depth of Last Discharge (mAh)
long Hc = Serial.parseInt();
long H3 = Serial.parseInt(); //Depth of Average Discharge (mAh)
long Hd = Serial.parseInt();
unsigned long H4 = Serial.parseInt(); //Number of Charge cycles
long He = Serial.parseInt();
unsigned long H5 = Serial.parseInt(); //Number of full discharges
long Hf = Serial.parseInt();
long H6 = Serial.parseInt(); //Cumulative Ah from battery (mAh)
long Hg = Serial.parseInt();
unsigned int H7 = Serial.parseInt(); //Minimum battery voltage (mV)
long Hh = Serial.parseInt();
unsigned int H8 = Serial.parseInt(); //Maximum battery voltage (mV)
long Hi = Serial.parseInt();
unsigned long H9 = Serial.parseInt(); //Hours since last full charge.
// long Hj = Serial.parseInt();
// unsigned long H10 = Serial.parseInt(); //Number of times BMV automatically synchronised
//long Hk = Serial.parseInt();
//unsigned long H11 = Serial.parseInt(); //Number of Low voltage alarms
//long Hl = Serial.parseInt();
//unsigned long H12 = Serial.parseInt(); //Number of High voltage alarms
// long Hm = Serial.parseInt();
//unsigned long H17 = Serial.parseInt(); //Amount of discharged energy
//long Hn = Serial.parseInt();
//unsigned long H18 = Serial.parseInt(); //Amount of charged energy
if ((V /1000.00) > 8)
{
// Serial.println("");
// Serial.println("---------------------------------------");
// Serial.println ("Voltage Above 8V - Send Data");
// Serial.println("---------------------------------------");
// Serial.println("");
//Code to send variable to Ubidots
float value = (V /1000.00);
float value_current = (I /1000.00);
int value_power = P;
int value_soc = (SOC / 10);
float value_ce = (CE /1000.000);
float value_ttg = (TTG /60.00);
float value_h2 = (H2 /1000.00);
float value_h4 = H4;
float value_h6 = (H6 /1000.00);
float value_h9 = (H9 /86400.00);
save_value(String(value));
save_value_current(String(value_current));
save_value_power(String(value_power));
save_value_soc(String(value_soc));
save_value_ce(String(value_ce));
save_value_ttg(String(value_ttg));
save_value_h2(String(value_h2));
save_value_h4(String(value_h4));
save_value_h6(String(value_h6));
save_value_h9(String(value_h9));
// Serial.println("");
// Serial.println("---------------------------------------");
// Serial.println("All Send Functions Complete");
// Serial.println("---------------------------------------");
//Serial.println("");
Serial.end();
delay(1000 * 30);
}
else
{
// Do Nothing. Wait till next reading.
// Serial.println("");
// Serial.println("---------------------------------------");
// Serial.println("Votage Reading Below 8V - Discard Data");
// Serial.println("---------------------------------------");
// Serial.println("");
Serial.end();
delay(1000);
}
}
}
}
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(300);
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();
}
void save_value_current(String value_current)
{
// if you get a connection, report back via serial:
int num=0;
String var = "{\"value\":"+ String(value_current) + "}";
num = var.length();
delay(300);
if(client.connect("things.ubidots.com", 80))
{
Serial.println("connected");
// New lines according to ubidots support:
client.println("POST /api/v1.6/variables/"+idvariable_current+"/values HTTP/1.1");
Serial.println("POST /api/v1.6/variables/"+idvariable_current+"/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();
}
void save_value_power(String value_power)
{
// if you get a connection, report back via serial:
int num=0;
String var = "{\"value\":"+ String(value_power) + "}";
num = var.length();
delay(300);
if(client.connect("things.ubidots.com", 80))
{
Serial.println("connected");
// New lines according to ubidots support:
client.println("POST /api/v1.6/variables/"+idvariable_power+"/values HTTP/1.1");
Serial.println("POST /api/v1.6/variables/"+idvariable_power+"/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();
}
void save_value_soc(String value_soc)
{
// if you get a connection, report back via serial:
int num=0;
String var = "{\"value\":"+ String(value_soc) + "}";
num = var.length();
delay(300);
if(client.connect("things.ubidots.com", 80))
{
Serial.println("connected");
// New lines according to ubidots support:
client.println("POST /api/v1.6/variables/"+idvariable_soc+"/values HTTP/1.1");
Serial.println("POST /api/v1.6/variables/"+idvariable_soc+"/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();
}
void save_value_ce(String value_ce)
{
// if you get a connection, report back via serial:
int num=0;
String var = "{\"value\":"+ String(value_ce) + "}";
num = var.length();
delay(300);
if(client.connect("things.ubidots.com", 80))
{
Serial.println("connected");
// New lines according to ubidots support:
client.println("POST /api/v1.6/variables/"+idvariable_ce+"/values HTTP/1.1");
Serial.println("POST /api/v1.6/variables/"+idvariable_ce+"/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();
}
void save_value_ttg(String value_ttg)
{
// if you get a connection, report back via serial:
int num=0;
String var = "{\"value\":"+ String(value_ttg) + "}";
num = var.length();
delay(300);
if(client.connect("things.ubidots.com", 80))
{
Serial.println("connected");
// New lines according to ubidots support:
client.println("POST /api/v1.6/variables/"+idvariable_ttg+"/values HTTP/1.1");
Serial.println("POST /api/v1.6/variables/"+idvariable_ttg+"/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();
}
void save_value_h2(String value_h2)
{
// if you get a connection, report back via serial:
int num=0;
String var = "{\"value\":"+ String(value_h2) + "}";
num = var.length();
delay(300);
if(client.connect("things.ubidots.com", 80))
{
Serial.println("connected");
// New lines according to ubidots support:
client.println("POST /api/v1.6/variables/"+idvariable_h2+"/values HTTP/1.1");
Serial.println("POST /api/v1.6/variables/"+idvariable_h2+"/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();
}
void save_value_h4(String value_h4)
{
// if you get a connection, report back via serial:
int num=0;
String var = "{\"value\":"+ String(value_h4) + "}";
num = var.length();
delay(300);
if(client.connect("things.ubidots.com", 80))
{
Serial.println("connected");
// New lines according to ubidots support:
client.println("POST /api/v1.6/variables/"+idvariable_h4+"/values HTTP/1.1");
Serial.println("POST /api/v1.6/variables/"+idvariable_h4+"/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();
}
void save_value_h6(String value_h6)
{
// if you get a connection, report back via serial:
int num=0;
String var = "{\"value\":"+ String(value_h6) + "}";
num = var.length();
delay(300);
if(client.connect("things.ubidots.com", 80))
{
Serial.println("connected");
// New lines according to ubidots support:
client.println("POST /api/v1.6/variables/"+idvariable_h6+"/values HTTP/1.1");
Serial.println("POST /api/v1.6/variables/"+idvariable_h6+"/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();
}
void save_value_h9(String value_h9)
{
// if you get a connection, report back via serial:
int num=0;
String var = "{\"value\":"+ String(value_h9) + "}";
num = var.length();
delay(300);
if(client.connect("things.ubidots.com", 80))
{
Serial.println("connected");
// New lines according to ubidots support:
client.println("POST /api/v1.6/variables/"+idvariable_h9+"/values HTTP/1.1");
Serial.println("POST /api/v1.6/variables/"+idvariable_h9+"/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();
}
So I just want to add a simple GET request in the main loop to check the state of a Ubidots variable using the code below:
float value11 = client.getValue(ID);
Serial.println(value);
Any help is Greatly Appericated!