40
HNR Weather Station ECET 3701 Embedded PC’s Project Report Prepared by: Rahu Bannister Norman Sims Harsh Saini Spring 2015 For professor Tippens

HNR Weather Station

Embed Size (px)

Citation preview

Page 1: HNR Weather Station

HNR Weather Station

ECET 3701 – Embedded PC’s

Project Report

Prepared by:

Rahu Bannister

Norman Sims

Harsh Saini

Spring 2015

For professor Tippens

Page 2: HNR Weather Station

2

Table of Contents

Table of Contents .......................................................................................................................................... 2

List of Figures ................................................................................................................................................ 4

ABSTRACT ...................................................................................................................................................... 5

INTRODUCTION ............................................................................................................................................. 6

OPERATING INSTRUCTIONS .......................................................................................................................... 7

User Interface: .......................................................................................................................................... 7

Operating Instructions: ............................................................................................................................. 8

Hardware Description ................................................................................................................................. 13

LEDs: ........................................................................................................................................................ 13

Switches: ................................................................................................................................................. 14

Pushbuttons: ........................................................................................................................................... 14

Serial LCD: ............................................................................................................................................... 14

SI1145 UV sensor: ................................................................................................................................... 15

BMP180 Temp. Pressure sensor: ............................................................................................................ 15

Software Description: ................................................................................................................................. 16

Conclusion ................................................................................................................................................... 18

Key Results and Findings ......................................................................................................................... 18

Difficulties ............................................................................................................................................... 18

Next steps ............................................................................................................................................... 18

APPENDIX 1 – Parts List ............................................................................................................................... 19

APPENDIX 2 – Wiring Diagram .................................................................................................................... 20

APPENDIX 3 – Schematic ............................................................................................................................. 21

APPENDIX 4 – Software Modeling Diagram ................................................................................................ 22

APPENDIX 4 – Source Code ......................................................................................................................... 23

WeatherStationMain.c ............................................................................................................................ 23

UVLEDs.c ................................................................................................................................................. 24

SI1145Handle.c ....................................................................................................................................... 25

SI1145main.c ........................................................................................................................................... 29

SerialLCD.c ............................................................................................................................................... 30

Page 3: HNR Weather Station

3

ManualMode.c ........................................................................................................................................ 32

LED_Switches.c ....................................................................................................................................... 34

BMP180Temp-Pressure.c ........................................................................................................................ 39

Page 4: HNR Weather Station

4

List of Figures Figure 1.User Interface ................................................................................................................................. 7

Figure 2. HNR Weather Station Ports ........................................................................................................... 8

Figure 3. Startup Display ............................................................................................................................... 8

Figure 4. Auto Mode-Current Temp ............................................................................................................. 9

Figure 5. Auto Mode-Baro. Pressure ............................................................................................................ 9

Figure 6. Auto Mode-UV Index ................................................................................................................... 10

Figure 7. Manual Mode ............................................................................................................................... 10

Figure 8. Manual Mode-Temperature ........................................................................................................ 11

Figure 9. Manual Mode-Current Temp ....................................................................................................... 11

Figure 10. Manual Mode-Pressure ............................................................................................................. 12

Figure 11. Manual Mode-Baro. Pressure .................................................................................................... 12

Figure 12. BeagleBone Black ....................................................................................................................... 13

Figure 13. LEDs ............................................................................................................................................ 13

Figure 14. Mode Selection Switch ............................................................................................................... 14

Figure 15. Scroll & Select Buttons ............................................................................................................... 14

Figure 16. Sparkfun Serial LCD .................................................................................................................... 14

Figure 17. SI1145 UV Index Sensor ............................................................................................................. 15

Figure 18. BMP180 Temp. & Baro. Pressure Sensor ................................................................................... 15

Figure 19. SI1145 Configuration.................................................................................................................. 16

Figure 20. BMP180 Configuration ............................................................................................................... 16

Figure 21. BMP180 Operation Code ........................................................................................................... 17

Page 5: HNR Weather Station

5

ABSTRACT The object of this project was to build a single compact system that could properly read various climate

conditions and then alert the host via text messaging of those conditions. The idea was sparked by the

coming of spring and the inconsistency of the weather. Our small boxed station provided the user quick

reminders of the current conditions outside without watching the news or even opening your phone.

BeagleBone Black and Cloud9 software was use to program one BeagleBone Black single board

computer. Two I2C sensors were used to take readings. One of the two was the SI1145 UV sensor, the

other was the BMP180 temperature and barometric pressure sensor. On the front panel is a single serial

LCD screen, four LEDs one of which is RGB, a toggle switch for mode selection and two pushbuttons for

manual manipulation. The LEDs are color coded for UV indications and the Serial LCD screen gives

prompts and shows visible readings.

The end game of the project is to model a portable, easy to set up, weather station that can notify anyone

what the outside conditions are at any given moment. The system can help prioritize ones activities and

time with precise readings and clear notifications.

Page 6: HNR Weather Station

6

INTRODUCTION The weather is constantly fluctuating throughout the day. At one time it can be sunny with no clouds in

sight then 30 minutes later it can be raining. On other days the UV rays can be really high and other it will

not be. These fluctuations on climates conditions can cause some people hassle and other to be ill

prepared to go outside.

This is why we engineered the weather station. This station helps utilize time management by pre-

prompting the user with valuable outside information catering to their condition. Some of these conditions

include but are not limited to temperature, humidity, barometric pressure and UV index. One does not

have to turn on the TV and wait for the news to inform them that it’s raining outside. One also does not

have to get a weather app or widget on their phone since the weather information will already be sent to

them.

Engineering is a way to simplify the future and help the mass public prioritize every measure of their life.

This small system helps that future prioritize.

Page 7: HNR Weather Station

7

OPERATING INSTRUCTIONS

User Interface: 4-Leds UV index:

1) UV index 0-2 (indicates safe UV reading, Green LED)

2) UV index 3-5 (indicates average UV reading, Yellow LED)

3) UV index 6-7 (indicates high UV reading, Orange LED)

4) UV index 8-10 (indicates very high UV reading, Red LED)

5) UV index 11+ (RGB LED shared with green, indicates dangerous levels of UV, Blue LED)

Figure 1.User Interface

1-Toggle Switch: Mode select (up-manual, down-automatic)

2-Push-buttons (manual mode only): Black (Scroll button, scrolls available reading options)

Red (Select button, Takes the reading indicated by the scroll push button)

1-Serial LCD screen: Displays information to user of which reading is currently displayed which mode you are in and

team name and information.

Page 8: HNR Weather Station

8

Operating Instructions: 1) Plug a 5v barrel jack into the system.

Figure 2. HNR Weather Station Ports

Upon being powered on the project name and the initials of the group member will be displayed.

Figure 3. Startup Display

After the startup, the system will be in auto mode.

In auto mode, the screen will cycle through the readings and light the corresponding LED.

2) At this point you can set the desired mode by flipping the toggle switch.(up-manual or down-

auto)

This can be set at any time after the startup. The device can transition from one mode to another

at any time during the device operation.

3) If auto is set (toggle-down), the system cycles through the reading as shown below.

Page 9: HNR Weather Station

9

Figure 4. Auto Mode-Current Temp

Figure 5. Auto Mode-Baro. Pressure

Page 10: HNR Weather Station

10

Figure 6. Auto Mode-UV Index

4) If manual mode is set (toggle-up), the system operates by pressing the pushbuttons.

By pressing the scroll, button the screen cycles through the readings.

After which by pressing the select button you have selected that reading for display.

Figure 7. Manual Mode

Page 11: HNR Weather Station

11

Figure 8. Manual Mode-Temperature

Figure 9. Manual Mode-Current Temp

Page 12: HNR Weather Station

12

Figure 10. Manual Mode-Pressure

Figure 11. Manual Mode-Baro. Pressure

Page 13: HNR Weather Station

13

Hardware Description

Figure 12. BeagleBone Black

The central controller is the BeagleBone Black. The capabilities of this single board computer include but

not limited to: 2 ports with 46 I/O pins, UART, SPI, I2C, 1GHz processor, and the Linux operating

system.

This single board controls all of the switches, LEDs, communication with the two I2C sensors,

communication with the serial LCD , call functions, and everything else that has to do with the project.

Through a series of serial and I2C commands the computer gets the sensor data and displays it on the

screen.

LEDs:

Figure 13. LEDs

The 4 LEDs provide real-time indication of the current UV index value. The first LED is a RGB LED that

shows both green and blue when indicated. The LEDs are setup to respond to an active high from the

BBB. The other three LEDs are red, yellow, and orange. These LEDs correspond to the UV index levels.

These LEDs are also connected to pins 7, 8, 9, 10, and 11 on the P8 header of the BBB.

Page 14: HNR Weather Station

14

Switches:

Figure 14. Mode Selection Switch

There is one SPST toggle switches that controls the mode selection. One side of the switch is tied to

ground and the other is tied to a controller pin and the BBB’s internal pull-up resistor. This switch is

connected to the P8 header pin 14 on the board.

Pushbuttons:

Figure 15. Scroll & Select Buttons

Two pushbuttons are implemented in the design. The pushbuttons are normally open circuits. On one side

of the pushbuttons is ground. The other side connects to P8 pin 12 and 13 on the board as well as a pull-

up resistor to 3.3v.

Serial LCD:

Figure 16. Sparkfun Serial LCD

A 3.3v 16x2 serial LCD display is used in the design. The LCD has 3 wires power, ground and receive.

The receive wire connects to the controller using P9 pins 13. The other two wires are connected to 3.3v

and ground on the board P9 pin 3 and P9 pin 1 respectively.

Page 15: HNR Weather Station

15

SI1145 UV sensor:

Figure 17. SI1145 UV Index Sensor

The SI1145 is a UV sensor that runs off 3.3v and is I2C compatible. The board has the standard data line,

clock line, power and ground. The board uses P9 pin 19 for SCL and P9 pin 20 for SDA. The power and

ground are connected to the on board 3.3v supply and ground. This sensor gives off the UV reading as a

16bit Hex number that has to be converted in to the proper UV index reading.

BMP180 Temp. Pressure sensor:

Figure 18. BMP180 Temp. & Baro. Pressure Sensor

The BMP180 is a temperature and pressure sensor. It runs off 3.3v and is I

2C compatible. The board has

the standard data line, clock line, power and ground. The board uses P9 pin 19 for SCL and P9 pin 20 for

SDA. The power and ground are connected to the on board 3.3v supply and ground. The sensor returns

the readings as character strings.

Page 16: HNR Weather Station

16

Software Description: The BBB computer sends data to the SI1145 and BMP180 respectively to initialize both sensors and take

a reading. To do this a series of system commands are sent through I2C connection to each board. In case

of the SI1145 one must first write 0x17 HEX to address 0x07 via the i2cset command. After a few other

set commands, when ready by writing 0x07 HEX to address 0x18, 0x12 HEX to address 0x18 and

reading registers 0x2C and 0x2D respectively you have just forced a measurement and read the return

value After this is done the read output is redirected into a file on the BBB for manipulating. See Figure

18 for a code example.

Figure 19. SI1145 Configuration

To use the BMP180, I2C must be enabled first on the BBB. This is done by using the echo command and

directing the output to /sys/device/bone_capemgr.9/slots. The output path is referred to as SLOTS in

Figure 19. Next the device drives must be load by using the command echo "bmp085 0x77" >

/sys/class/i2c-adapter/i2c-1/new_device. Next, the dmesg | grep bmp command is used to read the

kernel message to verify the device drivers were loaded properly

Figure 20. BMP180 Configuration

Page 17: HNR Weather Station

17

To use the BMP180 sensor the device file paths is defined in the BMP180Temp-Pressure.c file. These

defined paths are then used to directly communicate with the board through file handling. The fopen

command is used to open the device file and the data is read into a character string buffer. These

characters have to be converted into floats in order to properly be displayed on the LCD. The atof

function is used to convert the character string to a float value. Figure 20 shows the exact code snippet

that does what was mentioned above.

Figure 21. BMP180 Operation Code

Page 18: HNR Weather Station

18

Conclusion

Key Results and Findings

Designing an impromptu project on the fly with limited time and actually finish most of it was

great to see firsthand. By working in a group and dividing up the workload this was possible.

Unlike in digital we were able to skip some steps to speed up the process and used a better

controller and a higher level coding language. This helped the project run more smoothly but did

not extinguish all problems. We as a group had to be okay with making decisions that directly

affected our project and cope with major setbacks. This process helped developed skills that will

be essential as engineers. Skills from critical thinking to research and development were tested

and honed for future use in industry. The overall project experience was a step up from our last

one due to the upgrade in information and equipment. Therefore, this resulted in the process

running smoother.

Difficulties

Working with the BeagleBone Black single board computer was the focus of the project, but

surprisingly this was not the biggest issue we had. The I2C set up gave us the biggest hassle due

to the lack of information on I2C configuration. This caused us to use up valuable time in

understanding how our board uses its I2C ports and how to communicate with our sensors within

c programing. We also spent multiple days reading the sensor data sheets trying to understand

how they communicate through I2C. These tasks took away valuable time that could have been

used to refine and enhance the project. We had to remove one of the sensors as well as the Wi-Fi

dongle for text messaging. However, this simplified the project and taught us how to deal with

setbacks.

Next steps

Our original goal was to build a user friendly that would prepare the user for outside activates via

text messaging and/or email of the following conditions:

Temperature

Humidity

Barometric Pressure

UV index

Once this is accomplished we wanted to implement some type of internet connection that would

pull in more weather data and send that data to the user. Since these features were not

implemented in the current system, they are in the plans for future revisions. A better enclosure

that is durable for outside conditions should be used as well as some better wire connectors.

Lastly, some more LEDs for UV index should be included on the front panel to better display the

index.

Page 19: HNR Weather Station

19

APPENDIX 1 – Parts List

Hardware

Part name Part number Quantity Price Total Vendor Surplus

BeagleBoneBlack (P/N:83-16241) 1 $39.99 $39.99 Microcenter Surplus

SI1145 (P/N: 1777) 1 $9.95 $9.95 Adafruit ----------

BMP180 (P/N: 1603) 1 $9.95 $9.95 Adafruit Surplus

3.3 V Serial LCD (P/N: LCD-09066) 1 $24.95 $24.95 SparkFun Surplus

LED Holders (Catalog #: 2760080) 2 $1.99 $3.98 Radioshack Surplus

SPST Momentary

Switch

(Catalog #: 2751547) 1 $3.99 $3.99 Radioshack Surplus

Toggle Switch (Catalog #: 55050562) 1 $3.20 $3.20 Radioshack Surplus

RGB LED (P/N:NTE30115) 1 $4.99 $4.99 Microcenter Surplus

LEDS N/A 3 N/A N/A SPSU Tech. Shop ----------

M to F Jumper

Cable Wires

(P/N: GK1212750) 1 $2.51 $2.51 Amazon Surplus

Resistors 1kΩ N/A 5 N/A N/A SPSU Tech. Shop -----------

-

Breadboard (P/N: 090302_F) 1 $5.95 $5.95 Amazon Surplus

TOTAL $109.46

Page 20: HNR Weather Station

20

APPENDIX 2 – Wiring Diagram

Page 21: HNR Weather Station

21

APPENDIX 3 – Schematic

Page 22: HNR Weather Station

22

APPENDIX 4 – Software Modeling Diagram

Page 23: HNR Weather Station

23

APPENDIX 4 – Source Code

WeatherStationMain.c /****************************************************************************** * Name : WeatherStationMain.c * Author : Harshpreet Saini, Norman Sims, Rahu M. Bannister * Version : 1.0 * Copyright : 2015 * Description : * This code handles the running of the system while in manual * mode. *****************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include "WeatherStation.h" int main() int modeSwitchState; int trueLock =1; int increment = 0; char tempBuffer[7]; char pressureBuffer[11]; char startScreen1 [] = " HNR "; char startScreen2 [] = " Weather Station"; char tempString [] = " Current Temp: "; char tempPrintPosition [] = "\xC5"; char baroString [] = " Baro. Pressure:"; char baroPrintPosition [] = "\xC3"; char manualModeString [] = " Manual Mode "; char UVString [] = " UV Index: "; char UVindex[6]; char UVPrintPosition [] = "\xC6"; float UVvalue; clearScreen(); writeToScreen(startScreen1); writeToScreen(startScreen2); sleep(2); clearScreen(); initSi1145(); //--------------------------------------------------------------------------- // infinite loop // Continually gets the current temperature and barometric pressure and // writes it to the LCD screen. In addition, it continually gets the // UV index value and displays it on the screen and turns on the // corresponding LED. Also, this loop continually checks to see if the // Manual Mode Switch has been activated. //--------------------------------------------------------------------------- while (trueLock ==1) clearScreen(); writeToScreen(tempString);

Page 24: HNR Weather Station

24

strcpy(tempBuffer, getTemp()); //copies the return value of getTemp into //tempBuffer moveCursor(tempPrintPosition); writeToScreen(tempBuffer); ManualModeCheck(); clearScreen(); writeToScreen(baroString); strcpy(pressureBuffer, getPressure()); //copies the return value of //getPressure into pressureBuffer moveCursor(baroPrintPosition); writeToScreen(pressureBuffer); ManualModeCheck(); clearScreen(); resetLEDS(); UVvalue = readUV(); sprintf(UVindex, "%.3f",UVvalue); //float to string conversion writeToScreen(UVString); moveCursor(UVPrintPosition); writeToScreen(UVindex); turnOnUVIndexLEDs(UVvalue); ManualModeCheck(); return EXIT_SUCCESS;

UVLEDs.c /****************************************************************************** * Name : UVLEDs.c * Author : Harshpreet Saini, Norman Sims, Rahu M. Bannister * Version : 1.0 * Copyright : 2015 * Description : * Compares the inputted UV index value to determine which LED to * turn on. *****************************************************************************/ #include <stdio.h> #include "WeatherStation.h" /*============================================================================== * Function : turnOnUVIndexLEDs * Return : none * Parameters : float UVvalue: UV index value * Description : * Compares the inputted UV index value to determine which LED to * turn on. * * Green Yellow Orange Red Blue * 1 2 3 4 5 6 7 8 9 10 11 *============================================================================== */

Page 25: HNR Weather Station

25

void turnOnUVIndexLEDs(float UVvalue) if (UVvalue <=2.00) printf ("Green LED ON!\n"); setLed('g', LED_ON); if (UVvalue >2.00 && UVvalue <= 5.00) printf ("Yellow LED ON!\n"); setLed('y', LED_ON); if (UVvalue >5.00 && UVvalue <= 7.00) printf ("Orange LED ON!\n"); setLed('o', LED_ON); if (UVvalue >7.00 && UVvalue <= 10.00) printf ("Red LED ON!\n"); setLed('r', LED_ON); if (UVvalue >10.00) printf ("Blue LED ON!\n"); setLed('b', LED_ON);

SI1145Handle.c /****************************************************************************** * Name : SI1145Handle.c * Author : Harshpreet Saini, Norman Sims, Rahu M. Bannister * Version : 1.0 * Copyright : 2015 * Description : * Controls all the guts of the programming. The text output of the * readings are redirected into the "UV.txt" file. That file is then * read into a buffer which is compared to known HEX values for * decimal conversion: * * 'Handle' = Compares UV.txt to known HEX values. * 'Convert' = Makes decimal equivalent of HEX values. * *****************************************************************************/ #include <stdio.h> #include <math.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <unistd.h>

Page 26: HNR Weather Station

26

#include "WeatherStation.h" //--Global variables for loops and usleep constants--------------------------- int a; int b; int c; int d; /*============================================================================== * Function : Handle * Return : none * Parameters : none * Description : * Compares UV.txt to HEX char array and index holds value. * Calls Convert when all HEX compares are done. *============================================================================== */ void Handle(void) //--Clears all variables before compares are done------------------------- a = 0; b = 0; c = 0; d = 0; //--established standard array/w pointer--------------------------------- char UVband[17]='0','1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f'; //--Initialization of buffer array--------------------------------------- char UVbuffer[9]; FILE *fileHandle = NULL; //--Read UV.txt Hex values------------------------------------------------ if ((fileHandle = fopen("UV.txt", "r")) != NULL) fread(UVbuffer, sizeof(char), 9, fileHandle); fclose(fileHandle); //--Print what's in the UV.txt file to the front screen------------------ do printf("\n %c", UVbuffer[a]); a ++; while(a < 9); //--Clears the a variable to be reused for the compare------------------- a = a*0; //------------------------------------------------------------------------ // do while loop

Page 27: HNR Weather Station

27

// Comparing the frist Hex value in UV.txt to '0' in the char array. //------------------------------------------------------------------------ do //--Compares if valus is equal---------------------------------------- if (UVbuffer[2] == UVband[a]) //--if compare true sleep for a second-------------------------------- sleep(1); //--Handles prompt if nothing was matched----------------------------- else //--moves to the next index in char array------------------------ ++ a; //--Continues the do/while until a match is found------------------------ while(UVbuffer[2] != UVband[a]); //----------------------------------------------------------------------- // do while loop // Comparing the second Hex value in UV.txt to '0' in the char array. //------------------------------------------------------------------------ do //--Compares if valus is equal---------------------------------------- if (UVbuffer[3] == UVband[b]) //--if compare true sleep for a second-------------------------------- sleep(1); //--Handles prompt if nothing was matched------------------------------ else //--moves to the next index in char array------------------------- ++ b; //--Continues the do/while until a match is found------------------------ while(UVbuffer[3] != UVband[b]); //------------------------------------------------------------------------ // do while loop // Comparing the third Hex value in UV.txt to '0' in the char array. //------------------------------------------------------------------------ do //--Compares if valus is equal---------------------------------------- if (UVbuffer[7] == UVband[c]) //--if compare true sleep for a second-------------------------------- sleep(1); //--Handles prompt if nothing was matched------------------------------- else

Page 28: HNR Weather Station

28

//--moves to the next index in char array------------------------ ++ c; //--Continues the do/while until a match is found------------------------ while(UVbuffer[7] != UVband[c]); //------------------------------------------------------------------------ // do while loop // Comparing the fourth Hex value in UV.txt to '0' in the char array. //------------------------------------------------------------------------ do //--Compares if valus is equal---------------------------------------- if (UVbuffer[8] == UVband[d]) //--if compare true sleep for a second-------------------------------- sleep(1); //--Handles prompt if nothing was matched------------------------------- else //--moves to the next index in char array------------------------ ++ d; //--Continues the do/while until a match is found------------------------ while(UVbuffer[8] != UVband[d]); /*============================================================================== * Function : Convert * Return : none * Parameters : none * Description : * Called from Handle takes the global variables and adds them * together to get the decimal equivalent of those HEX values. Then * does the UV conversion turning the decimal into a float that * holds the UV index number. *============================================================================== */ float Convert() printf("\n you are in convert"); //--Local variables to hold float UV index------------------------------- float e = 0; //--Multiplying the Global variables by there decimal equivalent--------- a = a*256; b = b*16; //--Turns the decimal number into a float-------------------------------- e = (a + b + c)/1000.00; //--Prints float to the front screen------------------------------------- printf("\n %f", e); return e;

Page 29: HNR Weather Station

29

SI1145main.c /****************************************************************************** * Name : SI1145main.c * Author : Harshpreet Saini, Norman Sims, Rahu M. Bannister * Version : 1.0 * Copyright : 2015 * Description : * Controls all system commands and I2C setup for communicating to * the UV sensor. This code is also responsible for redirecting the * UV reading output into the UV.txt file for future comparison: * * 'main' = Sets all parameters for the I2C communication between * the UV board and the BBB. * 'reset' = Resets all registers and parameters in the UV board. * 'readUV' = Calls Handle and Convert for processing of the UV * reading. * *****************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <unistd.h> #include "WeatherStation.h" /*============================================================================== * Function : main * Return : none * Parameters : none * Description : * Sets up all I2C commands, UV board registers and parameters for * communication. *============================================================================== */ void initSi1145() //--I2C command to Initialize UV board-------------------------------------- system("i2cset -y 1 0x60 0x07 0x17"); usleep(100); //--I2C commands for setting proper coefficents for UV reading-------------- system("i2cset -y 1 0x60 0x13 0x29"); system("i2cset -y 1 0x60 0x14 0x89"); system("i2cset -y 1 0x60 0x15 0x02"); system("i2cset -y 1 0x60 0x16 0x00"); //--I2C command for setting enabling the UV reader------------------------- system("i2cset -y 1 0x60 0x17 0xf0"); system("i2cset -y 1 0x60 0x18 0xa1"); //--I2C command for UV enable confirmation--------------------------------- system("i2cget -y 1 0x60 0x2e"); //--Calls for a UV reading and reset when doone----------------------------

Page 30: HNR Weather Station

30

/*============================================================================== * Function : reset * Return : none * Parameters : none * Description : * Resets all prior set parameters and registers in case of error. *============================================================================== */ void reset(void) //--I2C comand for resetting all set registers and parameters--------------- system("i2cset -y 1 0x60 0x18 0x01"); /*============================================================================== * Function : readUV * Return : none * Parameters : none * Description : * Makes conversion in UV board pulls it out and populates UV.txt. * Also calls proper functions for hadling the UV.txt compares. *============================================================================== */ float readUV(void) //--I2C command for taking a forced reading and redirecting the output------ system("i2cset -y 1 0x60 0x18 0x07"); system("i2cset -y 1 0x60 0x18 0x12"); system("i2cget -y 1 0x60 0x2D > UV.txt"); system("i2cget -y 1 0x60 0x2C >> UV.txt"); //--Calls Handle and Convert for file manipulation------------------------- float indexValue; Handle(); indexValue=Convert(); return indexValue;

SerialLCD.c /****************************************************************************** * Name : SerialLCD.c * Author : Harshpreet Saini, Norman Sims, Rahu M. Bannister * Version : 1.0 * Copyright : 2015 * Description : * This file contains the functions necessary to use the * SparkFun 3.3V Serial Enabled 16x2 LCD. * *****************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "WeatherStation.h"

Page 31: HNR Weather Station

31

#define LCD_UART4_FILE "/dev/ttyO4" /*============================================================================== * Function : writeToScreen * Return : none * Parameters : String of chars * Description : * Writes the input string to the LCD screen. *============================================================================== */ void writeToScreen(char * buffer) FILE * filePtr; filePtr = fopen (LCD_UART4_FILE ,"r+"); //r+:Open a file for reading&write fputs (buffer,filePtr); fclose (filePtr); /*============================================================================== * Function : clearScreen * Return : none * Parameters : none * Description : * Sends the clear command (xFE followed by x01) to the LCD. *============================================================================== */ void clearScreen() char clearScreen[] = "\xFE\x01"; FILE * filePtr; filePtr = fopen (LCD_UART4_FILE ,"r+"); //r+:Open a file for reading&write fputs (clearScreen,filePtr); fclose (filePtr); /*============================================================================== * Function : moveCursor * Return : none * Parameters : String of chars: The cursor position in hex to move to. * Description : * Concatenates "xFE with the input char string. This will give * the LCD the command to move the cursor and the position to * move the cursor to. *============================================================================== */ void moveCursor(char * position) char moveCursor[7] = "\xFE"; strcat(moveCursor, position); //concatenates:(dest,src) FILE * filePtr; filePtr = fopen (LCD_UART4_FILE ,"r+"); //r+:Open a file for reading&write fputs (moveCursor,filePtr); fclose (filePtr);

Page 32: HNR Weather Station

32

ManualMode.c #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include "WeatherStation.h" /****************************************************************************** * Name : ManualMode.c * Author : Harshpreet Saini, Norman Sims, Rahu M. Bannister * Version : 1.0 * Copyright : 2015 * Description : * This code handles the running of the system while in manual * mode. *****************************************************************************/ int modeSwitchState; int increment = 0; char manualModeString [] = " Manual Mode "; char tempString [] = " Current Temp: "; char tempBuffer[7]; char tempPrintPosition [] = "\xC5"; char baroString [] = " Baro. Pressure:"; char baroPrintPosition [] = "\xC3"; char pressureBuffer[11]; /*============================================================================== * Function : ManualModeCheck * Return : none * Parameters : none * Description : * Checks if the Manual Modee Switch has been activated. If the * Manual Mode Switch has been activated it checks whether the * scroll switch has been pressed. If the Scroll Button has been * pressed it checks what the current increment is and whether the * Select Button has been pressed. The increment is used to * determine what to write to the screen. If the Select Button is * pressed and the increment is at one, the program will write the * current temperature value to the screen. If the increment is at * two, the program will write the current barometric pressure to * the screen. *============================================================================== */ void ManualModeCheck() //--Creates 2 sec delay and checks switch state--------------------------------- for (int index = 0; index <200; index++) modeSwitchState=isModeSwitchActive(); if (modeSwitchState==1) // if active clearScreen(); writeToScreen(manualModeString); sleep(2); clearScreen();

Page 33: HNR Weather Station

33

while (isModeSwitchActive() ==1) if (isScrollButtonActive() ==1) increment ++; if (increment==1) clearScreen(); writeToScreen(" Temperature "); while (increment ==1) //--checks state of the Select Button, Scrolll Button, and the Mode Switch------ if (isSelectButtonActive()==1) clearScreen(); writeToScreen(tempString); strcpy(tempBuffer, getTemp()); moveCursor(tempPrintPosition); writeToScreen(tempBuffer); break; if (isScrollButtonActive() ==1) increment ++; break; if (isModeSwitchActive() == 0) //Not active break; if (increment==2) clearScreen(); writeToScreen(" Pressure "); while (increment ==2) //--checks state of the Select Button, Scrolll Button, and the Mode Switch------ if (isSelectButtonActive()==1) clearScreen(); writeToScreen(baroString); strcpy(pressureBuffer, getPressure()); moveCursor(baroPrintPosition); writeToScreen(pressureBuffer); break; if (isScrollButtonActive() ==1) increment ++; break;

Page 34: HNR Weather Station

34

if (isModeSwitchActive() == 0) //Not active break; if (increment ==3) increment =0; increment =0; break; //--10 ms delay----------------------------------------------------------------- usleep(10000);

LED_Switches.c /****************************************************************************** * Name : LED_Switches.c * Author : Harshpreet Saini, Norman Sims, Rahu M. Bannister * Version : 1.0 * Copyright : 2015 * Description : * This file contains the functions to initailize the LEDs and * switches. In addition, this file contains the functions to check * the state of the switches and reset the LEDs to off. * *****************************************************************************/ #include <stdio.h> #include "WeatherStation.h" #define MODE_SWITCH_DIRECTION_FILE "/sys/class/gpio/gpio26/direction" #define MODE_SWITCH_VALUE_FILE "/sys/class/gpio/gpio26/value" #define SCROLL_BUTTON_DIRECTION_FILE "/sys/class/gpio/gpio44/direction" #define SCROLL_BUTTON_VALUE_FILE "/sys/class/gpio/gpio44/value" #define SELECT_BUTTON_DIRECTION_FILE "/sys/class/gpio/gpio23/direction" #define SELECT_BUTTON_VALUE_FILE "/sys/class/gpio/gpio23/value" #define RED_LED_DIRECTION_FILE "/sys/class/gpio/gpio68/direction" #define GREEN_LED_DIRECTION_FILE "/sys/class/gpio/gpio66/direction" #define BLUE_LED_DIRECTION_FILE "/sys/class/gpio/gpio45/direction" #define YELLOW_LED_DIRECTION_FILE "/sys/class/gpio/gpio67/direction" #define ORANGE_LED_DIRECTION_FILE "/sys/class/gpio/gpio69/direction" /*============================================================================== * Function : initSwitchLed

Page 35: HNR Weather Station

35

* Return : none * Parameters : none * Description : * Initialize switch hardware and set default led state. *============================================================================== */ void initSwitchLed(void) //--place files paths within arrays to enable indexing through LEDs------- const char *ledFiles[] = RED_LED_DIRECTION_FILE, GREEN_LED_DIRECTION_FILE, BLUE_LED_DIRECTION_FILE, YELLOW_LED_DIRECTION_FILE, ORANGE_LED_DIRECTION_FILE; FILE *fileHandle = NULL; int i; //--set initial LED state to off and port direction as output------------- for (i=0; i<5; i++) if ((fileHandle = fopen(ledFiles[i], "r+")) != NULL) fwrite("low", sizeof(char), 3, fileHandle); fclose(fileHandle); //--set switch port direction---------------------------------------------- if ((fileHandle = fopen(MODE_SWITCH_DIRECTION_FILE, "r+")) != NULL) fwrite("in", sizeof(char), 2, fileHandle); fclose(fileHandle); //--set button port direction---------------------------------------------- if ((fileHandle = fopen(SCROLL_BUTTON_DIRECTION_FILE, "r+")) != NULL) fwrite("in", sizeof(char), 2, fileHandle); fclose(fileHandle); //--set button port direction---------------------------------------------- if ((fileHandle = fopen(SELECT_BUTTON_DIRECTION_FILE, "r+")) != NULL) fwrite("in", sizeof(char), 2, fileHandle); fclose(fileHandle); /*============================================================================== * Function : resetLEDs * Return : none * Parameters : none * Description : * Turns off all of the LEDs. *============================================================================== */

Page 36: HNR Weather Station

36

void resetLEDS() //--place files paths within arrays to enable indexing through LEDs------- const char *ledFiles[] = RED_LED_DIRECTION_FILE, GREEN_LED_DIRECTION_FILE, BLUE_LED_DIRECTION_FILE, YELLOW_LED_DIRECTION_FILE, ORANGE_LED_DIRECTION_FILE; FILE *fileHandle = NULL; int i; //--set initial LED state to off and port direction as output------------- for (i=0; i<5; i++) if ((fileHandle = fopen(ledFiles[i], "r+")) != NULL) fwrite("low", sizeof(char), 3, fileHandle); fclose(fileHandle); /*============================================================================== * Function : setLed * Return : none * Parameters : led = Color * theState = SWITCHLED_ON, SWITCHLED_OFF * Description : * Change state of LED. *============================================================================== */ void setLed(char led, int theState) char *ledFile; FILE *fileHandle = NULL; switch(led) case 'r': ledFile = RED_LED_DIRECTION_FILE; break; case 'g': ledFile = GREEN_LED_DIRECTION_FILE; break; case 'b': ledFile = BLUE_LED_DIRECTION_FILE; break; case 'y': ledFile = YELLOW_LED_DIRECTION_FILE; break; case 'o': ledFile = ORANGE_LED_DIRECTION_FILE; break; if (theState == LED_ON) //--turn LED on-------------------------------------------------------

Page 37: HNR Weather Station

37

if ((fileHandle = fopen(ledFile, "r+")) != NULL) fwrite("high", sizeof(char), 4, fileHandle); fclose(fileHandle); else //--turn LED off------------------------------------------------------ if ((fileHandle = fopen(ledFile, "r+")) != NULL) fwrite("low", sizeof(char), 3, fileHandle); fclose(fileHandle); /*============================================================================== * Function : isModeSwitchActive * Return : int 0 if not active, 1 if active * Parameters : none * Description : * Test switch state then return. *============================================================================== */ int isModeSwitchActive(void) FILE *fileHandle = NULL; char buffer[2]; //--Read switch value----------------------------------------------------- if ((fileHandle = fopen(MODE_SWITCH_VALUE_FILE, "r+")) != NULL) fread(buffer, sizeof(char), 1, fileHandle); fclose(fileHandle); //--return appropriate value based upon switch position------------------- if (buffer[0]=='0') return 1; // Button is active. else return 0; // Button is not active. /*============================================================================== * Function : isScrollButtonActive * Return : int 0 if not active, 1 if active * Parameters : none * Description : * Test switch state then return. *============================================================================== */ int isScrollButtonActive(void)

Page 38: HNR Weather Station

38

FILE *fileHandle = NULL; char buffer[2]; //--Read switch value----------------------------------------------------- if ((fileHandle = fopen(SCROLL_BUTTON_VALUE_FILE, "r+")) != NULL) fread(buffer, sizeof(char), 1, fileHandle); fclose(fileHandle); //--return appropriate value based upon switch position------------------- if (buffer[0]=='0') return 1; // Button is active. else return 0; // Button is not active. /*============================================================================== * Function : isSelectButtonActive * Return : int 0 if not active, 1 if active * Parameters : none * Description : * Test switch to state then return. *============================================================================== */ int isSelectButtonActive(void) FILE *fileHandle = NULL; char buffer[2]; //--Read switch value----------------------------------------------------- if ((fileHandle = fopen(SELECT_BUTTON_VALUE_FILE, "r+")) != NULL) fread(buffer, sizeof(char), 1, fileHandle); fclose(fileHandle); //--return appropriate value based upon switch position------------------- if (buffer[0]=='0') return 1; // Button is active. else return 0; // Button is not active.

Page 39: HNR Weather Station

39

BMP180Temp-Pressure.c /****************************************************************************** * Name : BMP180Temp-Pressure.c * Author : Harshpreet Saini, Norman Sims, Rahu M. Bannister * Version : 1.0 * Copyright : 2015 * Description : * This file contains the functions necessary to use the * bmp180 temperature/barometric Pressure sensor. * *****************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "WeatherStation.h" #define TEMPERATURE_FILE "/sys/bus/i2c/drivers/bmp085/1-0077/temp0_input" #define PRESSURE_FILE "/sys/bus/i2c/drivers/bmp085/1-0077/pressure0_input" /*============================================================================== * Function : getTemp * Return : temperatureF: static char array * Parameters : none * Description : * Reads the temperature value from the BMP180 as celsius, then * converts it to fahrenheit. *============================================================================== */ char * getTemp() char buffer[4]; FILE * filePtr; float result; float celsiusValue; float fahrenheitValue; static char temperatureF[7]; filePtr = fopen (TEMPERATURE_FILE ,"r"); //r:Open a file for reading fread(buffer, sizeof(char), 4, filePtr); fclose (filePtr); result = atof(buffer); celsiusValue= (result /10); fahrenheitValue=((celsiusValue*9)/5)+32; //celsius to farenheit conversion sprintf(temperatureF, "%.1f",fahrenheitValue); //float to string conversion strcat(temperatureF, " F"); //append fahrenheit units return temperatureF; /*============================================================================== * Function : getPressure * Return : pressureInHG: static char array * Parameters : none * Description : * Reads the barometric pressure value in as Pascal from the BMP180 * then converts it to inches of mercury. *============================================================================== */

Page 40: HNR Weather Station

40

char * getPressure() char buffer[6]; FILE * filePtr; float result; //units = Pascal float hPaValue; //units = hectopascal float pressureValue; // units = inches of mercury float conversionNum = 0.0295301; static char pressureInHG[11]; filePtr = fopen (PRESSURE_FILE,"r"); //r:Open a file for reading fread(buffer, sizeof(char), 6, filePtr); fclose (filePtr); result = atof(buffer); //string to float conversion hPaValue = (result /100); //Pascal to hectopascal pressureValue= hPaValue*conversionNum; //conversion to inches of mercury sprintf(pressureInHG, "%.2f",pressureValue); //float to string conversion strcat(pressureInHG, " inHg"); //append inches of mercury units return pressureInHG;