A Banana Pro Weather Logger

wl_jobs; a C program to run the weather logger

Previously in Mark I and Mark II weather loggers I turned things on and off and polled data with a bunch of short BASH scripts. This time around I put together a C program, wl_jobs to do it all with different options for different tasks.

/ ws-jobs.c RM20190428                                          /
/                                                               /
/ This program is used to control Gpio pins in order to power   / 
/ up/down the circuit board, blink the LED, monitor a pin that  /	
/ records rain guage bucket tips and shuts down the Banana Pro  /
/ when the shut down button is pressed.                         /
/ Usage: wl_jobs [OPTION]                                       /
/     -u Power up circuit board and LED.                        /
/     -b Blink LED                                              /
/     -d Shut down circuit board and                            /
/     -p Poll LeoStick and get weather data.                    /
/     -s Monitor tip count pin and shutdown pin.                /
/ When wl_jobs monitors the tip and shutdown pins with a 50ms   /
/ delay the CPU usage on my Banana Pro is never more than.03%.  /

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <wiringPi.h>

int main(int argc, char *argv[])
    int optchar;
    while((optchar = getopt (argc, argv, "ubdps")) != -1)
        switch (optchar){ 
        case 'u':
            // Power up circuit
    	    pinMode(7, OUTPUT);		// Power pin = 7
    	    pinMode(0, OUTPUT);		// LED pin = 0
    	    pinMode(1, OUTPUT);		// Poll pin =1
    	    // Power up circuit board
    	    digitalWrite(7, HIGH);
    	    // Turn on LED
    	    digitalWrite(0, HIGH);
    	    // Set Poll pin to Low
    	    digitalWrite(1, LOW);

Note below that th "-p" option uses a system call to run get_w_data after setting GPIO 0 High so that fresh data will be available on the LeoStick. The command wl_jobs -p is used by the BASH script weather-logger.sh (to be discussed in a subsequent section) to poll data which it will process and format before appending it to the weather-data.csv file.

        case 'b':
        // Blink LED
        pinMode(0, OUTPUT);		// LED pin = 0
    	  int count_blinks = 1;
    	  while(count_blinks <= 12){
    	  	 digitalWrite(0, LOW);
    	  	 digitalWrite(0, HIGH);
        	 delay (75);
        case 'd':
    	    // Power down circuit board
    	    pinMode(7, OUTPUT);		// Power pin = 7
    	    pinMode(0, OUTPUT);		// LED pin = O
    	    pinMode(1, OUTPUT);		// Poll pin = 1
    	    // Power down circuit board
    	    digitalWrite(7, LOW);
    	    // Turn off LED
    	    digitalWrite(0, LOW);
    	    //Set Poll pin 1 to LOW
    	    digitalWrite(1, LOW);
        case 'p':
    	    // Poll pin 1 was set LOW when circuit powered up.
    	    // Now set it HIGH so LeoStick polls sensors then
    	    // run get_w_data to poll data on the LeoStick then
    	    // set pin 1 back to LOW.
    	    pinMode(1, OUTPUT);
    	    digitalWrite(1, HIGH);
    	    digitalWrite(1, LOW);

When run with the "-s" option an instance of wl_jobs will run in the background and monitor GPIO pins 2 and 3. GPIO pin 2 is connected to the rain guage. If the rain guage bucket tips a digitalRead on pin 2 will return "1" and a system call will run a short script that increments the number of bucket tips by 1. GPIO pin 3 is connected to a push button switch. If a digitalRead returns "1" on that pin the Banana Pro will shutdown.

On my Banana Pro this program running in the background with a 50 millisecond delay uses 0.3 of the CPU resources and 0.1&% of memory.

	case 's':
	    // Either count rain guage bucket tips or
	    // shut down system depending on which pin
	    // goes LOW, i.e. 1.
	    pinMode(2, INPUT); 		// Tip counter pin = 2
	    pinMode(3, INPUT); 		// Shutdown pin = 3
    		int countval = digitalRead(2);
    		int killval = digitalRead(3);
        	if (countval == 1){
        	if (killval == 1){
            	    system("/sbin/shutdown -h now");
    	}	// Closes switch
    }		// Closes while loop
return 0;


What's up?