Author Topic: building a pi weatherstation  (Read 1960 times)

0 Members and 1 Guest are viewing this topic.

Offline jgveill

  • Member
  • *
  • Posts: 11
building a pi weatherstation
« on: March 29, 2017, 06:15:08 PM »
Hello,

I've looked around and found a lot of people talking about pi weatherstation but I didn't see related codes.  Could someone indicate me where I could find working codes to start with ? 

I started this way with arduino and finally got a working wunderground weatherstation (temp, humidity, pressure, wind, wind direction, rain, and all calculated data coming from these sensors). See wunderground IQUBECVI10.  I also have a basic local console (4X20 lcd) but I'd like to go forward with more data like astronimical data, nicer and more complete console and I think pi is now the way to go. 

I imagine I will not find exactly what I look for but I'm sure I can start with basic sensors and then add / adapt other parts and functions.  My sensors are si7021, bmp180, RTC DS3231 (probably not required with pi), 8 pulse/turn anemometer and wind direction (https://wunderweatherstation.wordpress.com/), basic tipping bucket rain gauge, etc... 

Thanks J Guy 

Offline weathernick

  • Senior Member
  • **
  • Posts: 90
Re: building a pi weatherstation
« Reply #1 on: March 31, 2017, 02:11:21 PM »
You are more than welcome to use my code. I have bits and pieces of it floating around the forum but attachment is the latest(wxdatalogger.py). My raspberry pi2 (your 3 will be even better with the extra cores and wifi.) has Weewx on it that takes a comma delimited file using the custom driver I build (also attached, raspberrry_pi.py)). My script access all the sensors once a second and outputs a CSV file. The weewx driver then takes that files and decodes the appropriate fields and dumps it into weewx. From there it is just playing around with weewx to get what you want from it.

Enjoy and have fun. My code is never documented enough so if you have questions let me know. 

Also you can rename the files to end with py instead of txt. Had to do that to post. Clearly python doesn't care what you name the file.

Thanks,

Nickolas
PWS: Custom built with Raspberry Pi collecting the data from the sensors.
CWOP: EW5462
Wunderground: KAZYUMA27
Personal WX page: http://mccolls.weewx.s3-website-us-east-1.amazonaws.com/

Offline jgveill

  • Member
  • *
  • Posts: 11
Re: building a pi weatherstation
« Reply #2 on: March 31, 2017, 07:19:09 PM »
thank you so much Nickolas !

I had a very brief look at your code and I'm really close to you with my equipment : temp sensor is compatible, pressure is the same, windspeed and dir are the same.  That's real good news to me. 

Now, I must learn / understand how I can install this code on my pi !  I will learn with my bmp180 and si7021.  That may be not easy but easier for sure !

Thanks again, I'll keep you posted.    J Guy


Offline weathernick

  • Senior Member
  • **
  • Posts: 90
Re: building a pi weatherstation
« Reply #3 on: March 31, 2017, 07:24:32 PM »
That is excellent news. The precip code is the most complex as I scan the GPIO port 1000's of times a second and filter out what is noise (short pulse) or  a tip on the precipitation sensor (long pulse) I have found it has been perfect with no false positives.

Have fun and if you find better ways to do things pass the findings back to the forums. :)

Nickolas
PWS: Custom built with Raspberry Pi collecting the data from the sensors.
CWOP: EW5462
Wunderground: KAZYUMA27
Personal WX page: http://mccolls.weewx.s3-website-us-east-1.amazonaws.com/

Offline jgveill

  • Member
  • *
  • Posts: 11
Re: building a pi weatherstation
« Reply #4 on: April 01, 2017, 09:30:29 AM »
Thanks for the noise measurement information, I was wondering why ...

Maybe another question on library.  With arduino, I learned to be carefull with libraries cause many libraries are not perfectly working and finding where's the problem is not easy. HTU21d library gave me real problems and I worked hard to find one perfectly working. 

Are pi libraries less prone to problems ?  As your WS is really stable, may I know which HTU21d and BMP180 libraries are you using ? This could save me some time.

If I find something to enhance your code, you can be sure I will share it through the forum.

J Guy

Offline jgveill

  • Member
  • *
  • Posts: 11
Re: building a pi weatherstation
« Reply #5 on: April 01, 2017, 03:34:16 PM »
Nickloas,

Just one comment : as our windspeed sensor has 8 pulses / turn you count pulses to get speed. That's a way to do it but with my arduino I decided to count changes not pulses (rising is one change falling is another change). This way I get 16 changes per turn or 16 steps for 2.5 mph. This always gives better results specially at low wind speed.

Using 16 changes / turn is possible because output duty cycle is 57 % (pulse with is high 57 % of the time, low 43% of the time).  I  used a scope to measure that. For exact result, 50 % duty cycle would have been better, but 57 % is good enough to use 16 changes per turn. This way at low speed we get more precision. You may say, is it worth ? As it costs nothing, just few changes in the code, I think it's worth !  I will do it for sure.

I also looked at pi interrupts and found something interesting.  It seems that newer raspbian got more flexibility with interrupts. Look at that :
http://wiringpi.com/reference/priority-interrupts-and-threads/

We also get interrupts handling for both edges, exactly what we need for our windspeed.

J Guy   
« Last Edit: April 01, 2017, 04:01:40 PM by jgveill »

Offline weathernick

  • Senior Member
  • **
  • Posts: 90
Re: building a pi weatherstation
« Reply #6 on: April 01, 2017, 04:44:08 PM »
Hmmm impressive thought... I haven't actually thought about it enough. So are you saying that by measuring pulses I could be off on my wind speed? Wow I need to sit down with the math to figure this out. I know we can measure both pulses (precip does just that). I think I just need to wrap my head around how data is being lost here? I do believe my code doesn't lose track of the pulse count after it gets a measurement. This would prevent a lost pulse if I stopped counting halfway between a pulse.

Good stuff coming out of this. Keep it coming. By no way do I think my code is perfect and I welcome any improvements. :-D

Nickolas
PWS: Custom built with Raspberry Pi collecting the data from the sensors.
CWOP: EW5462
Wunderground: KAZYUMA27
Personal WX page: http://mccolls.weewx.s3-website-us-east-1.amazonaws.com/

Offline weathernick

  • Senior Member
  • **
  • Posts: 90
Re: building a pi weatherstation
« Reply #7 on: April 01, 2017, 05:14:14 PM »
Double checked my code and it turns out I monitor the GPIO port for events (in this case Rising pulse) and count them. Then every second I see what I have counted and divided by the time since the last count. (Linux doesn't allow for realtime access to events so there could be a slight time delay) That is my current winds for that one second of time. I can see a problem if there is delay from the time I check the value to the time I set it to zero. I assume that time is instantaneous but it isn't. If a pulse is picked up by the system during the setting to zero then it is lost forever. Is this what you are referring to? The easiest way would be to never set the pulse to zero and just take the different in pulses from second to second. The problem is you will hit a max for INT eventually and will have to reset to zero. I can see if you measure both rising and falling pulses then you have only lost 1/2 of a pulse and thus only 2.5mph/16=.15625mph. Is this what you were referring too?

By no way do I consider myself an expert in the field of electronics. I am self taught and learn something new every day. :-D

Also I believe my code already takes advantage of monitoring events via threads. This has been very nice and all you have to do is change the line from measuring Rising to Both and bam you have just implemented your improvement.

I did just do a calculation and it would take a lifetime to reach the max for a python INT for wind pulse counts. Assuming I measure 1.3million pulses per day. Python max INT seems to be 92,233,720,368. I might switch my code to also keep track of pulses and just query that value every second. As long as I store the previous value I can calculate wind speed. In that situation I keep track of all the pulses. Unless of course I didn't understand what you were saying in which case I might need to hit the books. Again. :-D

Nickolas
« Last Edit: April 01, 2017, 05:24:47 PM by weathernick »
PWS: Custom built with Raspberry Pi collecting the data from the sensors.
CWOP: EW5462
Wunderground: KAZYUMA27
Personal WX page: http://mccolls.weewx.s3-website-us-east-1.amazonaws.com/

Offline jgveill

  • Member
  • *
  • Posts: 11
Re: building a pi weatherstation
« Reply #8 on: April 01, 2017, 07:34:56 PM »
Nickloas,

I may be totally wrong with my thoughts, but it works perfectly with arduino.   

The more pulses or clicks we have, the more we get exact windspeed.  That's why we pay more to have 8 pulses / turn (16 changes or clicks).  So counting rising and falling edges double the precision compared to counting pulses. For that, the time pulse is high must be close to the time it's low.  Counting pulses would give me a multiply factor of 0.5029 km vs 0.2514 km with 16 changes.  Delta time being the same, precision is higher.   

I understand pi is not realtime but with the way I calculate windspeed, the delay where I count windclicks (falling and rising in my case) doesn't need to be exactly one second.  It could be few msec shorter or longer, it doesn't matter. What's really important is to be able to have ISR (Interrupt Sub Routine) called each time we have a level change or a pulse at the pin we monitor windspeed and keep this subroutine very short to keep exec time as short as possible. 

My wind speed is in km/h and I'm using 16 clicks / turn
Here's my logic with arduino and it should work with pi:
 
Main Loop
    -> Current_second = last_second
    -> Calculate Wind Speed function
    -> Calculate Rain function
    -> Scan other sensors
    -> update lcd screen
    -> Wait till next second
     - Start main loop again

Calculate Wind Speed Function (done each second)
    -> Delta_time_speed = now(in msec)- start_time_speed(msec)
    -> wind speed (kmh) = Wind_clicks X 0.2514605 (kmh) X (Delta_time_speed/1000)
    -> wind_clicks = 0   
    -> start_time_speed = now(in msec)
   
Wind_clicks function (Interrupt Sub Routine called each time we have a windspeed click.  Must be very short for reducing exec time)
   -> wind_clicks ++ (add one click)
   -> return

pi delay may destroy this process but if this happens, I hope being able to restore it with threads and priority.  Delay  between resetting windclick's count and start time to calculate windspeed should be close to zero, so no clicks should be lost. 

Also, I'm pretty sure I will not run weewx.  I may extract astronomical data and use what I currently have with my arduino, which is extremely less powerfull than the pi3.  All I need is a local console and send data to wunderground.  So keeping the load low for a pi, should also help keeping delays as short as possible and avoid delays problem.  Arduino mega is currently doing this job, I expect a pi3 to do the same as there's no comparison between power for these processors.  These are my hopes ....

As you've  certainly seen, english is not my mother's tongue, so forgive my bad english which may send you in the  wrong direction ...

J Guy   

Like I said, that's my thoughts so far.  I may be totally wrong but I will try.