Arduino

Pushing Arduino Data to MySQL via PHP

Arduino+EthernetThis is part of my little ongoing project of learning with the Arduino.  I want to give a mention to Tweaking4all’s guide to PHP, SQL and Arduino, because I started out using it as a base for this section of the project, though I altered a few parts to work with my sensors and Ethernet board.  That guide definitely pointed me in the right direction and made me realize just how simple it would be to actually push data to the server.

My initial approach to the problem was that I needed the Arduino to run some SQL queries and interact directly with the database and insert readings and other variables.  In the end, all I needed was a PHP file on the server to interact with the database, and the proper call from the Arduino to the PHP file.

I started out with basic code to dump dummy data (ie not variables) to a database running on my laptop.  I couldn’t get UIPEthernet linked int he Tweaking4all guide to work so I just used the default Arduino Ethernet libraries.  I also simplified the code down to one probe, though adding more temperature probes will be trivial.  After I managed to get the dummy data to post reliably, I moved on to adding variables in place of the dummy values.  Once again, I couldn’t get the OneWire library to load properly and work with my senors, so I went back to the basic DHT-11 library that I knew work.  This actually simplified things considerably, I used the same calls I had done previously in testing and instead of pushing them directly tot he serial port, I dumped them to some variables which are then passed to the SQL statement.

I also added variables and functions to read Humidity, since he original article doesn’t have humidity readings included.

The final hurdle I came across, for some reason, the delay() function wasn’t working properly.  I set it to poll every 15 minutes (in milliseconds) but it never posted a new update beyond the initial one when powered on.  If I tried a shorter interval, such as 5 minutes (in ms), I got new readings every 30 seconds or so.   In the end, I used a better method of handling time with currentmills.  This reads the current number of milliseconds since the last reading.  By reading currentmills and comparing it to the last reading “time” I can verify if it’s been 15 minutes since the last reading.  This method is not super precise and has some play on interval but I’m not doing anything requiring perfect timing with this project.

In the end, I ended up with the following code for the Arduino:

#include <SPI.h>
#include <Ethernet.h> // Used for Ethernet
#include “dht.h”

dht DHT;

#define DHT11_PIN 5

// **** ETHERNET SETTING ****
// Arduino Uno pins: 10 = CS, 11 = MOSI, 12 = MISO, 13 = SCK
// Ethernet MAC address – must be unique on your network – MAC Reads T4A001 in hex (unique in your network)
byte mac[] = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x31 };
// For the rest we use DHCP (IP address and such)

EthernetClient client;
char server[] = “SERVERIP“; // IP Adres (or name) of server to dump data to
int  interval = 360000; // Wait between dumps
unsigned long previousMillis=0;

void setup() {

Serial.begin(9600);
Ethernet.begin(mac);

Serial.println(“RamenJunkie’s Ethernet Temperature Probe based on Tweaking4All Probe”);
Serial.println(“-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n”);
Serial.print(“IP Address        : “);
Serial.println(Ethernet.localIP());
Serial.print(“Subnet Mask       : “);
Serial.println(Ethernet.subnetMask());
Serial.print(“Default Gateway IP: “);
Serial.println(Ethernet.gatewayIP());
Serial.print(“DNS Server IP     : “);
Serial.println(Ethernet.dnsServerIP());
}

void loop() {

unsigned long currentMillis=millis();

if((currentMillis – previousMillis) > 900000)
{
previousMillis=currentMillis;
// READ DATA
Serial.print(“DHT11, \t”);
int chk = DHT.read11(DHT11_PIN);
switch (chk)
{
case DHTLIB_OK:
Serial.print(“OK,\t”);
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print(“Checksum error,\t”);
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print(“Time out error,\t”);
break;
default:
Serial.print(“Unknown error,\t”);
break;
}

// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println(“-> Connected”);
// Make a HTTP request:
client.print( “GET /temps/add_data.php?”);
client.print(“serial=”);
client.print( “Probe1” );
client.print(“&”);
client.print(“temperature=”);
client.print(DHT.temperature,1);
//      client.print(“88”);
client.print(“&”);
client.print(“humid=”);
client.print(DHT.humidity,1);
//      client.print(“88″);
client.println( ” HTTP/1.1″);
client.print( “Host: ” );
client.println(server);
client.println( “Connection: close” );
client.println();
client.println();
client.stop();
}
else {
// you didn’t get a connection to the server:
Serial.println(“–> connection failed/n”);
}

}

Serial.print(currentMillis);
Serial.print(”  “);
Serial.println(previousMillis);

delay(10000);
}

A few notes to anyone wanting to try to use this.

  • You will need to set the server IP to the IP of your database.
  • You can alter the time interval between readings with the interval variable as well.
  • If you plan to use more than one Arduino probe (which you can), you’ll want to change the MAC address settings.  it can be any MAC value really since you’re just assigning it manually but having multiple devices with the same MAC will cause issues.  I’d recommend simply increasing the last value (31) by one.
  • You can change the line “client.print( “Probe1″ );” to name the probe whatever you want.  The original code read serial numbers but I’m not sure if DHT-11 handles serial numbers or not.  I’d also recommend changing it if you run more than one Arduino probe to the same database.

This will create basic output from the Arduino.  The other half of this is to set up a server running SQL to receive the data, but I’ll cover that in the next entry.  I based it off the code linked above in Tweaking4all but altered it to take in the Humidity as well as allow for viewing individual dates.

Arduino: Cheap Sensors Cheap Results…

Temperature and Humidity SenorsI mentioned last post I had ordered some cheap Temperature and Humidity Sensors for my Arduino boards.  Well they came in, and so I got to do some nice experimenting with code to test them out.  I already had a bit of code from the DHT Test Sketch (Arduino programs are called Sketches) that would poll one sensor and display the results over serial.  Sort of the ultimate goal here is potentially running several fo these sensors around the house and polling them to gather temperature data and store it in a data base that can be accessed via the web.

The first step is getting a Sketch that would poll each sensor and output the result over the serial monitor, preferably with labels.  I started out cutting and pasting the original single sensor code repeatedly and altering the pin before realizing a loop would be way more efficient for this task.  After some fiddling I came up with the following Sketch which polls pins 2-7 sequentially in 2 second intervals and outputs the data to the serial monitor.  This code requires the basic DHT includes from the DHT library.

//
//    FILE: Six Sensor Temp/Humidity Probe.ino
//  AUTHOR: Josh Miller
// VERSION: 0.1.00
// PURPOSE: DHT library based sketch for multiple Temp/Hum probes on Arduino
//     URL: http://www.joshmiller.net
//
// Released to the public domain
//

#include “dht.h”

dht DHT;

void setup()
{
Serial.begin(115200);
Serial.println(“Multi Sensor Temp/Humidity Readings”);
Serial.println(“Version 1.0”);
Serial.println(“Modified DHT Library Version”);
Serial.println(“By Josh Miller, josh@lameazoid.com”);
Serial.println();
}

void loop()
{
int i=1;

while (i<7)
{
// READ DATA

int chk = DHT.read11(i+1);
switch (chk)
{
case DHTLIB_OK:
Serial.print(“\t”);
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.print(“Checksum error,\t”);
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.print(“Time out error,\t”);
break;
default:
Serial.print(“Unknown error,\t”);
break;
}
// DISPLAY DATA
Serial.print(“Probe “);
Serial.print(i);
Serial.print(” : Humidity is: “);
Serial.print(DHT.humidity, 1);
Serial.print(“, Temperature is: “);
Serial.print(DHT.temperature, 1);
Serial.println(” C “);

i++;
delay(2000);
}

}
//
// END OF FILE
//

Arduino with 6 Senors...I don’t have my Network Shields yet so I can’t play around with getting these numbers to dump into an SQL database (probably one running on my NAS) just yet.  This basic proof of concept though is great since it allowed me to learn some code and test the basic functionality of the sensors.  This code can be modified for any number of sensors by changing the “while (i<7)” line to “while (i<# of Pins/sensors Used)”.

Wiring up the bread board was also a bit of a tricky task, mostly because of the sheer number of connections being made.  Each sensor gets a +Voltage and Ground connection off the bus bar and a wire from the senor.  The new cheap sensors use a different pin out from my “official” expensive senor, not really a problem, but something to look out for.

After wiring everything up, I could plug the Arduino into my laptop and open the serial monitor on the appropriate baud rate and record the results.  The results are actually a bit worrying but further testing will be needed to verify the integrity of the results…

Multi Sensor Temp/Humidity Readings
Version 1.0
Modified DHT Library Version
By Josh Miller, josh@lameazoid.com

Probe 1 : Humidity is: 58.0, Temperature is: 25.0 C
Probe 2 : Humidity is: 46.0, Temperature is: 27.0 C
Probe 3 : Humidity is: 41.0, Temperature is: 29.0 C
Probe 4 : Humidity is: 40.0, Temperature is: 26.0 C
Probe 5 : Humidity is: 35.0, Temperature is: 30.0 C
Probe 6 : Humidity is: 36.0, Temperature is: 32.0 C
Probe 1 : Humidity is: 58.0, Temperature is: 25.0 C
Probe 2 : Humidity is: 46.0, Temperature is: 27.0 C
Probe 3 : Humidity is: 41.0, Temperature is: 29.0 C
Probe 4 : Humidity is: 40.0, Temperature is: 26.0 C
Probe 5 : Humidity is: 35.0, Temperature is: 30.0 C
Probe 6 : Humidity is: 36.0, Temperature is: 31.0 C
Probe 1 : Humidity is: 58.0, Temperature is: 25.0 C
Probe 2 : Humidity is: 46.0, Temperature is: 27.0 C
Probe 3 : Humidity is: 42.0, Temperature is: 29.0 C
Probe 4 : Humidity is: 40.0, Temperature is: 26.0 C
Probe 5 : Humidity is: 36.0, Temperature is: 30.0 C
Probe 6 : Humidity is: 37.0, Temperature is: 30.0 C
Probe 1 : Humidity is: 58.0, Temperature is: 25.0 C
Probe 2 : Humidity is: 46.0, Temperature is: 27.0 C
Probe 3 : Humidity is: 42.0, Temperature is: 29.0 C
Probe 4 : Humidity is: 40.0, Temperature is: 26.0 C
Probe 5 : Humidity is: 36.0, Temperature is: 29.0 C
Probe 6 : Humidity is: 37.0, Temperature is: 30.0 C
Probe 1 : Humidity is: 58.0, Temperature is: 25.0 C
Probe 2 : Humidity is: 46.0, Temperature is: 27.0 C
Probe 3 : Humidity is: 43.0, Temperature is: 28.0 C
Probe 4 : Humidity is: 40.0, Temperature is: 26.0 C
Probe 5 : Humidity is: 36.0, Temperature is: 29.0 C
Probe 6 : Humidity is: 38.0, Temperature is: 30.0 C

Probe 1 is the official sensor, the one that cost me $10, the other 5 are cheap probes, they cost me like $9 total.  I have to assume that Probe 1 is pretty accurate and probably has gone through some quality testing before being sold.  The other 5, for all I know, are rejects that were swept off of some floor in China by the janitor.  The point is, all of the readings of the cheap sensors are pretty off from each other, especially the humidity.  This may actually be the case if these sensors were in separate rooms or even any distance from each other, however, these senzors are all within a few centimeters of each other on the same bread board.

There may be a few things happening here causing this discrepancy, and I only say this because they are at the very least consistent with themselves.

One, it’s possible, that there isn’t enough juice to run all of the sensors at once.  If they are being under powered, I would likely get bad readings.  I can easily test this by running each sensor independently with the old single probe Sketch.

Two, it’s possible that while no accurate to each other, they are accurate to themselves.  I don’t really know what these numbers actually mean, I am assuming % Humidity and Degrees C.  It may be some sort of internal scale factor and I simple need to adapt the program to return scaled results.  The issue with this will be that each probe is polled in a loop, so there isn’t really an easy way to apply a different scale factor to each sensor individually.

Whatever the case, the Temperature is at least semi precise, which is what I care more about anyway.  I’ll probably continue with the project with the idea that if I really want accurate results I could eventually invest in better sensors.  The next step on the software end will be to add network functionality to record the data.  On the hardware end I plan to use some CAT5 wire to allow the probes to be positioned in various places beyond just the Bread Board.

OSEPP Arduino Starter Kit

Arduino and a Raspberry Pi 2BSo, a month or so ago, while traveling for work, I was bored in the hotel room and found out there was a Fry’s Electronics around the corner from the hotel. I decided to pop in and look around and see if I could find anything interesting. I also wanted to see how much Raspberry Pi 2B boards were there (and if they were comparable to Amazon.) They had a couple of Pis, a large selection of basic electronic components and a pretty decent selection of Arduino shields and parts.

I’d been looking into getting an Arduino for a while and decided what the hell. There may have been single packed Arduino boards but I really couldn’t tell from the cards which was the base board an which was just an add on shield. I ended up with the OSEPP Arduino Basic Starter Kit.  Honestly, since I hadn’t ever used an Arduino, I didn’t own an Arduino, and I had some basic but not super amazing electronics skills, this really was the best choice.

The kid includes a lot of nice basic parts as well as a guide book to get you started using these basic parts.  The box itself promotes the major projects of “Volt Meter”, “LED Game” and “Electronic Buzzer”.  It also runs through the basics of lighting up some LEDs.  The tutorials are decent though I wish there was a little more on the basics of how to work the bread board and the little wires.  It’s not hard to figure out, but it also isn’t super self explanatory if you didn’t have any experience with electronics at all.  the Volt meter tutorial is less exciting than hoped and the Buzzer just runs up the music scales but it could be modified to play a tune using the little piezo buzzer.

WP_20150630_09_21_48_ProThe LED game is neat, but simple.  You make the lights flash in sequence and you must press the button when the appropriate light is flashing, which causes the lights to flash faster.  There is also a 7 segment LED in the kid, which you make count down from 9 to 0.  This actually has a lot of fun potential and I have some ideas for it but I need to figure out how to light up both numbers to display a number (such as say, a temperature).

After exhausting the fun of the starter manual, on my next trip to the area, I bought a temperature and humidity sensor and wired it up.  This was a bit trickier since it required figuring out how to find and download the necessary libraries and modifying the code a bit to get it to read properly.  In the end, I managed to get things working though, which was kind of great since the AC in my hotel room didn’t have a digital read out, so I could know exactly how cold it was and why I was either freezing or sweating.

So, fast forward a bit more again, I’ve got some fun ambitions going and the idea of playing with several Arduino projects.  Unfortunately, while cheap, Arduino boards can add up to expensive.  Unless you buy cheap Chinese knockoffs for $3 apiece off of AliExpress.  I also have some cheap Network boards and temperature sensors coming.  Five boards total, five sensors and two network shields, for around $35, really isn’t too bad of a deal if it all works.

And thus, I have a ton of Arduino boards to play with now.

IMGP5277

 

 

Security Phase 2 – Doors and Windows (Planning)

I just wanted to start off by saying, this isn’t a how to at all, it’s more some general ideas I’ve been looking over for how to add door and window security to my recently set up video monitoring system.  Basically, I want a log of when and if the doors and windows are opened and closed.  I’d prefer not to run a bunch of wires, so wireless sensors are in order.

What I ultimately want is for an indicator light on a webpage to change based on the status of the door or window, a timestamp log to be created and possibly for some sort of email or text alert to occur.  ULTIMATELY I may even use this whole project as an excuse to finally develop my Phone app skills and build an app that I can view it all on, but that’s farther down the road.  A basic web page is fine for now, and it’s something I can manage.

My worry with wireless sensors was that I’d have to constantly change the batteries.  Until I realized that it would be trivial to design the sensor so that it only transmits when the window is open, and while I am not building my own sensors (I probably could) I imagine the makers of such sensors realize that you could save a ton of battery life by designing the sensors to only work when open.  I did a bit of searching on Amazon and found a6 pack of sensors for less than ten dollars.  It appears that each sensor has it’s own receiver/indicator which emits an audible tone.

This is actually probably alright for my needs.

While I am not an Electrical Engineer, I am familiar with basic circuit flow and basic electronics.  I don’t have any of these sensors yet but I can make some basic assumptions based on what I know.   When the window/door is opened, some trigger is closed and the sensor starts transmitting some sort of radio signal.  The fob thing which is always listening will receive the signal and activate some sort of internal speaker. 

There are a couple of issues that need to be solved here.  One, while the sensors hopefully are designed to conserve battery power, the FOBs are probably not.  I also want the signal to go to a computer and not sound a mostly useless audible alarm.  The power issue isn’t a huge issue.  With the right transformer brick providing the proper voltage, it wouldn’t be hard to rewire the contacts to allow for the units to be plugged into the wall, all at once, off of one power supply.  They look to be small enough that they could be mounted inside an electrical box in a small array and wired for power.

A similar method can be used to transform the speaker signal into a trigger for some sort of simple IO board.  To activate the speaker, some sort of voltage is applied to the speaker, the speaker can easily be removed and the contact points that would previously have fed the speaker could be wired to am IO board.  When the voltage is supplied to the "speaker" it will instead be read by the IO board, triggering a signal that "the Window/Door is open."

The main puzzle I have right now is, what would work best for the IO board.  I see two options here, and both would function differently, and I may even require both.  I could go with an Arduino board or a Raspberry Pi.  Both of these options seem to have advantages and disadvantages, and there may even be a third option which corrects the disadvantages.

arduino

Let’s look at the Arduino.  The Arduino is ideal for dealing with the IO board.  It is, by design, meant to work with this sort of "hands on" electronics systems.  It even has build in contacts for working with two wire IO interfaces.  I don’t know anything about Process, the programming language of the Arduino, but I’ve been meaning to learn and I am capable of learning it.  The problem is on the output.  Arduino is not designed to work with PCs in any simple way.  From some quick research online, the Arduino would require a serial interface and likely special software running on the server to poll the Arduino at set intervals.  Which brings up another issue.  As near as I can tell, the Arduino is a "pull" interface, meaning the server would have to pull the status from the Arduino, as opposed to "push" where the Arduino would push the data to the server as it changes.

Raspberry_Pi_Photo

So there is also the Raspberry Pi.  The Raspberry Pi is a computer itself, which makes interfacing with the server simple and easy.  It runs a simplified OS but connecting the Pi to the network is simple and telling it to push even a text file status update to the server via FTP or some similar network protocol is trivial.  the issue comes in the need to read the sensors.  I’m not entirely sure the Raspberry Pi is even capable of reading an analogue IO interface right out of the box.  Some searching suggests it is possible through an additional interface board of some sort but not right out of the box.  Looks like there are a handful of GPIO points build into the PI.  The PI also feels like much more than is really needed for this simple application.

Its kind of a tricky decision, I’ve been wanting to play around with both the Pi and the Arduino so I may just pick up one of each and see what works.  Both are around the same price and the whole system in the end will end up costing around $50-$75, which is almost nothing.  The final step once the sensor data lands on the server is updating the web page but I already am familiar enough with building webpages and simple polling scripts that this step is the easy part.  Whatever I end up going with I’ll certainly document the results in the future.