Archive | ethernet

Online data analysis with Arduino and plotly

Introduction

It’s 2014 and the Internet-of-Things is flying along at a rapid rate with all sorts of services and devices that share data and allow control via the Internet. In the spirit of this we look a new service called plotly.

This is a “collaborative data analysis and graphing tool” which allows you to upload your own data to be analysed in many ways, and then graph the results using all sorts of plot types.

With plotly you can run your own mathematical functions over your data, run intense statistical analysis and use or edit one of the many APIs (PythonMATLABRJuliaRESTArduino, or Perl) to increase the level of customisation. Plotly works in conjunction with Google Drive to store your data, however this can be exported and imported without any issues. Futhermore plotly works best in Google Chrome.

For our review we’ll look at using plotly to quickly display and analyse data received from an Internet-connected Arduino – our EtherTen, or you can use almost any Arduino and Ethernet shield. The system isn’t completely documented however by revieiwng our example sketch and some experimenting with the interface plotly is very much usable, even in its current beta format.

Getting started with plotly

You will need to setup a plotly account, and this is simply accomplished from their main site. Some of you may be wondering what plotly costs – at the time of writing plotly is free for unlimited public use (that is – anyone can see your data with the right URL), but requires a subscription for extended private use. You can find the costs at the plans page.

Once you have a plotly account, visit your plotly home page, whose URL is https://plot.ly/~yourusername/# – then click “edit profile”. Another window will appear which amongst other things contains your plotly API key – make a note of this as you will need it and your username for the Arduino sketch.

Next, you’ll need some Arduino or compatible hardware to capture the data to log and analyse. An Arduino with an Ethernet or WiFi connection, and appropriate sensors for your application. We have our EtherTen that takes readings from a temperature/humidity sensor and a light level sensor:

Freetronics EtherTen Arduino Plotly

Now you need a new Arduino library, which is available from the plotly API page. Lots of APIs there… Anyhow, click “Arduino” and you will arrive at the github page. Download the entire .zip file, and extract the plotly_ethernet folder into Arduino libraries folder which in most installations can be found at ..\Arduino-1.0.x\libraries. 

plotly arduino library folder

Finally we’ll use a demonstration sketch provided by plotly and modify this to our needs, which can be downloaded from github. We’ll go through this sketch and show you what to update – so have a quick look and then at out example sketch at the end of this section.

First, insert any code required to get data from your sensors and store the data in a variable – do this so the values can be used in void loop. Next, update the MAC address and the IP address of your Ethernet-enabled Arduino with the following lines:

and change the MAC and IP if necessary. If Arduino and Ethernet is new to you, check out the tutorial. Now look for the following two lines and enter your plotly username and API key:

Next – it’s a good idea to set your time zone, so the time in plots makes sense. Add the following two lines in void setup():

You can find a list of time zones available for use with plotly here. Now you need to determine how many traces and points to use. A trace is one source of data, for example temperature. For now you will have one point, so set these parameters using the following lines:

For example, we will plot temperature, humidity and light level – so this requires three traces. The next step is to set the filename for the plot, using the following line:

This will be sent to plotly and your data will be saved under that name. At the point in your sketch where you want to send some data back to plotly, use:

… then the following for each trace:

where data is the variable to send back to plotly. We use millis() as our example is logging data against time.

To put all that together, consider our example sketch with the hardware mentioned earlier:

After wiring up the hardware and uploading the sketch, the data will be sent until the power is removed from the Arduino.

Monitoring sensor data

Now that your hardware is sending the data off to plotly, you can check it out in real time. Log into plotly and visit the data home page – https://plot.ly/plot – for example:

plotly home data page

Your data file will be listed – so just click on the file name to be presented with a very basic graph. Over time you will see it develop as the data is received, however you may want to alter the display, headings, labels and so on. Generally you can click on trace labels, titles and so on to change them, the interface is pretty intuitive after a few moments. A quick screencast of this is shown in this video.

To view and analyse the raw data – and create all sorts of custom plots, graphs and other analysis – click the “view data in grid” icon which is the second from the left along the bar:

view data grid button

At which point your data will be displayed in a new tab:

plotly arduino data grid

From this point you can experiment to your heart’s content – just don’t forget to save your work. In a short amount of time your data can be presented visually and analysed with ease:

plotly arduino data graph

Conclusion

Although plotly is still in beta form, it works well and the developers are responsive to any questions – so there isn’t much more to say but give it a try yourself, doing so won’t cost you anything and you can see how useful plotly is for yourself. And if you enjoyed this article, or want to introduce someone else to the interesting world of Arduino – check out my book (now in a third printing!) “Arduino Workshop”.

visit tronixlabs.com

Have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our forum – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

Posted in API, arduino, ethernet, plotly, tronixstuff, tutorial0 Comments

Arduino Tutorials – Chapter 16 – Ethernet

Learn how to connect your Arduino to the outside world via Ethernet

This is chapter sixteen of our huge Arduino tutorial seriesUpdated 06/12/2013

In this chapter we will introduce and examine the use of Ethernet networking with Arduino over local networks and the greater Internet. It will be assumed that you have a basic understanding of computer networking, such as the knowledge of how to connect computers to a hub/router with RJ45 cables, what an IP and MAC address is, and so on. Furthermore, here is a good quick rundown about Ethernet.

Getting Started

You will need an Arduino Uno or compatible board with an Ethernet shield that uses the W5100 Ethernet controller IC (pretty much all of them):

Arduino Ethernet shield

…or consider using a Freetronics EtherTen – as it has everything all on the one board, plus some extras:

Freetronics EtherTen

Furthermore you will need to power the board via the external DC socket – the W5100 IC uses more current than the USB power can supply. A 9V 1A plug pack/wall wart will suffice. Finally it does get hot – so be careful not to touch the W5100 after extended use. In case you’re not sure – this is the W5100 IC:

Wiznet W5100

Once you have your Ethernet-enabled Arduino, and have the external power connected – it’s a good idea to check it all works. Open the Arduino IDE and selectFile > Examples > Ethernet > Webserver. This loads a simple sketch which will display data gathered from the analogue inputs on a web browser. However don’t upload it yet, it needs a slight modification.

You need to specify the IP address of the Ethernet shield – which is done inside the sketch. This is simple, go to the line:

And alter it to match your own setup. For example, in my home the router’s IP address is 10.1.1.1, the printer is 10.1.1.50 and all PCs are below …50. So I will set my shield IP to 10.1.1.77 by altering the line to:


You also have the opportunity to change your MAC address. Each piece of networking equipment has a unique serial number to identify itself over a network, and this is normall hard-programmed into the equipments’ firmware. However with Arduino we can define the MAC address ourselves.

If you are running more than one Ethernet shield on your network, ensure they have different MAC addresses by altering the hexadecimal values in the line:


However if you only have one shield just leave it be. There may be the very, very, statistically rare chance of having a MAC address the same as your existing hardware, so that would be another time to change it.

Once you have made your alterations, save and upload the sketch. Now open a web browser and navigate to the IP address you entered in the sketch, and you should be presented with something similar to the following:

 Arduino webserver example sketch

What’s happening? The Arduino has been programmed to offer a simple web page with the values measured by the analogue inputs. You can refresh the browser to get updated values.

At this point – please note that the Ethernet shields use digital pins 10~13, so you can’t use those for anything else. Some Arduino Ethernet shields may also have a microSD card socket, which also uses another digital pin – so check with the documentation to find out which one.

Nevertheless, now that we can see the Ethernet shield is working we can move on to something more useful. Let’s dissect the previous example in a simple way, and see how we can distribute and display more interesting data over the network. For reference, all of the Ethernet-related functions are handled by the Ethernet Arduino library. If you examine the previous sketch we just used, the section that will be of interest is:

Hopefully this section of the sketch should be familiar – remember how we have used serial.print(); in the past when sending data to the serial monitor box? Well now we can do the same thing, but sending data from our Ethernet shield back to a web browser – on other words, a very basic type of web page.

However there is something you may or may not want to  learn in order to format the output in a readable format – HTML code. I am not a website developer (!) so will not delve into HTML too much.

However if you wish to serve up nicely formatted web pages with your Arduino and so on, here would be a good start. In the interests of simplicity, the following two functions will be the most useful:


Client.print (); allows us to send text or data back to the web page. It works in the same way as serial.print(), so nothing new there. You can also specify the data type in the same way as with serial.print(). Naturally you can also use it to send data back as well. The other useful line is:


which sends the HTML code back to the web browser telling it to start a new line. The part that actually causes the carriage return/new line is the <br /> which is an HTML code (or “tag”) for a new line. So if you are creating more elaborate web page displays, you can just insert other HTML tags in the client.print(); statement. If you want to learn more about HTML commands, here’s a good tutorial site. Finally – note that the sketch will only send the data when it has been requested, that is when it has received a request from the web browser.

Accessing your Arduino over the Internet

So far – so good. But what if you want to access your Arduino from outside the local network?

You will need a static IP address – that is, the IP address your internet service provider assigns to your connection needs to stay the same. If you don’t have a static IP, as long as you leave your modem/router permanently swiched on your IP shouldn’t change. However that isn’t an optimal solution.

If your ISP cannot offer you a static IP at all, you can still move forward with the project by using an organisation that offers a Dynamic DNS. These organisations offer you your own static IP host name (e.g. mojo.monkeynuts.com) instead of a number, keep track of your changing IP address and linking it to the new host name. From what I can gather, your modem needs to support (have an in-built client for…) these DDNS services. As an example, two companies are No-IP andDynDNS.com. Please note that I haven’t used those two, they are just offered as examples.

Now, to find your IP address… usually this can be found by logging into your router’s administration page – it is usually 192.168.0.1 but could be different. Check with your supplier or ISP if they supplied the hardware. For this example, if I enter 10.1.1.1 in a web browser, and after entering my modem administration password, the following screen is presented:

WAN IP address router

What you are looking for is your WAN IP address, as you can see in the image above. To keep the pranksters away, I have blacked out some of my address.

The next thing to do is turn on port-forwarding. This tells the router where to redirect incoming requests from the outside world. When the modem receives such a request, we want to send that request to the port number of our Ethernet shield. Using the:

function in our sketch has set the port number to 125. Each modem’s configuration screen will look different, but as an example here is one:

Arduino router port forwarding

So you can see from the line number one in the image above, the inbound port numbers have been set to 125, and the IP address of the Ethernet shield has been set to 10.1.1.77 – the same as in the sketch.

After saving the settings, we’re all set. The external address of my Ethernet shield will be the WAN:125, so to access the Arduino I will type my WAN address with :125 at the end into the browser of the remote web device, which will contact the lonely Ethernet hardware back home.

Furthermore, you may need to alter your modem’s firewall settings, to allow the port 125 to be “open” to incoming requests. Please check your modem documentation for more information on how to do this.

Now from basically any Internet connected device in the free world, I can enter my WAN and port number into the URL field and receive the results. For example, from a phone when it is connected to the Internet via LTE mobile data:

Arduino webserver example cellular

So at this stage you can now display data on a simple web page created by your Arduino and access it from anywhere with unrestricted Internet access. With your previous Arduino knowledge (well, this is chapter sixteen) you can now use data from sensors or other parts of a sketch and display it for retrieval.

Displaying sensor data on a web page

As an example of displaying sensor data on a web page, let’s use an inexpensive and popular temperature and humidity sensor – the DHT22. You will need to install the DHT22 Arduino library which can be found on this page. If this is your first time with the DHT22, experiment with the example sketch that’s included with the library so you understand how it works.

Connect the DHT22 with the data pin to Arduino D2, Vin to the 5V pin and GND to … GND:

arduino ethernet freetronics etherten dht22 humid

Now for our sketch – to display the temperature and humidity on a web page. If you’re not up on HTML you can use online services such as this to generate the code, which you can then modify to use in the sketch.

In the example below, the temperature and humidity data from the DHT22 is served in a simple web page:

It is a modification of the IDE’s webserver example sketch that we used previously – with a few modifications. First, the webpage will automatically refresh every 30 seconds – this parameter is set in the line:

… and the custom HTML for our web page starts below the line:

You can then simply insert the required HTML inside client.print() functions to create the layout you need.

Finally – here’s an example screen shot of the example sketch at work:

arduino ethernet freetronics etherten dht22 humid cellular

You now have the framework to create your own web pages that can display various data processed with your Arduino.

Remote control your Arduino from afar

We have a separate tutorial on this topic, that uses the teleduino system.

Conclusion

So there you have it, another useful way to have your Arduino interact with the outside world. Stay tuned for upcoming Arduino tutorials by subscribing to the blog, RSS feed (top-right), twitter or joining our Google Group. And if you enjoyed the tutorial, or want to introduce someone else to the interesting world of Arduino – check out my book (now in a third printing!) “Arduino Workshop” from No Starch Press.

tronixstuff

Posted in arduino, DEV-09026, DEV-11230, DHT22, ethernet, internet, iot, shield, tronixstuff, tutorial12 Comments

Arduino Tutorials – Chapter 30 – twitter

Learn how to tweet from your Arduino.

This is chapter thirty of our huge Arduino tutorial seriesUpdated 16/06/2014

In this article you will learn how to send messages from an Ethernet-enabled Arduino to twitter. For the uninitiated who may be thinking “what is all this twitter nonsense about?”, twitter is a form of microblogging. 

You can create a message with a maximum length of 140 characters, and broadcast this on the twitter service. For people to receive your messages (or tweets) they also need to be a member of twitter and choose to subscribe to your tweets.

Generally people will use the twitter service using one of two methods: either using a web browser, or using the twitter application on a smartphone or tablet computer. For example, here is a typical web browser view:

twitter web browser

… and here is an example of a twitter application running on an Android OS smartphone:

twitter android

The neat thing about twitter on a mobile device is that if your username is mentioned in a tweet, you will be notified pretty well immediately as long as you have mobile data access. More on that later. In some areas, you can set twitter to send tweets from a certain user to your mobile phone via SMS – however if doing so be careful to confirm possible charges to your mobile phone account.

Finally, if you are worried about privacy with regards to your tweets, you can set your account to private and only allow certain people to follow your tweets.

So let’s get started.

First of all – you will need a twitter account. If you do not have one, you can sign up for one here. If you already have a twitter account, you can always open more for other uses – such as an Arduino.

For example, my twitter account is @tronixstuff, but my demonstration machine twitter account is @tronixstuff2. Then I have set my primary account to follow my machine’s twitter account.

Now log into twitter with using the account you will have for your Arduino and visit this page and get yourself a token by following the Step One link. The process will take you through authorising the “tweet library” page to login to your twitter account – this is ok. It will then present you with a long text called a “token”, for example:

twitter oauth token

Save your token somewhere safe, as you will need to insert it into your Arduino sketch. Finally, don’t give it to others as then they will be able to post onto twitter using your account. Next, follow step two from the same page – which involves download and installation of the required Arduino library.

Now for the hardware.

You will need an Arduino Uno or compatible board with an Ethernet shield that uses the W5100 Ethernet controller IC (pretty much all of them) – or consider using a Freetronics EtherTen – as it has everything all on the one board, plus some extras:

Freetronics EtherTen

Furthermore you will need to power the board via the external DC socket – the W5100 IC uses more current than the USB power can supply. A 9V 1A plug pack/wall wart will suffice. Finally it does get hot – so be careful not to touch the W5100 after extended use. In case you’re not sure – this is the W5100 IC:

Wiznet W5100If you’re looking for an Arduino-twitter solution with WiFi, check out the Arduino Yún tutorials.

From this point it would be a good idea to check your hardware is working. To do so, please run the webserver example sketch as explained in chapter sixteen (Ethernet). While you do that, we’ll have a break…

Lop Buri Thailand

Sending your first tweet

If you want your Arduino to send a simple tweet consider the following sketch. We have a simple function tweet() which simply sends a line of text (which has a maximum length of 140 characters). Don’t forget to update your IP address, MAC address and token:

You can check the status of the tweeting via the serial monitor. For example, if the tweet was successful you will see:

arduino twitter success 2014

However if you try to send the same tweet more than once in a short period of time, or another error takes place – twitter will return an error message, for example:

arduino twitter duplicate

And finally if it works, the tweet will appear:

Arduino twitter works 2014

Previously we mentioned that you can be alerted to a tweet by your mobile device. This can be done by putting your own twitter account name in the contents of the tweet.

For example – my normal twitter account is @tronixstuff. If I put the text “@tronixstuff” in the text tweeted by my Arduino’s twitter account – the twitter app on my smartphone will let me know I have been mentioned – as shown in the following video:

You may have noticed in the video that a text message arrived as well – that service is a function of my cellular carrier (Telstra) and may not be available to others. Nevertheless this is a neat way of getting important messages from your Arduino to a smart phone or other connected device.

Sending data in a tweet

So what if you have  a sensor or other device whose data you want to know about via twitter? You can send data generated from an Arduino sketch over twitter without too much effort.

In the following example we’ll send the value from analogue pin zero (A0) in the contents of a tweet. And by adding your twitter @username you will be notified by your other twitter-capable devices:

You may have noticed a sneaky sprintf function in void loop(). This is used to insert the integer analogZero into the character array tweetText that we send with the tweet() function. And the results of the example:

Arduino Twitter Tutorial success

So you can use the previous sketch as a framework to create your own Arduino-powered data twittering machine. Send temperature alerts, tank water levels, messages from an alarm system, or just random tweets to your loved one.

Conclusion

So there you have it, another useful way to send information from your Arduino to the outside world. Stay tuned for upcoming Arduino tutorials by subscribing to the blog, RSS feed (top-right), twitter or joining our Google Group. Big thanks to @neocat for their work with the twitter  Arduino libraries.

And if you enjoyed the tutorial, or want to introduce someone else to the interesting world of Arduino – check out my book (now in a third printing!) “Arduino Workshop” from No Starch Press.

tronixstuff

In the meanwhile have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column? And join our friendly Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

Posted in arduino, ethernet, shield, tronixstuff, tutorial, twitter5 Comments

Arduino and the XOBXOB IoT Platform

Introduction

If you’re awake and an Internet user, sooner or later  you’ll come across the concept of the “Internet of Things”. It is the goal of many people and organisations to have everything connected to everything for the exchange of data and the ability to control things. And as time marches on, more systems (or “platforms”) are appearing on the market. Some can be quite complex, and some are very easy to use – and this is where our interests lay. In the past we’ve examined the teleduino system, watched the rise of Ninja Blocks, and other connected devices like the lifx bulb and more.

However the purpose of this article is to demonstrate a new platform – XOBXOB (pronounced “zob-zob”) that gives users (and Arduino users in particular) a method of having remote devices connect with each other and be controlled over the Internet. At the time of writing XOBXOB is still in alpha stage, however you’re free to give it a go. So let’s do that now with Arduino.

Getting Started

You’ll need an Arduino and Ethernet shield – or a combination board such as a Freetronics EtherTen, or a WiFly board from Sparkfun. If you don’t have any Ethernet hardware there is a small application you can download that gives your USB-connected Arduino a link to the XOBXOB service. However before that, visit the XOBXOB homepage and register for an account. From there you can visit the dashboard which has your unique API key and a few controls:

XOBXOB dashboard

Now download the Arduino libraries and copy them into the usual location. If you don’t have an Ethernet shield, also get the “connector” application (available for all three OSs). The connector application is used after uploading the XOBXOB-enabled sketches to your Arduino and links it to the XOBXOB service.

Testing with exanples

Moving on, we’ve started with the basic LED control Ethernet sketch which is included in the XOBXOB library. It’s a fast way to check the system is working and your Internet connection is suitable. When using the examples for the first time (or any other XOBXOB sketch, don’t forget to enter your API key and Ethernet MAC address, for example:

We have the EtherTen connected to the ADSL and control via a cellular phone. It’s set to control digital pin 8 so after inserting an LED it worked first time:

The LED is simply turned on and off by using the ON/OFF panel on the XOBXOB dashboard, and then clicking “SET”. You can also click “GET” to retrieve the status of the digital output. The GET function is useful if more than one person is logged into the dashboard controlling what’s at the other end.

Now for some more fun with the other included example, which controls a MAX7219 LED display driver IC. We used one of the boards from the MAX7219 test a while back, which worked fine with the XOBXOB example in the Arduino library:

If this example doesn’t compile for you, remove the line:

Once operating, this example is surprisingly fun, and could be built into a small enclosure for a simple remote-messaging system.

Controlling your own projects

The functions are explained in the Arduino library guide, which you should download and review. Going back to the LED blink example, you can see how the sketch gets and checks for a new on/off message in the following code:

So instead of the digitalWrite() functions, you can insert whatever you want to happen when the ON/OFF button is used on the XOBXOB dashboard.  For example with the use of a Powerswitch Tail you could control a house light or other device from afar.

If you want to control more than one device from the dashboard, you need to create another XOB. This is done by entering the “advanced” dashboard and clicking “New”. After entering a name for the new XOB it will then appear in the drop-down list in either dashboard page. To then assign that XOB to a new device, it needs to be told to request that XOB by name in the Arduino sketch.

For example, if you created a new XOB called “garagelight” you need to insert the XOB name in the XOB.requestXOB() function in the sketch:

and then it will respond to the dashboard when required. Later on we’ll return to XOBXOB and examine how to upload information from a device to the dashboard, to allow remote monitoring of temperature and other data.

Conclusion

Experimenting with XOBXOB was a lot of fun, and much easier than originally planned. Although only in the beginning stages, I’m sure it can find a use with your hardware and a little imagination. Note that XOBXOB is still in alpha stage and not a finished product. For more information, visit hte XOBXOB website. And if you made it this far – check out my new book “Arduino Workshop” from No Starch Press.

In the meanwhile have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column? And join our friendly Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

Posted in arduino, ethernet, etherten, iot, tronixstuff, tutorial, WRL-09954, XOBXOB4 Comments

Internet-controlled relays with teleduino and Freetronics RELAY8:

Control relays over the Internet with Arduino in chapter forty-seven of a series originally titled “Getting Started/Moving Forward with Arduino!” by John Boxall – A tutorial on the Arduino universe. The first chapter is here, the complete series is detailed here.

Updated 24/11/2012

In this article we’re going to look at controlling relays over the Internet. In doing so you will then be able to turn almost anything on and off as long as you have http access on an Internet-enabled device. Why would you want to do this? Connect an outdoor light – and turn it on before arriving home. Control the power to your TV setup – then you can control childrens’ TV viewing at a whim. Control farm water pumps without getting out of the truck. We’ll break this down into two stages. First we’ll explain how the RELAY8: relay control shield works and control it locally, then control it remotely using the teleduino service. We will be using Arduino IDE v1.0.1.

This tutorial will assume you have an understanding from three other articles – so please have a quick read of I2C bus, the MCP23017 I/O expander and teleduino. But don’t panic – we’ll try and keep it simple here.

The RELAY8: shield

First – our relay shield. We’ll be using the Freetronics RELAY8: shield:

Using the RELAY8: you can control eight relays using the I2C bus and the MCP23017 I/O expander – which saves your digital outputs for other purposes. There are three hardware settings you need to consider when using the shield:

  1. Power – how will you power the relay coils?
    • You can directly connect between 5 and 24V DC using the terminal block on the right-hand side of the shield – great for stronger relay coils.
    • You can power the relay coils using power from the Arduino. So whatever power is going to the Arduino Vin can power the shield. To do this jumper the two pins next to the Vin shield connector. In doing so – you must check that the combined current draw of all your relays on at once will not exceed what is available to the Arduino. Usually OK when using solid-state relays, as most examples use around 15mA of current to activate. However double-check your relay specifications before doing so.
    • You can also power the Arduino board AND the shield by feeding in external power to the shield and jumpering the two pins described above
  2. Which I2C address to use for each shield? By default it is 0x20. However you can alter the last three bits of the address by changing the jumpers at the bottom-left of the shield. Each jumper represents one bit of the bus address – no jumper means zero, and a jumper means one. So if you jumper ADDR0, the address will be 0x21 – etc. Using this method you can then stack up to eight shields – and control 64 relays!
  3. Are you using an Arduino Leonardo board? If so – your shield I2C pins aren’t A4/A5 – they’re over near the top of the board:

However this isn’t a problem. Solder in some header pins to the shield’s SCL/SDA holes (next to AREF). Then turn over the RELAY8: board and you will see some solder pads as shown below. With a thin knife, cut the copper tracks shown with the blue lines:

Doing this will redirect the I2C bus from the microcontroller to the correct pins at the top-left. Once you have decided on your power and I2C-bus options, it’s time to connect the relays. Doing so is simple, just connect the +  and – from the relay coil to the matching position on your RELAY8: shield, for example:

Today we’re just using prototyping wires, so when creating a permanent installation ensure the insulation reaches the terminal block. When working with relays you would use a diode across the coil to take care of back-EMF – however the shield has this circuitry, so you don’t need to worry about that at all. And if you’re wanting to control more than one shield – they stack nicely, with plenty of clearance between shields, for example:

Now to test the shield with a quick demonstration. Our sketch will turn on and off each relay in turn. We use the addressing format described in table 1.4 of the MCP23017 data sheet,  The relays 1 to 8 are controlled by “bank A” of the MCP23017 – so we need to set that to output in our sketch, as shown below:

The sketch simply sends the values of 1, 2, 4, 8, 16, 32, 64 and 128 to the shield – each value in turn represents relays 1 to 8. We send 0 to turn off all the relays. Here’s a quick video showing it in action – the LEDs on the shield show the relay coil power status:

Now there is one small caveat – every time you send a new command to the MCP23017, it overwrites the status of the whole bank of pins. For example if relay 3 is on, and we send the value 2 – this will turn on relay 2 and turn off 3. Why? Because the values are converted to binary when heading down to the relay shield. So if we send 1, in binary this is:

which turns on relay 1 – and turns off relays 2 to 7. But then if we send 4 to turn on relay 3, in binary this is:

which turns on relay 3, but turns off relays 1, 2, and 4 to 8. So how do we turn on or off all eight relays at once? Just do a little binary to decimal conversion. Let’s say you want relays 1, 3, 5 and 7 on – and 2, 4, 6 and 8 off. In binary our command value would be:

and in decimal this is 85. Want to turn them all on at once? Send 255. Then all off? Send zero.

Now let’s do it via the Internet…

You’re going to need an Ethernet-enabled Arduino board. This could involve adding an Ethernet shield to your existing board, or using an all-in-one board like the Freetronics EtherTen. We will now use the teleduino service created by Nathan Kennedy to send commands to our Arduino boards via the Internet. At this point, please review and understand the teleduino article – then, when you can successfully control a digital output pin – return here to continue.

First, get the hardware together. So ensure your relay shield is in the Arduino and you have uploaded the

sketch. For the first couple of times, it’s good to still have the teleduino status LED connected – just to keep an eye on it. Plug your Arduino into your router and the power. After it connects to teleduino (four blinks of the status LED) we have to send three commands via http. The first tells teleduino that we’re sending I2C commands. You only do this once after every Arduino reset or power-up situation. It is:

Remember to replace 999999 with your teleduino key. Then we send:

At this stage the relay shield is now ready to accept your bytes to turn on and off the outputs. Again, just like the sketch – we send two bytes. For example:

turns on all the outputs – however with the URL we need to send the byte representing the outputs in hexadecimal. So 255 is FF, 0 is 0, etc. For example to turn them all off, use:

or to turn on outputs 1, 2, 3 and 4 use:

Simple. You can simply bookmark your URLs for later use as well – and don’t forget to use a URL-shortener such as bit.ly to makes things simpler for you.

Conclusion

Now you have a way to control many relays either locally or remotely over the Internet. I hope you found this article useful or at least interesting. If you have any suggestions for further articles (and not thinly-veiled methods of asking me to do your work for you…) – email them to john at tronixstuff dot com. Thanks to Freetronics for the use of their hardware and Nathan Kennedy for teleduino, his support and advice.

LEDborder

Have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

Posted in arduino, ethernet, I2C, internet, lesson, remote, teleduino, tutorial9 Comments

Interact with Arduino over the Internet with Teleduino

Introduction

[Updated 06/12/2013]

Recently a new method of interacting with an Ethernet-enabled Arduino board and the Internet was brought to my attention – a new system called Teleduino. In this article we test a few of the basic features and see what is possible. Please note that these are my own experiments and that Teleduino is a work in progress. So follow along and see for yourself.

Getting Started

  1. You will need an Arduino Uno (or compatible) board and Ethernet shield with the Wiznet chip – or a Freetronics EtherTen (a much neater solution). Teleduino now supports Arduino Mega and the awesome EtherMega.
  2. Download and install the Teleduino Arduino library. This is available from the resources section of the home page. You will also need to be running Arduino IDE v1.0 or greater.
  3. Request an API key. This identified your particular Arduino from the rest.
  4. Get together some basic electronics components for testing, such as some LEDs and 560R resistors; sources of analog input such as an LDR or TMP36 temperature sensor; and a solderless breadboard.
  5. Don’t forget the Ethernet cable from your Arduino stack to the router!
  6. Finally, some rudimentary knowledge about networking will be useful. (IP address, DHCP, etc.)
The Teleduino system uses pin D8 for a status LED, so you may find connecting one up now useful while experimenting. Connect as such:

Controlling digital outputs

In this example we control an LED, turning it on and off. For demonstration purposes, connect another LED with a resistor to D6 in the same method as shown above. Next, you need to upload a sketch to the Arduino. It is the

which is included with the library examples. Before uploading, you need to make some modifications. The first of these is to add your API key. Go back to the email you received from Teleduino, and click on the link provided. It will take you to a website that shows a byte array variable named byte key[]. You will copy this into the sketch, replacing the same array full of hexadecimal zeros in the sketch – as shown below – with your own:

Next, scroll down to

… and change one of the hexadecimal numbers to 0x00… just in case there is a clash with other addresses on your network. You never know. Finally – depending on your network router, you may need to manually allocate the IP address for your Ethernet shield and/or set the DNS server to use. To do this, scroll down to

where you can change the useDHCP and/or useDNS variables to false, and update those values below. However if you’re not sure, just leave them be unless you need to change them. Finally – upload the sketch to your Arduino, get the hardware together and plug it into the network.

Watch your status LED – it will blink a number of times, depending on the status of things. The blink levels are:

  • 1 blink – initialising
  • 2 blinks – starting network connection
  • 3 blinks – connecting to the Teleduino server
  • 4 blinks – authentication successful
  • 5 blinks – session already exists for supplied key (sometimes happens after a quick restart – will work on next auto-restart)
  • 6 blinks – Invalid or unauthorised key – check your API key is correctly entered in the sketch as described earlier
  • 10 blinks – connection dropped

If all is well, after a minute yours should be on blink level 4, then it will idle back to blink level 1. Now to test the connection with our first command.

You send commands to the Arduino using a set of URLs that will contain various parameters. You will need your API key again for these URLs which is then inserted into the URL. The first will report the version of software on the Arduino. Send

however replace 999999 with your API key (and in all examples shown here). If successful, you should see something similar to the following in the web browser:

However if something is wrong, or there are connection difficulties you will see something like:

Before using digital outputs, and after every reset of the Arduino) you need to set the pin mode for the digital output to control. In our example, we use:

Note that the pin number and mode are set with single digits, as you can see above this is for pin 6, and we use mode=1 for output. You should save this as a bookmark to make life easer later on. When the command has been successfully sent, a message will be shown in the webpage, for example:

Moving forward – you turn the digital output on with the following:

and to turn it off, set the final part of the URL to

Easy. How did you go? It really is amazing to see it work. Now you can control your Arduino from almost anywhere in the world. Again, saving these as bookmarks to make things easier, or a URL shortening service.

At this point you should now have the gist of the Teleduino service and how it is operated.

There is so much more you can do, and currently the list includes (From the author):

  • Reset, ping, get version, get uptime, get free memory.
  • Define pin modes, set digital outputs, set analog outputs, read digital inputs, read analog inputs, or read all inputs with a single API call.
  • Define up to 2 ‘banks’ of shift registers. Each ‘bank’ can contain up to 32 cascaded shift registers, giving a total of 512 digital outputs.
  • Shift register outputs can be set, or merged, and expire times can be set on merges (you could set an output(s) high for X number of milliseconds).
  • Define, and read and write from serial port.
  • Read and write from EEPROM.
  • Define and position up to 6 servos.
  • Set preset values for the above functions, which get set during boot. Preset values are stored in the first 160ish bytes of the EEPROM.

[22/09/2012] New! You can also control the I2C bus – check out this tutorial for more information. For more information check the Teleduino web site, and further tutorials can be found here. Here is a simple example of Teleduino at work – controlling a light switch:

Conclusion

At this moment Teleduino is simple, works and makes a lot of ideas possible. We look forward to making more use of it in future projects, and hope you can as well. Kudos to Nathan Kennedy, and we look forward to seeing Teleduino advance and develop over the future. If all this Arduino is new to you, check out the tutorials.  Thanks to Freetronics for the use of their Ethernet-enabled hardware.

In the meanwhile have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column? And join our friendly Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

Posted in arduino, ethernet, internet, lesson, remote, teleduino, tutorial10 Comments

Review – Freetronics EtherMega

In this review we take a look at what is possibly the most fully-featured Arduino compatible board on the market today – the Freetronics EtherMega. This board combines the functionality of an Arduino Mega2560, a microSD card shield, and an Ethernet shield that supports power over Ethernet with optional 802.3af standard. So instead of having these three mashed together at a great expense:

thelot

… you can have this:

Freetronics EtherMega Tronixlabs Australia

Which saves space, time and money. Firstly, the specifications:

  • 100% compatible with the Arduino Mega2560. So you have the ATmega2560 microcontroller, 54 digital I/O pins with 14 PWM-capable, 256KB of flash memory, 8KB of SRAM and 4KB of EEPROM to play with, the Atmel 16u2 micrcontroller taking care of the USB interface;
  • However unlike the original, the EtherMega contains a switchmode power supply that allows operation from a DC power supply of between 7 and 28VDC without overheating;
  • Complete c0mpatibility with the Arduino Ethernet shield, using the Wiznet W5100 controller just like the original;
  • Network status LEDs on both the socket and the PCB;
  • Fixed SPI behaviour on Ethernet chipset;
  • Complete microSD card compatibility with SD library, and chip-select is on digital pin 4 so Ethernet and microSD can work together on the same sketch;
  • optional 802.3af power over Ethernet support at up to 48V using the optional regulator board which mounts on the EtherMega;
  • mini USB connector instead of the larger standard USB socket which can interfere with shields – and a USB cable is included

Furthermore there are a few modifications to make using the EtherMega easier or simpler. The first of these is the onboard prototyping area allowing you to add your own circuitry:

Also notice that the I2C pins have been brought out alongside the 5V and GND pins on the right. The only difference to take note of are the jumpers that are used to select either USB or DC socket power:

However that is a small price to pay compared to the convenience of the wide voltage-handling capability. Finally, unlike the original Arduino Mega2560 the designers have placed the TX/RX indicators at the top-left of the EtherMega so they are still visible when extra shields have been mounted:

The overall design and quality of the EtherMega is top notch, with a thick PCB, rounded corners, descriptive silk-screening, and packaging that can be reused as Mega or other part storage.

If you are looking for an Arduino Mega2560 and could use Ethernet, power-over-Ethernet, a microSD card interface and full, 100% Arduino compatibility you could do a lot worse than getting yourself an EtherMega. If you are interested in learning how to use Arduino and Ethernet – check out our tutorial here. Or to get your Arduino tweeting, visit here.

And if you enjoyed this article, or want to introduce someone else to the interesting world of Arduino – check out my book (now in a fourth printing!) “Arduino Workshop”.

visit tronixlabs.com

Have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our forum – dedicated to the projects and related items on this website.

Posted in 802.3af, arduino, ethermega, ethernet, freetronics, review, shield, tronixlabs0 Comments

Review: The Gravitech Arduino Nano family

Hello Readers

In this article we will examine a variety of products received for review from Gravitech in the United States – the company that designed and build the Arduino Nano. We have a Nano and some very interesting additional modules to have a look at.

So let’s start out review with the Arduino Nano. What is a Nano? A very, very small version of our Arduino Duemilanove boards. It contains the same microcontroller (ATmega328) but in SMD form; has all the I/O pins (plus two extra analogue inputs); and still has a USB interface via the FT232 chip. But more on that later. Nanos arrive in reusable ESD packaging which is useful for storage when not in use:

Patriotic Americans should note that the Nano line is made in the USA. Furthermore, here is a video clip of Nanos being made:

For those who were unsure about the size of the Nano, consider the following images:

You can easily see all the pin labels and compare them to your Duemilanove or Uno board. There is also a tiny reset button, the usual LEDs, and the in circuit software programmer pins. So you don’t miss out on anything by going to a Nano. When you flip the board over, the rest of the circuitry is revealed, including the FTDI USB>serial converter IC:

Those of you familiar with Arduino systems should immediately recognise the benefit of the Nano – especially for short-run prototype production. The reduction in size really is quite large. In the following image, I have traced the outline of an Arduino Uno and placed the Nano inside for comparison:

So tiny… the board measures 43.1mm (1.7″) by 17.8mm (0.7″). The pins on this example were pre-soldered – and are spaced at standard 2.54mm (0.1″) intervals – perfect for breadboarding or designing into your own PCB –  however you can purchase a Nano without the pins to suit your own mounting purposes. The Nano meets all the specifications of the standard Arduino Duemilanove-style boards, except naturally the physical dimensions.

Power can be supplied to the Nano via the USB cable; feeding 5V directly into the 5V pin, or 7~12 (20 max, not recommended) into the Vin pin. You can only draw 3.3V at up to 50 mA when the Nano is running on USB power, as the 3.3V is sourced from the FTDI USB>serial IC. And the digital I/O pins still allow a current draw up to 40 mA each. From a software perspective you will not have any problems, as the Nano falls under the same board classification as the (for example) Arduino Duemilanove:

Therefore one could take advantage of all the Arduino fun and games – except for the full-size shields. But as you will read soon, Gravitech have got us covered on that front. If the Arduino system is new to you, why not consider following my series of tutorials? They can be found here. In the meanwhile, to put the size into perspective – here is a short video of a Nano blinking some LEDs!

Now back to business. As the Nano does not use standard Arduino shields, the team at Gravitech have got us covered with a range of equivalent shields to enable all sorts of activities. The first of this is their Ethernet and microSD card add-on module:

and the underside:

Again this is designed for breadboarding, or you could most likely remove the pins if necessary. The microSD socket is connected as expected via the SPI bus, and is fully compatible with the default Arduino SD library. As shown in the following image the Nano can slot directly into the ethernet add-in module:

The Ethernet board requires an external power supply, from 7 to 12 volts DC. The controller chip is the usual Wiznet 5100 model, and therefore the Ethernet board is fully compatible with the default Ethernet Arduino library. We tested it with the example web server sketch provided with the Arduino IDE and it all just worked.

The next add-on module to examine is the 2MOTOR board:

… and the bottom:

Using this module allows control of two DC motors with up to two amps of current each via pulse-width modulation. Furthermore, there is a current feedback circuit for each motor so you measure the motor load and adjust power output – interesting. So a motorised device could sense when it was working too hard and ease back a little (like me on a Saturday). All this is made possible by the use of the common L298 dual full-bridge motor driver IC. This is quite a common motor driver IC and is easy to implement in your sketches. The use of this module and the Nano will help reduce the size of any robotics or motorised project. Stay tuned for use of this board in future articles.

Next in this veritable cornucopia of  add-on modules is the USBHOST board:

turning it over …

Using the Maxim MAX3421E host controller IC you can interface with all sorts of devices via USB, as well as work with the new Android ADK. The module will require an external power supply of between 7 and 12 volts DC, with enough current to deal with the board, a Nano and the USB device under control – one amp should be more than sufficient. I will be honest and note that USB and Arduino is completely new to me, however it is somewhat fascinating and I intend to write more about using this module in the near future. In the meanwhile, many examples can be found here.

For a change of scene there is also a group of Xbee wireless communication modules, starting with the Xbee add-on module:

The Xbee itself is not included, only shown for a size comparison. Turning the module over:

It is nice to see a clearly-labelled silk screen on the PCB. If you are unfamiliar with using the Xbee wireless modules for data communication, you may find my introductory tutorial of interest. Furthermore, all of the Gravitech Nano modules are fully software compatible with my tutorial examples, so getting started will be a breeze. Naturally Gravitech also produce an Xbee USB interface board, to enable PC communication over your wireless modules:

Again, note that the Xbee itself is not included, however they can be supplied by Gravitech. Turning the board over reveals another highly-detailed silk screen:

All of the Gravitech Xbee modules support both series 1.0 and 2.5 Xbees, in both standard and professional variants. The USB module also supports the X-CTU configuration software from Digi.

Finally – leaving possibly the most interesting part until last, we have the MP3 Player add-on board:

and on the B-side:

The MP3 board is designed around the VS1053B MP3 decoder IC. It can also decode Ogg Vorbis, AAC, WMA and MID files. There is a 3.5mm stereo output socket to connect headphones and so on. As expected, the microSD card runs from the SPI pins, however SS is pin 4. Although it may be tempting to use this to make a home-brew MP3 player, other uses could include: recorded voice messages for PA systems such as fire alarm notices, adding sound effects to various projects or amusement machines, or whatever else you can come up with.

Update – We have examined the MP3 board in more detail with a beginner’s tutorial.

The Arduino Nano and related boards really are tiny, fully compatible with their larger brethren, and will prove very useful. Although this article was an introductory review, stay tuned for further projects and articles that will make use of the Nano and other boards. If you have any questions or enquiries please direct them to Gravitech via their contact page. Gravitech products including the Arduino Nano family are available directly from their website or these distributors.

As always, thank you for reading and I look forward to your comments and so on. Furthermore, don’t be shy in pointing out errors or places that could use improvement. Please subscribe using one of the methods at the top-right of this web page to receive updates on new posts, follow on twitterfacebook, or join our Google Group.

[Disclaimer – the products reviewed in this article are promotional considerations made available by Gravitech]

High resolution photos are available on flickr.

Otherwise, have fun, be good to each other – and make something! 

Posted in arduino, ethernet, gravitech, microcontrollers, mp3, nano, part review, xbee0 Comments

Moving Forward with Arduino – Chapter 30 – twitter

Learn how to tweet from your Arduino in chapter thirty of a series originally titled “Getting Started/Moving Forward with Arduino!” by John Boxall – A tutorial on the Arduino universe.

[Updated 26/7/2013]

In this article we will learn how to send messages from our Arduino to twitter. For the uninitiated who may be thinking “what is all this twitter nonsense about?”, twitter is a form of microblogging. You can create a message with a maximum length of 140 characters, and broadcast this on the twitter service. For people to receive your messages (or tweets) they also need to be a member of twitter and choose to subscribe to your tweets.

Generally people will use the twitter service using one of three methods: using a web browser on a personal computer or internet device, on a mobile phone, or using a specific application such as TweetDeck on one of the aforementioned devices. For example, here is a typical web browser view:

And here is an example of a twitter application running on an Android OS smartphone:

tweetdeck

So as you can see, it is easy enough to read peoples’ tweets. Therein lies the reason for this article – we can harness twitter as an output device for our Arduino systems. We can broadcast various messages, so systems can be created to monitor specific parameters and report on their status at regular intervals, upon an event occurring, and so on.

In some areas, you can set twitter to send tweets from a certain user to your mobile phone via SMS – however if doing so be careful to confirm possible charges to your mobile phone account. Finally, if you are worried about privacy with regards to your tweets, you can set your account to private and only allow certain people to follow your tweets.

So let’s get started. First of all – you will need a twitter account. If you do not have one, you can sign up for one here. If you already have a twitter account, you can always open more for other uses – such as an Arduino. For example, my twitter account is @tronixstuff, but my demonstration machine twitter account is @tronixstuff2. Then I have set my primary account to follow my machine’s twitter account. Once you have logged into twitter with your machine account, visit this page and get yourself a token by following the Step One link. Save your token somewhere safe, you’ll need to insert it into your Arduino sketch.

Next, you will need some hardware. Apart from your usual Arduino board, you will need an Ethernet shield. However to save space and money I’ll be using the Freetronics EtherTen:

If you are unfamiliar with using Arduino and Ethernet, please review chapter sixteen before continuing forward with this article. From a software perspective, we will need another library for our Arduino IDE. Download and install the twitter library from here. Now, at this point – please run the Webserver example described in chapter sixteen and ensure it is working before moving forward from this point. While you do that, we’ll have a break…

lopburi-0606

Now it is time to send our first tweet. The following sketch is a modification of the demonstration version, in which we have isolated the tweet-sending into a separate function called (strangely enough) tweet();. It is not complex at all:

So after uploading the above sketch, running a network cable from your access point to the Ethernet shield, and powering up the Arduino board – your tweet should appear as such:

Excellent – it works. And I hope yours did as well. If it did not, open the serial monitor box to get some feedback from the sketch. From experimentation the most amount of errors are caused by incorrect IP and trying to send multiple tweets too quickly. If you get excited and try to run the sketch again by hitting reset, twitter will reply back with an error – it does not allow duplicate tweets to be sent (over a short period of time). Twitter will reply to your tweet with a code which describes the result of your tweet. This code is stored in an integer variable using the function:

For example, 200 means the tweet was sent successfully, and 403 means you have attempted a duplicate tweet. However you can omit the code-checking if you are not fussed about your tweet’s status.

Although it was fun tweeting Hello world, let’s create an example that reacts to various events and tweets about them. To simulate some events I have connected four buttons to digital inputs (using the button board from chapter twelve). Pressing a button sends of the matching message. However you can use any form of digital output or decision-making in your sketch. For now, here is the example sketch:

And here is a screen shot of the results after pressing buttons one, four, two then three:

So there you have it, another useful way to send information from your Arduino to the outside world. Stay tuned for upcoming Arduino tutorials by subscribing to the blog, RSS feed (top-right), twitter or joining our Google Group. Big thanks to @neocat for their work with the twitter  Arduino libraries.

LEDborder

Have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

Posted in arduino, cellular, ethernet, learning electronics, microcontrollers, tutorial, twitter2 Comments

Moving Forward with Arduino – Chapter 16 – Ethernet

Use Ethernet with Arduino in chapter sixteen of “Getting Started with Arduino!” by John Boxall – A tutorial on the Arduino universe. The first chapter is here, the complete series is detailed here.

[Updated 09/01/2013]

In this instalment we will introduce and examine the use of Ethernet networking with Arduino systems. This tutorial covers receiving data from an Arduino over the Internet. If you are interested in controlling an Arduino over the Internet, see here. It will be assumed that you have a basic understanding of computer networking, such as the knowledge of how to connect computers to a hub/router with RJ45 cables, what an IP and MAC address is, and so on. Furthermore, here is a good quick rundown about Ethernet.

First of all, you will need an Ethernet shield. There are a few on the market, such as the original version by the Arduino team. Readers of my articles will know my preference is for the Australian-designed Freetronics line of hardware, so I will be using their EtherTen – which combines an Arduino Uno-compatible board with an Ethernet shield. Plus it also has some interesting power-over-Ethernet features which you can read about here. However as long as your Arduino Ethernet shield has the W5100 controller IC – you’re fine.

Now, let’s get started!

This is an ethernet shield on top of an Arduino-compatible board. Nothing new here – just a nice RJ45 socket which you connect to your router/hub/modem with a patch lead:

shieldbss

First of all, let’s do something quick and easy to check that all is functional. Open the Arduino IDE and select File > Examples > Ethernet > Webserver. This loads a simple sketch which will display data gathered from the analogue inputs on a web browser. However don’t upload it yet, it needs a slight modification.

You need to specify the IP address of the ethernet shield – which is done inside the sketch. This is simple, go to the line:

And alter it to match your own setup. For example, in my home the router’s IP address is 10.1.1.1, the printer is 10.1.1.50 and all PCs are below …50. So I will set my shield IP to 10.1.1.77 by altering the line to:

You also have the opportunity to change your MAC address. Each piece of networking equipment has a unique serial number to identify itself over a network, and this is normall hard-programmed into the equipments’ firmware. However with Arduino we can define the MAC address ourselves. If you are running more than one ethernet shield on your network, ensure they have different MAC addresses by altering the hexadecimal values in the line:

However if you only have one shield just leave it be. There may be the very, very, statistically rare chance of having a MAC address the same as your existing hardware, so that would be another time to change it. Once you have made your alterations, save and upload the sketch to your Arduino or compatible board. If you haven’t already, disconnect the power and add your Ethernet shield.

Now, connect the shield to your router or hub with an RJ45 cable, and the Arduino board to the power via USB or external power supply. Then return to your computer, and using your web browser, enter your Ethernet shield’s IP address into the URL bar. The web browser will query the Ethernet shield, which will return the values from the analogue ports on the Arduino board, as such:

As there isn’t anything plugged into the analog inputs, their value will change constantly. Neat – your Arduino is now serving data over a network. It is quite motivating to see it actually work.

At this point – please note that the Ethernet shields use digital pins 10~13, so you can’t use those for anything else. Some Arduino Ethernet shields may also have a microSD card socket, which also uses another digital pin – so check with the documentation to find out which one. If you are considering using an Arduino Mega and Ethernet – check out the EtherMega.

Nevertheless, now that we can see the Ethernet shield is working we can move on to something more useful. Let’s dissect the previous example in a simple way, and see how we can distribute and display more interesting data over the network. For reference, all of the Ethernet-related functions are handled by the Ethernet Arduino library. If you examine the previous sketch we just used, the section that will be of interest is:

Hopefully this section of the sketch should be familiar – remember how we have used serial.print(); in the past when sending data to the serial monitor box? Well now we can do the same thing, but sending data from our Ethernet shield back to a web browser – on other words, a very basic type of web page. However there is something you may or may not want to  learn in order to format the output in a readable format – HTML code. I am not a website developer (!) so will not delve into HTML too much.

However if you wish to serve up nicely formatted web pages with your Arduino and so on, here would be a good start. In the interests of simplicity, the following two functions will be the most useful:

Client.print (); allows us to send text or data back to the web page. It works in the same way as serial.print(), so nothing new there. You can also specify the data type in the same way as with serial.print(). Naturally you can also use it to send data back as well. The other useful line is:

this sends the HTML code back to the web browser telling it to start a new line. The part that actually causes the carriage return/new line is the <br /> which is an HTML code (or “tag”) for a new line. So if you are creating more elaborate web page displays, you can just insert other HTML tags in the client.print(); statement.

Note that the sketch will only send the data when it has been requested, i.e. received a request from the web browser. So let’s put our new knowledge into action with some simple sensor hardware – measuring temperature and pseudo-light levels. In chapter fourteen we did this and sent the results over the air using XBee wireless modules. Now we shall make that data available to a web browser instead.

We will need:

  • Arduino Uno or compatible board and Ethernet shield, or
  • Freetronics EtherTen
  • Analog Devices TMP36 temperature sensor
  • 10 k ohm resistor
  • light-dependent resistor/photocell

Here is the schematic for the circuit:

example16p1sch

and in real life. If you were to construct a permanent application, the Freetronics shield is great as you have all that prototyping space:

exam16p1hardss

and download the sketch from here. Finally, the example in action, on the desktop PC:

exam16p1chrome

… and on a phone via my internal wireless access point (the screen is a little fuzzy due to the adhesive screen protector):

exam16p1desiress

Now you can see how easy it is to send data from your Arduino via an Ethernet network to a web browser. But that is only to a local web browser. What if I wanted to read that data using my phone from an Internet cafe in downtown Vientiane? It can be done, but is a little bit tricky for the uninitiated – so let’s get initiated!

You will need a static IP address – that is, the IP address your internet service provider assigns to your connection needs to stay the same. If you don’t have a static IP, as long as you leave your modem/router permanently swiched on your IP shouldn’t change.

However, if your internet service provider cannot offer you a static IP at all, you can still move forward with the project by using an organisation that offers a Dynamic DNS. These organisations offer you your own static IP hostname (e.g. mojo.monkeynuts.com) instead of a number, keep track of your changing IP address and linking it to the new hostname. From what I can gather, your modem needs to support (have an in-built client for…) these DDNS services. As an example, two companies are No-IP and DynDNS.com. Please note that I haven’t used those two***, they are just offered as examples.

Now, to find your IP address, usually this can be found by logging into your router’s administration page. For this example, if I enter 192.168.0.1 in a web browser, and after entering my modem administration password, the following screen is presented:

wanip

What you are looking for is your WAN IP address, as artistically circled above. To keep the pranksters away, I have blacked out some of my address. The next thing to do is turn on port-forwarding. This tells the router where to redirect incoming requests from the outside world. When the modem receives such a request, we want to send that request to the port number of our Ethernet shield. Using the Server server(80); function in our sketch has set the port number to 80. Each modem’s configuration screen will look different, but as an example here is one:

bobportfwdss

So you can see from the line number one, the inbound port numbers have been set to 80, and the IP address of the Ethernet shield has been set to 192.168.0.77 – the same as in the sketch. After saving the settings, we’re all set. The external address of my Ethernet shield will be the WAN:80, e.g.  213.123.456.128:80 into the browser of a web device will contact the lonely Ethernet hardware back home. Furthermore, you may need to alter your modem’s firewall settings, to allow the port 80 to be “open” to incoming requests. Please check your modem documentation for more information on how to do this.

Now from basically any Internet connected device in the free world, I can enter my WAN and port number into the URL field and receive the results. For example, from a phone when it is connected to the Internet via 3.5G mobile data:

How neat is that? The web page, not the phone. Well, the phone is pretty awesome too.

OK, it’s just the temperature – but with your other Arduino knowledge from our tutorials and elsewhere – you can wire up all sorts of sensors, poll them from your Arduino and use the Ethernet shield and an Internet connection to access that data from anywhere. Here are some applications that spring to mind, all can be made possible with details from our previous tutorials:

  • Sensitive temperature monitoring (e.g. a smoke house, tropical fish tank, chemical storage room, and so on);
  • “Have the children come home from school?” – children must swipe their RFID tag when they arrive home. Arduino stores time and tag number, which can be converted into display data for web output;
  • For single room-mates – perhaps a remote, high-tech version of a necktie on a doorknob… when the “busy” flatmate arrives home, they turn a switch which is read by the Arduino, and is then polled by the sketch – the other flatmates can poll from their phone before coming home;
  • Using reed switch/magnet pairs, you could monitor whether important doors or windows (etc.) were open or closed.
  • A small RFID could be placed on the collar of your pet – two RFID readers on each side of a cat/dog flap door. Using simple logic the Arduino could calculate if the pet was inside or outside, and the last time the pet went through the door.
  • send twitter messages

The possibilities are only limited by your imagination or requirements.

LEDborder

Have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

Posted in arduino, DEV-09026, education, ethernet, etherten, learning electronics, lesson, microcontrollers, shield, tronixstuff, tutorial13 Comments


Subscribe via email

Receive notifications of new posts by email.

The Arduino Book

Arduino Workshop

Für unsere deutschen Freunde

Dla naszych polskich przyjaciół ...

Australian Electronics!

Buy and support Silicon Chip - Australia's only Electronics Magazine.

Use of our content…

%d bloggers like this: