At this point I have a little “main” program that sits in a loop reading most of the sensors and simply printing them to the screen. I’ve created an individual little file for each sensor and given them names – like getHumidity() ; rather than something a little lower level – even if it’s just a one-line wrapper in many cases – e.g. this is the core humidity.c file:
#include <wiringPi.h> #include "pins.h" #include "humidity.h" /* * getHumidity: * Return the relative humidity reading - result * 10 ********************************************************************************* */ int getHumidity (void) { return analogRead (HTU21D_HUMIDITY) ; } /* * getTemperature2: * Return the temperature read from the humidity sensor ********************************************************************************* */ int getTemperature2 (void) { return analogRead (HTU21D_TEMP) ; }
the humidity.h file looks like:
// Function prototypes extern int getHumidity (void) ; extern int getTemperature2 (void) ;
which is just definitions (prototypes) for the functions so they can be used elsewhere. The compilier will check the usage against the definitions here and tell you if there are any mis-matches.
I’ve cracked on and created little functions/files for most of the sensors now – even though some are just 1-line calls, they allow the main program to read a little better, so we can easily change the main program using readable words than sensor names. e.g. the ground temperature probe is literally:
int getTemperature0 (void) { return analogRead (GROUND_T) ; }
The main program now looks like:
#include "airq.h" #include "groundT.h" #include "windDirection.h" #include "pressure.h" #include "humidity.h" int main (void) { wiringPiSetup () ; mcp3422Setup (ADC_PIN_BASE, 0x69, 0,0) ; bmp180Setup (BMP180_PIN_BASE) ; ds18b20Setup (GROUND_T_PIN_BASE, "0000053af458") ; htu21dSetup (HTU21D_PIN_BASE) ; for (;;) { printf ("%4d, %10s", getAirQuality(), directions [getWindDirection ()]) ; printf (", %4d", getTemperature0 ()) ; printf (", %4d, %4d", getAirPressure (), getTemperature1 ()) ; printf (", %4d, %4d", getHumidity (), getTemperature2 ()) ; printf ("\n") ; } }
So nothing desperately difficult there. It’s printing nothing more than the values separated by commas – an example output:
40, East, 131, 10177, 156, 511, 160 40, East, 131, 10178, 156, 511, 160 39, East, 131, 10178, 156, 512, 160 40, East, 131, 10178, 156, 512, 160 40, East, 131, 10177, 156, 512, 160 40, East, 131, 10177, 156, 512, 160 39, East, 131, 10178, 156, 512, 160
The 3 temperature sensors are a bit all over the place this morning – 13.1, 15.6 and 16.0, however it’s really just sitting in a pile on the floor – the ground temperature probe is somewhat distant from the others.
I’ve also noticed that reading the 1-wire sensor takes almost a second. Weird.
However – as I mentioned earlier, I’ve left the hard parts to last. The wind speed and rainfall sensors… Now it’s time to code them