Good morning,
I have some code which is similar to the below (I just did a few fixes for the overloaded string) which basically takes 6 x 1-wire sensors and uploads the results to thingspeak. For some reason though the timing of the below code is not working correctly. It appears every 15 seconds to upload the code to thing speak even though the interval should be 150 seconds. Also when I try to change the 150 * 1000 to another value (e.g. 300) it no longer uploads as all. Is there something in my code that would prevent this from working correctly, as I want it to upload say every 5 minutes instead of every 15 seconds.
Many thanks!
/*
Arduino --> ThingSpeak Channel via Ethernet
The ThingSpeak Client sketch is designed for the Arduino and Ethernet.
This sketch updates a channel feed with an analog input reading via the
ThingSpeak API (https://thingspeak.com/docs)
using HTTP POST. The Arduino uses DHCP and DNS for a simpler network setup.
The sketch also includes a Watchdog / Reset function to make sure the
Arduino stays connected and/or regains connectivity after a network outage.
Use the Serial Monitor on the Arduino IDE to see verbose network feedback
and ThingSpeak connectivity status.
Getting Started with ThingSpeak:
* Sign Up for New User Account - https://thingspeak.com/users/new
* Create a new Channel by selecting Channels and then Create New Channel
* Enter the Write API Key in this sketch under "ThingSpeak Settings"
Arduino Requirements:
* Arduino with Ethernet Shield or Arduino Ethernet
* Arduino 1.0+ IDE
Network Requirements:
* Ethernet port on Router
* DHCP enabled on Router
* Unique MAC Address for Arduino
Created: October 17, 2011 by Hans Scharler (http://www.nothans.com)
Additional Credits:
Example sketches from Arduino team, Ethernet by Adrian McEwen
*/
#include <SPI.h>
#include <Ethernet.h>
// Local Network Settings
byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network
#include <OneWire.h>
//Get DallasTemperature Library here: http://milesburton.com/Main_Page?title=Dallas_Temperature_Control_Library
#include <DallasTemperature.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define ONE_WIRE_BUS_PIN 3
/*-----( Declare objects )-----*/
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS_PIN);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
/*-----( Declare Variables )-----*/
DeviceAddress Probe01 = { 0x28, 0x82, 0xEC, 0x0A, 0x06, 0x00, 0x00, 0xAB };
DeviceAddress Probe02 = { 0x28, 0xEF, 0xEB, 0x0A, 0x06, 0x00, 0x00, 0x73 };
DeviceAddress Probe03 = { 0x28, 0x26, 0xB0, 0x0A, 0x06, 0x00, 0x00, 0x9F };
DeviceAddress Probe04 = { 0x28, 0xF0, 0x84, 0x09, 0x06, 0x00, 0x00, 0xFA };
DeviceAddress Probe05 = { 0x28, 0x48, 0x8E, 0x0A, 0x06, 0x00, 0x00, 0x69 };
DeviceAddress Probe06 = { 0x28, 0xFF, 0xB3, 0x0A, 0x06, 0x00, 0x00, 0x83 };
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "API_KEY_HERE";
const int updateThingSpeakInterval = 150 * 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval)
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
// Initialize Arduino Ethernet Client
EthernetClient client;
void setup()
{
// Start Serial for debugging on the Serial Monitor
Serial.begin(9600);
sensors.begin();
// set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
sensors.setResolution(Probe01, 10);
sensors.setResolution(Probe02, 10);
sensors.setResolution(Probe03, 10);
sensors.setResolution(Probe04, 10);
sensors.setResolution(Probe05, 10);
sensors.setResolution(Probe06, 10);
// Start Ethernet on Arduino
startEthernet();
}
void loop()
{
// Read value from Analog Input Pin 0
// 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("...disconnected");
Serial.println();
client.stop();
}
// Update ThingSpeak
if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
{
sensors.requestTemperatures();
delay(1000);
String TempValue1 = String(sensors.getTempC(Probe01), DEC);
String TempValue2 = String(sensors.getTempC(Probe02), DEC);
String TempValue3 = String(sensors.getTempC(Probe03), DEC);
String TempValue4 = String(sensors.getTempC(Probe04), DEC);
String TempValue5 = String(sensors.getTempC(Probe05), DEC);
String TempValue6 = String(sensors.getTempC(Probe06), DEC);
updateThingSpeak("field1="+TempValue1+"&field2="+TempValue2+"&field3="+TempValue3+"&field4="+TempValue4+"&field5="+TempValue5+"&field6="+TempValue6);
}
// Check if Arduino Ethernet needs to be restarted
if (failedCounter > 3 ) {startEthernet();}
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();
}
delay(1000);
}