Connected arduino with thingspeak (simple)

Hello, my name is Arief. I am from Indonesia. I have a project and am having trouble uploading data to Thingspeak via w5100 ethernet.

I do not make ip static to connect with thingspeak. Initially Thingspeak managed to display the data. But after 3 minutes, Thingspeak does not display data. Is the procedure wrong?

i have code :

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

int pin = 0; // analog pin
int tempc = 0,tempf=0; // temperature variables
int samples[8]; // variables to make a better precision
int maxi = -100,mini = 100; // to start max/min temperature
int i;

// Local Network Settings
byte mac = { 0xD4, 0xA8, 0xE2, 0xFE, 0xA0, 0xA1 }; // Must be unique on local network
byte ip = { 192,168,0,230 }; // Must be unique on local network
byte gateway = { 192,168,0,1};
byte subnet = { 255, 255, 255, 0 };

// ThingSpeak Settings
char thingSpeakAddress = “api.thingspeak.com”;
String writeAPIKey = “C09IRCIHKMPF19HP”; // Write API Key for a ThingSpeak Channel
const int updateInterval = 10000; // Time interval in milliseconds to update ThingSpeak

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

// Initialize Arduino Ethernet Client
EthernetClient client;

void setup()
{

Serial.begin(9600);
Ethernet.begin(mac, ip, gateway, subnet);
delay(1000);
Serial.print("ETHERNET SHIELD ip is : ");
Serial.println(Ethernet.localIP());
// Start Ethernet on Arduino
startEthernet();
}

void loop()
{

tempc = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;

String analogPin0 = String(tempc);

// 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=”+analogPin0);
}

lastConnected = client.connected();
}

void updateThingSpeak(String tsData)
{
if (client.connect(thingSpeakAddress, 80))
{
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();

if (client.connected())
{
Serial.println(“Connecting to ThingSpeak…”);
Serial.println();

failedCounter = 0;
}
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();
Serial.println(“Data being uploaded to THINGSPEAK Server…”);
Serial.println();
}

delay(1000);
}


Does your program produce any error messages when the data isnt sent to Thingspeak?

Suggest you add some code to read the response in case that gives you a clue. I didnt see where you do client.stop() after you send either.

   client.print("Content-Length: ");
    client.print(tsData.length());
    client.print("\n\n");

    client.println(tsData);  // change this to println

    // add this to print out response
    while (client.available()) {
      char c = client.read();
      Serial.print(c);
    }