#include <Ethernet.h>
#include <string.h>
#include <stdio.h>
unsigned int getTemp();
unsigned int getLight();
boolean sendUpdate();
#define TEMP 0 //Analog temperature sensor
#define LIGHT 1 //Analog Light sensor
boolean sendUpdate(){
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 117 }; // this is the ip within my lan
byte gateway[] = { 192, 168, 1, 1 }; // neccessary to get access to the internet via your router
byte subnet[] = { 255, 255, 255, 0 };
byte server[] = { 128, 121, 146, 100 }; // Twitter's ip
unsigned int temp=getTemp();
unsigned int light=getLight();
char tempinterest[]="";
char lightinterest[]="";
char msg[140] = "";
char stats[] = "status=";
char tempc[5];
itoa(temp, tempc, 10);
if(temp > 100){
strcpy(tempinterest,"So Hot I'm going to die");
} else if(temp >= 95){
strcpy(tempinterest,"Hot as hell");
} else if(temp > 80){
strcpy(tempinterest,"Hot");
} else if(temp < 65){
strcpy(tempinterest,"Cold");
} else if(temp < 60){
strcpy(tempinterest,"real cold");
} else if(temp < 55){
strcpy(tempinterest,"freezing");
} else {
strcpy(tempinterest,"Comfortable");
}
if(light > 70){
strcpy(lightinterest,"bright");
} else if(light < 40){
strcpy(lightinterest,"dark");
} else {
strcpy(lightinterest,"not too dark");
}
strcat(msg, "It's ");
strcat(msg, tempinterest);
strcat(msg, " (");
strcat(msg, tempc);
strcat(msg, "F) and ");
strcat(msg, lightinterest);
strcat(msg, " in here");
Serial.println(msg);
Ethernet.begin(mac, ip, gateway, subnet);
Client client(server, 80);
Serial.println("connecting...");
if (client.connect()) {
Serial.println("connected");
client.println("POST http://twitter.com/statuses/update.json HTTP/1.1");
client.println("Host: twitter.com");
client.println("Authorization: Basic ??????");
client.println("Content-type: application/x-www-form-urlencoded");
client.println("Content-length: 139");
client.println("Connection: Close");
client.println();
strcat(stats, msg);
client.print(stats);
} else {
Serial.println("connection failed");
return false;
}
client.stop();
return true;
}
unsigned int getTemp(){
// @return degrees in F
int tempc = 0,tempf=0; // temperature variables
int samples[8]; // variables to make a better precision
for(int i = 0;i<=7;i++){ // gets 8 samples of temperature
samples[i] = ( 5.0 * analogRead(TEMP) * 100.0) / 1024.0;
tempc = tempc + samples[i];
delay(1000);
}
tempc = tempc/8.0; // better precision
tempf = (tempc * 9)/ 5 + 32; // converts to fahrenheit
return tempc-5;
}
unsigned int getLight(){
// @return light in %
return (analogRead(LIGHT)/849.0)*100.0;
}
void setup(){
Serial.begin(9600); // start serial communication
}
void loop(){
sendUpdate();
delay(3600000); //wait an hour before next check
}
I have this code that monitors the temperature and ambient light and twitters it.
however, I can’t figure out why, but the function sendUpdate(); freezes immediately when its run.
the Arduino restarts in about a minute.
If theres something inherently wrong with my code it would be appreciated if you could point it out, i’m out of ideas. :’(