Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
227 lines (174 sloc) 5.69 KB
/*
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
*/
//modified by Stephen Borsay for Udemy Arduino wireless course
#include <SPI.h>
#include <Ethernet.h>
#include <dht11.h>
dht11 DHT11;
String humid;
String tempCel;
String tempFehr;
String tempKelv;
String dewPnt;
String fastDew;
// Local Network Settings
byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA2 }; // Must be unique on local network
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String writeAPIKey = "TKQB5JN1A8P14ABY";
const int updateThingSpeakInterval = 16 * 1000; // Time interval in milliseconds to update ThingSpeak
// 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);
Serial.println("DHT11 TEST PROGRAM ");
DHT11.attach(2);
// Start Ethernet on Arduino
startEthernet();
}
void loop()
{
int chk = DHT11.read();
humid = (String)DHT11.humidity;
tempCel =(String)DHT11.temperature;
tempFehr = (String)DHT11.fahrenheit();
tempKelv= (String)DHT11.kelvin();
dewPnt = (String)DHT11.dewPoint();
fastDew = (String) DHT11.dewPointFast();
Serial.println("\n");
Serial.print("Read sensor: ");
switch (chk)
{
case 0: Serial.println("OK"); break;
case -1: Serial.println("Checksum error"); break;
case -2: Serial.println("Time out error"); break;
default: Serial.println("Unknown error"); break;
}
// Serial.print("Temperature (°F): ");
// Serial.println(DHT11.fahrenheit(), DEC);
// Read value from Analog Input Pin 0
// String analogValue0 = String(analogRead(A0), DEC);
//
//String temp = (String)(DHT11.fahrenheit());
delay(1000);
// 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))
{
updateThingSpeak( "field1="+humid+
"&field2="+tempCel+
"&field3="+tempFehr+
"&field4="+tempKelv+
"&field5="+dewPnt+
"&field6="+fastDew);
// Serial.println("sent temp humidty data");
// updateThingSpeak("field2="+tempCel);
// Serial.println("\nsent temp Celdata");
// updateThingSpeak("field3="+tempFehr);
// Serial.println("\nsent temp Fehr data");
// updateThingSpeak("field4="+tempKelv);
// Serial.println("\nsent temp Kelv data");
// updateThingSpeak("field5="+dewPnt);
// Serial.println("\nsent temp Dewpoint data");
// updateThingSpeak("field6="+ fastDew);
// Serial.println("\nsent temp fastdew data");
}
// 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);
}
You can’t perform that action at this time.