Showing posts with label ethernet shield. Show all posts
Showing posts with label ethernet 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.

Thursday, July 28, 2011

ADK / Ethernet Shield Problem Solved!

Fixed! A quick trip to Radio Shack for some hookup wire (22 gauge, solid) and I manually wired the ICSP pins from the Ethernet Shield and now I have a web server that's just a bit bigger than a deck of cards.



Here is the ISCP pin mapping to the ADK pins for future reference:


(MISO) 50 <-- 1 o o 2 --> +5V
(SOCK) 52 <-- 3 o o 4 --> 51 (MOSI)
RESET <-- 5 o o 6 --> GND


Only pins 1,3 and 5 need to be connected (orange wires in the picture). The Ethernet shield still works if you leave pins 2, 5 and 6 disconnected (red, yellow and green wires in picture).

Android ADK and Arduino Ethernet Shield Woes

I think I figured out why my Arduino Ethernet shield won't play nicely with my Android ADK Arduino board.

You see the black, six-pin header connector on the right side of the blue board (the Ethernet Shield)? That's the the ICSP bus in Arduino-speak. That's supposed to connect into a matching set of pins on the white Arduino board so the Ethernet shield can to its "talkin' to the interweb" thing. Do you see matching pins on the ADK board? Yeah, neither do I.



Turns out this is a bit of a problem, because the Arduino Ethernet Shield documentation states:

Arduino communicates with both the W5100 and SD card using the SPI bus (through the ICSP header).


Like I said, bit of a problem. Looks like there are exposed copper pads on the bottom of the ADK board for the ICSP connections, so I could solder in some connections if absolutely necessary. Those pads (MISO1, MOSI1, SOCK1) are clearly wired to 50,51 and 52 as described in the Arduino Mega 2560 schematics.

Time for some hand wiring...

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.