I have a sketch that intercepts rf signals in my house and then tweets the result. Source code is here GitHub - pauly/rf-butler: Lightwaverf home automation 433mhz control for Arduino, without relying on the wifi link hardware and I'll paste it below too. At some stage in its development I've introduced an error whereby the sketch just stops, nothing more output on the serial monitor (and no more tweets) after the first one. I think perhaps I'm writing over the end of a string or something similar, but possibly there is an issue with how I'm posting my requests. Any help gratefully received.
/**
* Uses https://github.com/roberttidey/LightwaveRF
* and maybe bits of https://github.com/lawrie/LightwaveRF
*
* Arduino home automation experiments. Hardware:
* Arduino http://amzn.to/UCKWsq
* Ethernet shield http://amzn.to/1akfeTY
* LightwaveRF kit: http://amzn.to/RkukDo and http://amzn.to/V7yPPK
* RF transmitter and receiver http://bit.ly/HhltyI
* Air quality meter from coolcomponents.co.uk
*
* @author PC <paul.clarke+paulclarke@holidayextras.com>
* @date Wed 23 Oct 2013 20:57:15 BST
*/
#include <LwRx.h>
#include <SPI.h>
#include <Ethernet.h>
// put your twitter.com credentials in Tweet.h - copy Tweet.h.sample - optional and experimental!
#include "Tweet.h"
#define TIME_BETWEEN_NAGS 60
EthernetClient client; // to make outbound connections
byte mac[] = { 0x64, 0xA7, 0x69, 0x0D, 0x21, 0x21 }; // mac address of this arduino
byte len = 10;
byte lastCommand[10];
int airQualityPin = A0; // input pin for the air quality
int smell = 0; // air quality value
long transmitTimeout = 0;
unsigned long lastAlerted = 0;
void setup ( ) {
Ethernet.begin( mac ); // dhcp
delay( 1000 );
lwrx_setup( 2 ); // set up with rx into pin 2
Serial.begin( 9600 );
Serial.println( "Set up completed" );
}
void loop ( ) {
if ( lwrx_message( )) {
byte msg[ len ];
lwrx_getmessage( msg, &len );
log( msg, len );
Serial.println( "logged, back in loop( )" );
}
sniff( );
Serial.println( "sniffed, back in loop( )" );
delay( 500 );
}
/**
* Check the air quality
*/
void sniff ( ) {
smell = analogRead( airQualityPin );
if ( smell > 400 ) {
Serial.print( "air: " );
Serial.println( smell );
if (( lastAlerted == 0 ) || (( millis( ) - lastAlerted ) / 1000 > TIME_BETWEEN_NAGS )) {
char data[128];
data[0] = 0;
strcat( data, "t=something+smells!+air+quality+" );
char ss[4] = "";
itoa( smell, ss, 10 );
strcat( data, ss );
strcat( data, "&c=" );
strcat( data, consumer_secret );
strcat( data, "&a=" );
strcat( data, access_token_secret );
tweet( data );
lastAlerted = millis( );
}
delay( 1000 );
}
}
void tweet ( char *data ) {
post( tweetHost, tweetPath, data );
}
void post ( char *host, char *path, char *data ) {
Serial.println( data );
if ( client.connect( host, 80 )) {
delay( 100 );
client.flush( );
client.print( "POST " );
client.print( path );
client.print( "?_=" );
client.print( millis( ));
client.println( " HTTP/1.1" );
client.print( "Host: " );
client.println( host );
client.println( "User-Agent: Arduino/1.0" );
client.println( "Content-Type: application/x-www-form-urlencoded" );
client.print( "Content-length: " );
client.println( strlen( data ));
client.println( "Connection: close" );
client.println( );
client.println( data );
client.stop( );
}
else {
Serial.print( "failed to connect to " );
Serial.println( host );
}
}
void log ( byte *msg, byte len ) {
if ( compare( msg, lastCommand, 0, len )) {
// Serial.println( "msg + lastCommand were the same" );
return;
}
for ( int i = 0; i < len; ++i ) {
lastCommand[i] = msg[i];
}
char data[128];
data[0] = 0;
strcat( data, "t=" );
strcat( data, device( msg ));
strcat( data, "+sent+" );
strcat( data, command( msg ));
strcat( data, "&c=" );
strcat( data, consumer_secret );
strcat( data, "&a=" );
strcat( data, access_token_secret );
tweet( data );
}
/**
* get a useful string out of an array of bytes
*/
char * tos ( byte *msg, int start, int end ) {
char * name = "";
int index = 0;
for ( int i = start; i < end; i ++ ) {
name[index++] = alpha( msg[i] );
}
name[index++] = '\0';
return name;
}
/**
* get a command string from response
*/
char * command ( byte *msg ) {
return tos( msg, 0, 4 );
}
/**
* get device name from response
*/
char * device ( byte *msg ) {
byte b[6] = { 0x00, 0x0D, 0x0C, 0x02, 0x08 };
if ( compare( msg, b, 5, 10 )) {
return "remote b";
}
return tos( msg, 4, 9 );
}
/**
* turn a hex value into char for display, like sprintf
*/
char alpha ( byte a ) {
return (char)( a < 10 ? a + 48 : a + 55 );
}
boolean compare ( byte a[], byte b[], int start, int end ) {
for ( int i = start; i < end; ++ i ) {
if ( a[i] != b[i] ) return false;
}
return true;
}
It doesn't post directly to twitter from this sketch, but rather via a web proxy I have hosted somewhere The first time it runs all is fine, it gets a result and posts to http://twitter.com/ourduino but after that just silence.