Go Down

Topic: Wireless Tweeting fridge (Read 3252 times) previous topic - next topic


Nov 14, 2010, 10:12 pm Last Edit: Nov 14, 2010, 10:20 pm by lukewoolfson Reason: 1
Hi, I wanted to progress my learning so I created a tweeting fridge shelf, which tweets the amount of milk in a fridge using VirtualWire & Twitter librarys, an Ethernet shield, a FSR and a RF pair. This code could be applied to any analog input. I used a FSR in this instance to measure the force so its not that accurate but does the job.

This is my first Ardunio project which combines several tutorials (all referenced in code). I'm aware my coding isn't great and could probably be refined any assistance on tidying it up would be welcomed.

Code: [Select]
Transmit an analog value via RF.

Arduino Mini Pro 328 3.3v
FTDI Basic Breakout - 3.3V
NCP1400-3.3V Step-Up Breakout
RLP 434-A RF transmitter module
Square Force Sensitive Resistor (FSR)
2xAA Batterys

code probably needs some tidying up
make sure your antenna is the correct length (17cm for 434mHz)

#include <VirtualWire.h> //load virtual wire library for RF communication see http://www.open.com.au/mikem/arduino/VirtualWire.pdf
#include <avr/sleep.h> //load sleep library to allow the Ardunio to go to sleep see http://www.arduino.cc/playground/Learning/ArduinoSleepCode

int sleepStatus = 0;   // variable to store a request for sleep
int ledPin =  13;     //LED
int buttonPin = 3;    //micro switch
int val = 0;          //value of pressure pad
char *msg = "0";      //message to send via virtual wire
int buttonState = 0;  //current state of the button
int f = 250;          //flash delay(ms) interval
int count = 3;        //timer

void wakeUpNow()      // here the interrupt is handled after wakeup


void setup()
 vw_setup(2000); //Virtual wire bits per sec
 pinMode(buttonPin, INPUT);
 pinMode(ledPin, OUTPUT);
 attachInterrupt(1, wakeUpNow, LOW); //either 0 or 1 for interrupts on pin 2 or 3.

void sleepNow()         // here we put the Ardunio to sleep
 attachInterrupt(1,wakeUpNow, LOW);

void LEDflash() //blink LED

void loop()                    
 buttonState = digitalRead(buttonPin); //read button
 val = (analogRead(3)/4)+1;
 msg[0] = val;
 if (buttonState == HIGH) { //if fridge door is close the microswitch goes high
 Serial.print(" ");
 count--;               //count down
 LEDflash();            //flash LED
  if (count == 0) { //when counter is 0 send analog value then go to sleep
     Serial.println(" ");
     Serial.println(val); //just for error checking
     vw_send((uint8_t *)msg, strlen(msg)); //send the value of the analog input
     digitalWrite(ledPin, LOW);
     delay(100);     //this delay is needed, the sleep
                     //function will provoke a Serial error otherwise!!
     count = 3;
     sleepNow();     // sleep function called here

} else {
 count = 3; //reset counter to 3
 digitalWrite(ledPin, HIGH); //turn on LED to show Ardunio is awake
 Serial.println("waiting for fridge door to close");

Code: [Select]

Tweet a text string when a value is received via RF.

Arduino 328 Duemilanove
Ethernet sheild
RLP 434-A RF recevier module
blue/red LED

code probably needs some tidying up
make sure your antenna is the correct length (17cm for 434mHz)

#include <Ethernet.h>       //load Ethernet library
#include <EthernetDHCP.h>
#include <EthernetDNS.h>
#include <Twitter.h>        //load twitter library see http://www.arduino.cc/playground/Code/TwitterLibrary

#include <VirtualWire.h>    //load virtual wire library for RF communication see http://www.open.com.au/mikem/arduino/VirtualWire.pdf
#define BledPin 7
#define RledPin 6

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
Twitter twitter("INSERT YOUR CODE HERE"); //insert your token here see http://arduino-tweet.appspot.com/
char buffer[80];

char* myStrings[]={ //array of text strings split in to 4 groups
"Dam all gone better get to the shops",             //0
"Oh no! where did I leave my cow?",                 //1
"I did tell you there wasn't much left",            //2

"Breaky for 1 get in quick",                        //3
"Someone got greedy and didn't leave you alot",     //4
"Shame you wern't quicker out of bed this morning", //5

"Just enough left, phew",                           //6
"That was close, dont forget tomorrow",             //7
"Running low, only enough for breaky",              //8

"No worries you got plenty, fancy a milkshake?",    //9
"Have 2 bowls of coco pops just because you can",   //10
"Drink up before it goes bad",               //11

int m;          
int i;
int post_number = 0;
float a;
int r;          //random int number
int str_no;     //myStrings text mapping
int val;

//a method of non linear mapping see http://interface.khm.de/index.php/lab/experiments/nonlinear-mapping/
float nodepoints[11][2]= {
   0,0  }
 , {
   57,0  }
 , {
   80,25  }
 , {
   121,50  }
 , {
   140,75  }
  , {
   149,100  }
 , {
   159,125  }
   168,150  }
   176,175  }
   182,200  }
   188,225  }

int reMap(float pts[16][2], int input) {
 int rr;
 float bb,mm;

 for (int nn=0; nn < 10; nn++) {

   if (input >= pts[nn][0] && input <= pts[nn+1][0]) {
     mm= ( pts[nn][1] - pts[nn+1][1] ) / ( pts[nn][0] - pts[nn+1][0] );
     mm= mm * (input-pts[nn][0]);
     mm = mm +  pts[nn][1];
     rr = mm;

void setup(){
 pinMode(BledPin, OUTPUT);
 pinMode(RledPin, OUTPUT);
 vw_setup(2000);                         // set Bits per sec for virtual wire
 vw_rx_start();                          // Start the receiver PLL running
 vw_set_rx_pin(9);                       //set the rx pin 9 so doesn't interfere with the Ethernet shield

void loop(){
 uint8_t buf[VW_MAX_MESSAGE_LEN];
 uint8_t buflen = VW_MAX_MESSAGE_LEN;  
 if (vw_get_message(buf, &buflen)) { // check to see if anything has been received
   for (i = 0; i < buflen; i++) {
     val = buf[i];                           //assign the number received from virtual wire to val
     //Sets the Milk level text depending on the value received, relates to char* myStrings[]
     if (val >= 0 && val <= 69){  m = 0;}         //text group 1
     if (val >= 70 && val <= 89){  m = 3;}        //text group 2
     if (val >= 90 && val <= 130){  m = 6;}       //text group 3
     if (val >= 131 && val <= 255){  m = 9;}      //text group 4
     r=random(0,3);                               //generate a random int number between 0 and 3
     str_no = r + m;                              //add the random number, which is 0, 1 or 2, to the 'm' number to define which string to insert
     float result = reMap(nodepoints,val);        //map the val to the result using the nonlinear mapping array
     a = result/100;
     int a1 = (a - (int)a) * 10;                  //work around as can't post floating numbers in a sprintf so separate the integer from the decimal
     sprintf(buffer, "Post: %d You have %d.%d pints of Milk. %s", post_number, (int)a, a1, myStrings[str_no]);     //text to post see http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
     post_number++;                               //increase the post number so every tweet is different as you can't post the same tweet twice in a short period of time
     /* error checking
     Serial.println(" ");                    
     Serial.println("Raw code:");
     Serial.println("Text to tweet:");
      if (twitter.post(buffer)) {                 //post 'buffer' string
       int status = twitter.wait();
         if (status == 200) {
           digitalWrite(BledPin, HIGH);           //turn on blue LEd if tweet posted ok
           digitalWrite(BledPin, LOW);
         } else {
           Serial.print("failed : code ");
           digitalWrite(RledPin, HIGH);           //turn on red LED if tweet failed
           digitalWrite(RledPin, LOW);
       } else {
         Serial.println("connection failed.");

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131