Hi guys. I’m trying to figure out a way to update (POST) 2 or more variables using Collections (as suggested in this topic: http://community.ubidots.com/t/how-to-implement-2-variables-with-the-sim900/110/4). The issue in that topic was not resolved so I’m basically asking the question again. What is the correct way of posting multiple variables within one POST command using Collections and an Arduino board.
Here is my code, for the reference:
#include VirtualWire.h
#include Wire.h
#include SPI.h
#include Ethernet.h
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
// Inicializacija spremenljivk
String temperatura = (""); //Init za String sporocilo
bool meritev = false; //ali je prisprela nova RF meritev?
// Nastavitve za oblak
String idvariable1 = "565cbeb07625422e68139107";
String idvariable2 = "565cc36b76254239c5b67cea";
String token = "BDYIvU87JspcOyaabxFRcv3Q207qWKRa4fp6yMlOZvMtY3bMzRBVcVuLCoS8";
void setup() {
//Odprem serial port za debug
Serial.begin(115200);
//Define the receiver pin and rate
vw_set_rx_pin(12); //Sets pin D12 as the RX Pin
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
// 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...");
}
void loop()
{
//Photoresistor (field2)
int photoSens = digitalRead(7);
if (photoSens == 1) {
photoSens = 0;
} else {
photoSens = 1;
}
//Temperature monitor preko RF433MHz
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if ( vw_get_message(buf, &buflen) )
{
temperatura = (""); //reset temperature
meritev = true; //prispela nova meritev! Update oblak!
Serial.print("Temp2 [C]: ");
int i;
for (i = 0; i < buflen; i++)
{
char vrednost = ((char)buf[i]);
temperatura = temperatura + String(vrednost);
}
temperatura.trim();
Serial.println(temperatura);
}
//Osvežimo podatke v oblaku
if (meritev == true)
{
save_value(String(temperatura), String(photoSens));
meritev = false; //reset RF meritve
}
}
void save_value(String value1, String value2)
{
// if you get a connection, report back via serial:
int num = 0;
String var = "[{\"variable\":" + idvariable1 + ", \"value\":" + value1 + "}, {\"variable\":" + idvariable2 + ", \"value\":" + value2 + "}]";
num = var.length();
delay(2000);
if (client.connect("things.ubidots.com", 80))
{
Serial.println("connected");
// New lines according to ubidots support:
client.println("POST /api/v1.6/collections/values HTTP/1.1");
Serial.println("POST /api/v1.6/collections/values HTTP/1.1");
client.println("Accept: application/json; indent=4");
Serial.println("Accept: application/json; indent=4");
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");
Serial.println("Konec prenosa!");
}
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();
}