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;
}