16 November 2015

Get Arduino Data over the internet using jQuery and AJAX

Description

Have you ever wanted to transmit Arduino data over the internet?

In this tutorial, we will design a web page that will retrieve Analog readings from the Arduino's Analog Pins and display them on a bar chart within the web page.

The web page will use jQuery and AJAX to request the data from the Arduino Web Server, allowing us to update the bar chart dynamically, without having to refresh the entire web page. The Arduino Web Server will send the Analog readings to the web page in JSON format where it will be processed and displayed accordingly.

In this tutorial, I will not have anything connected to the Arduino's Analog pins, which means the data retrieved will be that of randomly floating analog pins. Feel free to connect a potentiometer, temperature sensor or any other analog sensor to these pins if you want to create a more "useful" project.

The main aim here was to show you how to transmit the data in JSON format, and to update only a portion of the web page using asynchronous communication (using AJAX), to improve the performance of data retrieval and visualisation.


Parts Required:

Please note: The WIZnet ioShield-A ver1.1 actually comes with the WIZ550io board. So if you buy the ioShield-A, you will receive both boards. I have provided the link to the WIZ550io shield because you can buy that shield on its own. Regardless, you will need to use both boards for this tutorial.


Arduino Libraries and IDE

To program the Arduino you will need to download the Arduino IDE, and install the WIZnet Ethernet Library. The Arduino IDE version used in this tutorial was version 1.6.4.
You may want to read the WIZnet wiki information for each WIZnet shield before use.


 

ARDUINO CODE:

Full description of the Arduino code is included in the YouTube video above. Once you have set up your Arduino Web Server, you should be able to ping it. Look at this website, if you don't know how to use the windows ping feature.


Getting the Arduino Board onto the internet:

There isn't anything really to hook up for this project. You just have to align the pins for each board and stack them. You can power the Arduino via the USB cable. This will also be useful for debugging and printing to the Serial monitor. An ethernet cable needs to connect the WIZ550io board's ethernet port to your internet/network router

  • The WIZ550io board goes on the top

  • The ioShield-A is in the middle

  • The Arduino UNO is on the bottom

  • This is what it looks like when they are stacked together

  • If you want to gain easy access to the Analog or digital pins without de-soldering the ioShield-A, you can introduce some female headers like this:

  • Please note that the ioShield-A utilises a number of pins on the Arduino UNO - including: D2, D4, D7, D10, GND, and IOREF, RESET, 5V, GND, GND and ICSP pins
  • All Analog pins are available for use


 

Set the Arduino Web Server on your local network

You can test this project on your local network. You just have to choose an available IP address and PORT within your router's IP range. If you don't know your local IP address range - you can have a look at this site to give you a helping hand.


Set the Arduino Web Server to be accessed from anywhere in the world

If you want to access your Arduino from anywhere in the world, you need to set up Port Forwarding on your internet network router. The following useful guides will hopefully get you on the right track, if you have never set up Port forwarding.


In my case, I programmed the Arduino UNO Web Server to take the following ip address on my internal network: 10.1.1.99

I programmed the Arduino Web Server to listen for Web Browsers on port 8081.
So if I wanted to connect to the Arduino Web Server through my home network, I just had to type in this web address into my web browser: http://10.1.1.99:8081

If you plan to connect to the Arduino using port 80 (which is the default port for web browsers), you can just type the IP address without specifying the port (eg. http://10.1.1.99/ )

The web browser should display the Arduino data in JSON format (the YouTube video above will show you what that looks like).

Once I knew I could connect to the Arduino in my internal network, I then set up port forwarding on my router so that I could type in my external IP address and special port to tunnel my way into my Arduino Web Server on my internal network. This is what I had to do on my router, but you may need to do something different.

  1. My first step was to find out my public/external IP address by typing "what is my IP address" into google. If you want to know your external IP address click here.
  2. I then typed my router's ip address into a web browser, and logged into my router.
  3. I went to the advanced settings tab
  4. Selected "Port Forwarding" from the side menu
  5. Filled out all of the details on the first line of the Ports list
    • Enable box = ticked
    • Description = Arduino
    • WAN interface = pppoe_atm0/ppp0
    • Inbound port = 8082
    • Type = TCP
    • Private IP address = 10.1.1.99
    • Private port = 8081
  6. Saved the settings

Now that I had port forwarding enabled, I could type the ip address (that I obtained in step 1) into my browser and specified port 8082 (instead of 8081) - eg. http://190.11.70.253:8082/

And now I can access my Arduino Web server from anywhere in the world. I can even access it from my smart phone. Once again, this will only return the Analog data in JSON format.


The Web Page GUI

The Arduino is now on the internet, so there are two options. You can either

Instructions on how to use these web pages, are listed below the HTML code.



To retrieve data from your Arduino Web Server, please make sure that it is connected and visible from outside of you local network. You will need to have port forwarding enabled. Information on port forwarding is described above.

  1. Find what your external IP address is.
  2. Enter this address using the IP address drop-down boxes within the "ArduinoBasics Webserver Data viewer" web page
  3. Enter the port forwarding port number (eg. 8082) into the box labelled "Port"
  4. Then click on the "Click here to start getting data" button - you should start to see the bar charts moving and status should be OK
  5. If the bar charts do not move, and the status message says "Failed to get DATA!!" - then the web page was unable to connect to the Arduino for some reason.



Troubleshooting

  • You may want to type in the web address into your web browser, to make sure that data is being retrieved.
  • You can also open the Serial monitor in the Arduino IDE to make sure that an IP address is being displayed
  • Ensure that you have enabled the port forwarding option on your router
  • Have a look at Developer Tools within Google Chrome to help diagnose web page related issues.
  • The web page will not work properly if you use Internet Explorer or if you have javascript disabled within your browser.

Concluding comments

This tutorial showed you how to connect to your Arduino UNO over the internet, and retrieve data in JSON format using jQuery and AJAX. The web page can be modified to suit your own needs, plus it would be more useful if the Arduino was actually monitoring something, rather than logging data from floating pins. It would also be useful if the Arduino could be controlled to blink an LED, or to turn a motor... but I will leave that project for another day. I hope you enjoyed this tutorial - if it helped you in any way, please consider donating a small "tip" into my money jar. Thank you.


If you like this page, please do me a favour and show your appreciation :

 
Visit my ArduinoBasics Google + page.
Follow me on Twitter by looking for ScottC @ArduinoBasics.
I can also be found on Pinterest and Instagram.
Have a look at my videos on my YouTube channel.



However, if you do not have a google profile...
Feel free to share this page with your friends in any way you see fit.

22 comments:

  1. I just checked out the prices from your links and that is almost $40, while I got a generic Net Shield for $8. I will use that to try this out.

    ReplyDelete
    Replies
    1. Excellent. Let me know how you go. And please tell me whether anything within the tutorial needs further explanation.

      Regards
      Scott

      Delete
    2. Hi Kurt,
      The ioShield-A actually comes with the WIZ550io board. I don't think I made that clear within my post. So I will update that section. You can buy the WIZ550io board on it's own - which is why I provided that link....but if you buy the ioShield-A, you get both boards - and that is only $20. Still not $8, though :)

      Delete
  2. Thank you for the clarification, that makes the boards more affordable.
    I will test this out this weekend.

    ReplyDelete
  3. While not quite Voodoo or Magic, this does work like a charm!
    Thank you!

    I will hook up some variable components and try that out.

    ReplyDelete
    Replies
    1. Excellent. Glad it worked for you.
      Thank you for the feedback.

      Delete
  4. I followed everything in this tutorial, but it kept saying "Failed to get DATA". I also did the port forwarding. In the drop down boxes where you input the IP address and port, did you input the external ip address and port, or the local ip address and port?

    ReplyDelete
    Replies
    1. When you are port forwarding, you use the external IP and port. When you are not port forwarding, you use the internal IP and Port, but you must be connected to the local network router at the time.

      Delete
  5. Does it matter if I used the Arduino Ethernet shield instead of the WizNet for this tutorial?

    ReplyDelete
    Replies
    1. You should be able to follow this tutorial using the Arduino Ethernet Shield. It should work for either. You just have to make sure that you have the correct library for your Ethernet shield.

      Delete
  6. I got it to work now! I made a typo error. Thank you for the tutorial!

    ReplyDelete
    Replies
    1. aznprodude12 : I am glad it worked for you.

      Delete
  7. Hello. I was browsing through your tutorial and it looks interesting. Right now, my arduino has LED pins in 3, 5, 6, 9 ,10, 11. These LED lights will blink with the music. I want to get the music frequency data from these pins and graph them in the website. Will this tutorial help me achieve this? Thanks!

    ReplyDelete
    Replies
    1. Hi Paul,

      You will need to modify the output pins to the LEDS, because the ioShield-A uses digital pin 10. The tutorial also states all of the other pins used by the Ethernet shield. You may need to check the data sheet of your ethernet shield to see what pins you can use for the LEDs.

      This tutorial will show you how to transmit data to the web page, but it transmits 5 analog readings per second. I managed to get this down to a transmission per half second, but the speed of transmission will depend on your router and network connection.

      Not sure how you plan to get the music frequency data from those pins, however, once you have the data, this tutorial will show you how package it into JSON format.

      My advice is - try the tutorial as it stands, see how it works, and then adapt to your needs. I have no idea whether this tutorial will be suitable for your project... but once you understand how this project works - you may be able to work out whether it is suitable for your needs or not. The video tutorial - shows the project in action and gives further instructions and detail.

      Delete
  8. Scott,
    great project, I tried to implement for by using a single ESP8266 module (no need for a UNO + WIZ550, of course not too many ADC available as well) however failed in adapting - any plans or idea if you can or will try this out?

    ReplyDelete
    Replies
    1. Hi Tobi - will probably give it a go in future - as I do have an ESP8266, and have started to play around with it, but probably will not surface for quite some time - as I have a number of projects keeping me busy at the moment....

      Delete
  9. Thank you for the clarification, that makes the boards more affordable.

    ReplyDelete
  10. Hello
    have any questions
    1 was in the code of the web page sends the request to get the data?
    2 can have buttons on the website that allows arduine do something? like for example. to light a lamp.
    Sincerely
    Martin

    ReplyDelete
    Replies
    1. Hi Martin,
      1. When you click on the "Click here to Start getting data" button, the web page requests data from the Arduino. The code is supplied above. See the video for more explanation of the process.

      2. Yes - you could get buttons on a website to control an Arduino. But that would require a whole new tutorial to explain. But essentially, you can extract variables from the web request. There are many tutorials out there that describe this process. But none on my site.

      Delete
    2. And I should also mention that controlling an Arduino over the internet, or via your smart phone (if you have an web connected Arduino) is really easy with Cayenne. They provide a neat free service. Click here for Cayenne

      Delete
    3. Hello again
      I know how to do when sending data from a Web page. The problem is I do not see where you send the request web. for it should come 1s and not from the start button in the line "timeout = setTimeout ('getMyData ()', 1000)," go to the getMyData not the start button. So what rows sends
      Web request.
      Sincerely
      Martin

      Delete
    4. Once again I would recommend that you watch the video. It does go into some detail how this is all put together.

      In the HTML code:
      The getMyData() function is the function that is responsible for requesting data from the Arduino.
      Pay attention to line 107.
      The ajax request is where all the magic happens.
      It requests information from the Arduino, and expects to receive it in json format.
      A timeout is setup within that function so that new data is requested every second. But when you click on the stop button, the timeout is cleared, so there are no more requests for new data.
      But yes - it all triggered by the start button.
      See the whole video to get a better explanation.

      Delete

Feel free to leave a comment about this tutorial below.
Any questions about your particular project should be asked in the ArduinoBasics forum.

Comments are moderated due to large amount of spam.