Archive for the ‘ Weather Station ’ Category

 

With this new enclosure, I have created a new sensor board that brings all my sensors together in one location as well as setting up 10 pins on the board for bringing together like wires for communication back to the Pi.  Due to the need of the mast needing 5V, I am using 2 separate voltages on the opposite rails.  The rail on the left is 5v and the one on the right is 3.3v

There are 3 sections that I’m going to cover:

  • Battery Circuit – for monitoring of the battery charge
  • Sensors – The wiring of the sensors within the enclosure as well as the communication back from the mast
  • Sensor board to RPi communication

Battery Circuit Wiring:

  • GND from Powerboost to GND on board
  • Low Battery indicator from Power boost to voltage divider on board.  Resistors: R1 = 2.2kΩ, R2 = 5.1kΩ (If you’re not familiar with how this works, LBI goes to rail containing one side of R1, Other side of R1 goes to rail that contains one side of R2 as well as read wire.  Other side of R2 goes to ground.  This is used to cut down voltages.  The powerboost is outputting 5V, but the MCP30008 ADC can only handle 3.3v which is why the divider circuit)
  • Voltage divider read wire to CH0 on MCP3008
  • MCP3008 wiring
    • Vdd & Vref to pin 10 on board (3.3V power rail)
    • AGND & DGND to pin 9 on board (3.3v ground rail)
    • CLK to pin 5 on board
    • Dout to pin 6 on board
    • Din to pin 7 on board
    • CS to pin 8 on board

Sensor Wiring

  • BMP180
    • Vin to pin 1 on board (5V power rail)
    • GND to pin 0 on board (5V ground rail)
    • SCL to pin 3 on board
    • SDA to pin 2 on board
  • MOD-1016
    • Vcc to pin 1 on board (5V power rail)
    • IRQ to pin 4 on board (interrupt pin on RPi)
    • SCL to pin 3 on board
    • SDA/MOSI to pin 2 on board
    • GND to pin 0 on board (5V ground rail)
  • Mast
    • Blue-white to pin 0 on board (5V ground rail)
    • Blue to pin 1 on board (5V power rail)
    • Green-white to pin 3 on board
    • Green to pin 2 on board

Board pinout to Rasberry Pi

  • Pin 0 (5V GND) to pin 7 on Pi
  • Pin 1 (5V Power) to pin 4 on Pi
  • Pin 2 (SDA) to pin 3 on Pi
  • Pin 3 (SCL) to pin 5 on Pi
  • Pin4 (Int) to pin 11 on Pi (GPIO 17)
  • Pin 5 (SPI CLK) to pin 12 on Pi (GPIO 18)
  • Pin 6 (SPI OUT) to pin 16 on Pi (GPIO 23)
  • Pin 7 (SPI IN) to pin 18 on Pi (GPIO 24)
  • Pin 8 (SPI SC) to pin 22 on Pi (GPIO 25)
  • Pin 9 (3V GND) to pin 20 on Pi
  • Pin 10 (3V Power) to pin 17 on Pi

 

it will take a while before I post the Python code on the Pi.  I am still testing/working on the code for the battery monitoring.  Once I’m done, I’ll post it all here as well as on Github.

Anemometer Reading

It was too late last night when I finished the post about building the anemometer that I didn’t want to get into the code to read the wind speed. As I said before, basically what I do is I count the number of “triggers” within a 10 second period and then feed that into the formula that came from testing it with our car.

Here is the code I use for Raspberry Pi…I run this every 5 minutes (with the rest of my measurements) to get the wind speed at that time.

# calculate windspeed
# set up the GPIO to be an input and activate the internal resistor to pull down the pin to low
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

# This is the interrupt function. It opens a file, pulls the number from the file and adds one to it, then rewrites it to the file.
def wind_callback(channel):
windfile = open("/home/pi/wind_count.txt")
windtext = windfile.readline()
windfile.close()

wind_count = int(windtext)
wind_count += 1
windfile = open("/home/pi/wind_count.txt", "w")

windfile.write(str(wind_count))
windfile.close()

# Here is the interrupt setup. We are setting it up on pin 22, looking for it to rise, calling the function "wind_callback" when it is triggered and
# debouncing it by 100ms
GPIO.add_event_detect(22, GPIO.RISING, callback=wind_callback, bouncetime=100)
# Before we start the count, we open the file and set the number to 0
windfile_clear = open("/home/pi/wind_count.txt", "w")
wind_count_clear = 0
windfile_clear.write(str(wind_count_clear))
windfile_clear.close()

# now we sleep for 10 seconds to get the readings
time.sleep(10)

# After sleeping, we open the file and read the number
wind_file = open("/home/pi/wind_count.txt")
wind_count = wind_file.readline()
wind_file.close()

# in order to use it in some math, we need to convert it to an int
wind_count_comp = int(wind_count)

# here is the formula created from testing. Basically it is .0023427x^2 + .46244x
windspeed_a = wind_count_comp * wind_count_comp * .0023427
windspeed_b = wind_count_comp * .46244
windspeed = windspeed_a + windspeed_b

# round it off to 1 decimal.
windspeed = round(windspeed, 1)

I am actually in the middle of a redesign on my weather station due to the Raspberry Pi not being the best at interrupts for the rain gauge. For the one at my father-in-laws farm, I will be using an Adafruit trinket to capture all the data from the anemometer, rain gauge, and wind direction sensor before relaying it to the Pi. This type of sensing is better for a micro controller than it is for a Linux computer such as the Pi. For my home weather station, I’ll be using a Arduino Pro Mini as I have a lot more sensors on my mast. The Trinket code is a little more difficult due to the ATTiny85 that runs it. I’ll post info on how to connect it to a Raspberry Pi later.

Here is the code for the same principal as above on a trinket. The big difference here is that this runs constantly. Every 10 seconds I have a wind speed reading, although I only pull it every 5 minutes.


#include avr/interrupt.h

// setup cbi & sbi for interrupts
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

// Set up variables used
volatile int windSpeed = 0; // keep track of interrupts from anemometer
volatile int windSpeedSend = 0; // used to send 10 second interrupt value to pi
long debouncing_time_wind = 100; //Debouncing Time in Milliseconds for anemometer
volatile unsigned long last_micros_wind; // micros since last anemometer interrupt
volatile int val2 = 0; // pin 4 value at interrupt

void setup()
{
// Setup Pins for interrupts
pinMode(4,INPUT);

sbi(GIMSK,PCIE); // Turn on Pin Change interrupt
sbi(PCMSK,PCINT4); // Which pins are affected by the interrupt. Turn on Pin 2

}

void loop()
{

// anemometer counts interrupts for 10 second interval. This is sent to master for calculation
windSpeed = 0;
tws_delay(10000);
windSpeedSend = windSpeed;

}

// debounce anemometer. make sure new reading is greater than old reading + debounce wind micros
void debounceWindSpeed() {
if((long)(micros() - last_micros_wind) >= debouncing_time_wind * 1000) {
windSpeedFunc();
last_micros_wind = micros();
}
}

// function to increment anemometer count
void windSpeedFunc() {
windSpeed = windSpeed + 1;
}

// interrupt service routine. What to do when an interrupt occurs as all pin change interrupts
// on ATTiny85 trigger PCINT0 vector.
ISR(PCINT0_vect) {
// get value of pin 4 @ interrupt
val2 = digitalRead(4);

// if pin 4 is HIGH (caused the interrupt), proceed with wind speed (anemomether) increment function
if (val2 == HIGH) {
debounceWindSpeed();
}

}

I finally completed migrating the weather sensors for the weather station that is at my father-in-law’s farm. I had issues with the rain gauge not working too well with the pi directly, so I decided to use an Adafruit Trinket to capture the values of the sensors and send them to the pi for tracking. The communication between the two is accomplished using I2C.

image

Here’s the completed mast with sensors attached. (This is actually a pic of it at my house for testing) What you’ll need for this is 1 of each of an anemometer, wind vane & rain gauge. It works with the sensors I built, but I’m sure you could incorporate it to use any sensors you have as well.

I’ve used PVC to build all my sensors as it’s easy to connect together. In my anemometer & wind vane posts I have used certain PVC pieces to allow for connection to my mast. For the rain gauge, it’s a little different. I used a piece of cedar fence board to secure the sensor, then some u-bolts to attach the board to a 1 inch pvc pipe, plugged one end and drilled a hole for the wire to run through.

For the rest of the top part of the mast I use a 1″ T-connector at the top with about 8 or so inches of 1″ pipe to connect the anemometer & wind vane. I run the wires down through the pipes. I then use a small piece of 1 inch pipe (about 3 or so inches) to connect another T-connector for the anemometer. I then use another portion of 3 or so inches of 1″ pvc pipe to connect to the lower portion. (Sorry, but I don’t have any pics of this…)

The lower mast is where the trinket comes in…I connect all the sensor wires to a single cat 5 cable to connect to trinket and then another wire for the I2C communication. The idea is to house the trinket in some 3/4″ pipe that is waterproofed and have it sit inside some 1 1/2″ pipe. (You can see this bulge in the pic above) I use some connectors to move from 1″ to 1 1/2″ pipe. Then a portion of 1 1/2″ pipe (I think it’s 12 inches long) and then connect it back to another 1″ piece of pipe to mount it.

Before wiring up your trinket, make sure you upload the code to it. Here is what I use which tracks the sensors and sends the data via I2C:

#include avr/interrupt.h
#include TinyWireS.h

// setup cbi & sbi for interrupts
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

//Define address of device
#define I2C_SLAVE_ADDRESS 0x04

// Set up registers to hold data to send
volatile uint8_t reg[] =
{

0xDE,
0xAD,
0xBE,
0xEF,
0xEE,
0xED
};

// Set up variables used

volatile byte reg_position; // keep track of reg position sent from master
volatile int rainGauge = 0; // keep track of interrupts from rain gauge
volatile int windSpeed = 0; // keep track of interrupts from anemometer
volatile int windDirection = 0; // keep track of value of wind direction sensor
volatile int windSpeedSend = 0; // used to send 10 second interrupt value to pi
long debouncing_time_rain = 300; //Debouncing Time in Milliseconds for rain gauge
volatile unsigned long last_micros_rain; // micros since last rain gauge interrput
long debouncing_time_wind = 100; //Debouncing Time in Milliseconds for anemometer
volatile unsigned long last_micros_wind; // micros since last anemometer interrupt
volatile int val1 = 0; // pin 1 value at interrupt
volatile int val2 = 0; // pin 4 value at interrupt

// Function to send data when it is requested. Sends 2 register positions
// for each request
void onRequest()
{

// Load all values into registers. (even though only 2 will go to master)
reg[0] = lowByte(windDirection);
reg[1] = highByte(windDirection);
reg[2] = lowByte(windSpeedSend);
reg[3] = highByte(windSpeedSend);
reg[4] = lowByte(rainGauge);
reg[5] = highByte(rainGauge);

// send 2 reg positions to master
TinyWireS.send(reg[reg_position]);
reg_position++;

}
// Function to set which register positions you wish to receive
void receiveEvent(uint8_t howMany)
{
reg_position = TinyWireS.receive();

// if requested position is 7, reset rain gauge variable. Will be sent at midnight each day
if(reg_position == 7) {
rainGauge = 0;
}

}

// Setup for connection
void setup()
{
TinyWireS.begin(I2C_SLAVE_ADDRESS);
TinyWireS.onReceive(receiveEvent);
TinyWireS.onRequest(onRequest);
pinMode(1,INPUT);
pinMode(4,INPUT);

sbi(GIMSK,PCIE); // Turn on Pin Change interrupt
sbi(PCMSK,PCINT1); // Which pins are affected by the interrupt. Turn on Pin 1
sbi(PCMSK,PCINT4); // Which pins are affected by the interrupt. Turn on Pin 2
}

void loop()
{

// anemometer counts interrupts for 10 second interval. This is sent to master for calculation
windSpeed = 0;
tws_delay(10000);
windSpeedSend = windSpeed;

// read pin 3 analog value of wind direction sensor
windDirection = analogRead(3);

// Check for connection to be stopped.
TinyWireS_stop_check();
}

// debounce rain gauge. make sure new reading is greater than old reading + debounce rain micros
void debounceRainGauge() {
if((long)(micros() – last_micros_rain) >= debouncing_time_rain * 1000) {
rainGaugeFunc();
last_micros_rain = micros();
}
}

// debounce anemometer. make sure new reading is greater than old reading + debounce wind micros
void debounceWindSpeed() {
if((long)(micros() – last_micros_wind) >= debouncing_time_wind * 1000) {
windSpeedFunc();
last_micros_wind = micros();
}
}

// function to increment rain gauge count
void rainGaugeFunc() {
rainGauge = rainGauge + 1;
} // end of rainGauge

// function to increment anemometer count
void windSpeedFunc() {
windSpeed = windSpeed + 1;
} // end of rainGauge

// interrupt service routine. What to do when an interrupt occurs as all pin change interrupts
// on ATTiny85 trigger PCINT0 vector.
ISR(PCINT0_vect) {
// get value of pin 1 @ interrupt
val1 = digitalRead(1);
// get value of pin 4 @ interrupt
val2 = digitalRead(4);

// if pin 1 is HIGH (caused the interrupt), proceed with rain gauge increment functions
if (val1 == HIGH) {
debounceRainGauge();
}

// if pin 4 is HIGH (caused the interrupt), proceed with wind speed (anemomether) increment function
if (val2 == HIGH) {
debounceWindSpeed();
}

}

So, now to the wiring:
image

Here is the wiring diagram I drew out that shows how everything is connected. When connecting, make sure you use solder and shrink tube to protect the joints.

image

After all the wires are connected, drill a hole in a 3/4″ pvc plug and push the wire through that hole prior to connecting to the trinket. You may want to run it through a piece of 3/4″ pipe as well (about 8 or so inches).

All 3 sensors will need 1 wire connected to the +3v on the sensor. I managed to get all 3 of the cat 5 wires into the hole on the trinket. The read wires will need to go different pins. For the anemometer and rain gauge, you will need to connect a 10k resistor to ground to pull the pin to ground. The rain gauge read wire should go to pin 1 and the read side of the anemometer should go to pin 4. The read wire for the wind vane should go to pin 3. Connecting all the ground wires was fun. What I did was run 1 wire out of the trinket and connected all the ground wires to it and put shrink tube over all of them. (You can see it in the pics below.)

image

image

I didn’t take a lot of single step pics on this and I apologize. You can see the connections for the I2C wire as well in these pics. The last part is connecting the wires for the I2C communications. You’ll want to put the wire through hole in another 3/4″ pvc plug before you connect them to the trinket to allow for waterproofing. Connect the +v to the battery pin (I use 5 volts out of my raspberry pi to power, but you can power it any way you want. I used a 3 volt trinket for this setup). Ground to your ground wire, SDA to pin 0 and SCL to pin 2. Once this is done, slide it all into the 3/4″ pvc and plug the other side. It should look like this:

image

Place some hot glue on each plug where the wires go in to keep the water out, then shove this into the 1 1/2″ pvc pipe and then connect the rest of your mast.

This is really set up to communicate with a pi, but I’m pretty sure it would work with an Arduino as well. In the code you basically send the trinket a number and it will give you the response. Sending 0 will give you the wind direction reading, 2 will give you the wind speed reading and 4 will give you the rain gauge reading. Sending it 7 will cause the rain gauge count to reset. Here’s a quick bit of code you can use for the pi:


#!/usr/bin/python
# Import needed libraries
import smbus
import time

# Set I2C address of device you wish to access
DEV_ADDR = 0x04

bus = smbus.SMBus(1)

# Request values from device. Number is start register position.
while True:
print 'Testing for connection...'
while True:
try:
wind_dir_read = bus1.read_word_data(DEV_ADDR, 0)
wind_count = bus1.read_word_data(DEV_ADDR, 2)
rain_count = bus1.read_word_data(DEV_ADDR, 4)
break
except IOError:
print 'No connection...'
time.sleep(5)
#break
if wind_dir_read < 1030: break else: print 'Value too high.' time.sleep(5) # Use this to reset a variable: #d_val = bus.read_word_data(DEV_ADDR, 7)

It doesn't seem that wordpress knows how to deal with indents in it's code sections, so make sure you indent it properly. Sometimes the trinket can give you some funny business which I've handled via the error trapping above. If you keep this bit of code you shouldn't run into any problems.

Weather Station

20131128-093102.jpg

I know it’s been a while since I posted something, I guess it was just getting difficult to find time to post something after completing a sensor before bed. My most recent project has been building my own weather station. This sort of came from the greenhouse automation project. Once I was able to measure humidity, that was it; I wanted to measure everything else about the weather that I could.

In building this, I feel I learned a lot about some other aspects of the raspberry pi, coding and some other sensors (reed switches specifically). In some places, I kind of took the easy route and took something someone else made and re-wired it to work for me, (rain gauge & wind direction pointer) but this still involved a lot of innovation on the coding side to use it. I plan to write other posts detailing the building of the other sensors; just plan to space them out some as some of them are pretty involved. I think eventually I might make a wireless self-contained version (wifi, batteries, & solar power), but that’ll be a while 😉

Oh…..forgot to mention the first time I published it…..you can see the current weather at my house by clicking on the “Current Weather” page on my blog…