Jump to content









Photo

Sending streams with an Arduino YUN

arduino arduinoYUN code tutorial

  • Please log in to reply
1 reply to this topic

#1 alvaro

alvaro

    Carriots CTO

  • Administrators
  • 72 posts

Posted 24 March 2014 - 05:44 PM

Hi all.

 

Just to let you know that we have published some code to send data to the platform with an Arduino YUN. You can check it here: https://www.carriots...eam/arduino_yun

The code uses the Process library from the YUN's Bridge to launch a curl command.

I drop the code here also. Let me know if I can help you with anything related to Carriots.

 

#include <Process.h>

#define APIKEY        "98346..ab4" // TO BE REPLACED with your Carriots APIKEY
#define DEVICE        "test@carriots" // TO BE REPLACED with your Device's ID developer

// set up net client info:
const unsigned long postingInterval = 10000;  //delay between updates to Carriots
unsigned long lastRequest = 0;      // when you last made a request
String dataString = "";

void setup() {
  // start serial port:
  Bridge.begin();
  Serial.begin(9600);

  while (!Serial);   // wait for Network Serial to open
  Serial.println("Carriots client");

  // Do a first update immediately
  updateData();
  sendData();
  lastRequest = millis();
}

void loop() {
  // get a timestamp so you can calculate reading and sending intervals:
  long now = millis();

  // if the sending interval has passed since your
  // last connection, then connect again and send data:
  if (now - lastRequest >= postingInterval) {
    updateData();
    sendData();
    lastRequest = now;
  }
}

void updateData() {
  // convert the readings to a String to send it:
  dataString = "{\"protocol\":\"v1\",\"checksum\":\"\",\"device\":\"";
  dataString += DEVICE;
  dataString += "\",\"at\":\"now\",\"data\":{\"temp\":";
  dataString += random(10) + 20;
  dataString += "}}";
  Serial.println("\n\nDataString:\n"+dataString);
  
}

void sendData() {
  // form the string for the APIKEY header parameter:
  String apiString = "carriots.apikey: ";
  apiString += APIKEY;

  // Send the HTTP POST request
  
  Process carriots;
  Serial.print("\n\nSending data... ");
  carriots.begin("curl");
  carriots.addParameter("-k");
  carriots.addParameter("--request");
  carriots.addParameter("POST");
  carriots.addParameter("--data");
  carriots.addParameter(dataString);
  carriots.addParameter("--header");
  carriots.addParameter(apiString);
  carriots.addParameter("https://api.carriots.com/streams/");
  carriots.run();
  Serial.println("done!");

  // If there's incoming data from the net connection,
  // send it out the Serial:
  while (carriots.available() > 0) {
    char c = carriots.read();
    Serial.write(c);
  }

}

 

Enjoy Carriots!


Alvaro Everlet

Carriots CTO


#2 snehal.jabade

snehal.jabade

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 16 September 2015 - 11:00 AM

Hi,

 

  I have run the same code with Arduino YUN by replacing API key & Device's ID developer with my own.In serial monitor I am able to see :

Carriots client


DataString:
{"protocol":"v1","checksum":"","device":"test@acarriots","at":"now","data":{"temp":27}}


Sending data... done!

 

 

But In carriots control panel's stream list, I am not able to see this data.

Would you please suggest me what I am doing wrong?

Please do needful.

Thanks







Also tagged with one or more of these keywords: arduino, arduinoYUN, code, tutorial

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users