Sunday, September 27, 2020

Weather Station - Finale

For several months I've been exploring various ways of gathering and displaying all of my environmental sensor data.  Well I'm now ready to wrap this project up.  The final two pieces are a wind speed monitor and a rain gauge.  I chose an ESP32 module for processing the incoming data and hooked it up to a .96" OLED display.  The image below shows the completed unit w/o the clear top installed.  The pushbutton on the left allows me to switch between metric and English units of measure.  An external, four channel analog to digital converter sits to the right of the ESP32.

I purchased the anemometer from Adafruit (anemometer).  It outputs a voltage that's proportional to the wind speed and connects to one of the  analog inputs of the ESP32.  However, after several attempts I finally gave up on using the internal ADC.  I didn't know at the time but it turns out that problems with this feature were well documented on the internet so I decided to go with an external ADC (ADS1115) instead.  This seemed to solve all of my problems and I'm happy with the results.

The rain gauge came from a source off of eBay and is mechanical in operation.  As water enters the unit it causes a collector to fill with water.  When the collector is full it tips over (tilts) and empties its contents.  Each tilt forces a reed switch to momentarily close via a magnet attached to the collector arm.  This switch closure is easily monitored by a GPIO pin on my Raspberry Pi.  The number of tilts are counted and converted to an equivalent depth.  The ESP32 then polls the RPi every 15 minutes to get an updated rain fall amount via an MQTT connection.

Since OLEDs are known to have a limited lifespan, I decided to shut off the entire display at midnight and then re-enable everything at 8:00am.  The ESP32 "knows" the time because it periodically retrieves the actual time via an NTP (network time protocol) server.  I also chose to leave the sensor display readings blank if there was no wind or rainfall to report, just to keep the display uncluttered.

All in all, a pretty fun little project.

Monday, December 30, 2019

Making a Magnetic Loop Antenna

I've been using my recently acquired SDR for a while now hooked to a simple wire antenna, but as you might expect the results were far from ideal.  So, I made the decision to build (not buy) my own magnetic loop antenna.  The big advantages of this type of antenna over others are their small size, portability and decent bandwidth.

The antenna consists of a large, outer resonant loop for picking up the RF signals and a smaller, magnetically coupled, inner loop that connects to the radio.  See diagram below left:

(Source: http://www.aa5tb.com/loop.html)
For the large loop I used 10' of 1/4" copper tubing inserted inside an identical length of 1/2" PEX tubing to aid in stiffness and durability.  Pigtails were soldered to either end of the copper and then connected to an air variable capacitor I had in my junk box (thanks Bob, WA1EDJ), creating a parallel resonant circuit. The measured inductance of the copper loop was ~3.25uH and the variable cap I had selected had a range of ~10-200pF.  This gave me a calculated tuneable bandwidth of ~6.2MHz to 27.9MHz.

A 2' length of copper tubing was then formed into a smaller loop with one end connected to the center conductor of a length of RG-58 coax while the other end was connected to the outer braid.  The other end of the coax was mated with a BNC connector to hook up with the receiver. This inner loop was then positioned in close proximity to the outer loop and secured in place with zip ties.

Some close ups of the various construction details:

Copper tubing inside PEX (large loop)
with a soldered pigtail on one end

Coax connected to ends of small
copper (inner) loop

Inner loop positioned closely to outer tube

Vertical support strut added
Tee connector at bottom of large loop




Completed antenna minus tuning capacitor

After assembly, I hooked it up to the radio to see if reception had improved.  Definitely!!  I also took measurements to determine the actual tuneable bandwidth.  It seemed to show signal peaks at ~6MHz on the low end and ~18.5MHz on the high end.  This corresponds roughly to the 40-17m amateur bands.  I can only attribute the difference in high end response (27.9MHz calculated vs 18.5MHz actual) to additional parasitic capacitance of ~13pF.

I'm not worried about weather proofing the variable cap since the entire antenna unit can easily be moved indoors at a moments notice in case of rain or snow.  The next task at hand will be to make additional antennas to cover different bands.

Thursday, December 19, 2019

Software Defined Radio

This Christmas I thought I'd splurge on a Software Defined Radio (SDR) from SDRPlay (SDRplay.com).  I chose the RSP1A model for ~$100.00 that includes the software to setup and run the user interface.  It covers the radio spectrum from 1kHz to 2GHz with a displayable bandwidth of  up to 10MHz.  All that's needed is a PC to run the software and an external antenna and its ready to go. I plan on making my own antenna soon (probably a magnetic loop), but for now I'll just string a wire from the unit and hope for the best.

Here is the unit showing the USB interface on the right along with a length of wire attached to the antenna port on the left.  That's it!...The PC does all the data and display processing.

RSP1A

This image is a screenshot of the SDRuno software user interface.  This can be customized by each user to meet their particular needs (filter selection, spectrum bandwidth, band/mode of interest, etc).

Here I'm listening in on a CW signal on the 40m band (7.044.5MHz).

Looking forward to hours of experimenting (aka playing) and exploring the rich feature set available.

Friday, September 6, 2019

Data Logging with a Raspberry Pi

A major limitation to the PC board I created for my ESP-WROOM-02 module described in the previous post was that it could only support one digital input (even though the ESP module itself supports many).  This was done intentionally to keep things simple.  In a real-world application, however, this would not be very useful.  Therefore, I decided to go with a different approach for round two.

I chose a Raspberry Pi 3B+ with battery backup to monitor, control and display everything.  This small but powerful platform would allow me to gather data from multiple sensors with plenty of capacity for future expansion.  The actual task of gathering sensor information and then processing it involved installing four pieces of software onto the RPi:

1) Collecting the data from each wireless sensor - I chose Aedes to serve as my MQTT broker/server.  MQTT is a messaging protocol ideally suited for the Internet of Things.  Devices connected to the service (clients) can transmit data (publish) and/or receive data (subscribe).  The broker essentially acts like a post office, handling all messages between devices while providing a level of security.  Note - at the moment only one of my sensors is wireless (a motion detector), while the rest (temperature and humidity) are hard wired directly to the Raspberry Pi via an I2C interface so no MQTT messaging is required.

2) Data manipulation - Node Red was my choice for handling all data once it was received.  It is essentially a visual programming tool consisting of nodes (units of pre-developed code) that perform a specific task such as writing to a database, converting raw data into a visual gauge or bar chart, filtering data, and so on.  Nodes are connected via virtual wires to create a flow.  The following image is part of a flow I created specifically for this project:

Node Red flow

In this example, the reddish-brown node in the center retrieves the hard wired sensor data (temperature and humidity) for the outside and basement sensors, the light orange nodes perform some post retrieval processing and the light brown nodes on the far right forward all processed data to the database software for storage.

3) Database storage and 4) Visualization - I decided upon InfluxDB for storing all my sensor data and Grafana for the user display.  InfluxDB is a time series database that allows for easy data logging and when coupled with additional software like Grafana provides a user friendly way to visualize data in real time.  The screenshot below shows my basement temperature and humidity over a 24hr period along with maximum, minimum and average values:

Grafana dashboard

A final touch was to create a simple web page that could display this information in real time using the browser on my smartphone, home computer, etc.  Node Red made adding this feature a snap.  Below is a screenshot of the display (I chose to show temperature only to keep things simple):

To access the page I just type in the IP Address of the Raspberry Pi (assigned by my WiFi router on initial startup).  Code for the gauge templates came from "canvas-gauges.com" but everything else was written by me.  This setup works fine when I'm around the house in range of our WiFi network but what about when I'm not at home?  The solution was to create a VPN (virtual private network) so that I could access this info anywhere in the world that has internet access.  I simply setup the RPi as a VPN server using a YouTube tutorial I found and it was ready to go.

Below is a picture of the Raspberry Pi (lower left) along with the battery charger (from DFRobot) and a +12V sealed lead acid battery enclosed in a plastic storage container kept in the basement.  The wires connected to the RPi go to my environmental sensors.


Update (Spring 2022)
I decided to completely overhaul my webpage by adding a few more bells and whistles as seen in the screenshot below.  The current temperature and rainfall values still come from my onsite sensors, but the 12-hour forecast readings come from an AccuWeather API.  I also added two temperature charts: one to show the hourly values throughout the day and one to show the daily low and high values for the week.  Finally, I added a background image to provide an interesting backdrop.  This came from a picture taken on one of our hikes near the Asheville area.



Saturday, June 15, 2019

Using a Sensor with an ESP8266

After playing with my newly acquired Sonoff switch I got the bug to explore additional wireless uses.  This time I wanted to hook up an environmental sensor to a WiFi capable device and then send the acquired data to a Blynk app for display.  

For the controller I chose an inexpensive ESP-WROOM-02 module (based upon the ESP8266 WiFi microchip).  When the ESP module first arrived I quickly realized I would need to either purchase or make my own break-out board in order to access the pins since the unit was an SMD device.  I knew it would be quicker if I bought a pre-made adapter but what fun would that be, so I used my PC board layout software to create a custom design, then etched and drilled a copper board.  The pictures below show a finished blank board alongside a fully populated one:

Break-out board
Fully populated board
If you look closely, you can see the embedded WiFi antenna running along the upper blue region of the board.

For testing purposes I decided to use a DHT22, which is one of the many inexpensive, single wire temperature and humidity sensors commonly available on the market.

After writing and compiling my program, the next step was to apply power, then flash the code into the chip and check for functionality.  It worked (no smoke!) but I soon discovered that the WiFi module was resetting itself periodically.  After much agonizing over potential coding issues I found that many other people were having the same problem.  It seems that when the module is in transmit mode the extra current demand causes a sudden drop in voltage from the power supply that randomly forces a hard reset.  The fix was to add two, 220uF capacitors in parallel across the chip's power and ground pins to smooth out the voltage spikes.  For extra insurance I also added a .1uF capacitor to reduce any high frequency noise.  Both additions seemed to do the trick.  Pretty exciting to finally see this working!  

Update (Late Fall 2019)
My module is now officially off the grid!  I decided to add some circuitry to eliminate the need to power my module from line voltage.  Taking a 6V solar panel I had purchased years ago, I added a 3.7V Lipo battery pack and a Lipo battery charger (from DFRobot) to create a stable 3.7-4.2V reference that would work 24/7.  Next, I fed this into a 3.3V LDO voltage regulator to provide the correct voltage for the ESP module and I was all set.

Initial testing was disappointing to say the least.  The 1700mAH battery was only lasting a few hours before it needed recharging.  In idle (receive) mode the circuitry consumes ~75mA while during data transmission it jumps to over 250mA.  The poor battery never had a chance.  After researching ideas on the web I found I could perform a software activated deep sleep operation on the chip, effectively shutting down most of the ESP module circuitry for a fixed period of time.  The only part of the chip still active would be a real time clock.  In this state the WiFi module consumes less than 1mA.  Then, when the time period expires (defined in software), a specific I/O pin on the module sends a signal (pulse) needed by the reset pin to wake up the chip.  All I had to do was add a couple of lines of code and hard wire the two pins together.  I decided that sleeping for 15 minutes, then waking up and processing data for 15-20 seconds before resuming another sleep cycle would be a good compromise for my project.

The nearly completed unit, mounted to the base of a plastic enclosure is shown below.  The Wifi module is actually on the underside of the copper board so it is not visible.

Friday, May 31, 2019

Making PC Boards

I've always enjoyed making my own PC boards for custom projects but have struggled mightily with the process over the years . Either the board design wouldn't transfer or stick properly to the copper or if it did it would end up pitted after etching.  Well, I finally hit upon a formula that appears almost full-proof.  Some of this came about from trial and error on my part and some from other people's ideas.  The image below shows the results of my first board:

Not perfect by any means but pretty darn good for my needs.  I will list the steps that work for me in hopes that others might find them useful.








PC Board Production Steps
• layout board with ExpressPCB or similar software
• print design onto special toner transfer paper (print on shiny side) using laser
       printer (Staples, etc. will do this); note - I use paper from PulsarProFX but
       glossy magazine paper should work as well
prep and clean copper board (scrub with Scotch-Brite pad, wash with soap and
       water then clean with alcohol)
apply a few drops of an acetone:alcohol mixture (3:8) to board, then spread
       evenly with fingers (this mixture will cause the toner to release from the paper
       and stick to the copper); note - this mixture is the critical part of the whole
       process
position paper design onto board and gently press into place
apply a few more drops of mixture onto paper until thoroughly covered then wait
       5-10 seconds
apply even hand pressure with a paper towel for ~90 seconds, then remove towel
       and wait ~30 seconds
place board with paper into a cold-water bath for ~2 minutes then peel off paper
       backing
wait ~15-20 minutes for toner to dry then turn on laminator set on low heat
       (3mm setting, I use a model GBC H220)
once laminator is ready, cover board with green TRF film (front side taped to
       board and shiny side up); note - this material is also from PulsarProFX and
       helps to reduce pitting of the copper
run board thru laminator once then allow to air cool for ~15-20 minutes
gently remove green TRF film
patch voids with fingernail polish if needed (must be done after using green TRF
       film)
now ready for etching with hydrogen peroxide:muriatic acid mixture (2:1)

Thursday, April 25, 2019

Sonoff Smart Switches

The Sonoff series of smart switches (produced by Itead) are based upon the ESP8266 and ESP8285 microcontrollers.  They are Wi-Fi compatible thus making them useful for interfacing remotely with iOS and Android devices via a number of commonly available apps such as Blynk, eWeLink, etc.

Basic R2
For basic switching operations, these units are designed to work right out of the box using their standard, built in firmware.  All you need is to load an app onto your phone for controlling it and you're all set.  The app sends a command to the Sonoff which in turn activates a relay on the board, thus controlling the power delivered to a load.  However, I wanted to go one step further and use custom firmware to make the device self contained (no app required).

I purchased a basic model (Sonoff Basic R2) for ~$7.00 and begin experimenting with different code libraries freely available on the web.  The goal was to turn an external device hooked to the Sonoff on and off at a predetermined time each day.  I found a library that contained code for accessing an NTP (Network Time Protocol) server.  This allowed me to fetch the current time off the web.  Next, I hard coded specific times for turning my device on and off.  The drawback to using fixed times is they can only be changed by altering the program and recompiling it.  For now this will work fine, but down the road I might use an app on my Smartphone to program an on time and off time on the fly.  This, of course would require a one-time modification to the program.

In order to load (flash) the compiled code onto the Sonoff board I needed an interface between the computer and the ESP chip.  This was because the USB end uses 5.0V while the ESP circuitry uses 3.3V.  I purchased an FTDI based USB to TTL interface board from Amazon, transferred my code and was up and running in no time.

Neat little devices.

Update (June 2019)
After using my Sonoff for a while with hardcoded on/off times embedded in the firmware, I finally decided to experiment with the Blynk app on my Smartphone to allow for dynamic changes to the on/off parameters.  Blynk comes with coding examples already written for specific applications so I used one suited for my needs and simply copied and pasted the appropriate sections into my existing program.   I also made sure to store any changes to the on/off parameters into non-volatile memory to ensure the Sonoff would work when power was lost and then restored.  After loading the newly compiled code into the Sonoff chip, it ran w/o any hiccups.  So easy!  Now I can change the on/off times if needed w/o reprogramming the chip and the Sonoff will respond accordingly.