Jump to content









Photo

ARD wifi shield not streaming data


  • Please log in to reply
No replies to this topic

#1 Abdul786

Abdul786

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 18 August 2016 - 02:18 PM

Hi

i am using arduino wifi Shied to post data on carriots however is not working.

i followed the online tutorial

 

/*
    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 <WiFi.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 = "514a9f48abe748db686149f315ae58e5d909ad22f428aac6cd040f1508dd2dac"; // Replace with your Carriots apikey
const String DEVICE = "defaultDevice@Abdul786.Abdul786"; // 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
char ssid[] = "route-Factory";      //  your network SSID (name)
char pass[] = "r0b0t2014";   // your network password

int status = WL_IDLE_STATUS;

// initialize the library instance:
WiFiClient client;
IPAddress server(82,223,244,60);  // api.carriots.com IP Address



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"));


 while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }
}

// 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 its night. We have to turn on the LED
    digitalWrite(ledPin, HIGH);   // Turn the LED on (HIGH is the voltage level)
  }
  else {
    newLights = ON;               // Now its 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 theres 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©;
  }

  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 theres 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 didnt get a connection to the server:
    Serial.println(F("connection failed"));
  }


}
  






3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users