Author Topic: Working with the Carriots API  (Read 7360 times)

digi_guy

  • Jr. Member
  • **
  • Posts: 87
Working with the Carriots API
« on: May 02, 2016, 07:31:03 am »
I wanted to share a bit of the work I've been doing that involves posting to, and reading from, the Carriots server. Carriots is an IOT system similar to the Particle cloud but with one major advantage: the ability to push data. You  can create what they call "listeners" which allow users to write pretty complicated scripts on their servers that can process the data we upload. The listeners can then use "triggers" to push data in a variety of ways including text, email, and update webpages. Carriots can provide an interesting option for connecting your oak to the rest of the world.

As an example, you could set up your oak to monitor temperature and send it to the Carriots cloud (just like with Particle). If the temp gets too hot the "listener" could send you an email notification with an alert. Within that email you could put a link to a webpage that then sends data back to the Carriots server. From there you could either have an Oak listening for instructions, or you could have the Carriots server send a message to your Oak to turn something on or off.

A few years ago I was using Carriots with a raspberry pi web server to walk to one of the original Digisparks.  Now with the Oak it's possible to use it as a stand alone device.

Setting up an account is free, and certainly worth checking out.

Here is some sample code that uploads time and temperature:
Code: [Select]
#include "ESP8266WiFi.h"

const String APIKEY = "put_you_api_key_here"; // Replace with your Carriots apikey
const String DEVICE = "put_your_device_here"; // Replace with the id_developer of your device
IPAddress server(82,223,244,60);  // api.carriots.com IP Address check this

int analogPin = A0;     // potentiometer wiper (middle terminal) connected to analog pin 3
int val = 0;           // variable to store the value read
double temp = 3.3;
double Vout = 3.3;
double Vref = 3.18;
double reading = 3.3;
char buf;
String s;

unsigned long lastTime = 0UL;
char publishTemp[40];
char publishTime[40];

WiFiClient client;



void setup() {
 
}


void loop() {
  unsigned long now = millis();

    //Every 15 seconds send data to Carriots
    if (now-lastTime>15000UL) {
        lastTime = now;
        // now is in milliseconds
        unsigned nowSec = now/1000UL;
        unsigned sec = nowSec%60;
        unsigned min = (nowSec%3600)/60;
        unsigned hours = (nowSec%86400)/3600;
        sprintf(publishTime,"%u:%u:%u",hours,min,sec);
        Particle.publish("Uptime",publishTemp);
       
        reading = analogRead(analogPin);    // read the input pin and convert to C
        Vout = reading*Vref/1024;
        temp = (Vout - 1.25)/0.005;
        s = String(temp);
        s.toCharArray(publishTemp, 40);
        Particle.publish("TempC",publishTemp);

        sendStream();  //call function to send data
    }

   while (client.available()) {
    String msg = client.readString();
    Particle.publish("Status", msg.c_str());
    }

  if (!client.connected()) {
      client.stop();
  }

}



void sendStream()
{

  if (client.connect(server, 80)) {   // If there's a successful connection
   
    // Build the data field
    String json = "{\"protocol\":\"v2\",\"device\":\""+DEVICE+"\",\"at\":\"now\",\"data\":{\"Temp\":\""+publishTemp+"\",\"Time\":\""+publishTime+"\"}}";

    // Make a 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 you didn't get a connection to the server:
    Particle.publish("Status", "connection failed");
  }
}

Here is the code to request data from the Carriots server:
Code: [Select]
#include <ESP8266WiFi.h>

const String APIKEY = "put_you_api_key_here"; // Replace with your Carriots apikey
const String DEVICE = "put_your_device_here"; // Replace with the id_developer of your device
IPAddress server(82,223,244,60);  // api.carriots.com IP Address check this

int msg;

WiFiClient client;

void setup() {


  if (client.connect(server, 80)) {   // If there's a successful connection
    Particle.publish("Status", "Starting");


    client.println("GET https://api.carriots.com/streams/?device="+DEVICE+"&max=1&order=-1 HTTP/1.1"); //this will request 1 message, and -1 means it will be the last one sent
    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.println("Connection: close");
    client.println();

  }



}

void loop() {
      while (client.available()) {
        if (client.find("200 OK")){ Particle.publish("Status", "success"); delay(1000);}
        int i=0;
        char buf[64];
       
        if (client.find("_id\":")){
          char comma = char(44);
          String new_data = "ID: ";
          new_data.concat(client.readStringUntil(comma));
          new_data.toCharArray(buf, 64);
          Particle.publish("Status", buf);
          delay(1000);
        }

        if (client.find("device\":")){
          char comma = char(44);
          String new_data = "Device: ";
          new_data.concat(client.readStringUntil(comma));
          new_data.toCharArray(buf, 64);
          Particle.publish("Status", buf);
          delay(1000);
        }
         
        if (client.find("data\":{")){
          char comma = char(125);
          String new_data = "Data: ";
          new_data.concat(client.readStringUntil(comma));
          new_data.toCharArray(buf, 64);
          Particle.publish("Status", buf);
          delay(1000);
        }
         
        if (client.find("created_at\":")){
          char comma = char(44);
          String new_data = "Created: ";
          new_data.concat(client.readStringUntil(comma));
          new_data.toCharArray(buf, 64);
          Particle.publish("Status", buf);
          delay(1000);
        }

        if (client.find("owner\":")){
          char comma = char(125);
          String new_data = "Owner: ";
          new_data.concat(client.readStringUntil(comma));
          new_data.toCharArray(buf, 64);
          Particle.publish("Status", buf);
          delay(1000);
        }

}











This is what the data from Carriots looks like:

Code: [Select]



This is what Carriots returns
[code]
// "result": [
//        {
//            "_id": "571d22b25c5d752b1e57b7e9",
//            "_t": "str",
//            "at": 1461527218,
//            "device": "temp@username",
//            "protocol": "v2",
//            "data": {
//                "Temp": "386.00",
//                "Time": "0:22:0"
//            },
//            "id_developer": "uniqueID@username",
//            "created_at": 1461527218,
//            "owner": "username"       
//       





digi_guy

  • Jr. Member
  • **
  • Posts: 87
Re: Working with the Carriots API
« Reply #1 on: May 02, 2016, 10:03:17 am »
Oops, forgot to include some links.

The Carriots main page is www.carriots.com

And Adafruit has a pretty good tutorial for connecting an arduino to an email alert:
https://learn.adafruit.com/wireless-gardening-arduino-cc3000-wifi-modules/sending-data-to-carriots

Carriots also has a number of tutorials for connecting arduino, RPi, as well as a few other IoT systems.

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: Working with the Carriots API
« Reply #2 on: May 07, 2016, 02:50:48 pm »
Thanks for sharing this - I hadn't heard of carriots before - looks like they have a solid free plan!