Saturday 23 November 2013

Arduino - Talking plant, with Twitter (how to tweet data with Arduino)

After I finished the first test with the Ethernet shield and serving your own page from Arduino (see post here) I said, ok, but if I don't have my own static IP address!?!? Most of us don't have a static IP address at home. Yeah it was fun with the other one to play in a LAN or use the university network to get an own IP, but at home this device renders useless. I tried with other social networks or email providers but it looks like Facebook, Yahoo mail and Gmail have quite some problems talking with Arduino and Arduino has some problems talking with them, the only one available remained Twitter.
Having as starting point http://arduino-tweet.appspot.com/ I modified my previous code to this one:


#include <SPI.h> // needed in Arduino 0019 or later
#include <Ethernet.h>
#include <Twitter.h>

// The includion of EthernetDNS is not needed in Arduino IDE 1.0 or later.
// Please uncomment below in Arduino IDE 0022 or earlier.
//#include <EthernetDNS.h>

// Ethernet Shield Settings
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// If you don't specify the IP address, DHCP is used(only in Arduino 1.0 or later).
//byte ip[] = { 192, 168, 9, 122 }; // 192.168.9.122

// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("please put here your token");

// Message to post - just to initialize the variable
char msg[] = "Hello, World! I'm Arduino!";

void setup()
{
  delay(10000);
  Serial.begin(9600);
    // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  Serial.println("connecting ...");
  if (twitter.post(msg)) {
    // Specify &Serial to output received response to Serial.
    // If no output is required, you can just omit the argument, e.g.
    // int status = twitter.wait();
    int status = twitter.wait(&Serial);
    if (status == 200) {
   
      Serial.println("OK.");
   
    } else {
      Serial.print("failed : code ");
      Serial.println(status);
    }
 
  } else {
    Serial.println("connection failed.");
  }
}

//Temperaturmessung
//*
int temp = 1;
int oldtemp = 1;
int val;

void loop()
{
  delay(30000);
  val = analogRead(0);

  if (val > 40) { temp = 1; }

  if (val <20 ) { temp = 0; }

  if (oldtemp != temp) {
    oldtemp = temp;
    if (temp == 1) {
      //Serial.println("FEUER!");
      sendTwitterUpdate("Im Gewächshaus brennt es!");
    }
    else {
      //Serial.println("Kalt!");
      sendTwitterUpdate("Es ist zu kalt für die Pflanzen!");
    }
  }

  fetchTwitterUpdate();
}


Now you can get more creative and do more out of it.

Arduino - Talking plant, with HTTP server (or how to see sensor data via internet)


One of the small assignments used for learning Arduino was the classical "talking plant". Actually was more of a build own humidity sensor, sensor reading and sending the data to the Ethernet shield and also use LED lights to display the status of the soil moisture. 

What?
First step is to build a sensor, very simple! Of course you can buy a humidity sensor, no one says not to do so, but makes more fun to built your own, cheaper and simpler.
After the sensor is build all you have to do is just to read the values and then use 3 LED (red, yellow, green) to visually display the soil moisture and at the same time send the values to the Ethernet shield so you can check the humidity of your precious plant over the internet.
Of course you can get more creative than this once you have the data, but I wasn't in the mood to invest money in more cooler ideas. I kept them for my mood lights.

How to build the soil moisture sensor
Actually this is the simplest part. You can use two nails, two thick wires or basically anything that is allowing electricity to pass. The trick is to pick a material that does not get rusty or oxides to quick, also must have a small electric resistance because we are talking here about very small values ... 5V and less than 1A. 
I used two fine steel nails about 5cm long with a 3mm distance between. The nails were fixed on a plastic base 5mm high (to keep the distance between the nails), you can use any material that is resistant to electricity (does not allow electricity to pass). Having a 5cm nail minus 5mm plastic base I remained with a 4.5cm for each nail as the sensing surface.
Note: the nails must be parallel with each other. 
Soldering a wire on each nail head is the only "hard part" of this sensor.

How it works
You need just to "stick" the sensor in the flower pot (you don't want to damage the roots of your precious plant so be careful where you stick it) connect it to the Arduino board like you will do with any other simple sensor (e.g. photoresistor see how to connect a photoresistor)
Unfortunately I have no drawing for this project but the wiring is not so hard. Actually you can get it only by reading the code.
Next step is to connect the 3 LED lights and then calibrate your sensor (see code). The values will differ from one soil type to the other ( depending on the pH value).
Plug your Ethernet shield, use your network values and have fun! (see in the code where)

The code
The code is well commented but if you have questions just ask :)

//#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>

// Ethernet shield attached to pins 10, 11, 12, 13
//analog sensor
int sensorPin = 0;     // 1st sensor is connected to a0
int sensorReading;     // the analog reading from the analog port

//status LEDs
int LEDred =7;              
int LEDyellow =8;  
int LEDgreen =9;

//threshold values - calibrate sensor
int RED = 200;
int GREEN = 500;

//string that holds the status
String Status ="";

//Server part

//define mac address for ethernet shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
//define ip for ethernet shield
byte ip[] = { 192, 168, 1, 2 };           // ip in lan
//gateway - router ip address and mask
byte gateway[] = { 192, 168, 1, 20 };            // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                   //subnet mask


// Initialize the server library and port (port 80 is default for HTTP)
Server server(80);                                      //server port


//byte sampledata=50;

void setup(void) {
  pinMode(LEDred, OUTPUT);  //set LED1 pin to output mode
  pinMode(LEDyellow, OUTPUT);  //set LED2 pin to output mode
  pinMode(LEDgreen, OUTPUT);  //set LED3 pin to output mode

  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //send debugging information via the Serial monitor
  Serial.begin(9600);
}

void HTTPserv () {
// listen for incoming clients
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
 
    //---------------HTML----------------------------
 //set background color
          client.print("<body style=background-color:yellow>");
          //send first heading
          client.println("<font color='red'><h1>Soil humidity</font></h1>");
          client.println("<hr />");
          client.println("<hr />");
          client.println("<br />");
          client.println("<br />");
 //drawing simple table
          client.println("<font color='black'>Simple table: </font>");
          client.println("<br />");
          client.print("<table border=1><tr><td>Humidity</td>");
 client.print("<td>");
 client.print(sensorReading);
 client.print("</td></tr>");
          client.println("<tr>");
 client.print("<td>Status</td><td>");
 client.print(Status);
 client.print("</td></tr></table>");
          client.println("<br />");
          client.println("<hr />");
 //text variant
          client.println("<hr>");
          client.print("Humidity ");
          client.print(" = ");
          client.print(sensorReading);
          client.println("<br />");
          client.print("Status ");
          client.print(" = ");
          client.print(Status);
          client.println("<br />");
          client.println("<hr>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

void loop(void) {

  digitalWrite(LEDred,LOW); //initial state
  digitalWrite(LEDyellow,LOW); //initial state
  digitalWrite(LEDgreen,LOW); //initial state

  sensorReading = analogRead(sensorPin);

  Serial.println(sensorReading);

  if (sensorReading <= RED) {
   digitalWrite(LEDred,HIGH);
   digitalWrite(LEDyellow,LOW);
   digitalWrite(LEDgreen,LOW);
   Status = "needs urgent attention";
   HTTPserv () ;
 }
 else if (sensorReading > RED && sensorReading < GREEN ) {
   digitalWrite(LEDred,LOW);
   digitalWrite(LEDyellow,HIGH);
   digitalWrite(LEDgreen,LOW);
   Status = "needs attention";
   HTTPserv () ;
 }
 else if (sensorReading >= GREEN ) {
   digitalWrite(LEDred,LOW);
   digitalWrite(LEDyellow,LOW);
   digitalWrite(LEDgreen,HIGH);
   Status = "needs no attention";
 }
 else {
   digitalWrite(LEDred,LOW);
  digitalWrite(LEDyellow,LOW);
  digitalWrite(LEDgreen,LOW);
  Status = "";
  HTTPserv () ;
 }
 delay(1000);
}


Wednesday 20 November 2013

365 Challenge - day 337


New hobby in town - bone jewelry. First try is a plain ring. Using as close to the "old school" tools as possible. No drilling machine, no dremel or any other electric tool - just the good old handwork ;)

If you have questions you are free to ask.


Sunday 17 November 2013

365 Challenge - day 336


Another example of perfect summer day.


365 Challenge - day 335


Geek stuff with kinect and projectors. Sometimes being surrounded by nerds can be fun.


365 Challenge - day 334


The new trend in street-art


365 Challenge - day 333


And now my new friend has a lady, life becomes fun.


365 Challenge - day 332


My new friend 


365 Challenge - day 331


Have a good and fresh morning!


365 Challenge - day 330


How you know it's summer? Simple! The street that used to be full it's empty now, looks like everyone is taking the summer holidays ... not me ...


365 Challenge - day 329


Toys for nerds or when soviet technology meets capitalist technology


365 Challenge - day 328


A nice sunny summer day ... I can skip the sunny part and keep the summer


365 Challenge - day 327


Probably the happiest window I have ever seen.


365 Challenge - day 326


Some people have nice activities in their free time ... others don't have time


365 Challenge - day 325

Big sky, small plane  - looks like a mosquito in a room


365 Challenge - day 324


Every time I pass near a Harley-Davidson shop and I see all those choppers, even if we are in 2013 I still see them with a vintage color filter ... maybe is just me


365 Challenge - day 323


Just a summer landscape


365 Challenge - day 322


Geek stuff, just a small part of a 24 projectors instalation


365 Challenge - day 321


My favorite model :)


365 Challenge - day 320


I am always amazed when I look at the shapes created by nature.


365 Challenge - day 319


Still I don't get the meaning and / use of these "buildings" in the forest. Maybe someone could give me a explanation. I presume that is not for games like "cowboys and indians" nor some occult sect is building sacred altars  :))