05 January, 2019

Raspberry Pi and 3d printed Rain meter - Part 3 - Solution Structure

In this part I will briefly describe my solution / architecture.

Broadly speaking this is my overall use case:The Raspbeery pi will take the measurements. The Contabo (Ubuntu) server will use these measurements and formulate the rainfall, and make it available for consumption onto the internet (Example: Web page, API, SMS Telegram Bot)
Something must be built to check if the Raspberry Pi is up and running, and user (me) alerted when not able to be reached.

In future more more sensor data can be added.
 
This is my solution for the Raspberry Pi:
  1. SQLite database to store the tips.
  2. Command line program (aka sensorDeamon) that will be systemded to listen and save the tips to the db.
  3. The SensorDeamon will also send out an alert that a tip has occurred.
  4. An nginx hosted API to give remote access to the SQLite data.
This is my solution for the Ubuntu server
  1. An nginx hosted API that will listen for alerts.
  2. On receiving an alert it will go and retrieve all data since last retrieval from the RPi.
  3. Do the calculations, and store data in Postgres database.
  4. Make this data available via:
    1. SMS  (Send me an alert when it starts raining)
    2. Web   (So I am other people can view the info and history)
    3. API    (Other systems can query my data)
    4. Telegram (Get alerts and stats via telegram
  5. I would have loved to have done something for Whats app, but there is no public API available for it.
This is my proposed directory structure on the Pi:
  •  sensorpi
    • configs
    • database
      • scripts
    • sensordeamon
    • webapi
Some notes on the above structure:
  1. sensorpi just holds the whole solution in one place.
  2. configs folder will contain the configuration settings for:
    1. sensordaemon, and
    2. webapi
  3. This will allow me to put the source code onto git without revealing my private passwords and settings.
  4. Since the database only exists on the Pi and I dont have a dev I will put the database script in a sub folder and shell file for me to recreate it as needed.
  5. sensordeamon wfolder will contain the command line app that listens to the tips.
  6. webapi will allow remote access (from the Ubuntu server) to my database.

Raspberry Pi and 3d printed Rain meter - Part 2 - Loading and testing dotnet core

I am a c# developer by day, and although I can kind of work in Python, I dont like it.
Since dotnet core now runs on most Linux systems including the Raspberry Pi, I thought I would give it a go and see how it works. Only problem might be talking to the GPIO pins on the board but I am sure I can find some type of solution


  1. Loading dotnet Core onto the RPi
    I followed the instructions on this page with a few minor changes after I ran into trouble.
    These are the commands I used:
    $ apt-get install wiringpi

    $ sudo apt-get -y update


    $ sudo apt-get -y install libunwind8 gettext


    $ wget https://dotnetcli.blob.core.windows.net/dotnet/Sdk/2.1.300/dotnet-sdk-2.1.300-linux-arm.tar.gz


    $ wget https://dotnetcli.blob.core.windows.net/dotnet/aspnetcore/Runtime/2.1.0/aspnetcore-runtime-2.1.0-linux-arm.tar.gz


    $ sudo mkdir /opt/dotnet

    $ sudo tar -xvf dotnet-sdk-2.1.300-linux-arm.tar.gz -C /opt/dotnet/

    $ sudo tar -xvf aspnetcore-runtime-2.1.0-linux-arm.tar.gz -C /opt/dotnet/

    $ sudo ln -s /opt/dotnet/dotnet /usr/local/bin
  2. SSH Deploy to the rescue.
    I have VS2017 installed on a windows PC, and decided to use that for my development environment but there is a problem ... How do I easily get the code from my Windows PC onto the RPI to test. Luckily I found this program called SshDeploy. This made my life much easier.
    Following the instructions on the site I installed it added the xml tags to my project and tested it. Each time after a successful compile it auto sends the program data to the pi.
  3. Finding a Nuget Package for the GPIO pins.
    Next thing was to proof that I can communicate with the GPIO pins correctly. I did a search on NUGET for GPIO and went to each web site to see how much info there was in case I got stuck. I settled on  Unosquare Raspberry IO.Lots of help and a good description. What sold me on the solution was the ability to use callbacks so no need for a while (true) loop.
  4. Testing the GPIO pins.
    I created a new dotnet core 2.1 command line program, followed the steps of SSH Deploy and added this code:
    using System;
    using Unosquare.RaspberryIO;
    using Unosquare.RaspberryIO.Gpio;

    namespace TestGpio
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Gpio Callback Test 3");

                var pin = Pi.Gpio.Pin00;
                pin.PinMode = GpioPinDriveMode.Input;
                pin.InputPullMode =

                  GpioPinResistorPullMode.PullUp;

                pin.RegisterInterruptCallback (

                  EdgeDetection.RisingAndFallingEdges,
                  Pin00Callback);

                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();
            }

            static void Pin00Callback()
            {
                var pin = Pi.Gpio.Pin00;
                var state = pin.ReadValue();
                Console.WriteLine("Pin Activated..." + state);
            }
        }
    }
  1. I compiled the code, and confirmed it SFTPed it to the Raspberry Pi.
  2. I logged into the RaspBerry Pi with my user name and went to the deployed folder and verified the files were recent.
  3. Then I ran the program using this command:
    $ sudo dotnet TestGpio.dll
  4. This is the output:








Raspberry Pi and 3d printed Rain meter - Part 1

See the Introduction.


As a side note:
This is where I buy my components from. Luckily I had them all already.
  1. G T Electronic Components
  2. Roleen Electronic Components
  3. Micro Robotics  (In Centurion) 
Steps as proof of concept
  1. I downloaded Raspbian Stretch Lite  from the official web site.
    I downloaded the lite version as I have no need for a desktop.
  2. I downloaded  Etcher from their web site.
  3. Since my Pi had an existing 4Gig SD Card I just used that, and put the image on the SD card following the instructions from here.
  4. I booted up the pi, and all was well.
  5. I then enabled wifi, and remote ssh login.
  6. I then created a new user for myself to log in with rather than the "pi" user.
  7. I then searched google for "hall sensor raspberry pi" and found this web site. and followed all the instructions there, but will repeat them here for clarity in my own words.
    1. I built my little circuit on a breadboard.
       
    2.  I then created a new folder in my home folder on the pi called "hallsensortest" by executing this command:
      mkdir hallsensortest
    3.  Go into the folder by
      cd hallsensortest
    4. And then executing the command to get the python script from bitbucket

      wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/hall.py
    5. Since I had logged in using my own user name (and not pi [good security practice]) I had to execute:
      sudo python hall.py
      The reason for the sudo is to gain access to the GPIO pins with elevated privileges.
    6. Then all that was left to do is test it. I got a magnet and waved it in front of the hall sensor, and the screen updated accordingly.

Next step is to get this working in dotNet core.

04 January, 2019

Raspberry Pi and 3d printed Rain meter - Intro

I decided to try and build something using my Raspbrry Pi.

A work colleague had said he had built a weather station to 80% completion and had 3d printed the rain meter.

I then asked if he would be so kind as to print me one as well and I would build it over the December holiday. As I was busy with other things so I only started this project in the new year (2019).

This is what the 3d printed rain meter looks like:
  1. Is the main container.
  2. Is the tipping bucket
  3. Is the lid that goes on the top to guide the rain drops over the tipping bucket.
  4. Is the outlet hole (one each side)
How it works:
The lid (3) guids the rain drops into the center, which then drips into the tipping bucket (2). A very small amount of rain will make it tip, let out the water through the hole (4) and put the other side of the bucket (2) ready to catch more water. There is a small thin magnet (yellow line) attached to the tipping bucket (2), and this will activate a hall sensor attached to the raspberry pi (see Part 1 for more details)

I suspect I would have to sand the lid (item 3) down a bit to make it smooth for the raindrops. And paint it with a UV resistent paint to guard it against the African sun.

The yellow line I suspect is where the magnet will be attached once I assemble the whole thing.

The plans can be downloaded from thingivers if you want to print your own.


Part 1 - Testing the hall sensor

01 January, 2019

Plans for 2019.

My plan was to build a raspberry pi based robot in 2017.

I bought an extra Raspbery Pi 2 end of 2016 with a camera, and a battery pack.

I never got so far as building it.

My long term plans are to build a grandfather clock, it will be the grandfather clock to end all grandfather clocks. But that finished product is still 10 or so years away. Lots of other things need to take place fist before I get to that project.

I work colleague, has a 3d printer, and has some amazing projects that he is busy with. From 3d printed planes, to cnc machines.

Also Trish and I keep track of the rain fall in our suburb. She takes the reading, then WhatApps it to me and I add it into my Diary (a spreadsheet). We have been keeping track of it for 3 year so far. Btw the rainfall in Isandovale for the last 3 years was:
  1. 2016 - 458 mm
  2. 2017 - 689 mm
  3. 2018 - 593 mm
I have been wanting to do something with my Raspberry pi's again, and when J at work spoke about a weather station he had build to 80% completion, I asked about the rain gauge. After the chat I diced to build one and J offered to 3d print me the housing. The next morning when I arrived at work there was a plastic packed on my desk and it had the 3d printed parts and some electronic chips (The ESP8266) being one of them. This looks like an amazing chip.

However I decided I would first build it using my Raspberry Pi. So after completing a few other (video) projects I started with the Rain meter project. The details will be in a separate blog post.

So my plans for 2019 are:
  1. More blog posts for the 2 people who read them.
    (It actually is just a diary for myself)
  2. Create a rain meter using the Raspberry Pi.
  3. Create a small CNC  pen plotter machine using floppy drives (like this).
  4. Create a bigger CNC using NEMA17 motors like this.
  5. If there is time enhance the rain meter into a weather station.
This is a very tall order since I did not do much of this in 2018.