Connecting a Freetronics Twenty Ten to a HanRun Ethernet Adapter

Connecting a Freetronics Twenty Ten to a HanRun Ethernet Adapter (ENC28J60 Ethernet LAN+Network Module)

hanrun2

hanrun1

What hardware you will need

  • 1x Freetronics TwentyTen
  • 1x HanRun HR911105A 12/18 Ethernet Board (eBay)
  • Misc Solder-less breadboard cables (Male/Female)

Router Configuration

I prefer to assign static IP’s to any of my test devices (Arduino or Raspberry Pi). This way I know what the IP is in case I need to connect to it. If you want to use your arduino in a Dynamic IP network then you may not have much luck with this guide as I am configuring the arduino to use an static ip that I have reserved in my router.

    Arduino Static

The HanRun Ethernet adapter does not have a mac address built in so I made one up ( 00:12:34:43:21:00 ), you can generate mac addresses here http://www.miniwebtool.com/mac-address-generator/.

Software Configuration

  • Arduino.cc IDE (Set the ‘Board’ to ‘Arduino Nano w/ ATMmegda 328’ and the virtual serial port for your USB cable)
  • Download ‘Ethercard’ from https://github.com/jcw/ethercard (add it as a library into arduino.cc IDE)

The Wiring

HanRun Side | Arduino Side

    GND = Power Ground
    SCK = Pin 13
    SO = Pin 12
    VCC = Power 3V3
    CS = Pin 8
    SI = Pin 11

photo 2

    The Code
    Now this is my code solution for returning a date from a website (php script). Yu can change the IP, Gateway, Server Name, IP and Path to your server if need be.

    // This demo does web requests to a fixed IP address, using a fixed gateway.
    // 2010-11-27 http://opensource.org/licenses/mit-license.php
    // 2013-11-23 Modified by Simon Fearby

    #include
    #include

    #define SLEEP_TIME_AFTER_SETUP 10000 // How often do we poll the server

    //static long timersetup; // Used to remember the last millisecond for the last call (during setup)
    static long timerloop; // Used to remember the last millisecond for the last call (after setup)

    static byte mymac[] = { 0x00, 0x12, 0x34, 0x43, 0x21, 0x00 }; // ethernet interface mac address
    static byte myip[] = { 192,168,1,201 }; // ethernet interface ip address
    static byte gwip[] = { 192,168,1,1 }; // gateway ip address
    static byte hisip[] = { 175,107,162,121 }; // remote website ip address and port

    char website[] PROGMEM = "fearby.com"; // remote website name

    byte Ethernet::buffer[1000]; // a very small tcp/ip buffer is enough here

    void setup () {
    Serial.begin(57600);
    while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
    }
    Serial.println("Ready");
    if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println( "Error Failed to access Ethernet controller");

    ether.staticSetup(myip, gwip); // Our router has a Static IP already setup for us so dont ask for a IP via DHCP
    ether.copyIp(ether.hisip, hisip); // Is it the IP we expect?

    ether.printIp("My Gateway: ", ether.myip); // Output the routers IP (Gateway)
    ether.printIp("My IP: ", ether.gwip); // Output the Arduino's IP)
    ether.printIp("Remote Server: ", ether.hisip); // Output the IP of the remote server.

    while (ether.clientWaitingGw()) // Check to see if the gateway is up before moving on.
    ether.packetLoop(ether.packetReceive());

    Serial.println("Gateway found"); // Congrats, Move on

    Serial.println("Waiting 10 seconds before polling data."); // Congrats, Move on

    }

    static void my_result_cb (byte status, word off, word len) { // called when the client request is complete
    Serial.print("<<< reply "); // We got a reply from the web server Serial.print(millis() - timerloop); // Time Stuff Serial.println(" ms"); // Print the time diff between the call and data received Serial.println((const char*) Ethernet::buffer + off); // Ok print the contents of the webpage } void loop () { ether.packetLoop(ether.packetReceive()); // Process Incoming Packets if (millis() > timerloop + SLEEP_TIME_AFTER_SETUP) { // Do we need to sleep? We dont want to load a website 10,000 a second.
    timerloop = millis(); // Remember the last time we loaded the webpage
    Serial.println("\n>>> Webpage Request Sent.."); // Lets get a Webpage
    ether.browseUrl(PSTR("/temp/printdate.php"), "?", website, my_result_cb); // Query Google
    }
    }

    Result from the Arduino

    hanrun3

    Where to now

    Remember that this code running on a small circuit board that runs < 20Mhz 8bit processor that can be powered by AA batteries.

    You can point to your own scripts and save/read database values or trigger other Arduinos or external processes.

    The next step is to have this device read from any external sensors or control the physical world around us with expansion cards.