3 Replies Latest reply on Nov 9, 2016 6:18 AM by Intel Corporation

    Data is not being sent to ThingSpeak via Edison

    SwatiD

      My ping command to ThingSpeak is not responding when I am trying to connect my edison to ThingSpeak. I am using Arduino IDE.

      I need to update once to ThingSpeak. Is there any mistake in code?

      Please let me know.

       

      #include <Wire.h>

      #include <SPI.h>

      #include <WiFi.h>

       

      //Ultrasonic Sensor

      #define echoPin 11

      #define trigPin 12

      #define indicatorLedPin 13

       

      // Configuring Wifi

      char ssid[] = "WirelessNetworkName";  // wireless network name

      char password[] = "WirelessNetworkPassword"; // wireless password

      int status = WL_IDLE_STATUS;

      WiFiClient client;

       

      // ThingSpeak Settings

      char thingSpeakAddress[] = "api.thingspeak.com";

      String APIKey = "4XZ9NA14HQZ7BYD7";             // enter your channel's Write API Key

      const int updateThingSpeakInterval = 20 * 1000; // 20 second interval at which to update ThingSpeak

       

      // Variable Setup

      long lastConnectionTime = 0;

      boolean lastConnected = false;

      int calibrationTime = 10000;

      static int count = 1;

       

      void setup() {

        // Start Serial for debugging on the Serial Monitor

        Serial.begin(115200);

        pinsInit();

        connectWifi();

        printWifiStatus();

        Serial.println("Starting cloud implementation of computation of traffic density...");

        turnOnLED(); // turn on the indicator LED to indicate that calibration is happening

        calibrate();

        turnOffLED(); // turn off the indicator LED to indicate that calibration is completed

        delay(50);

        vehicleDensity();

      }

       

      /********************************/

      // This function //

      /********************************/

       

       

      void pinsInit()

      {

        pinMode(echoPin, INPUT);

        pinMode(trigPin, OUTPUT);

        pinMode(indicatorLedPin, OUTPUT);

      }

       

      void connectWifi() {

        // Attempt to connect to wifi network

        while ( status != WL_CONNECTED) {

          Serial.print("Attempting to connect to SSID: ");

          Serial.println(ssid);

          status = WiFi.begin(ssid);

          // Wait 10 seconds for connection

          delay(10000);

        }

      }

       

      void printWifiStatus() {

        // Print the SSID of the network you're attached to

        Serial.print("SSID: ");

        Serial.println(WiFi.SSID());

       

        // Print your WiFi  IP address

        IPAddress ip = WiFi.localIP();

        Serial.print("IP Address: ");

        Serial.println(ip);

       

        // Print the received signal strength

        long rssi = WiFi.RSSI();

        Serial.print("signal strength (RSSI):");

        Serial.print(rssi);

        Serial.println(" dBm");

      }

       

      void turnOnLED()

      {

        digitalWrite(indicatorLedPin,HIGH);

      }

       

      void turnOffLED()

      {

        digitalWrite(indicatorLedPin,LOW);

      }

       

      void calibrate ()

      {

        Serial.println("Ultrasonic sensor");

        // add a calibration setup

        // calibrate during the first ten seconds

        Serial.println("Please calibrate your ultrasonic sensor in the next 10 seconds");

        digitalWrite(indicatorLedPin, HIGH); // turn on the indicator LED to indicate that calibration is happening

        for (int i = 0; i < calibrationTime; i++) {

          Serial.print(".");

          delay(100);

        }

        Serial.println(" done");

        Serial.println("SENSOR ACTIVE");

      }

       

       

      void vehicleDensity()

      {

        long duration, distance;

        int offset = 1;

        //Ultrasonic Sensor

        for (int i = 10; i > -1; i--) {

          digitalWrite(trigPin, LOW);

          delayMicroseconds(2);

          digitalWrite(trigPin, HIGH);

          delayMicroseconds(10);

          digitalWrite(trigPin, LOW);

          duration = pulseIn(echoPin, HIGH);

          distance = (duration / 2) / 29.1;

          Serial.print("Count: ");

          Serial.println(count);

          Serial.print("Distance: ");

          Serial.print(distance);

          Serial.println(" cm");

          count++;

        }

        Serial.print("No of vehicles:");

        Serial.println(count);

        delay(1000);

        Serial.println("");

        String countVal = String(count,DEC);

       

        // Disconnect from ThingSpeak

        if (!client.connected() && lastConnected) {

          Serial.println("...disconnected");

          Serial.println();

          client.stop();

        }

       

        // Update ThingSpeak

        if (!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval)) {

          updateThingSpeak("field1=" + countVal);

          Serial.println(countVal);

        }

        lastConnected = client.connected();

       

        // Print Update Response to Serial Monitor

        rcv_ans();

      }

       

      void updateThingSpeak(String tsData) {

        if (client.connect(thingSpeakAddress, 80)) {

        Serial.println("connecting...");

         // POST URI

          client.print("POST /update HTTP/1.1\n");

        Serial.println("POST /update HTTP/1.1\n");

       

        // Host header

          client.print("Host: api.thingspeak.com\n");

        Serial.println("Host: api.thingspeak.com\n");

       

          client.print("Connection: close\n"); ////////////////////////////////////////////////////////////

       

        // ThingSpeak Services application key

          client.print("X-THINGSPEAKAPIKEY: " + APIKey + "\n");

        Serial.println("X-THINGSPEAKAPIKEY: " + APIKey + "\n");

       

          client.print("Content-Type: application/x-www-form-urlencoded\n");

          client.print("Content-Length: ");

          client.print(tsData.length());

          client.print("\n\n");

          client.print(tsData);

          lastConnectionTime = millis();

       

       

          while (client.connected()) {

            Serial.println("Connecting to ThingSpeak...");

            Serial.println();

          }

        }

        else {

        // If you couldn't make a connection:

          Serial.println("Connection failed");

          Serial.println("Disconnecting.");

          client.stop();

        }

      }

       

       

      void rcv_ans(){

         while (client.available()) {

          char c = client.read();

          Serial.print(c);

        }

      }