/*
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 (http://community.thingspeak.com/documentation/)
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://www.thingspeak.com/users/new
* Register your Arduino by selecting Devices, Add New Device
* Once the Arduino is registered, click Generate Unique MAC Address
* Enter the new MAC Address in this sketch under "Local Network Settings"
* 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.iamshadowlord.com)
Additional Credits:
Example sketches from Arduino team, Ethernet by Adrian McEwen
updated by Rick Klink to read DS18B20 temperatures and upload to thingspeak
updated by Ondrej Gottwald to read 4 DS18B20 temperatures and upload to thingspeak
*/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Ethernet.h>
// Local Network Settings
byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xD5, 0x63 }; // Must be unique on local network
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
//**********************************************
// Tady zadejte writeAPIkey získaný z ThingSpeak
//**********************************************
String writeAPIKey = "XXXXXXXXXXXXXXXX";
const int updateThingSpeakInterval = 30 * 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;
// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3
#define TempRes 10 // set the resolution to 10 bit (resolution for ds18b20 9, 10, 11, 12)
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Assign the addresses of 1-Wire temp sensors.
// Tady zadejte vlastni adresy cidel DS18B20
DeviceAddress RedTemp = { 0x28, 0xD2, 0xCA, 0xF3, 0x00, 0x00, 0x00, 0xD2 }; // ruda, adresa: 0x28, 0xD2, 0xCA, 0xF3, 0x00, 0x00, 0x00, 0xD2
DeviceAddress YellowTemp = { 0x28, 0x7E, 0x22, 0xF9, 0x03, 0x00, 0x00, 0xE8 }; // zluta, adresa: 0x28, 0x7E, 0x22, 0xF9, 0x03, 0x00, 0x00, 0xE8
DeviceAddress BlueTemp = { 0x28, 0x37, 0xBF, 0x50, 0x03, 0x00, 0x00, 0x6A }; // modra, adresa: 0x28, 0x37, 0xBF, 0x50, 0x03, 0x00, 0x00, 0x6A
DeviceAddress GreenTemp = { 0x28, 0xBC, 0x28, 0xF9, 0x03, 0x00, 0x00, 0xA4 }; // zelena, adresa: 0x28, 0xBC, 0x28, 0xF9, 0x03, 0x00, 0x00, 0xA4
void setup()
{
// Start Serial for debugging on the Serial Monitor
Serial.begin(9600);
// Start Ethernet on Arduino
startEthernet();
sensors.begin();
// set the resolution to TempRes value
sensors.setResolution(RedTemp, TempRes);
sensors.setResolution(YellowTemp, TempRes);
sensors.setResolution(BlueTemp, TempRes);
sensors.setResolution(GreenTemp, TempRes);
}
void loop(void)
{
// Read value from Analog Input Pin 0
//String analogPin0 = String(analogRead(A0), DEC);
// 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();
}
//******************
//Print Temperatures
//******************
Serial.print("Getting temperatures...\n\r");
sensors.requestTemperatures();
float RT = sensors.getTempC(RedTemp); //float = xx,xx ; int = xx
Serial.print("Red Temp is: ");
Serial.print(RT);
Serial.print("C: ");
Serial.print("\n\r");
float YT = sensors.getTempC(YellowTemp); //float = xx,xx ; int = xx
Serial.print("Yellow Temp is: ");
Serial.print(YT);
Serial.print("C: ");
Serial.print("\n\r");
float BT = sensors.getTempC(BlueTemp); //float = xx,xx ; int = xx
Serial.print("Blue Temp is: ");
Serial.print(BT);
Serial.print("C: ");
Serial.print("\n\r");
float GT = sensors.getTempC(GreenTemp); //float = xx,xx ; int = xx
Serial.print("Green Temp is: ");
Serial.print(GT);
Serial.print("C: ");
Serial.print("\n\r");
// Update ThingSpeak
if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
{
updateThingSpeak("field1="+String(RT, DEC)+"&field2="+String(YT, DEC)+"&field3="+String(BT, DEC)+"&field4="+String(GT, DEC));
}
// 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);
}