thingSpeak

Hi, newbie here... i was using some example code for updating data on thingspeak....

there's a portion of the code that should disconnect from thingspeak in order to conect again to repost...

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

thing is, i can't get it to disconect... unless i change && for ||.... can somebody explain me please...

(again i m new ay this...)

my code:
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <Boards.h>
#include <Firmata.h>
#include<SPI.h>
#include<SD.h>

//thingspeak setting
char thingSpeakAddress[] = "api.thingspeak.com"; //"184.106.153.149/update";
String writeAPIKey = "D1CX8RE5JTF8S4E6";
const int sInterval = 16000;

long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
//..

byte mac[] = { 0xD3, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 };
const int temperaturePin = 0;
const int potPin = 1;
const int ledPin = 7;
const int chipSelect = 4;
EthernetClient client;

void setup()
{

Serial.begin(9600);
pinMode(ledPin, OUTPUT);
startEthernet();

}

void loop()
{
float voltage, degreesC,threshold;
float conversion;

voltage = getVoltage(temperaturePin);
threshold = getThreshold(potPin);

degreesC = (voltage - 0.5) * 100.0;
conversion = (round((threshold - 0.5) * 100.0));

if (client.available())
{
char c = client.read();
Serial.print(c);
}

Serial.print("voltage: ");
Serial.print(voltage);
Serial.print(" deg C: ");
Serial.print(degreesC);
Serial.print(" Voltajepot : ");
Serial.print(threshold);
Serial.print(" conversion.round : ");
Serial.println(conversion);

if (degreesC > conversion) { digitalWrite(ledPin, HIGH); }
else
if (degreesC < conversion) { digitalWrite(ledPin, LOW); }
Serial.print("esperando");
delay(sInterval);

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

// Update ThingSpeak
if(!client.connected() && (millis() - lastConnectionTime > sInterval))
{
updateThingSpeak("field1="+String(degreesC));
}
// Check if Arduino Ethernet needs to be restarted
if (failedCounter > 3 ) {startEthernet();}
lastConnected = client.connected();
}

float getVoltage(int pin)
{

return (analogRead(pin) * 0.004882814);
}

float getThreshold(int pin)
{

return (analogRead(pin) * 0.004882814);
}

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
{
if (Ethernet.begin(mac) == 1)
Serial.println("Arduino connected to network using DHCP");
Serial.println();
}
delay(1000);
}

Your problem as I see is:

if (!client.connected() && lastConnected)

That '!' says IF NOT client.connected(), it should say, IF client.connected().
So, just remove the exclamation point '!' and you should be done.