I bought a bunch of sensors recently, which included:
- 1-wire Dallas temperature sensor DS18B20
- Rain sensor module with LM393
- Light dependant resistor LDR5528 (+10k resistor)
- Soil moisture sensor with LM393
- ESP8266 wifi module
- 9v Solar panel + 1N4007 diode
- Water float sensors (x2)
The goal is to setup a stand-alone Arduino that will connect to my wifi (signal may be an interesting problem, we'll come to that) and then feed back a lot of environmental information to my server, that I can then graph/log and alert depending on the inputs.
First job was to get the ESP8266 wifi module working. This was a new item to me, but it's principle was simple, it was a stand-alone wifi module that you could communicate using 2-wires serial (TX and RX), so you could open a serial session, connect to your wifi AP using WPA2, WEP, etc, and then set it to send data in an HTTP GET session. The simple commands made this an excellent add-on for me to include in my project, and the price is excellent (Around £3).
Connecting it up, you need power (3.3v), also you need to pull CH_PD to positive, which I did through a 10k resistor. So my wiring diagram worked out as:
(Credit to enio in arduino forums for image)
I connect VCC, GND, CH_PD for power. then TX and RX to the Arduino. To test the connectivity I used my Arduino UNO, loaded a blank sketch and connect RX to data_rx and TX to data-tx (pins 0 and 1 digital), that let me type into the serial console and it was passed straight to the ESP8266. Various AT command sets are available all over the internet, so a few tests I did:
(Full list on wikipedia http://wiki.iteadstudio.com/ESP8266_Serial_WIFI_Module#AT_Commands)
After receiving valid replies I knew the unit was operating correctly, and making a quick sketch to connect to my WIFI using softserial started working (using softserial allowed me to use alternative digital pins for talking to the ESP8266 whilst still having my serial console to monitor and upload sketches).
After some trial and error, I found this wifi module dropped out or didn't respond to commands. Upon checking various forums and information, I found this was probably due to the current this was pulling. Originally I was powering it via the 3.3v output on the Arduino, however this couldn't provide enough power, especially when I added all my other sensors, etc and I started to get issues with no reply to the AT commands. Therefore I decided I needed to power this separately. The solution (since I would be powering this from a bank of AA batteries) was to use a LM2596 DC-DC step-down power supply (also known as a buck converter).
I set this up on my multimeter and set it to produce 3.3v so this would supply the ESP8266 directly. (I'll have to see over time how inefficient this method would be, as I know it'll be wasting energy from the battery packs)
1-wire temperature sensor DS18B20
I've used these temperature sensors many times before, I have one in each of the Raspberry PI's around the house and have created a basic temperature 'network' within the house to monitor room temperatures in previous blog posts. I'm using the same sensor again, so as before simply connecting the 3 pins up and including the "OneWire.h and DallasTemperature.h" libraries into my sketch and it was up and running. PIN connections for the DS18B20 are as follows:
So in my setup (You can't quite see this in the picture unfortunately as it's wrapped in black tape) I have connected GND to ground, VDD to +5v, DQ to a digital pin, and then via a 4k7 resistor DQ is also connected to 5v.
Soil moisture sensor with LM393
The soil moisture sensor is a regular two pin/probe style that you stick in the ground, and then measure the resistance between pins. The unit I bought came with a basic LM393 control circuit shown below:
The pin connections are simple, on the left you connect the soil measuring probe, the right are 4 connections, top and bottom are +VE (3.3v) and GND, then you have an analogue out or digital out. The digital out (high or low) is controlled using the variable resistor on the board, so you can set the threshold. I opted for analogue so connecting to the analogue inputs and then read the values shown. Read this as a typical integer from the digital input.
Rain sensor with LM393
This is the same as the soil moisture sensor and came with the LM393 sensor as above.
9v solar panel
To power my arduino I decided to go with a battery pack and use a solar panel to top the batteries up. I purchased a battery pack, the model I chose had 6xAA batteries so produced approx 9v. This created several problems. Firstly the cheap solar panel I bought only produced 220ma 2 watts at 9v, so even at full sunlight it was producing the same voltage as the batteries, therefore this would not charge them (generally you need a higher voltage to charge) and at low current.
Therefore I decided to modify my battery pack to exclude 2 AA batteries, taking the voltage down to 6v which would be more suitable for the solar panel to charge them, and also will reduce the amount of power lost through the heat dissipation on the Arduino Uno.
(NOTE, I'm not an expert on batteries and power, so please do correct me if I make some glaring errors here!)
These are the solar panels I'm using (A chinese import) and currently with their protective plastic film over them. I'm planning on fitting these into a plastic container, to keep them protected from the elements. Although this will reduce their output slightly it will protect them over time.
I should also mention that I have connected in a 1N4007 1A 400v rectifier diode to ensure no power from the batteries are lost back into the solar panels during darkness, etc.
Below is the power pack that I've put in my two dummy batteries to reduce the voltage/number of cells:
Testing the Arduino Uno and the LM2596 powering all the devices the pack was providing enough power. I'll monitor this for amount of current consumed, etc, later in the testing/implementation.
Light Dependant Resistor LDR5528
This was a simple off the shelf LDR that I connected up to the Arduino analogue input ports with a 10k resistor in parallel (to the 5v feed). I then read this during the normal sampling periods.
The code I've used is a combination of various of my older projects put together with some libraries to control the various input sensors. The libraries I've used are as follows:
#include <LowPower.h> - This is to be used to help reduce idling power consumption
#include <stdlib.h> - for dtoi and related functions
#include <SoftwareSerial.h> - for communication to the ESP8266
#include <OneWire.h> - for the 1-wire temperature communication protocol
#include <DallasTemperature.h> - 1-wire temperature
I set a few definitions near the top of the code (for SSID, WPA2 password and the url to GET when sending data).
Setup involves setting the various input and output pins to their relevant values and initialise the softwareserial.
The main code loop is fairly straight forward, I read each of the sensors in turn, display the output to the serial monitor and then use the ESP8266 to send the data to my webserver.
The code is available in full on my github page, so feel free to take a look, and if you can spot any improvements please commit them as I'd like to get some feedback and improvements on my code:
(NOTE: There are a few extras I've missed off here, I'll post further blog entries with tweaks and changes as I make them and when I implement the final system)