#ifdef ENABLE_THINGSPEAK_UPDATE
// ----------------- update Thingspeak
if (!b_waiting_for_NTP_response && (millis() > TStimer) && (millis() - TStimer) >= TS_UPDATE_PERIOD_MILLIS) {
Thingspeak_timeout_count += 1;
system_temperature = read_temperature ();
cpu_voltage = read_5V_system_voltage ();
battery_voltage = read_12V_system_voltage ();
system_current = read_system_current ();
float system_watts = battery_voltage * system_current;
// ether.copyIp(ether.hisip, thingspeak_ip);
// ether.printIp(F("Stored Thingspeak ip: "), ether.hisip);
// if (thingspeak_ip[0] > 0 && Thingspeak_timeout_count < TS_TIMEOUT_LIMIT) { // keep using stored TS ip for at least 5 timeouts
// ether.copyIp(ether.hisip, thingspeak_ip);
// ether.printIp(F("Stored Thingspeak ip: "), ether.hisip);
// } else {
// Serial.println(F("ThingSpeak DNS lookup in process"));
// if (!ether.dnsLookup(TSwebsite)) {
// Serial.println(F("ThingSpeak DNS failed"));
// } else {
// ether.copyIp(thingspeak_ip, ether.hisip);
// Thingspeak_timeout_count = 0; // reset timeout count
// }
// }
ether.printIp(F("Thingspeak: "), ether.hisip);
byte sd = stash.create();
stash.print(F("field1="));
stash.print(battery_voltage);
stash.print(F("&field2="));
stash.print(cpu_voltage);
stash.print(F("&field3="));
stash.print(system_current);
stash.print(F("&field4="));
stash.print(system_watts);
stash.print(F("&field5="));
stash.print(system_temperature);
stash.save();
// generate the header with payload - note that the stash size is used,
// and that a "stash descriptor" is passed in as argument using "$H"
Stash::prepare(PSTR("POST /update HTTP/1.0" "\r\n"
"Host: $F" "\r\n"
"Connection: close" "\r\n"
"X-THINGSPEAKAPIKEY: $F" "\r\n"
"Content-Type: application/x-www-form-urlencoded" "\r\n"
"Content-Length: $D" "\r\n"
"\r\n"
"$H"),
TSwebsite, // "api.thingspeak.com"
PSTR(TS_APIKEY),
stash.size(),
sd);
// send the packet - this also releases all stash buffers once done
session = ether.tcpSend();
// added from: http://jeelabs.net/boards/7/topics/2241
int freeCount = stash.freeCount();
if (freeCount <= 3) {
Stash::initMap(56);
}
TStimer = millis();
}
const char* reply = ether.tcpReply(session);
if (reply != 0) {
Serial.println(F(" >>> TSREPLY:"));
Serial.println(reply);
ether.printIp(F("TS IP: "), ether.hisip);
if (strncmp(reply, "HTTP/1.1 404 Not Found", 22) == 0) { // PSTR isn't working here
Thingspeak_timeout_count = TS_TIMEOUT_LIMIT;
// thingspeak_ip[0] = 0; // Invalidate the TS ip address, force a new DNS lookup
} else {
Thingspeak_timeout_count = 0;
}