Introduction: Connect to Carriot With ESP8266

About: I am a physician by trade. After a career in the pharmeceutical world I decided to take it a bit slower and do things I like. Other than my hobbies that involves grassroots medicine in S.E.&P Asia. I have buil…
Carriots.com, now Altair Smartworks, is a flexible IoT platform that can connect to a variety of devices and can collect data in XML or JSON format. It can process that data and on the basis of that send out data, e.g. to mail or SMS and I believe it can even Tweet. Apparently it is quite popular to attach a PIR sensor that then can send you a mail id it detects motion Carriots does have free accounts. Those are limited in the number of devices one can connect (only 2), the number of emails (max 100/day) and the number of SMS' (max 5/day) but it is enough for the average hobby use. For now, I will focus on how to get the data into Carriots, so i will not go into detail about the inner workings of carriots, that in fact can be a bit overwhelming for those used to 'simple' Thingspeak, but I think it took me about 10-15 minutes to figure it all out and set up my account. One warning though, one of the setting screens does have some info in a non scrolling sidebar. If you have set your screen to zoom in (Ctrl +) you may not see that info. Therefore I advise to take the quick tour after signing up so you have some quick orientation about the various screens. Anyway after signing up most of the work is done from the cpanel, but it comes down to "connecting" a device that then defines a datastream. Don't be put off, once you are there, it becomes quite clear. The ESP8266 is not between the default devices so just take "Other". Once you are done you need to go the API screen to get your API and you need to make note of your DeviceID. The latter is often said to be "streamname@userid", but in my case it was "streamname@userid.userid". You need the API Key and the DeviceID to send data to Carriots. The below code reads the Analog port of the ESP8266 and uploads that to your Carriot datastream
#include "ESP8266WiFi.h"
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const char* server = "api.carriots.com";
// Replace with your Carriots apikey
const String APIKEY = "47777777777778c98cbb";
const String DEVICE = "xxxxx@yyy.yyy"; // your deviceID
WiFiClient client;

int val = 0;

void setup() {
Serial.begin(115200);
delay(1000);
// start wifi
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
// Send data to Carriot

void sendStream()
{
//const int httpPort = 80;
if (client.connect(server, 80)) { // If there's a successful connection
Serial.println(F("connected"));

// construct the data json
String json = "{\"protocol\":\"v2\",\"device\":\"" + DEVICE + "\",\"at\":\"now\",\"data\":{\"moisture\":\"" + val + "\"}}";

// Make an HTTP request
client.println("POST /streams HTTP/1.1");
client.println("Host: api.carriots.com");
client.println("Accept: application/json");
client.println("User-Agent: Arduino-Carriots");
client.println("Content-Type: application/json");
client.print("carriots.apikey: ");
client.println(APIKEY);
client.print("Content-Length: ");
int thisLength = json.length();
client.println(thisLength);
client.println("Connection: close");
client.println();
client.println(json);
}
else {
// If server connection failed:
Serial.println(F("connection failed"));
}
}
void loop() {
val = analogRead(A0);
Serial.println(val);
Serial.println(F("Send Data"));
sendStream();
delay(30000);
}