Showing posts with label web. Show all posts
Showing posts with label web. Show all posts

Saturday, January 14, 2012

Arduino + ENC28J60 Ethernet Module - Part 2

So at the end of my last adventure with the ENC28J60 and my Arduino Uno I got it working with the 0021 IDE  (so I expect it will work with 0022 and 0023 but I have not tested) and the nuelectronics.com library available from here http://www.nuelectronics.com/download/projects/etherShield.zip  using the following wiring to an Uno.

VCC - 3.3V
GND - GND
SCK - Pin 13
SO - Pin 12
SI - Pin 11
CS - Pin 10

After my success with this I thought I'd try it with IDE 1.0 and it failed.  The nuelectronics library for the ENC28J60 does not work (as of writing) with IDE 1.0.

After a little more hunting I found the ethercard library https://github.com/jcw/ethercard that works with IDE 1.0.  I loaded up the backSoon web server sketch that even uses DHCP to get it's IP addess.
FAIL...

I was beaten and abandoned the effort until today.
Well, not really today as between Thursday and today I have been trawling the web to find anything that would help me and I found it.  Not any official update or guide but a post in the forum for the ethercard with the statement:
a new optional 3rd arg to EtherCard.begin() lets you specify the chip select pin (8..10, default is 8)
Wait a second the EtherCard library uses Pin 8 by default for CS.

So, today I setup up the Arduino and ENC28J60 with the following wiring

VCC - 3.3V
GND - GND
SCK - Pin 13
SO - Pin 12
SI - Pin 11
CS - Pin 8

Started IDE 1.0, loaded the backSoon Sketch.
Compiled and ran it.
Checked my routers list of DHCP clients and there was 1 for Arduino-DB witn an IP of 192.168..1.2
Well an IP address has been assigned
Fired up Firefox and went to 192.168.1.2
SUCCESS!!!!!

Now it's all about figuring out all the technical gubbins on the library so I can do my own stuff.
I want to be able to send data to a webserver and also read data from a webserver and do something based on the data.


Not asking too much but at the moment I have no idea how to make the most of the EtherCard library so a bit of Googling and testing required.  This will keep me entertained through the cold winter months...


UPDATE August 2012: OK, maybe the cold winter  months passed before I got back to this.  A bit more fun with the ENC28J60 and using a web server coded in PHP to control it. http://winkleink.blogspot.co.uk/2012/08/arduino-ethernet-ethercard-xamp-web.html

Thursday, July 14, 2011

Arduino - Web server controlled LED using serial connection between the Arduino and web server

I was chatting to a friend who was working on home automation.  Controlling lights, heating and other stuff around the house using a touch screen controller and I thought if you used a web site as the interface then connected to the Arduino over a serial link it would be possible to switch things on and off from any devlice that could display a web page and access the web server.

Personally, I would not recommend using a serial link from the server to the Arduino as the Arduino resets itself everytime something is sent over serial,  More correctly an ethernet shield would be great.  Giving more scalability as the web server can talk to mutliple Arduinos with no great extra effort.

I installed XAMPP on my netbook as the webserver and used PHP code on the server to present the web interface and send the command to the Arduino over serial.  The Arduino is waiting for a serial command and then depending on what it is will turn on or off the mounted LED.

So, not the most impressive looking demo, but as a proof of concept if I can turn on a LED I can control a relay to turn on an kind of device.


Below is the code.  Be aware I'm more about getting the Arduino to do something and so the code will be a bit rough around the edges.  It works but I definitely do not consider it best practise or even efficent code.
Use at your own risk.


PHP on the server:

<?php

exec('mode com4: baud=9600 data=8 stop=1 parity=n xon=no');

$switch1 = "";

/* Serial script for pan/tilt Camera with servos */
/* Script by Aneal Khimani, 2-12-10 */

//check the GET action SuperGlobal var to see if an
//action is to be performed

if (isset($_GET['action'])) {

$switch1 = $_GET['action'];

//Action required

switch ($switch1) {
case "on":
$fp = fopen("com4", "w");
fwrite($fp, chr(97));
fclose($fp);
break;

case "off":
$fp = fopen("com4", "w");
fwrite($fp, chr(98));
fclose($fp);
break;


}
}

?>

<html>
<body>
<center>

<p>
<font size="4">Flick Switch</font><br />
<table width="30">
<tr><td><a href="<?=$_SERVER['PHP_SELF'] . "?action=on" ?>">On</a></td><td><?php if($switch1 == 'on'){echo "<b>ON</b>";} ?></td></tr>
<tr><td><a href="<?=$_SERVER['PHP_SELF'] . "?action=off" ?>">Off</a></td><td><?php if($switch1 == 'off'){echo "<b>OFF</b>";} ?></td></tr>
</table>
</p>

</center>
</body>
</html>


Arduino Code:

/*

  Used with the PHP code
  http://localhost/arduino.php
 */

void setup() {               

  Serial.begin(9600); // initialize serial communication:

  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);    
}

void loop() {

// read the serial port:

if (Serial.available() > 0) {
int inByte = Serial.read();

switch (inByte) {
case 'a':
  digitalWrite(13, HIGH);   // set the LED on
  Serial.println("HIGH");
 
break;

case 'b':
  digitalWrite(13, LOW);   // set the LED off
  Serial.
  println("LOW");

break;
}

}

}