Introduction: Internet Controlled Arduino (no Ethernet Shield)
Ethernet Shields are a lot of fun, but they can be expensive. In this project I will show you how to control your Arduino from any computer that is connected to the internet, without using an Ethernet shield. This project is completely free, assuming you already have an Arduino.
Step 1: Ingredients:
- An Arduino with USB cable
- Internet Connected Computer
- Web Hosting with php support (I use biz.nf for free)
- Processing App (Which you can get here https://processing.org/download )
Step 2: Getting Started: Arduino Part
This is where we add the functionality we want to the Arduino. This part is very similar to setting up a simple Arduino project; the difference is the input the arduino receives.
Set up an arduino sketch that does whatever you like upon receiving the input of a certain character from the serial port. This is done by starting the serial conversatin with Serial.begin(9600); in the setup. Then in the loop if (Serial.available() > 0) checks for serial input and Serial.read(); returns a string of input text. The example I will refer to throughout opens and closes my blinds for me. You can see that my Arduino will open my blinds upon receiving the character 2 and will close them upon receiving the character 1.
Step 3: The Internet Side
In this step we will create a webpage that takes user input, and using that input edits a text file on the server.
The website does not not to be complex, it just has to take some input and send it back to the server for processing. The easiest way to do this is with a get request. When making the form to receive the input, define method as "get".My whole HTML consists of just a form.
The form is submitted back to my page itself, and is handled my the PHP. This is where the magic happens. If it receives a get request, it opens my file Blinds.txt on the server. When it opens the file in the "w" mode it deletes everything in the file and then writes whatever you tell it to on the blank file. I have mine write either a one or two, then replace it with a zero after five seconds. The delay comes from sleep(5);. I have the number, either one or two, written the the file from only five seconds because I am sure that my Processing app can detect the change during that interval. I reset it to zero afterwards because otherwise, if I want to open my shades twice in a row, the second time I updated Blinds.txt my processing app wouldn't see any difference and therefore wouldn't do anything.
Step 4: Put It Together With Processing
Processing is a useful little tool that comes in handy when interfacing the Arduino with the computer (among other times).
The point of the processing app is to retrieve the data from the text file on your server and feed it, through the serial port, to the Arduino. To send the data we need the processing serial library. To start we set up the serial port at the same baud rate as we set the Arduino (9600). In the draw loop we begin by retrieving the data from my text file using loadstrings("url"); which returns an array of strings, each element being a line of text from the document. I then parse the array element into an int so it is a primitive instead of a reference variable, and I can compare its value. After waiting a second I retrieve another value and if they are different I know that something has happened and I need to tell my Arduino, so I send the data using ComPort.write(data);. I also test for nullity throughout so I do not accidentally pass on misread data.
Step 5: Test It Out!
- Make sure your Arduino is plugged into your computer
- Upload Arduino Sketch (You cannot do this while your processing app is running).
- Run Processing app
- Go to your website and submit your forms!