Category Archives: Arduino

HTTP GET Parameters with Arduino

For a while now Kevin B has been working on writing a library for communication using the NRF24l01 radio with the Arduino’s. The idea is to use them for home automation purposes. But of course that’s only one piece of the puzzle… what good is home automation unless it’s also internet ready?

I took it upon myself to start looking into that portion, since it’s right up my alley (between server-side scripting such as PHP along with a database such as MySQL, and the API needing to working with Android of course).

Kevin’s thought was to use RS-232 between the Arduino and a Raspberry Pi or Seagate Dockstar. At first it seemed like a great idea until I started reading up on different methods to make this work.

Using Python + PHP

This seems to be the most popular recommendation, but I’ve never touched Python before. The idea was to have Python and LAMP running on a Raspberry Pi while connected to RS-232 and send/receive commands that way. Seemed simple enough, but boy was I wrong. Python boasts being extremely easy to learn for people new to programming, but after seeing a handful of examples, I didn’t even want to touch it. Maybe it’s from years of Java and over a decade of PHP, but my brain just didn’t want to even look at Python never mind understand it. NEXT!

PHP with Serial Library

My next thought was that there must be a library to do RS-232 communication through PHP alone. There were a few limitations that bothered me though. First, PHP is a server-side scripting language. It’s not really meant to run in loops forever waiting for serial data to be received in the same way an Arduino runs in a loop forever. Most web servers (such as Apache) come default with a 30 second max execution time, so if your script runs for more than 30 seconds the server will just terminate it anyway. There is also the issue that the Arduino resets every time you open a serial connection with it, which means even if you don’t loop the PHP script and just call it when needed, the serial data will be reset as soon as the script runs… not good. Kevin pointed out that this is due to DTR signal on RS-232 pulling the reset pin to ground, which isn’t an issue if using a Pro Mini without DTR connected, but in my case I had planned to use one of my Arduino Duemilanove boards as the base station (USB is built on board, so “it is what it is”).

Too Many Layers

Lastly since RS-232 doesn’t do any form of acknowledgements it means there would have to be another software layer in between to control and confirm data. At this point just to get the Arduino to talk to a web server, there was waaay too many points of failure for me:

The Simple Solution

Then it hit me… the route I should have been going all along. An ethernet shield. This will take care of nearly all the problems. The Arduino can act as a web server, and with a bit of programming we can have it serving data or receiving and processing data. Using HTTP response headers, we already have a form of acknowledgements built-in. And lastly, later down the road I could actually remove the Pi from the picture completely if I wanted and instead use my VPS located off site, or even control multiple houses from a single server without the need of separate Pis everywhere or being limited by the processing power of the Pi.

So I plugged my Ethernet shield into my Arduino, along with a battery source for power (my USB ports weren’t giving enough current to keep it powered on by itself), and off to the races I went.

HTTP GET Parameters with Arduino

While not secure by any means, HTTP GET provides an easy solution. The idea is simple. Commands can be sent from the Pi by a GET parameter in the URL to the Arduino, the Arduino can then parse this data and do whatever needs to be done and then output the result (eg. Success or Fail). The Pi can wait for the output/result, and then process whatever it needs to do (eg. informing the user of the result).

First we need to set up the network information for the shield. The Ethernet library does support DHCP, but its easier to use a static IP for testing.

// Setup the static network info for you arduino
byte ip[] = { 192, 168, 1, 177 };   // IP Address
byte subnet[] = { 255, 255, 255, 0 }; // Subnet Mask
byte gateway[] = { 192, 168, 1, 1 }; // Gateway
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address

Next we setup the ethernet server on port 80 (default HTTP port) as well as a string to store the GET parameter in.

EthernetServer server = EthernetServer(80); // Port 80
String HTTPget = "";

The setup() function is pretty straight forward. Remember we can’t use pins 10 through 13 for any thing else as they are used for Ethernet. Unfortunately the NRF also needs some of those pins though, so I will have to look into using a SoftSPI library or figuring out how to use the SS pin (slave select) down the road.

void setup() { // pin 10, 11, 12 and 13 are used by the ethernet shield Ethernet.begin(mac, ip, gateway, subnet); // setup ethernet with params from above server.begin(); } continue reading

Temperature Sensing with DHT11 on Arduino

Ahhh temperature sensing, something so simple yet can be integrated into so many different projects. How about a temperature controlled RGB LED? Or perhaps an Arduino controlled thermostat? Heck, even a full blown weather station! Your imagination is the limit.

There are a few different choices when it comes to temperature sensing such as a completely water proof analog temperature probe to digital sensors. I will be focusing on the DHT11 which is a very affordable (~$1) digital temperature and humidity sensor. Its good for sensing 20% to 80% humidity within 5% accuracy, and 0c to 50c within 2% accuracy, which is more than enough for any indoor projects. If you want something a little more capable and accurate, the DHT22 is another option for digital sensors.

The DHT11 has 4 pins in total as seen above (with 0.1″ spacing – perfect for breadboards). VCC, a Data pin, and Ground. Pin 3 is not used.

Wiring it up is fairly straight forward. Start by connecting VCC on the DHT11 to VCC on your Arduino, and Ground to GND. Next connect the DHT11 data pin to pin 10 on the Arduino. Lastly, we need a 10k (Brown Black Orange) resistor in parallel to the DHT11 connected between the VCC and Data pins. This is our pull up resistor.

Now its just a matter of uploading our sketch and checking the output in the serial monitor

//First we need to include the DHT Library #include "DHT.h" //Next we define which pin the DHT11 is connected to, in this case its 10 #define sensor 10 //We also need to define the type of sensor were using, which is the DHT11. //Other options are DHT21, and DHT22 if you are using those sensors #define sensorType DHT11 //Initialize the DHT sensor using the above variables DHT dht(sensor, sensorType); void setup() { //Start our serial port and dht sensor Serial.begin(9600); dht.begin(); } void loop() { //Read the temperature sensor, in celcius int curTemp = dht.readTemperature(); //Read the humidity, in percentage int curHumidity = dht.readHumidity(); //Output the information to our serial monitor Serial.print("Temp: "); Serial.print(curTemp); Serial.print("°C / Humidity: "); Serial.print(curHumidity); Serial.print("%"); Serial.println(); //Wait for a few moments as the sensor can only update so quickly delay(3000); } continue reading

Build your own Arduino starter kit

I have a friend that keeps asking me about micro controllers and costs associated with getting started. He has no prior programming experience, and doesn’t own a soldering iron. By the sounds of it, he’s a little on the fence (rightfully so) and isn’t sure whether he should try it or not. Totally understandable when starting with something new. So I figured hey, lets break it all down and see just what it would take to build your own Arduino starter kit!

Before I get into the actual costs I just want to point out that while previous programming experience is handy when it comes to Arduino, its not at all necessary! The Arduino community is literally huge (250k+ members worldwide, 2 million posts) which means finding a sketch and wiring examples that do exactly what you want it to do (at least when you are first getting started) is going to be very easy. My recommendation when starting out is to concentrate on small projects with the thousands of ready made examples (blinking, fading, controlling, etc.). This will allow you to familiarize yourself with the platform (uploading, debugging, etc.). Once you have a solid base is when the real fun begins and you can let your imagination run wild! continue reading

PWM control with Arduino

Just a real quick snippet today. If you followed along my first Arduino post (Getting started with Arduino) then you should already have a simple blinking LED on Pin 10 (or pin of your choice). I wanted to take that just a step further and use PWM (Pulse Width Modulation) for a fade in / fade out effect.

PWM uses a pulsing digital signal (high/low) to simulate the effect of an analog signal. If you really want to learn the technical details of PWM, head on over to the Arduino page – http://arduino.cc/en/Tutorial/PWM

The best part, no hardware changes! Simply upload this new sketch with the breadboard layout in the original post (below, in case you missed it)

// Our LED is connected to Pin 10 // Set a variable called LED for Pin #10 int led = 10; void setup() { // No Setup } void loop() { // Fade in from min (0) to max (255) for(int fade = 0 ; fade <= 255; fade++) { // Set the pin to output the value of fade analogWrite(led, fade); // Delay for 5 milliseconds delay(5); } // Fade out from max (255) to min (0) for(int fade = 255 ; fade >= 0; fade--) { // Set the pin to output the value of fade analogWrite(led, fade); // Delay for 5 milliseconds delay(5); } } continue reading

Getting started with Arduino

Well its been so beautiful outside here lately *sarcasm alert* (averaging -40 degrees Celsius…) I decided now might be a good time to dust off my Arduino boards.

First off, if you have any experience with Arduino micro controllers, this post probably wont be for you. Its been many… many years since I have done any work with them (ahem… high school). I need to build my own skill set back up, so this is a recap for myself, and for those who might be just starting out (or looking to start out) with Arduino boards.

Second off, if you’ve ever used or are still using the Arduino IDE… scrap that. I came across a web app: Codebender which is basically a much better version of the IDE entirely through your browser. It features a fancy editor with syntax highlighting, much better error reporting (in my opinion), a cloud based compiler, and a built in serial monitor. It has all the features of the Arduino IDE, but adds loads more.

I have several different types of Arduino boards (Pro Mini, Uno R3, Duemilanove, and some mini knock-offs that don’t work worth crap – dswy_robot), but I will probably spend most of my time with the Pro Minis. They are amazingly cheap so if you blow up a board, you’re only out a couple dollars instead of $20-$30…sweet!

What You’ll Need

  • Arduino Pro Mini (or Arduino of your choice)
  • USBasp programmer (assuming you’re using the Pro Mini)
  • An LED
  • A resistor, 470 ohm is fairly safe when working with LEDs
  • Male header, or bread board wires

Getting Your Programmer Ready

Before we jump into it, we need to make a cable to connect the Arduino up to the USBasp programmer. Your programmer should have come with a ribbon cable, so its just a matter of connecting those pins up to the proper pins on the Arduino with some breadboard wires. You can use bread board wires to go from the 10 pin connector to your breadboard, or you can cut one end off completely and solder it to a header. It takes a few minutes longer to make the cable, but trust me, you’ll save time in the long run when you need to switch breadboards

Make sure you are oriented correctly, Pin 1 on your ribbon cable is the blue wire in the image above. When you’re all done, ideally you want to end up with a cable like this:

Make It Blinky Blink!

Alright, so lets get our LED wired up

Very simple. the anode (longer wire) of the LED connects to pin 10 on the Arduino, and the cathode (shorter wire, typically with flat side on LED) connects up to our resistor. Depending on your LED you can use a smaller resistor, I was using a very bright blue LED so I decided to go with a 470 ohm to dim a down just a touch.

Next we need to upload the sketch to the Arduino using the USBasp, so plug everything in, connect the programmer up to your computer, head over to Codebender, and upload this sketch.

// Our LED is connected to Pin 10 // Set a variable called LED for Pin #10 int led = 10; // The setup() function is called once when the Arduino is first powered up void setup() { // Set our LED pin as an output pinMode(led, OUTPUT); } // The loop() function runs over and over again forever void loop() { digitalWrite(led, HIGH); // Turn on the LED (pin 10) delay(500); // stop for 500ms digitalWrite(led, LOW); // Turn off the LED (pin 10) delay(500); // stop for 500ms } continue reading