Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Swati_S_Intel
Employee
1,046 Views

Data is not being sent to ThingSpeak via Edison

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

# include

# include

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

}

}

0 Kudos
3 Replies
idata
Community Manager
67 Views

Hi SwatiD,

 

 

The first thing when configuring a Thingspeak is creating a channel. Once the channel is created you'll have the option to add data by sending a POST or GET to a provided URL. Make sure you've put the correct URL into your code. You can see a descriptive video on how to add data in Thingspeak here http://www.instructables.com/id/INTEL-EDISONWIFITHINGSPEAK/?ALLSTEPS http://www.instructables.com/id/INTEL-EDISONWIFITHINGSPEAK/?ALLSTEPS .

 

 

Now, when doing the Arduino code, it's important to set the correct variables with the Thingspeak settings, such as the address and the writeAPIKey. One thing I noticed from your code is you don't have a loop () in your code. I'd suggest you to take a look at https://software.intel.com/en-us/articles/internet-of-things-a-plant-monitor-on-the-intel-edison-boa... https://software.intel.com/en-us/articles/internet-of-things-a-plant-monitor-on-the-intel-edison-boa... . This link has an example on how to use Thingspeak with Arduino and has the sample code divided into the different sections needed to build the custom code. It has sections such as declaring the variables, read from the feed, what information should be on the setup () and loop () sections of the code, and how to interact with different sensors.

 

 

I'd suggest you to take a look at the previous link and have it as a reference on what sections should be on your code and what functions are used to communicate with Thingspeak.

 

 

Let us know if you find this useful.

 

 

-Sergio

 

Swati_S_Intel
Employee
67 Views

HI Sergio,

Thank-you for replying. I got my code working with few amendments.

I have intentionally put blank loop () since I don't need to record response time of ALERT as part of my project.

Thank-you for the help

idata
Community Manager
67 Views

Thank you for letting us know you managed to get the code working. If you have other questions don't hesitate to open a new thread to receive help from the community.

 

 

-Sergio

 

Reply