Jump to content









Photo

Arduino connection to Carriots

arduino

  • Please log in to reply
4 replies to this topic

#1 chileM2M

chileM2M

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 19 February 2013 - 06:18 PM

Hello, I'm developing a test project with an Arduino and everything seems fine (no compile errors) but I don't manage to connect to Carriots.

 

This is my arduino code:

 

/*
    Carriots.com
    Created 27 Dec 2012

    This sketch sends streams to Carriots according to the values read by a LDR sensor

*/
#include <SPI.h>
#include <Ethernet.h>

const int ON = 1;    // Constant to indicate that lights are on
const int OFF = 2;  // Constant to indicate that lights are off
const String APIKEY = "my apikey"; // Replace with your Carriots apikey
const String DEVICE = "defaultDevice@chileM2M"; // Replace with the id_developer of your device

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,16);       // Your IP Address
IPAddress server(82,223,244,60);  // api.carriots.com IP Address

EthernetClient client; // Initialize the library instance

int ledPin = 7;        // Led pin number
int LDRPin = 3;        // LDR sensor pin number

int lights = OFF;       // Current status
int newLights = OFF;    // New status



// The setup routine runs once when you press reset
void setup() {

  pinMode(ledPin, OUTPUT);        // Initialize the digital pin as an output

  Serial.begin(9600);             // Start serial port
  Serial.println(F("Starting"));

  Ethernet.begin(mac,ip);         // Start the Ethernet connection
  delay(2000);                    // Give the Ethernet shield a second to initialize
}

// The loop routine runs over and over again forever
void loop() {

  int val = analogRead(LDRPin);   // Read the value from the sensor
  Serial.println(val);

  if (val > 990) {  // This is the value limit between day or night with our LDR sensor. Maybe you need adjust this value.
    newLights = OFF;             // Now it's night. We have to turn on the LED
    digitalWrite(ledPin, HIGH);   // Turn the LED on (HIGH is the voltage level)
  }
  else {
    newLights = ON;               // Now it's day. We have to turn off the LED
    digitalWrite(ledPin, LOW);    // Turn the LED off by making the voltage LOW
  }

  if (lights != newLights) {        // Check if we have a change in status
    Serial.println(F("Send Stream"));
    lights = newLights;             // Status update and send stream
    sendStream();
  }

  delay(500);

  // If there's incoming data from the net connection, send it out the serial port
  // This is for debugging purposes only
  while (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

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

// Send stream to Carriots
void sendStream()
{
  String txt = "";          // Text to send
  if ( lights == OFF ) {   // Alarm OFF
     txt = "OFF";
  } else {                  // Alarm ON
     txt = "ON";
  }
  Serial.println(txt);      // For debugging purpose only

  if (client.connect(server, 80)) {   // If there's a successful connection
    Serial.println(F("connected"));
    // Build the data field
    String json = "{\"protocol\":\"v2\",\"device\":\""+DEVICE+"\",\"at\":1356390000,\"data\":{\"light\":\""+txt+"\"}}";

    // 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:
    Serial.println(F("connection failed"));
  }
}

I've replaced mi api key with the one picked at my account page.

 

Any help is appreciated.

 

Thanks.

 

R



#2 jpastor

jpastor

    Development leader

  • Administrators
  • 159 posts

Posted 20 February 2013 - 08:08 AM

Hello friend

Your problem is that you are using the MAC address of our example

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

You have to use the MAC address of your own Ethernet shield. The Ethernet shields have a MAC address printed on a sticker.

If you have any other question, please use this forum or our support email.

Hope it helps!

Enjoy Carriots



#3 chileM2M

chileM2M

    Newbie

  • Members
  • Pip
  • 5 posts

Posted 20 February 2013 - 11:20 AM

Thanks! It now works fine, I can see my streams in Carriots!



#4 karthikld

karthikld

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 10 July 2017 - 01:43 AM

HI there! I have entered my APIKEY and DEVICE ID along with my ethernet MAC address, computer's IP address but still can't receive the streams into my carriots account? Can you please help me @jpastor ?



#5 jpastor

jpastor

    Development leader

  • Administrators
  • 159 posts

Posted 11 July 2017 - 10:01 AM

Hi karthikld

 

With this information is a bit difficult help you.

What error are you receiving?
Could share with us your code?

 

Regards!







Also tagged with one or more of these keywords: arduino

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users