Monday, August 1, 2016

Rain sensor using ESP8266

Well here is my second project using the ESP-8266.

I already wrote a few stories about this small wonderboard. The previous real project which you can read by clicking here was about controlling a ledstrip over wifi.

Previous stories I wrote about the ESP-8266 were about making a programming board for the ESP-8266-01. The small board with just 2 I/O pins. And you will need this programming board as I am going to use that same ESP-8266 version here again. You can find the instructions for making the programming board here.
The second story was about flashing the right firmware on the ESP-8266. And you really want to be up to date with the latest firmware so you are going to need that storry to. You can find it here.

This story is going to be different. It is going to be my second real complete project using the ESP-8266.

In my last blog entry I showed you that I tested some of my 3D prints to prove wether they were water proof. In this article I mentioned that I was going to do a project that involves bringing the electronics outdoors. Well here it is.


So what is it going to be. It is a rain detection system that sends notice over wifi to your computer/laptop/tablet or phone if it is raining. The project is not just a practice for building something with the ESP but it really is practical too. My girlfriend and I are in some ways old-fashioned. So when she has done the laundry she puts it outside to dry. And sometimes we are not paying attention and it starts to rain. That's why I wanted a sensor that would inform me that it started to rain.

Water level meter / Rain detector

I bought for a few dollarcent a water level meter/rain detector.




It is a small board with long strips of cupper-leads and 3 connections power (+), gnd (-) and Signal (s). As it did not have any documentation accompanying it I had to do some research myself.

Before attaching it to my ESP-board I hooked it up to a power supply (3 AA batteries) and my multimeter. The setup was the following. The power and ground were connected to the battery case. The ground was also connected to my multimeter and the signal line was also connected to my multimeter. Then I started doing some tests.





First I measured resistance.
When the board was dry I measured 105 Ohm. When humidified with a wet finger I measured 1 mega Ohm. This could surely be usable if the ESP8266-01 had an analogue input. But it hasn't. It's larger brother the ESP8266-12 does have an analogue input just like the fantastic NodeMCU board. But using these would be overkill for this project.






So next I measured voltage.
The dry board gave me 0.03 volt. And when I humidified the board again with a wet finger I measured 2.68 volt. Now we are getiing somewhere.

So when the board is dry it gives a LOW or 0 signal. And when it is wet it will give a HIGH or 1 signal. That is usable.

Connecting the ESP to the Rain detector.

Well this is straightforward.

For testing purposes I used my programming board. At the end of my board is a header in which I can put breadboard cables. So I plugged in a cable for the power, ground and on the GPIO2.

These cables were then connected to the water level sensor/rain detector. That's all. At least I thought it was.

When I started testing this I noticed two things.
- The signal the rainmeter was giving was not quite strong enough to trigger the ESP
- When using AAA batteries they were drained too fast.

Lets tackle the first problem.

The signal the rain meter was sending to the ESP was too weak. So the ESP would not be triggered if it started to rain.

First I wondered why this would be. But then it occurred to met that this was not only a rain meter it was also a water level meter. The signal would get stronger as it was submerged more into the water. This would not be a problem if I would attach the rain sensor to an analogue pin. However I did not have an analogue pin on my ESP8266-01. And I was not really interested in the water level. So I had to make the sensor work as a switch. As soon as a drop of water would fall onto the sensor it would have to give a signal to the ESP8266.

Piece of cake. I would feed the sensor output to a transistor who would amplify the signal. Then it would trigger the ESP8266.

Well not exactly a piece of cake. I was at first trying to get this working with 2 AA batteries. However this did not work. The currect was to low and the voltage would drop too much. So I decided to try some really bad design practice. I would feed the project with 5 volt for testing purposes and with 3 AA batteries in the field. And indeed that worked like a charm.

Now this is bad practice as the ESP works on 3.3 volts and not on 5 volts. However it works and that is what really matters to me. Two AA batteries were just to weak. Use this at your own risk. If you want to play safe add a voltage step-down circuit as described in the led controller story (click here).


Last minute update. At the time I am writing this there is a discussion going on on Hackaday wether the ESP is 5 volt tolerant. And there was a comment from Espressif on Twitter that confirmed that the ESP I/O pins are indeed 5 volt tolerant. You can find that communique at 
https://twitter.com/esp8266com and look at the 30 july entry from Teo Swee AnnSo if you want to play on the safe side you could attacht 5 volts to the rain meter and pick up the 3Volt current from two of the batteries for the ESP. 
However I am not going down that road and use the 3 battery setup as it works fine in my case. Again: do not blame me if it blows your ESP.







So look again at the breadboard layout and schematics. I put a 68ohm resistor between the rain-sensor and the base of the BC547 and I limited the current with a 1 kilo-ohm resistor. This has been working for several weeks now without any problems.

STA or AP

On to the software. There were two possibillities. I could make a stand-alone network server for this putting the ESP8266 in AP (Acces Point) mode. This way it would not take an IP number on my router. However it would be impossible then to access the ESP8266 from my desktop system. It would be accessible over wifi from my tablet or phone. Then again the tablet or phone should be dedicated-connected to the ESP8266 and could not access the internet at the same time.

I decided to put the ESP in STA (station mode). That way it would be connected to my router. It could be accessed (if I wanted) from anywhere in the world so not only when I was home. And I can access it from any device in my home network being my desktop computer, any of my raspberries, my tablet(s) or phone(s). For now that seamed the best option. In the long term I want to make the rain-maker part of my home-automation system. I need to reprogram the ESP for that and that is why I did not solder it to my strip-board but put it on headers.

Programming the ESP8266

First start with flashing the ESP8266 with the latest firmware. You can find my stories about the programming board and flashing the firmware by clicking here and here.

Next step is to load the LUA program in Esplorer and flash it into the ESP8266.

sensor = 4 -- GPIO2
gpio.mode(sensor, gpio.INPUT, gpio.PULLDOWN)
wifi.setmode(wifi.STATION);
wifi.sta.config("MYROUTER","ROUTERPASSWORD")
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)

    conn:on("receive", function(client,request)
   
        local buf = "";
        buf = buf..'<head><meta http-equiv="refresh" content="20" /></head>'
        buf = buf.."<h1> ESP8266 Rain detector</h1>";
        buf = buf.."<h1> Copyright Luc Volders 2016</h1>";
        if(gpio.read(sensor)==0)then    
        buf = buf..'<h1><span style="color:red">It rains !!!!!</span></h1>';
        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)


Test it and make sure it really works like you expect.




Then go to the right side of  the Esplorer screen. Click on reload and a list of all programs in the ESP's memory is displayed. Left click on regensta.lua and rename it in init.lua. This way the program will automatically start when the ESP gets power.

And this is all. A complete web-server and rain-checker in just 21 program lines. The ESP-8266 is truly a wonderboard.

The program is mostly straightforward except some points I would like to bring to your attention.

gpio.mode(sensor, gpio.INPUT, gpio.PULLDOWN)

This line sets the GPIO pin as an input pin and makes sure it is pulled down. This makes sure that the input normally is at 0 (ground) level. It only becomes 1 (HIGH) when it is actually raining.

buf = buf..'<head><meta http-equiv="refresh" content="20" /></head>'

This line is especially interesting.
Normally your webpage is static. It only refreshes when you actually re-enter the ip-adress of the page. that is not a really handy feature as we would need to refresh the page regularly manually to check if it is raining. Now this particular program line makes the page refresh automatically every 20 seconds. And that is what we really want.

This way I open a small browser window in which I have this  webpage open all the time. This webpage sits somewhere on my computerscreen and I can get an automatic update every 20 seconds about the weather condition. And if I am not at my computer I can check it on my phone or tablet.

Where to find the webpage.

First open the configuration screen of your network router in your browser. Now look at the screen that shows you all connected devices.



In the above screen you can see that there are several wired devices attached : my computer, my Buffalo NAS and my Raspberry Printerserver with an Epson printer connected. Above these you can see an unknow device. That'll be the ESP8266. It's IP number on my router is 192.168.1.75.



So when I enter that IP number in my browser I will be presented with the ESP8266's webpage.



And when it rains TADA !!!!

As you can see in the picture above several tests and resetting the ESP-8266 did make the IP-Adress change.

How to use it.

When I am working at my computer I just open a browser window, enter the IP number and adjust the window so that only the the few lines of text are visible. If I need to surf the web I just open another browser window and adjust the size to what is needed to properly surf the web.
I can also open the rain-detectors webpage on my Android Phone and display the information there. That way I do not have to be at my computer to get the weather condition.

This works however is not really practical.
To make it more practical I am going to show you in an upcoming story how the ESP can send a Twitter message to my phone. This is then displayed on my Android's main screen which draws far more attention.

Casing

As the software will change in the future I know I am going to keep the hardware the same. So I made a casing for this project.



First about the interior. It is made by first arranging 3 battery holders in the position where I want them. This way I have a space in the centre where the hardware fits in. You can find the individual files and description for the battery holder by clicking here.

As you can see I am using bended paperclips as battery contacts. This works as a charm.



The above picture shows you how all the circuitry fits in.



And here is the closed case, ready for the great outdoors.

This will run approximately a day on 3 AA batteries which is more as enough for getting the laundry dry.

For those of you that are eager to make one of these yourself and have a 3D printer I will provide you with the STL files.

Rainsensor Case
Lid for Rainsensor Case

So till next time.
Have fun

Luc Volders