No Data Transfered to Thingspeak | Arduino | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
No Data Transfered to Thingspeak
Avatar
sheahudson
New Member
Forum Posts: 1
sp_UserOfflineSmall Offline
1
May 15, 2016 - 4:58 am
sp_Permalink sp_Print

Hello, can anyone help me. I am new in learning about ThingSpeak and Arduino. I want to make send data from LM35 that connected to Arduino to ThingSpeak using Ethernet Shield. The program can be run but ThingSpeak does not update any of the data receive. How do I fixed this? This is the sample code that I have try.

Thank You

 

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

int val;
int tempPin = 0;
float tempC;

// Local Network Settings
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF }; // Must be unique on local network

// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "24AU1NCYHNIXFYEN";

// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;

// Initialize Arduino Ethernet Client
EthernetClient client;

void setup()
{
Serial.begin(9600);

// Start Ethernet on Arduino
startEthernet();

}

void loop()

{
int val = 0;
for(int i = 0; i < 10; i++)
{

val += analogRead(tempPin);
delay(200);

}
float tempC = val*50.0f/1023.0f;
char buf[16];

// Read value from Input Pin
String LM35 = dtostrf(tempC,4,1,buf);

Serial.println(LM35);

// Print Update Response to Serial Monitor
if (client.available())
{
char c = client.read();
Serial.print(c);
}

// Disconnect from ThingSpeak
if (!client.connected() && lastConnected)
{
Serial.println("...disconnected");
Serial.println();

client.stop();
}

// Update ThingSpeak
if (!client.connected())
{
updateThingSpeak(LM35);
delay(25000);
}
// Check if Arduino Ethernet needs to be restarted
if (failedCounter > 3 ) {
startEthernet();
}

lastConnected = client.connected();
}

//----- update the Thingspeak string with 1 value
void updateThingSpeak(String M)
{
if (client.connect(thingSpeakAddress, 80))
{
String cmd = writeAPIKey +"&field1=" + M;
Serial.println("Connecting to ThingSpeak...");
Serial.println("Sending Data");
client.print("POST /update HTTP/1.1
");
client.print("Host: api.thingspeak.com
");
client.print("Connection: close
");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"
");
client.print("Content-Type: application/x-www-form-urlencoded
");
client.print("Content-Length: ");
client.print(cmd.length());
client.print("

");

client.print(cmd);
Serial.println(F("Done"));
Serial.println(F("Waiting 30 sec for next sending"));

lastConnectionTime = millis();

if (client.connected())
{
failedCounter = 0;
client.stop();
}
else
{
failedCounter++;

Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
Serial.println();
}

}
else
{
failedCounter++;

Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
Serial.println();

lastConnectionTime = millis();
}
}

void startEthernet()
{

client.stop();

Serial.println("Connecting Arduino to network...");
Serial.println();

delay(1000);

// Connect to network amd obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0)
{
Serial.println("DHCP Failed, reset Arduino to try again");
Serial.println();
}
else
{
Serial.println("Arduino connected to network using DHCP");
Serial.println();
}

delay(1000);
}

//dtostrf (float to String)
char *dtostrf (float val, signed char width, unsigned char prec, char *sout)
{
char fmt[20];
sprintf(fmt, "%%%d.%df", width, prec);
sprintf(sout, fmt, val);
return sout;
}

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
23 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

dannymm1

KamorkasOr

GRV2903

Jusikaclaiz

Raymondpoers

ScottDus

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee