Last active
August 29, 2015 14:09
-
-
Save BenGriffiths/08c3defca3925f69ce89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <LiquidCrystal.h> | |
#include <SPI.h> | |
#include <Ethernet.h> | |
#include <string.h> | |
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7); | |
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; | |
char server[] = "www.mysite.com"; | |
IPAddress ip(192,168,0,56); | |
EthernetClient client; | |
int sendAfter = 55; | |
float sinceLastSend = 55; | |
int timer = 55; | |
int loopDelay = 100; | |
int therm1 = 0; | |
int therm2 = 1; | |
const int numReadings = 60; | |
int tempReadings[numReadings]; | |
int tempIndex = 0; | |
float tempTotal = 0; | |
float tempAverage = 0; | |
float tempCurrent = 0; | |
char tempBuffer[20]; | |
void setup() { | |
Serial.begin(9600); | |
lcd.clear(); | |
lcd.begin(16, 2); | |
lcd.setCursor(0,0); lcd.print("Temperature"); | |
lcd.setCursor(0,1); lcd.print("Monitor Tool"); | |
delay(3000); | |
for (int thisReading = 0; thisReading < numReadings; thisReading++) { tempReadings[thisReading] = 0; } | |
lcd.clear(); | |
lcd.begin(16, 2); | |
lcd.setCursor(0,0); lcd.print("Initializing"); | |
lcd.setCursor(0,1); lcd.print("Ethernet..."); | |
delay(1000); | |
if (Ethernet.begin(mac) == 0) { | |
lcd.setCursor(0,0); | |
lcd.print("No"); | |
lcd.setCursor(1,0); | |
lcd.print("Ethernet!"); | |
Ethernet.begin(mac, ip); | |
} | |
else | |
{ | |
lcd.clear(); | |
lcd.setCursor(0,0); | |
lcd.print("Address:"); | |
lcd.setCursor(0,1); | |
lcd.print(Ethernet.localIP()); | |
} | |
delay(3000); | |
lcd.clear(); | |
} | |
void loop() { | |
lcd.setCursor(0,0); | |
lcd.print("Next Upload: "); | |
lcd.setCursor(13,0); | |
lcd.print(timer); lcd.print(" "); | |
if(sinceLastSend <= 0){ | |
sinceLastSend = sendAfter; | |
timer = sendAfter; | |
sendData(); | |
}else{ | |
int reading1 = analogRead(therm1); | |
int reading2 = analogRead(therm2); | |
float voltage1 = reading1 * 5.0; voltage1 /= 1024.0; | |
float voltage2 = reading2 * 5.0; voltage2 /= 1024.0; | |
float temperatureC1 = (voltage1 - 0.5) * 100; | |
float temperatureC2 = (voltage2 - 0.5) * 100; | |
// I find that when you use the USB connection instead of | |
// mains power, you might need to remove around 5 degrees | |
// from the final value. | |
float averageTemp = ((temperatureC1 + temperatureC2) / 2); | |
tempTotal= tempTotal - tempReadings[tempIndex]; | |
tempReadings[tempIndex] = averageTemp; | |
tempTotal= tempTotal + tempReadings[tempIndex]; | |
tempIndex = tempIndex + 1; | |
if (tempIndex >= numReadings) { tempIndex = 0; } | |
tempAverage = tempTotal / numReadings; | |
// | |
averageTemp = tempAverage; | |
tempCurrent = averageTemp; | |
lcd.setCursor(0,1); lcd.print("Temp: "); lcd.print(averageTemp); | |
sinceLastSend -= 0.1; | |
if(sinceLastSend < timer){ timer--; } | |
} | |
delay(loopDelay); | |
} | |
String URLEncode(const char* msg) | |
{ | |
const char *hex = "0123456789abcdef"; | |
String encodedMsg = ""; | |
while (*msg!='\0'){ | |
if( ('a' <= *msg && *msg <= 'z') | |
|| ('A' <= *msg && *msg <= 'Z') | |
|| ('0' <= *msg && *msg <= '9') ) { | |
encodedMsg += *msg; | |
} else { | |
encodedMsg += '%'; | |
encodedMsg += hex[*msg >> 4]; | |
encodedMsg += hex[*msg & 15]; | |
} | |
msg++; | |
} | |
return encodedMsg; | |
} | |
void sendData() { | |
lcd.clear(); | |
lcd.setCursor(0,0); lcd.print("Connecting..."); | |
dtostrf(tempCurrent, 4, 2, tempBuffer); | |
String getRequest = "GET /homemonitor/log.php?temperature="; | |
getRequest += URLEncode(tempBuffer); | |
getRequest += " HTTP/1.1"; | |
if (client.connect(server, 80)) { | |
lcd.clear(); | |
lcd.setCursor(0,0); lcd.print("Connected"); | |
lcd.setCursor(0,1); lcd.print(server); | |
client.println(getRequest); | |
client.print( "Host: " ); | |
client.println(server); | |
client.println("User-Agent: Arduino-Box"); | |
client.println(); | |
client.stop(); | |
client.flush(); | |
delay(4000); | |
} | |
else { | |
lcd.clear(); | |
lcd.setCursor(0,0); | |
lcd.print("Connection"); | |
lcd.setCursor(0,1); | |
lcd.print("Failed! :("); | |
delay(5000); | |
} | |
lcd.clear(); | |
lcd.begin(16, 2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment