Connect to Carriot With ESP8266

691

5

5

Posted in TechnologyWireless

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 bu...
Carriots.com 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);
}
 

Share

    Recommendations

    • Space Challenge

      Space Challenge
    • Pocket-Sized Contest

      Pocket-Sized Contest
    • Spotless Contest

      Spotless Contest
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    5 Comments

    this coding is use ESP8266 module if i use Ardiuno UNO and connect to EPS8266 how is the coding. could explain me..

    1 reply

    It is a bit unclear to me what you are asking. You want to use an Arduino UNO and then use an ESP8266 to connect the UNO to Carriot?
    That is quite a different project, but in short you need to use the ESP8266 to make the connection to the internet and to Carriot and then get the data from your uno vis the RxTx UART or via I2C.
    Forgive me for asking why you would want to do that, it seems a very complicated way. The ESP8266 is much more powerfull than the UNO and if you e.g. are just using the Arduino to get more pins, then there are easier ways to do that

    #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);
    }

    #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);
    }