Hello!
This is a little sketch for Initial State, but for the normal arduino with ethernet shield.
They have two examples, one for the Yun and another for arduino and a wifi shield with AT commands (YUK!)
Neither of the two was ideal in my opinion, since you really don’t want to waste a Yun only for posting a couple of sensor readings, or use a strange wifi shield.
I also needed a big sketch size since my project was doing active controls on some appliances, with a http gui and other stuff.
So, after some days of troubleshooting (i am really an amateur programmer) i ended up with a working sketch, derived from the wifi example that works and that i feel to share!
To make it work you have to register, there’s a free account you can try, generate a “Streaming Access Key” in “My Account”
After that you have to create your bucket, i have not made a function for it since only needs to be done once, so for me it’s a waste of code when uploaded.
There are other ways to create the bucket, if you have access to a linux or mac you can simply edit the following command with your access key, bucket key and bucket key and execute it
// curl -k -v -X "POST" -H "Content-Type:application/json" -H "Accept-Version:0.0.1" -H "X-IS-AccessKey: YOUR KEY HERE" -d "{\"bucketKey\": \"YOUR BUCKET KEY HERE\", \"bucketName\": \"YOUR BUCKET NAME HERE\"}" https://groker.initialstate.com/api/buckets
After that put the same keys in the sketch, don’t forget to change your ip, gateway and dns.
#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0D, 0xD5, 0xB6
};
byte ip[] = {
192, 168, 5, 10
};
byte gateway[] = {
192, 168, 5, 3
};
byte subnet[] = {
255, 255, 255, 0
};
byte mydns[] = {
8, 8, 8, 8
};
#define ISDestURL "insecure-groker.initialstate.com"
// Bucket key (hidden reference to your bucket that allows appending):
#define bucketKey "YOUR BUKET KEY HERE"
// Bucket name (name your data will be associated with in Initial State):
#define bucketName "YOUR BUCKET NAME HERE"
// Access key (the one you find in your account settings):
String accessKey ="YOUR ACCESS KEY HERE";
// How many signals are in your stream? You can have as few or as many as you want
const int NUM_SIGNALS = 3;
// What are the names of your signals (i.e. "Temperature", "Humidity", etc.)
// This array is to store our signal data later
String signalName[NUM_SIGNALS] = {"Temperature", "Humidity", "Wind"};
// This array is to store our signal data later
String signalData[NUM_SIGNALS];
//------------------------------SETUP----------------------------//
void setup(void)
{
Serial.begin(9600);
Ethernet.begin(mac, ip, mydns, gateway);
Wire.begin();
}
//------------------------------LOOP----------------------------//
void loop()
{
delay(5000);
postData();
}
void postData()
{
EthernetClient client2;
int temperature;
int humidity;
int Wind;
long xx, yy;
signalData[0] = String(random(18, 25));
if (client2.connect("insecure-groker.initialstate.com", 80))
{
signalData[0] = String(random(18, 40));
signalData[1] = String(random(1, 100));
signalData[2] = String(random(0, 15));
String toSend = "POST /api/events HTTP/1.1\r\n";
toSend += "Host: "ISDestURL"\r\n";
toSend += "Content-Type: application/json\r\n";
toSend += "User-Agent: Arduino\r\n";
toSend += "Accept-Version: ~0\r\n";
toSend += "X-IS-AccessKey: " + accessKey + "\r\n";
toSend += "X-IS-BucketKey: " bucketKey "\r\n";
String payload = "[{\"key\": \"" + signalName[0] + "\", ";
payload += "\"value\": \"" + signalData[0] + "\"}]";
payload += "\r\n";
toSend += "Content-Length: " + String(payload.length()) + "\r\n";
toSend += "\r\n";
toSend += payload;
xx = toSend.length();
for ( yy = 0; yy < xx; yy++)
{
client2.write(toSend[yy]);
Serial.print(toSend[yy]);
}
client2.stop();
Serial.println("Data sent");
}
else
{
Serial.println("POST DATA: Cannot connect to Server");
}
}
I hope this sketch may be useful, i am not a skilled programmer, and i have only made possibly 10% of this sketch, so forgive me for that 10% of so so coding
Fabrizio