problem with connecting thing speak

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

https://thingspeak.com/channels/75093/private_show

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

You have two different variables named humidity

char humidity;

declared at global level, and

 String humidity=dtostrf(hum,0,2,hum_buffer);

declared in the loop() function.

Later on in loop() you have

updateThingSpeak("field1=" + String(humidity));

Which of the two variables it uses I don't know but from the symptoms that you describe I would guess that it uses the global one which is never updated from its original value of zero. Delete the global one and see what happens.

You might also look at the examples associated with the ThingSpeak Communication Library:

chaitraacharya376: byte server[] = { 184, 106, 153, 149 }; // IP Address for the ThingSpeak API

That is NOT the api.thingspeak.com server.

Please be aware: IP addresses on the Internet may change quickly. So íf you want to connect to a server that is present on the internet, connect to the NAME OF THE SERVER and NOT THE IP ADDRESS OF THE SERVER.

Perhaps use:

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

Generally, I agree with jurs -- you shouldn't hardcode IP addresses. In this case, however, it's correct. That is the published, fixed IP address of the ThingSpeak server (load balancer). Ideally, api.thingspeak.com is a better choice.

-Rob

Thingspeak is connected only if i use byte server[] = { 184, 106, 153, 149 }; // IP Address for the ThingSpeak API If i use: char server[] = "api.thingspeak.com"; the com port shows only values like 288 282 215 and so on but no updation in thingspeak. Even now I get a flat red line at 0 with red dots denoting the updation.

chaitraacharya376: Even now I get a flat red line at 0 with red dots denoting the updation.

If you send the data to "thingspeak.com" instead of "api.thingspeak.com", it is perfectly normal, that there are no data available at "api.thingspeak.com", other than a zero-line.

You don't do the slightest HTTP error handling, so you never will know, if the (wrong) server respnds to the request with something like perhaps: Error 500 (Internal Server Error when sending your data to the wrong server.

No error handling ==> no error message ==> you never will know whats going wrong

Which is the original sketch you tried to adapt? Is it possibly: https://github.com/iobridge/ThingSpeak-Arduino-Examples/blob/master/Ethernet/Arduino_to_ThingSpeak.ino Or what?

And which changes do you want to make to the working example sketch? Your code looks like you want to send fields like "humidity" and "temperature". Where do they come from? analogRead? Serial.read()? Reading sensors and doing calculations? Float or int?

If you're getting zeros on the chart at ThingSpeak, then you're sending data -- but it's all zeros. Check your sketch to see if the string your passing to updateThingSpeak -- try sending the string to the serial port to see what's in it.