Hi friends i got a thing speak code from arduino forum and i created channel and try to upload with that code it is updating but its showing zero even if my sensor value changes
and the code is as follows
#include <SPI.h>
#include <Ethernet.h>
// Local Network Settings
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xAA, 0xE2 }; // Must be unique on local network
byte ip [] ={192,168, 12, 32};
// ThingSpeak Settings
byte server[] = { 184, 106, 153, 149 }; // IP Address for the ThingSpeak API
String writeAPIKey = "ST1P2E6BTKGI4KQN"; // Write API Key for a ThingSpeak Channel
const int updateInterval = 30000; // Time interval in milliseconds to update ThingSpeak
EthernetClient client;
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int resetCounter = 0;
// All the weather variables from weather station to send to ThingSpeak
float hum = 0; // " " #1
float tempF = 0; // " " #2
char humidity;
char temperature;
void setup()
{
Ethernet.begin(mac,ip);
Serial.begin(9600);
// Serial1.begin(9600);
// delay(1000);
}
void loop()
{
String analogPin0 = String(analogRead(A0), DEC);
// String analogPin1 = String(analogRead(A1), DEC);
while(Serial.available() > 0){
int inByte = Serial.read();
}
if (Serial.find("*")){
hum = Serial.parseFloat();
//tempF = Serial.parseFloat();
}
char hum_buffer[7];
String humidity=dtostrf(hum,0,2,hum_buffer);
//String humidity = "1";
// char tempF_buffer[7];
// String temperature=dtostrf(tempF,0,2,tempF_buffer);
//String temperature = "2";
// 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();
Serial.println("...disconnected.");
Serial.println();
client.stop();
}
// Update ThingSpeak
if(!client.connected() && (millis() - lastConnectionTime > updateInterval))
{
//updateThingSpeak("field1=" + String(humidity) + "&field2=" + String(temperature));
updateThingSpeak("field1=" + String(humidity));
}
lastConnected = client.connected();
}
void updateThingSpeak(String tsData)
{
if(client.connect(server,80)>0)
{
Serial.println("Connected to ThingSpeak...");
Serial.println();
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\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();
resetCounter = 0;
}
Serial.println(analogRead(A0));
}