“Tutto ciò che è alimentato elettricamente è anche connesso a internet.”
(Tom Igoe)
La lampada del mio comodino twitta automaticamente i suoi cambiamenti di stato, grazie a
- Arduino 2009
- Ethernet Shield
- Sensore luce
Bisognarà che le crei un account dedicato su twitter. Se sei interessato al codice, è qui sotto.
Essendo una prova, tengo aperta la porta seriale per controllare dalla consolle quel che succede, ma in produzione i vari Serial.print vengono tolti.
Se trovi errori o difetti e vuoi segnalarmeli, te ne sarò grato.
/*
Twitter Light Client 0.2
created on jan 28 2012
modified on jan 30 2012
by Gaspar Torriero
with many thanks to the Arduino community
at http://arduino.cc/en/Tutorial/HomePage
*/
#include <SPI.h>
#include <Ethernet.h>
#include <Twitter.h>
boolean oldStatus = 0;
boolean newStatus = 0;
int tPin = 0; // Temperature sensor pin
int lPin = 5; // Light sensor pin
float temp = 0.0; // Temperature in C°
int luce = 0; // light in Lux
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {
192, 168, XXX, XXX }; // IP address of your Arduino
byte gateway[] = {
192, 168, XXX, XXX }; // IP address of your router
byte subnet[] = {
255, 255, 255, 0 };
Twitter twitter("XXXXXX-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); // Your Twitter API code
String stringOne="La luce sul comodino è stata spenta";
String stringTwo="La luce sul comodino è stata accesa";
char msg[140];
void setup()
{
Ethernet.begin(mac, ip, gateway, subnet);
// Serial.begin(9600);
luce = (analogRead(lPin) * 10000.0) / 1024.0; //Conversione voltaggio sensore in Lux
if(luce < 201) {
oldStatus=0;
}
else if(luce > 200) {
oldStatus=1;
}
}
void loop() {
delay(50000);
Serial.print(oldStatus);
Serial.print(newStatus);
Serial.println();
Serial.println(luce);
temp = ( 5.0 * analogRead(tPin) * 100.0) / 1024.0; // Conversione voltaggio sensore in temperatura
luce = (analogRead(lPin) * 10000.0) / 1024.0; //Conversione voltaggio sensore in Lux
if(luce < 201) {
newStatus=0;
stringOne.toCharArray(msg, 140);
}
else if(luce > 200) {
newStatus=1;
stringTwo.toCharArray(msg, 140);
}
if(newStatus != oldStatus) {
Serial.println("connecting ...");
if (twitter.post(msg)) {
int status = twitter.wait(&Serial);
if (status == 200) {
Serial.println("OK.");
}
else {
Serial.print("failed : code ");
Serial.println(status);
}
}
else {
Serial.println("connection failed.");
}
delay(10000);
oldStatus = newStatus;
Serial.print(oldStatus);
Serial.print(newStatus);
Serial.println();
Serial.println(luce);
}
}