Showing posts with label relay shield. Show all posts
Showing posts with label relay shield. Show all posts

Monday, August 1, 2011

Web Relay Test Arduino Sketch

I wrote this Arduino sketch to test firing the relays on 2 of my Seeed Studio Relay Shields. Shield 1 is connected to digital pins 2 to 5 and Shield #2 is connected to digital pins 6 to 9.

This sketch enables all those pins for output and then turns them on and off for half a second (500 milliseconds) in sequence.

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


byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x50, 0xA0 };    //physical mac address
byte ip[] = { 192, 168, 1, 177 };   // ip in lan
byte gateway[] = { 192, 168, 1, 1 };   // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                   //subnet mask
Server server(80);                                      //server port
byte sampledata=50;            //some sample data - outputs 2 (ascii = 50 DEC)            

String readString = String(30); //string for fetching data from address

void setup(){
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);

//Set pins 2 through 9 to output
for (int i = 2; i <= 9; i++){
  pinMode(i, OUTPUT);  
}

//enable serial data print  
  Serial.begin(9600);
}
void loop(){
// Create a client connection
Client client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        //read char by char HTTP request
        if (readString.length() < 30) {
          //store characters to string
          readString.concat(c);
        }  
        //output chars to serial port
        Serial.print(c);
        //if HTTP request has ended
        if (c == '\n') {
          if(readString.indexOf("test=all") > -1) {

httpReply(client);

// cycle through pins 2 to 9
for (int i = 2; i <= 9; i++){
               Serial.print("Writing pin ");
               Serial.print(i);
               Serial.println(" HIGH");
               digitalWrite(i, HIGH);    // set the LED on
               delay(500);
               Serial.print("Writing pin ");
               Serial.print(i);
               Serial.println(" LOW");
               digitalWrite(i, LOW);
               delay(500);
             }
          }
          //clearing string for next read
          readString="";
          //stopping client
          client.stop();
        }
      }
    }
  }
}

void httpReply(Client client){
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println();
  client.println("{\"status\" : \"executing\" , \"cmd\" : \"test=all\"}");
  client.stop();
}
This code is loosely derived from code by Mareika Giacobbi of nerdydog.it for his Domotic Home project.

ADK + Ethernet Shield + 2 Relay Shields = Almost Working Prototype

Here is the current iteration of my Arduino Irrigation Controller rig:


I now have 2 of the 3 Relay Shields connected to the ADK with Ethernet. Did I say connected? It's a bit fragile to say the least. Don't breathe on it! Those green wires going into the middle of the Relay PCBs are basically held their by the tension of the wires wanting to straighten out.

I really wish the Seeeduino guys had put on those extra long headers and pins on these Relay Shields like on the Arduino Ethernet Shield. It's basically impossible to attach them to the Arduino board or the Ethernet Shield because these huge freakin' pins from the screw terminals extend 2mm out the bottom of the board (and cause shorts against tall components, if you're not careful).

Now I'm waiting on some parts from AdaFruit so I can connect the 3rd Relay Shield and create a more reliable connections to the first two boards. But, where am I going to put Relay Shield #3? Hmmm...

One other thing that confuses/frustrated me with the Relay Shields is that they cannot run off USB power (5v), you have to plug them into their own 9v power supply, or plug the Arduino into a 9v+ power supply and route the Arduino's 9v pin (not the 5v pin) and ground to the Relay Shields. That's a bit annoying. The important thing, though, is that it works. On to the next stage.

Pin 0 and 1: Don't Use for Output!

Oh, and one other thing: If you are thinking of using digital pin 1 and pin 2 to control the Relay Shields: Don't. I tried using them in an earlier version of this prototype and saw some worrying behavior. Namely, when uploading a sketch to the Arduino board, pins 1 and 2 flash on and off very quickly which turns on the connected relays on and off very quickly and makes them rather hot. I didn't have a catastrophic failure; the relays still work, but that'll be the last time I try using pin 1 and 2 for anything other than TX and RX.

Saturday, July 16, 2011

Let the Arduino Hacking Begin!

I have jumped into Arduino hardware hacking and bought some shields to use with the ADK I got from work. The idea is to use these to remotely control my irrigation system at home.