I'm still struggling with this. Hope y'all don't mind looking at my code, maybe spot what I'm doing wrong. I did pare my sketch back to the bare minimum and it still exhibits the same symptom. The code below simply posts a single number once a minute to ThingSpeak.
I've run it at two other locations and both have the same issue. Both locations have 2Wire routers supplied by AT&T. At my house it works fine, I have a D-Link router. So it seems something is different but I don't know what. I hesitate to blame the routers, because obviously PCs etc. work OK.
I'm starting to fiddle with resetting the WIZ811MJ when a failure occurs. That's probably code I will retain anyway, but I really want to understand what's going on, else I'm just treating the symptom.
Thanks again!
#include <Ethernet.h>
#include <SPI.h>
#include <Streaming.h>
#define WIZ811MJ_RESET 9 //to WIZ811MJ reset pin
#define HB_LED A2 //heartbeat LED
#define SYNC_LED A3 //illuminated when data is sent, extinguish when response received
#define TX_INTERVAL 60 //seconds between data transmissions
#define HB_INTERVAL 1000 //blink interval for heartbeat LED, ms
byte mac[6] = {2, 0, 192, 168, 0, 202};
byte ip[4] = {192, 168, 0, 202};
byte gateway[4] = {192, 168, 0, 1};
byte server[4] = {184, 106, 153, 149}; //ThingSpeak API IP Address
Client client(server, 80); //http client
unsigned long ms; //current time from millis()
unsigned long msLastSend; //ms when last data was sent
boolean lastConnected = false;
char apiKey[] = "16-byte-api-key."; //Thingspeak API key
//Store the larger parts of the fixed post text in progmem.
PROGMEM prog_char post0[] = "POST /update HTTP/1.1\nHost: api.thingspeak.com\nX-THINGSPEAKAPIKEY: ";
PROGMEM prog_char post1[] = "\nContent-Type: application/x-www-form-urlencoded\nContent-Length: ";
PROGMEM prog_char post2[] = "\nConnection: close\n\n";
PROGMEM char *post[] = {post0, post1, post2};
void setup(void)
{
pinMode(HB_LED, OUTPUT);
pinMode(SYNC_LED, OUTPUT);
pinMode(WIZ811MJ_RESET, OUTPUT);
digitalWrite(WIZ811MJ_RESET, LOW); //reset the WIZ811MJ
delay(1000);
digitalWrite(WIZ811MJ_RESET, HIGH);
delay(3000);
Ethernet.begin(mac, ip, gateway);
Serial.begin(115200);
delay(3000);
Serial << _DEC(millis()) << " Starting" << endl;
}
void loop(void)
{
static long count;
ms = millis();
heartBeat();
readServerResponse();
if (msLastSend == 0 || ms - msLastSend >= TX_INTERVAL * 1000UL) {
msLastSend = ms;
sendData(++count);
}
}
void sendData(long data)
{
char cData[8];
int dataLen;
uint8_t connectStatus;
digitalWrite(SYNC_LED, HIGH);
Serial << endl << _DEC(millis()) << " SEND" << endl;
ltoa(data, cData, 10);
dataLen = strlen(cData) + 7; //7 for "field1="
if (!client.connected()) {
if ( (connectStatus = client.connect()) == 0 ) {
lastConnected = true;
Serial << _DEC(millis()) << " CONNECTED" << endl; //connected to server
pmToClient(post[0]);
client << apiKey;
pmToClient(post[1]);
client << _DEC(dataLen);
pmToClient(post[2]);
client << "field1=" << cData << '\n';
Serial << _DEC(millis()) << " POST: " << _DEC(data) << endl;
}
else {
Serial << _DEC(millis()) << " CONNECT FAIL: " << _DEC(connectStatus) << endl;
}
}
else {
Serial << _DEC(millis()) << " WAIT FOR DISCONNECT " << endl;
}
}
void readServerResponse(void)
{
char c;
boolean connected;
if (client.available()) {
Serial << _DEC(millis()) << " SERVER RESP" << endl;
while (client.available()) {
c = client.read();
Serial << c;
}
digitalWrite(SYNC_LED, LOW);
Serial << endl;
}
connected = client.connected();
if (!connected && lastConnected) {
Serial << _DEC(millis()) << " DISCONNECT" << endl << endl;
client.stop();
}
if (connected != lastConnected) {
Serial << _DEC(millis()) << " Connected=" << _DEC(connected) << endl;
lastConnected = connected;
}
}
//Send a progmem string to Ethernet client
void pmToClient(char *s)
{
while (char c = pgm_read_byte_near(s++)) {
client << c;
}
}
void heartBeat(void)
{
static boolean ledState;
static unsigned long msHB; //ms when HB_LED state was changed
if (ms - msHB >= HB_INTERVAL) {
msHB = ms;
digitalWrite(HB_LED, ledState = !ledState);
}
}