Weather Station using an mBed microcontroller and a Windows CE Device

Dependencies:   TextLCD mbed HMC6352

Committer:
mafischl
Date:
Thu Dec 15 13:38:51 2011 +0000
Revision:
0:75e6a2e50519

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mafischl 0:75e6a2e50519 1 #include "mbed.h"
mafischl 0:75e6a2e50519 2 #include "GPS.h"
mafischl 0:75e6a2e50519 3 #include "MODSERIAL.h"
mafischl 0:75e6a2e50519 4 #include "TextLCD.h"
mafischl 0:75e6a2e50519 5 #include "HMC6352.h"
mafischl 0:75e6a2e50519 6 #define MESSAGE_BUFFER_SIZE 2048
mafischl 0:75e6a2e50519 7 #define REVID 0x00 //ASIC Revision Number
mafischl 0:75e6a2e50519 8 #define OPSTATUS 0x04 //Operation Status
mafischl 0:75e6a2e50519 9 #define STATUS 0x07 //ASIC Status
mafischl 0:75e6a2e50519 10 #define START 0x0A //Constant Readings
mafischl 0:75e6a2e50519 11 #define PRESSURE 0x1F //Pressure 3 MSB
mafischl 0:75e6a2e50519 12 #define PRESSURE_LSB 0x20 //Pressure 16 LSB
mafischl 0:75e6a2e50519 13 #define TEMP 0x21 //16 bit temp
mafischl 0:75e6a2e50519 14
mafischl 0:75e6a2e50519 15
mafischl 0:75e6a2e50519 16 extern "C" void mbed_reset(); // Allows for software resets
mafischl 0:75e6a2e50519 17
mafischl 0:75e6a2e50519 18 MODSERIAL serialIO(p28,p27); // RS-232 to PC (tx, rx)
mafischl 0:75e6a2e50519 19 GPS gps(p13,p14); // RS-232 to GPS (tx, rx)
mafischl 0:75e6a2e50519 20 TextLCD lcd(p23, p21, p22, p11, p12, p8); // rs, e, d4-d7
mafischl 0:75e6a2e50519 21 HMC6352 compass(p9, p10); // I2C (sda, scl)
mafischl 0:75e6a2e50519 22 SPI spi(p5, p6, p7); // mosi, miso, sclk
mafischl 0:75e6a2e50519 23 DigitalOut cs(p25); // chip select
mafischl 0:75e6a2e50519 24 DigitalOut drdy(p26); // DRDY pin
mafischl 0:75e6a2e50519 25 AnalogIn humidity(p17); // Humidity Analog from Phidgets
mafischl 0:75e6a2e50519 26 AnalogIn temperature2(p20); // Temperature Analog from Phidgets
mafischl 0:75e6a2e50519 27 AnalogIn light(p15); // Light Analog Sensor
mafischl 0:75e6a2e50519 28 InterruptIn windSpeed(p18); // Anemometer for Wind Speed
mafischl 0:75e6a2e50519 29 AnalogIn windDirection(p19); // Wind Direction
mafischl 0:75e6a2e50519 30 InterruptIn rainGauge(p16); // Rain Gauge Click
mafischl 0:75e6a2e50519 31 DigitalOut wdtLED(LED4); // WatchDog Indicator
mafischl 0:75e6a2e50519 32 DigitalOut heartBeat(LED1); // Heartbeat
mafischl 0:75e6a2e50519 33 DigitalOut led2(LED2); // Test for message process
mafischl 0:75e6a2e50519 34 char messageBufferIncoming[MESSAGE_BUFFER_SIZE];
mafischl 0:75e6a2e50519 35 char messageBufferOutgoing[MESSAGE_BUFFER_SIZE];
mafischl 0:75e6a2e50519 36 LocalFileSystem local("local");
mafischl 0:75e6a2e50519 37 Timer t;
mafischl 0:75e6a2e50519 38 int timeCount;
mafischl 0:75e6a2e50519 39 int state;
mafischl 0:75e6a2e50519 40 int error;
mafischl 0:75e6a2e50519 41 int progressBarCount = 0;
mafischl 0:75e6a2e50519 42 int pressureDirection = 0;
mafischl 0:75e6a2e50519 43 int rainLevel = 0; // Rain Level (3 = heavy rain, 2 = moderate rain, 1 = light rain, 0 = no rain)
mafischl 0:75e6a2e50519 44 int rainInterval = 780; // Start at high count, as soon as the rain gauge clicks once, the level is based on the the rain interval.
mafischl 0:75e6a2e50519 45 int rainCount = 0;
mafischl 0:75e6a2e50519 46 int speedCount = 0;
mafischl 0:75e6a2e50519 47 int lightLevel = 0; // light level (3 = sunny, 2 = partly cloudy, 1 = mostly cloudy, 0 = night)
mafischl 0:75e6a2e50519 48 float lightCount = 0.0;
mafischl 0:75e6a2e50519 49 float lightValue;
mafischl 0:75e6a2e50519 50 int lightTimeCount = 1;
mafischl 0:75e6a2e50519 51 bool GPSlock = 0;
mafischl 0:75e6a2e50519 52 float prevLatitude = 0.0;
mafischl 0:75e6a2e50519 53 float prevLongitude = 0.0;
mafischl 0:75e6a2e50519 54 float prevPressure = 0.0;
mafischl 0:75e6a2e50519 55 float getHumidity;
mafischl 0:75e6a2e50519 56 float w_direction = 0.0;
mafischl 0:75e6a2e50519 57 float wSpeed = 0.0;
mafischl 0:75e6a2e50519 58 float getTemperature = 0.0;
mafischl 0:75e6a2e50519 59 float rainAmount = 0.0;
mafischl 0:75e6a2e50519 60 float temp_in;
mafischl 0:75e6a2e50519 61 unsigned long pressure_lsb;
mafischl 0:75e6a2e50519 62 unsigned long pressure_msb;
mafischl 0:75e6a2e50519 63 unsigned long temp_pressure;
mafischl 0:75e6a2e50519 64 unsigned long pressure;
mafischl 0:75e6a2e50519 65 char forecastOutput[14]; // Forecast Output String
mafischl 0:75e6a2e50519 66 char outputString[161]; // Output string for getWeather()
mafischl 0:75e6a2e50519 67
mafischl 0:75e6a2e50519 68
mafischl 0:75e6a2e50519 69 // WATCHDOG FUNCTION
mafischl 0:75e6a2e50519 70
mafischl 0:75e6a2e50519 71 class Watchdog {
mafischl 0:75e6a2e50519 72 public:
mafischl 0:75e6a2e50519 73 // Load timeout value in watchdog timer and enable
mafischl 0:75e6a2e50519 74 void kick(float s) {
mafischl 0:75e6a2e50519 75 LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK
mafischl 0:75e6a2e50519 76 uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4
mafischl 0:75e6a2e50519 77 LPC_WDT->WDTC = s * (float)clk;
mafischl 0:75e6a2e50519 78 LPC_WDT->WDMOD = 0x3; // Enabled and Reset
mafischl 0:75e6a2e50519 79 kick();
mafischl 0:75e6a2e50519 80 }
mafischl 0:75e6a2e50519 81 // "kick" or "feed" the dog - reset the watchdog timer
mafischl 0:75e6a2e50519 82 // by writing this required bit pattern
mafischl 0:75e6a2e50519 83 void kick() {
mafischl 0:75e6a2e50519 84 LPC_WDT->WDFEED = 0xAA;
mafischl 0:75e6a2e50519 85 LPC_WDT->WDFEED = 0x55;
mafischl 0:75e6a2e50519 86 }
mafischl 0:75e6a2e50519 87 };
mafischl 0:75e6a2e50519 88
mafischl 0:75e6a2e50519 89 Watchdog wdt;
mafischl 0:75e6a2e50519 90
mafischl 0:75e6a2e50519 91 const float tbl_windvane[16][2] = {
mafischl 0:75e6a2e50519 92 {0.0, 33000}, {22.5, 6570}, {45.0, 8200}, {67.5, 891},
mafischl 0:75e6a2e50519 93 {90.0, 1000}, {112.5, 688}, {135.0, 2200}, {157.5, 1410},
mafischl 0:75e6a2e50519 94 {180.0, 3900}, {202.5, 3140}, {225.0, 16000}, {247.5, 14120},
mafischl 0:75e6a2e50519 95 {270.0, 120000}, {292.5, 42120}, {315.0, 64900}, {337.5, 21880}
mafischl 0:75e6a2e50519 96 };
mafischl 0:75e6a2e50519 97
mafischl 0:75e6a2e50519 98 char read_register(char register_name) {
mafischl 0:75e6a2e50519 99 register_name <<=2;
mafischl 0:75e6a2e50519 100 register_name &= 0xFC;
mafischl 0:75e6a2e50519 101 cs=0; //Select SPI device
mafischl 0:75e6a2e50519 102 spi.write(register_name); //Send register location
mafischl 0:75e6a2e50519 103 char register_value=spi.write(0x00);
mafischl 0:75e6a2e50519 104 cs=1;
mafischl 0:75e6a2e50519 105 return register_value;
mafischl 0:75e6a2e50519 106 }
mafischl 0:75e6a2e50519 107
mafischl 0:75e6a2e50519 108
mafischl 0:75e6a2e50519 109 void write_register(char register_name, char register_value) {
mafischl 0:75e6a2e50519 110 register_name <<= 2;
mafischl 0:75e6a2e50519 111 register_name |= 0x02; //le estamos diciendo que escriba
mafischl 0:75e6a2e50519 112 cs=0; //Select SPI device
mafischl 0:75e6a2e50519 113 spi.write(register_name); //Send register location
mafischl 0:75e6a2e50519 114 spi.write(register_value); //Send value to record into register
mafischl 0:75e6a2e50519 115 cs=1;
mafischl 0:75e6a2e50519 116 }
mafischl 0:75e6a2e50519 117
mafischl 0:75e6a2e50519 118 float read_register16(char register_name) {
mafischl 0:75e6a2e50519 119 register_name <<= 2;
mafischl 0:75e6a2e50519 120 register_name &= 0xFC; //Read command
mafischl 0:75e6a2e50519 121 cs=0; //Select SPI Device
mafischl 0:75e6a2e50519 122 spi.write(register_name); //Write byte to device
mafischl 0:75e6a2e50519 123 int in_byte1 = spi.write(0x00);
mafischl 0:75e6a2e50519 124 int in_byte2 = spi.write(0x00);
mafischl 0:75e6a2e50519 125 cs=1;
mafischl 0:75e6a2e50519 126 float in_word= (in_byte1<<=8) | (in_byte2);
mafischl 0:75e6a2e50519 127 return(in_word);
mafischl 0:75e6a2e50519 128 }
mafischl 0:75e6a2e50519 129
mafischl 0:75e6a2e50519 130 void w_rain() {
mafischl 0:75e6a2e50519 131 rainCount++;
mafischl 0:75e6a2e50519 132 if (rainInterval < 129) {
mafischl 0:75e6a2e50519 133 rainLevel = 3; // heavy rain
mafischl 0:75e6a2e50519 134 } else if ((rainInterval >= 130) && (rainInterval < 390)) {
mafischl 0:75e6a2e50519 135 rainLevel = 2; // moderate rain
mafischl 0:75e6a2e50519 136 } else if ((rainInterval >= 391) && (rainInterval < 780)) {
mafischl 0:75e6a2e50519 137 rainLevel = 1; // light rain
mafischl 0:75e6a2e50519 138 } else {
mafischl 0:75e6a2e50519 139 rainLevel = 0; // no rain
mafischl 0:75e6a2e50519 140 }
mafischl 0:75e6a2e50519 141 rainInterval = 0;
mafischl 0:75e6a2e50519 142 }
mafischl 0:75e6a2e50519 143
mafischl 0:75e6a2e50519 144 void w_speed() {
mafischl 0:75e6a2e50519 145 speedCount++;
mafischl 0:75e6a2e50519 146 }
mafischl 0:75e6a2e50519 147
mafischl 0:75e6a2e50519 148 float wdirection() {
mafischl 0:75e6a2e50519 149 int i;
mafischl 0:75e6a2e50519 150 float v;
mafischl 0:75e6a2e50519 151
mafischl 0:75e6a2e50519 152 v = windDirection * 3.3f; // V
mafischl 0:75e6a2e50519 153 v = v / ((3.3f - v) / 10000.0f); // ohm
mafischl 0:75e6a2e50519 154 for (i = 0; i < 16; i ++) {
mafischl 0:75e6a2e50519 155 if (v > tbl_windvane[i][1] * 0.9 && v < tbl_windvane[i][1] * 1.1) {
mafischl 0:75e6a2e50519 156 return tbl_windvane[i][0];
mafischl 0:75e6a2e50519 157 }
mafischl 0:75e6a2e50519 158 }
mafischl 0:75e6a2e50519 159 return 0;
mafischl 0:75e6a2e50519 160 }
mafischl 0:75e6a2e50519 161
mafischl 0:75e6a2e50519 162
mafischl 0:75e6a2e50519 163 void messageReceive(MODSERIAL_IRQ_INFO *q) {
mafischl 0:75e6a2e50519 164 MODSERIAL *sys = q->serial;
mafischl 0:75e6a2e50519 165 sys->move(messageBufferIncoming, MESSAGE_BUFFER_SIZE);
mafischl 0:75e6a2e50519 166
mafischl 0:75e6a2e50519 167
mafischl 0:75e6a2e50519 168
mafischl 0:75e6a2e50519 169 // Routines for Received Commands
mafischl 0:75e6a2e50519 170
mafischl 0:75e6a2e50519 171 //Testing Routines
mafischl 0:75e6a2e50519 172 if (!strncmp(messageBufferIncoming, "LED2:1", sizeof("LED2:1")-1)) led2 = 1; // For testing
mafischl 0:75e6a2e50519 173 else if (!strncmp(messageBufferIncoming, "LED2:0", sizeof("LED2:0")-1)) led2 = 0; // For testing
mafischl 0:75e6a2e50519 174 else if (!strncmp(messageBufferIncoming, "LED2:2", sizeof("LED2:2")-1)) led2 = !led2; // For testing
mafischl 0:75e6a2e50519 175
mafischl 0:75e6a2e50519 176 // Reset mBed microcontroller
mafischl 0:75e6a2e50519 177 else if (!strncmp(messageBufferIncoming, "reset", sizeof("reset")-1)) mbed_reset(); // Force Reset of mBed microcontroller
mafischl 0:75e6a2e50519 178
mafischl 0:75e6a2e50519 179 // Set State of mbed microcontroller
mafischl 0:75e6a2e50519 180 else if (!strncmp(messageBufferIncoming, "setState(Active)", sizeof("setState(Active)")-1)) {
mafischl 0:75e6a2e50519 181 state = 1; // Set State of Program
mafischl 0:75e6a2e50519 182 serialIO.printf("State set to: Active\r\n");
mafischl 0:75e6a2e50519 183 } else if (!strncmp(messageBufferIncoming, "setState(Paused)", sizeof("setState(Paused)")-1)) {
mafischl 0:75e6a2e50519 184 state = 0; // Set State of Program
mafischl 0:75e6a2e50519 185 serialIO.printf("State set to: Paused\r\n");
mafischl 0:75e6a2e50519 186 }
mafischl 0:75e6a2e50519 187
mafischl 0:75e6a2e50519 188 // Status of mbed microcontroller
mafischl 0:75e6a2e50519 189 else if (!strncmp(messageBufferIncoming, "getStatus()", sizeof("getStatus()")-1)) {
mafischl 0:75e6a2e50519 190 if (state == 0) serialIO.printf("Paused\r\n");
mafischl 0:75e6a2e50519 191 if (state == 1) serialIO.printf("Active\r\n");
mafischl 0:75e6a2e50519 192 // Extra Messages
mafischl 0:75e6a2e50519 193 }
mafischl 0:75e6a2e50519 194
mafischl 0:75e6a2e50519 195 // Error Messages
mafischl 0:75e6a2e50519 196 else if (!strncmp(messageBufferIncoming, "getErrorMsg()", sizeof("getErrorMsg()")-1)) {
mafischl 0:75e6a2e50519 197 if (error == 0) serialIO.printf("No Errors\r\n");
mafischl 0:75e6a2e50519 198 // Extra Messages
mafischl 0:75e6a2e50519 199 }
mafischl 0:75e6a2e50519 200
mafischl 0:75e6a2e50519 201 // GPS Location
mafischl 0:75e6a2e50519 202 else if (!strncmp(messageBufferIncoming, "getLocation()", sizeof("getLocation()")-1)) {
mafischl 0:75e6a2e50519 203 // Get Location
mafischl 0:75e6a2e50519 204 if (state == 1) {
mafischl 0:75e6a2e50519 205 if ((gps.lock)&&(GPSlock)) {
mafischl 0:75e6a2e50519 206 serialIO.printf("Location = %.3f, %.3f\r\n", gps.latitude, gps.longitude);
mafischl 0:75e6a2e50519 207 } else {
mafischl 0:75e6a2e50519 208 serialIO.printf("Location = Not Locked\r\n");
mafischl 0:75e6a2e50519 209 }
mafischl 0:75e6a2e50519 210 } else {
mafischl 0:75e6a2e50519 211 serialIO.printf("Information is currently unavailable, please try again later\r\n");
mafischl 0:75e6a2e50519 212 }
mafischl 0:75e6a2e50519 213 }
mafischl 0:75e6a2e50519 214
mafischl 0:75e6a2e50519 215 // GPS Altitude
mafischl 0:75e6a2e50519 216 else if (!strncmp(messageBufferIncoming, "getAltitude()", sizeof("getAltitude()")-1)) {
mafischl 0:75e6a2e50519 217 // Get Altitude
mafischl 0:75e6a2e50519 218 if (state == 1) {
mafischl 0:75e6a2e50519 219 if ((gps.lock)&&(GPSlock)) {
mafischl 0:75e6a2e50519 220 serialIO.printf("Altitude = %.3f meters (MSL)\r\n", gps.altitude);
mafischl 0:75e6a2e50519 221 } else {
mafischl 0:75e6a2e50519 222 serialIO.printf("Altitude = Not Locked\r\n");
mafischl 0:75e6a2e50519 223 }
mafischl 0:75e6a2e50519 224 } else {
mafischl 0:75e6a2e50519 225 serialIO.printf("Information is currently unavailable, please try again later\r\n");
mafischl 0:75e6a2e50519 226 }
mafischl 0:75e6a2e50519 227 }
mafischl 0:75e6a2e50519 228
mafischl 0:75e6a2e50519 229 // GPS Date
mafischl 0:75e6a2e50519 230 else if (!strncmp(messageBufferIncoming, "getGPSDate()", sizeof("getGPSDate()")-1)) {
mafischl 0:75e6a2e50519 231 // Get Date
mafischl 0:75e6a2e50519 232 if (state == 1) {
mafischl 0:75e6a2e50519 233 if ((gps.lock)&&(GPSlock)) {
mafischl 0:75e6a2e50519 234 serialIO.printf("Date = %f\r\n", gps.GPSdate);
mafischl 0:75e6a2e50519 235 } else {
mafischl 0:75e6a2e50519 236 serialIO.printf("Date = Not Locked\r\n");
mafischl 0:75e6a2e50519 237 }
mafischl 0:75e6a2e50519 238 } else {
mafischl 0:75e6a2e50519 239 serialIO.printf("Information is currently unavailable, please try again later\r\n");
mafischl 0:75e6a2e50519 240 }
mafischl 0:75e6a2e50519 241 }
mafischl 0:75e6a2e50519 242
mafischl 0:75e6a2e50519 243 // GPS Time
mafischl 0:75e6a2e50519 244 else if (!strncmp(messageBufferIncoming, "getGPSTime()", sizeof("getGPSTime()")-1)) {
mafischl 0:75e6a2e50519 245 // Get Time
mafischl 0:75e6a2e50519 246 if (state == 1) {
mafischl 0:75e6a2e50519 247 if ((gps.lock)&&(GPSlock)) {
mafischl 0:75e6a2e50519 248 serialIO.printf("Time = %.3f\r\n", gps.GPStime);
mafischl 0:75e6a2e50519 249 } else {
mafischl 0:75e6a2e50519 250 serialIO.printf("Time = Not Locked\r\n");
mafischl 0:75e6a2e50519 251 }
mafischl 0:75e6a2e50519 252 } else {
mafischl 0:75e6a2e50519 253 serialIO.printf("Information is currently unavailable, please try again later\r\n");
mafischl 0:75e6a2e50519 254 }
mafischl 0:75e6a2e50519 255 }
mafischl 0:75e6a2e50519 256
mafischl 0:75e6a2e50519 257 // GPS Lock
mafischl 0:75e6a2e50519 258 else if (!strncmp(messageBufferIncoming, "getGPSLock()", sizeof("getGPSLock()")-1)) {
mafischl 0:75e6a2e50519 259 // Get Lock
mafischl 0:75e6a2e50519 260 if (state == 1) {
mafischl 0:75e6a2e50519 261 if ((gps.lock)&&(GPSlock)) {
mafischl 0:75e6a2e50519 262 serialIO.printf("Locked\r\n");
mafischl 0:75e6a2e50519 263 } else {
mafischl 0:75e6a2e50519 264 serialIO.printf("Not Locked\r\n");
mafischl 0:75e6a2e50519 265 }
mafischl 0:75e6a2e50519 266 } else {
mafischl 0:75e6a2e50519 267 serialIO.printf("Information is currently unavailable, please try again later\r\n");
mafischl 0:75e6a2e50519 268 }
mafischl 0:75e6a2e50519 269 }
mafischl 0:75e6a2e50519 270
mafischl 0:75e6a2e50519 271 // GPS Direction
mafischl 0:75e6a2e50519 272 else if (!strncmp(messageBufferIncoming, "getDirection()", sizeof("getDirection()")-1)) {
mafischl 0:75e6a2e50519 273 // Get Direction
mafischl 0:75e6a2e50519 274 if (state == 1) {
mafischl 0:75e6a2e50519 275 serialIO.printf("Direction = %.2f degrees\r\n", (compass.sample() / 10.0));
mafischl 0:75e6a2e50519 276 } else {
mafischl 0:75e6a2e50519 277 serialIO.printf("Information is currently unavailable, please try again later\r\n");
mafischl 0:75e6a2e50519 278 }
mafischl 0:75e6a2e50519 279 }
mafischl 0:75e6a2e50519 280
mafischl 0:75e6a2e50519 281 // Temperature
mafischl 0:75e6a2e50519 282 else if (!strncmp(messageBufferIncoming, "getTemperature()", sizeof("getTemperature()")-1)) {
mafischl 0:75e6a2e50519 283 if (state == 1) {
mafischl 0:75e6a2e50519 284 // Get Temperature
mafischl 0:75e6a2e50519 285 drdy = 1;
mafischl 0:75e6a2e50519 286 wait(0.1);
mafischl 0:75e6a2e50519 287 temp_in = read_register16(TEMP);
mafischl 0:75e6a2e50519 288 wait(0.1);
mafischl 0:75e6a2e50519 289 drdy = 0;
mafischl 0:75e6a2e50519 290 wait(0.1);
mafischl 0:75e6a2e50519 291 temp_in = (temp_in*3.3)/5; // Calibration offset
mafischl 0:75e6a2e50519 292 temp_in = ((temp_in*9) / 100) + 32; // Convert to fahrenheit
mafischl 0:75e6a2e50519 293 serialIO.printf("Temperature = %.2f degrees F\r\n", temp_in);
mafischl 0:75e6a2e50519 294 } else {
mafischl 0:75e6a2e50519 295 serialIO.printf("Information is currently unavailable, please try again later\r\n");
mafischl 0:75e6a2e50519 296 }
mafischl 0:75e6a2e50519 297 }
mafischl 0:75e6a2e50519 298
mafischl 0:75e6a2e50519 299 // Humidity
mafischl 0:75e6a2e50519 300 else if (!strncmp(messageBufferIncoming, "getHumidity()", sizeof("getHumidity()")-1)) {
mafischl 0:75e6a2e50519 301 if (state == 1) {
mafischl 0:75e6a2e50519 302 // Get Humidity
mafischl 0:75e6a2e50519 303 getHumidity = humidity;
mafischl 0:75e6a2e50519 304 getHumidity = getHumidity*38.12f;
mafischl 0:75e6a2e50519 305 serialIO.printf("Humidity = %.2f%%\r\n", getHumidity);
mafischl 0:75e6a2e50519 306 } else {
mafischl 0:75e6a2e50519 307 serialIO.printf("Information is currently unavailable, please try again later\r\n");
mafischl 0:75e6a2e50519 308 }
mafischl 0:75e6a2e50519 309 }
mafischl 0:75e6a2e50519 310
mafischl 0:75e6a2e50519 311 // Pressure
mafischl 0:75e6a2e50519 312 else if (!strncmp(messageBufferIncoming, "getPressure()", sizeof("getPressure()")-1)) {
mafischl 0:75e6a2e50519 313 // Get Pressure
mafischl 0:75e6a2e50519 314 if (state == 1) {
mafischl 0:75e6a2e50519 315 drdy = 1;
mafischl 0:75e6a2e50519 316 wait(0.1);
mafischl 0:75e6a2e50519 317 pressure_msb = read_register(PRESSURE);
mafischl 0:75e6a2e50519 318 pressure_msb &= 0x07;
mafischl 0:75e6a2e50519 319 pressure_lsb = read_register16(PRESSURE_LSB);
mafischl 0:75e6a2e50519 320 wait(0.1);
mafischl 0:75e6a2e50519 321 drdy = 0;
mafischl 0:75e6a2e50519 322 wait(0.1);
mafischl 0:75e6a2e50519 323 pressure = ((pressure_msb<<16)| pressure_lsb);
mafischl 0:75e6a2e50519 324 if (pressure != prevPressure) {
mafischl 0:75e6a2e50519 325 if (prevPressure < pressure - 2000) {
mafischl 0:75e6a2e50519 326 pressureDirection = 1; // Pressure is increasing
mafischl 0:75e6a2e50519 327 prevPressure = pressure;
mafischl 0:75e6a2e50519 328 pressure /= 4; // Convert to pascals
mafischl 0:75e6a2e50519 329 pressure /= 3376.85f; // Convert from pascals to inches of mercury (60 deg F)
mafischl 0:75e6a2e50519 330 serialIO.printf("Pressure = %.2u, increasing\r\n", pressure);
mafischl 0:75e6a2e50519 331 } else if (prevPressure > pressure + 2000) {
mafischl 0:75e6a2e50519 332 pressureDirection = -1; // Pressure is decreasing
mafischl 0:75e6a2e50519 333 prevPressure = pressure;
mafischl 0:75e6a2e50519 334 pressure /= 4; // Convert to pascals
mafischl 0:75e6a2e50519 335 pressure /= 3376.85f; // Convert from pascals to inches of mercury (60 deg F)
mafischl 0:75e6a2e50519 336 serialIO.printf("Pressure = %.2u, decreasing\r\n", pressure);
mafischl 0:75e6a2e50519 337 } else {
mafischl 0:75e6a2e50519 338 pressureDirection = 0; // Pressure is relatively unchanged
mafischl 0:75e6a2e50519 339 pressure /= 4; // Convert to pascals
mafischl 0:75e6a2e50519 340 pressure /= 3376.85f; // Convert from pascals to inches of mercury (60 deg F)
mafischl 0:75e6a2e50519 341 serialIO.printf("Pressure = %.2u, no change\r\n", pressure);
mafischl 0:75e6a2e50519 342 }
mafischl 0:75e6a2e50519 343 }
mafischl 0:75e6a2e50519 344 } else {
mafischl 0:75e6a2e50519 345 serialIO.printf("Information is currently unavailable, please try again later\r\n");
mafischl 0:75e6a2e50519 346 }
mafischl 0:75e6a2e50519 347 }
mafischl 0:75e6a2e50519 348
mafischl 0:75e6a2e50519 349 // Light
mafischl 0:75e6a2e50519 350 else if (!strncmp(messageBufferIncoming, "getLight()", sizeof("getLight()")-1)) {
mafischl 0:75e6a2e50519 351 if (state == 1) {
mafischl 0:75e6a2e50519 352 // Get Light
mafischl 0:75e6a2e50519 353 serialIO.printf("Light = %.2f\r\n", lightValue);
mafischl 0:75e6a2e50519 354 } else {
mafischl 0:75e6a2e50519 355 serialIO.printf("Information is currently unavailable, please try again later\r\n");
mafischl 0:75e6a2e50519 356 }
mafischl 0:75e6a2e50519 357 }
mafischl 0:75e6a2e50519 358
mafischl 0:75e6a2e50519 359 // Light Level
mafischl 0:75e6a2e50519 360 else if (!strncmp(messageBufferIncoming, "getLightLevel()", sizeof("getLightLevel()")-1)) {
mafischl 0:75e6a2e50519 361 // Get Light Level
mafischl 0:75e6a2e50519 362 if (state == 1) {
mafischl 0:75e6a2e50519 363 switch (lightLevel) {
mafischl 0:75e6a2e50519 364 case 0:
mafischl 0:75e6a2e50519 365 serialIO.printf("It is Dark Outside.\r\n");
mafischl 0:75e6a2e50519 366 break;
mafischl 0:75e6a2e50519 367 case 1:
mafischl 0:75e6a2e50519 368 serialIO.printf("It is Mostly Cloudy.\r\n");
mafischl 0:75e6a2e50519 369 break;
mafischl 0:75e6a2e50519 370 case 2:
mafischl 0:75e6a2e50519 371 serialIO.printf("It is Partly Cloudy.\r\n");
mafischl 0:75e6a2e50519 372 break;
mafischl 0:75e6a2e50519 373 case 3:
mafischl 0:75e6a2e50519 374 serialIO.printf("It is Mostly Sunny.\r\n");
mafischl 0:75e6a2e50519 375 break;
mafischl 0:75e6a2e50519 376 default:
mafischl 0:75e6a2e50519 377 serialIO.printf("Unknown.\r\n");
mafischl 0:75e6a2e50519 378 }
mafischl 0:75e6a2e50519 379 } else {
mafischl 0:75e6a2e50519 380 serialIO.printf("Information is currently unavailable, please try again later\r\n");
mafischl 0:75e6a2e50519 381 }
mafischl 0:75e6a2e50519 382 }
mafischl 0:75e6a2e50519 383
mafischl 0:75e6a2e50519 384 // Wind Direction
mafischl 0:75e6a2e50519 385 else if (!strncmp(messageBufferIncoming, "getWindDirection()", sizeof("getWindDirection()")-1)) {
mafischl 0:75e6a2e50519 386 // Get Wind Direction
mafischl 0:75e6a2e50519 387 if (state == 1) {
mafischl 0:75e6a2e50519 388 w_direction = wdirection();
mafischl 0:75e6a2e50519 389 w_direction = w_direction + (compass.sample() / 10.0); // Offset that the unit is no aligned by using compass
mafischl 0:75e6a2e50519 390 if (w_direction > 360) w_direction = w_direction - 360;
mafischl 0:75e6a2e50519 391 // Convert to cardinal direction
mafischl 0:75e6a2e50519 392 if (w_direction < 22.5) serialIO.printf("Wind Direction = N\r\n");
mafischl 0:75e6a2e50519 393 else if ((w_direction >= 22.5) && (w_direction < 67.5)) serialIO.printf("Wind Direction = NE\r\n");
mafischl 0:75e6a2e50519 394 else if ((w_direction >= 67.5) && (w_direction < 112.5)) serialIO.printf("Wind Direction = E\r\n");
mafischl 0:75e6a2e50519 395 else if ((w_direction >= 112.5) && (w_direction < 157.5)) serialIO.printf("Wind Direction = SE\r\n");
mafischl 0:75e6a2e50519 396 else if ((w_direction >= 157.5) && (w_direction < 202.5)) serialIO.printf("Wind Direction = S\r\n");
mafischl 0:75e6a2e50519 397 else if ((w_direction >= 202.5) && (w_direction < 247.5)) serialIO.printf("Wind Direction = SW\r\n");
mafischl 0:75e6a2e50519 398 else if ((w_direction >= 247.5) && (w_direction < 292.5)) serialIO.printf("Wind Direction = W\r\n");
mafischl 0:75e6a2e50519 399 else if ((w_direction >= 292.5) && (w_direction < 337.5)) serialIO.printf("Wind Direction = NW\r\n");
mafischl 0:75e6a2e50519 400 else serialIO.printf("Wind Direction = N\r\n");
mafischl 0:75e6a2e50519 401
mafischl 0:75e6a2e50519 402 } else {
mafischl 0:75e6a2e50519 403 serialIO.printf("Information is currently unavailable, please try again later\r\n");
mafischl 0:75e6a2e50519 404 }
mafischl 0:75e6a2e50519 405 }
mafischl 0:75e6a2e50519 406
mafischl 0:75e6a2e50519 407 // Wind Speed
mafischl 0:75e6a2e50519 408 else if (!strncmp(messageBufferIncoming, "getWindSpeed()", sizeof("getWindSpeed()")-1)) {
mafischl 0:75e6a2e50519 409 // Get Wind Speed
mafischl 0:75e6a2e50519 410 if (state == 1) {
mafischl 0:75e6a2e50519 411 serialIO.printf("Wind Speed = %.2f mph\r\n", (wSpeed));
mafischl 0:75e6a2e50519 412 } else {
mafischl 0:75e6a2e50519 413 serialIO.printf("Information is currently unavailable, please try again later\r\n");
mafischl 0:75e6a2e50519 414 }
mafischl 0:75e6a2e50519 415 }
mafischl 0:75e6a2e50519 416
mafischl 0:75e6a2e50519 417 // Wind
mafischl 0:75e6a2e50519 418 else if (!strncmp(messageBufferIncoming, "getWind()", sizeof("getWind()")-1)) {
mafischl 0:75e6a2e50519 419 // Get Wind Direction
mafischl 0:75e6a2e50519 420 if (state == 1) {
mafischl 0:75e6a2e50519 421 if (wSpeed < 1) {
mafischl 0:75e6a2e50519 422 serialIO.printf("There is no wind.\r\n");
mafischl 0:75e6a2e50519 423 } else if ((wSpeed >= 1) && (wSpeed < 5)) {
mafischl 0:75e6a2e50519 424 serialIO.printf("There are only calm winds.\r\n");
mafischl 0:75e6a2e50519 425 } else {
mafischl 0:75e6a2e50519 426 w_direction = wdirection();
mafischl 0:75e6a2e50519 427 w_direction = w_direction + (compass.sample() / 10.0); // Offset that the unit is no aligned by using compass
mafischl 0:75e6a2e50519 428 if (w_direction > 360) w_direction = w_direction - 360;
mafischl 0:75e6a2e50519 429 // Convert to cardinal direction
mafischl 0:75e6a2e50519 430 if (w_direction < 22.5) serialIO.printf("Wind traveling N at %.2f mph\r\n", wSpeed);
mafischl 0:75e6a2e50519 431 else if ((w_direction >= 22.5) && (w_direction < 67.5)) serialIO.printf("Wind traveling NE at %.2f mph\r\n", wSpeed);
mafischl 0:75e6a2e50519 432 else if ((w_direction >= 67.5) && (w_direction < 112.5)) serialIO.printf("Wind traveling E at %.2f mph\r\n", wSpeed);
mafischl 0:75e6a2e50519 433 else if ((w_direction >= 112.5) && (w_direction < 157.5)) serialIO.printf("Wind traveling SE at %.2f mph\r\n", wSpeed);
mafischl 0:75e6a2e50519 434 else if ((w_direction >= 157.5) && (w_direction < 202.5)) serialIO.printf("Wind traveling S at %.2f mph\r\n", wSpeed);
mafischl 0:75e6a2e50519 435 else if ((w_direction >= 202.5) && (w_direction < 247.5)) serialIO.printf("Wind traveling SW at %.2f mph\r\n", wSpeed);
mafischl 0:75e6a2e50519 436 else if ((w_direction >= 247.5) && (w_direction < 292.5)) serialIO.printf("Wind traveling W at %.2f mph\r\n", wSpeed);
mafischl 0:75e6a2e50519 437 else if ((w_direction >= 292.5) && (w_direction < 337.5)) serialIO.printf("Wind traveling NW at %.2f mph\r\n", wSpeed);
mafischl 0:75e6a2e50519 438 else serialIO.printf("Wind traveling N at %.2f mph\r\n", wSpeed);
mafischl 0:75e6a2e50519 439 }
mafischl 0:75e6a2e50519 440 } else {
mafischl 0:75e6a2e50519 441 serialIO.printf("Information is currently unavailable, please try again later\r\n");
mafischl 0:75e6a2e50519 442 }
mafischl 0:75e6a2e50519 443 }
mafischl 0:75e6a2e50519 444
mafischl 0:75e6a2e50519 445 // Rain Amount
mafischl 0:75e6a2e50519 446 else if (!strncmp(messageBufferIncoming, "getRainAmount()", sizeof("getRainAmount()")-1)) {
mafischl 0:75e6a2e50519 447 // Get Rain Amount
mafischl 0:75e6a2e50519 448 if (state == 1) {
mafischl 0:75e6a2e50519 449 rainAmount = rainCount * 0.011f;
mafischl 0:75e6a2e50519 450 serialIO.printf("Rain Amount = %f inches today\r\n", (rainAmount));
mafischl 0:75e6a2e50519 451 } else {
mafischl 0:75e6a2e50519 452 serialIO.printf("Information is currently unavailable, please try again later\r\n");
mafischl 0:75e6a2e50519 453 }
mafischl 0:75e6a2e50519 454 }
mafischl 0:75e6a2e50519 455
mafischl 0:75e6a2e50519 456 // Current Weather Command
mafischl 0:75e6a2e50519 457 else if (!strncmp(messageBufferIncoming, "getWeather()", sizeof("getWeather()")-1)) {
mafischl 0:75e6a2e50519 458 // Message Sample: Current Condition is (forecast) at (temperature) degrees (wind condition). Pressure is (pressure) and humidity is (humidity). Observed at (location).
mafischl 0:75e6a2e50519 459 if (state == 1) {
mafischl 0:75e6a2e50519 460 //Predict conditions based on data collected from rain gauge and light sensor.
mafischl 0:75e6a2e50519 461 if (rainLevel == 3) {
mafischl 0:75e6a2e50519 462 if (temp_in >= 37.0) { // Heavy Rain
mafischl 0:75e6a2e50519 463 strcpy(forecastOutput, "heavy rain at ");
mafischl 0:75e6a2e50519 464 } else if ((temp_in < 37.0) && (temp_in > 32.0)) { // Heavy Mix
mafischl 0:75e6a2e50519 465 strcpy(forecastOutput, "heavy rain/snow at ");
mafischl 0:75e6a2e50519 466 } else if (temp_in <= 32.0) { // Heavy Snow
mafischl 0:75e6a2e50519 467 strcpy(forecastOutput, "heavy snow at ");
mafischl 0:75e6a2e50519 468 }
mafischl 0:75e6a2e50519 469 } else if (rainLevel == 2) {
mafischl 0:75e6a2e50519 470 if (temp_in >= 37.0) { // Moderate Rain
mafischl 0:75e6a2e50519 471 strcpy(forecastOutput, "rain showers at ");
mafischl 0:75e6a2e50519 472 } else if ((temp_in < 37.0) && (temp_in > 32.0)) { // Moderate Mix
mafischl 0:75e6a2e50519 473 strcpy(forecastOutput, "rain/snow showers at ");
mafischl 0:75e6a2e50519 474 } else if (temp_in <= 32.0) { // Moderate Snow
mafischl 0:75e6a2e50519 475 strcpy(forecastOutput, "snow showers at ");
mafischl 0:75e6a2e50519 476 }
mafischl 0:75e6a2e50519 477 } else if (rainLevel == 1) {
mafischl 0:75e6a2e50519 478 if (temp_in >= 37.0) { // Light Rain
mafischl 0:75e6a2e50519 479 strcpy(forecastOutput, "light rain at ");
mafischl 0:75e6a2e50519 480 } else if ((temp_in < 37.0) && (temp_in > 32.0)) { // Light Mix
mafischl 0:75e6a2e50519 481 strcpy(forecastOutput, "light rain/snow at ");
mafischl 0:75e6a2e50519 482 } else if (temp_in <= 32.0) { // Light Snow
mafischl 0:75e6a2e50519 483 strcpy(forecastOutput, "snow flurries at ");
mafischl 0:75e6a2e50519 484 }
mafischl 0:75e6a2e50519 485 } else if (rainLevel == 0) { // No Precipitation
mafischl 0:75e6a2e50519 486 if (lightLevel == 0) { // Dark Skies (Nighttime)
mafischl 0:75e6a2e50519 487 strcpy(forecastOutput, "night skies at ");
mafischl 0:75e6a2e50519 488 } else if (lightLevel == 1) { // Mostly Cloudly
mafischl 0:75e6a2e50519 489 strcpy(forecastOutput, "mostly cloudy at ");
mafischl 0:75e6a2e50519 490 } else if (lightLevel == 2) { // Partly Cloudy
mafischl 0:75e6a2e50519 491 strcpy(forecastOutput, "partly cloudy at ");
mafischl 0:75e6a2e50519 492 } else { // Mostly Sunny
mafischl 0:75e6a2e50519 493 strcpy(forecastOutput, "mostly sunny at ");
mafischl 0:75e6a2e50519 494 }
mafischl 0:75e6a2e50519 495 }
mafischl 0:75e6a2e50519 496
mafischl 0:75e6a2e50519 497
mafischl 0:75e6a2e50519 498 // Temperature
mafischl 0:75e6a2e50519 499 drdy = 1;
mafischl 0:75e6a2e50519 500 wait(0.1);
mafischl 0:75e6a2e50519 501 temp_in = read_register16(TEMP);
mafischl 0:75e6a2e50519 502 wait(0.1);
mafischl 0:75e6a2e50519 503 drdy = 0;
mafischl 0:75e6a2e50519 504 wait(0.1);
mafischl 0:75e6a2e50519 505 temp_in = (temp_in*3.3)/5; // Calibration offset
mafischl 0:75e6a2e50519 506 temp_in = ((temp_in*9) / 100) + 32; // Convert to fahrenheit
mafischl 0:75e6a2e50519 507
mafischl 0:75e6a2e50519 508 // Pressure
mafischl 0:75e6a2e50519 509 drdy = 1;
mafischl 0:75e6a2e50519 510 wait(0.1);
mafischl 0:75e6a2e50519 511 pressure_msb = read_register(PRESSURE);
mafischl 0:75e6a2e50519 512 pressure_msb &= 0x07;
mafischl 0:75e6a2e50519 513 pressure_lsb = read_register16(PRESSURE_LSB);
mafischl 0:75e6a2e50519 514 wait(0.1);
mafischl 0:75e6a2e50519 515 drdy = 0;
mafischl 0:75e6a2e50519 516 wait(0.1);
mafischl 0:75e6a2e50519 517 pressure = ((pressure_msb<<16)| pressure_lsb);
mafischl 0:75e6a2e50519 518 pressure /= 4;
mafischl 0:75e6a2e50519 519 pressure /=3376.85f;
mafischl 0:75e6a2e50519 520
mafischl 0:75e6a2e50519 521 // Humidity
mafischl 0:75e6a2e50519 522 getHumidity = humidity;
mafischl 0:75e6a2e50519 523 getHumidity = getHumidity*38.12f;
mafischl 0:75e6a2e50519 524
mafischl 0:75e6a2e50519 525 // Determine Wind Condition
mafischl 0:75e6a2e50519 526
mafischl 0:75e6a2e50519 527 if (wSpeed < 1) { // No Wind
mafischl 0:75e6a2e50519 528 serialIO.printf("Current Condition is %s%d degrees. Pressure is %.2u and humidity is %.2f. Observed at %.3f, %.3f\r\n", forecastOutput, (int)temp_in, pressure, getHumidity, gps.latitude, gps.longitude);
mafischl 0:75e6a2e50519 529 } else if ((wSpeed >= 1) && (wSpeed < 5)) { // Calm Winds
mafischl 0:75e6a2e50519 530 serialIO.printf("Current Condition is %s%d degrees with calm winds. Pressure is %.2u and humidity is %.2f. Observed at %.3f, %.3f\r\n", forecastOutput, (int)temp_in, pressure, getHumidity, gps.latitude, gps.longitude);
mafischl 0:75e6a2e50519 531 } else {
mafischl 0:75e6a2e50519 532 if (w_direction < 22.5) serialIO.printf("Current Condition is %s%d degrees with wind traveling N at %d mph. Pressure is %.2u and humidity is %.2f. Observed at %.3f, %.3f\r\n", forecastOutput, (int)temp_in, (int)wSpeed, pressure, getHumidity, gps.latitude, gps.longitude);
mafischl 0:75e6a2e50519 533 else if ((w_direction >= 22.5) && (w_direction < 67.5)) serialIO.printf("Current Condition is %s%d degrees with wind traveling NE at %d mph. Pressure is %.2u and humidity is %.2f. Observed at %.3f, %.3f\r\n", forecastOutput, (int)temp_in, (int)wSpeed, pressure, getHumidity, gps.latitude, gps.longitude);
mafischl 0:75e6a2e50519 534 else if ((w_direction >= 67.5) && (w_direction < 112.5)) serialIO.printf("Current Condition is %s%d degrees with wind traveling E at %d mph. Pressure is %.2u and humidity is %.2f. Observed at %.3f, %.3f\r\n", forecastOutput, (int)temp_in, (int)wSpeed, pressure, getHumidity, gps.latitude, gps.longitude);
mafischl 0:75e6a2e50519 535 else if ((w_direction >= 112.5) && (w_direction < 157.5)) serialIO.printf("Current Condition is %s%d degrees with wind traveling SE at %d mph. Pressure is %.2u and humidity is %.2f. Observed at %.3f, %.3f\r\n", forecastOutput, (int)temp_in, (int)wSpeed, pressure, getHumidity, gps.latitude, gps.longitude);
mafischl 0:75e6a2e50519 536 else if ((w_direction >= 157.5) && (w_direction < 202.5)) serialIO.printf("Current Condition is %s%d degrees with wind traveling S at %d mph. Pressure is %.2u and humidity is %.2f. Observed at %.3f, %.3f\r\n", forecastOutput, (int)temp_in, (int)wSpeed, pressure, getHumidity, gps.latitude, gps.longitude);
mafischl 0:75e6a2e50519 537 else if ((w_direction >= 202.5) && (w_direction < 247.5)) serialIO.printf("Current Condition is %s%d degrees with wind traveling SW at %d mph. Pressure is %.2u and humidity is %.2f. Observed at %.3f, %.3f\r\n", forecastOutput, (int)temp_in, (int)wSpeed, pressure, getHumidity, gps.latitude, gps.longitude);
mafischl 0:75e6a2e50519 538 else if ((w_direction >= 247.5) && (w_direction < 292.5)) serialIO.printf("Current Condition is %s%d degrees with wind traveling W at %d mph. Pressure is %.2u and humidity is %.2f. Observed at %.3f, %.3f\r\n", forecastOutput, (int)temp_in, (int)wSpeed, pressure, getHumidity, gps.latitude, gps.longitude);
mafischl 0:75e6a2e50519 539 else if ((w_direction >= 292.5) && (w_direction < 337.5)) serialIO.printf("Current Condition is %s%d degrees with wind traveling NW at %d mph. Pressure is %.2u and humidity is %.2f. Observed at %.3f, %.3f\r\n", forecastOutput, (int)temp_in, (int)wSpeed, pressure, getHumidity, gps.latitude, gps.longitude);
mafischl 0:75e6a2e50519 540 else serialIO.printf("Current Condition is %s%d degrees with wind traveling N at %d mph. Pressure is %.2u and humidity is %.2f. Observed at %.3f, %.3f\r\n", forecastOutput, (int)temp_in, (int)wSpeed, pressure, getHumidity, gps.latitude, gps.longitude);
mafischl 0:75e6a2e50519 541 }
mafischl 0:75e6a2e50519 542
mafischl 0:75e6a2e50519 543 } else {
mafischl 0:75e6a2e50519 544 serialIO.printf("Information is currently unavailable, please try again later\r\n");
mafischl 0:75e6a2e50519 545 }
mafischl 0:75e6a2e50519 546 }
mafischl 0:75e6a2e50519 547
mafischl 0:75e6a2e50519 548 // Easter Eggs
mafischl 0:75e6a2e50519 549
mafischl 0:75e6a2e50519 550 // Hi
mafischl 0:75e6a2e50519 551 else if (!strncmp(messageBufferIncoming, "Hi", sizeof("Hi")-1)) {
mafischl 0:75e6a2e50519 552 serialIO.printf("Hello foolish person.\r\n");
mafischl 0:75e6a2e50519 553 }
mafischl 0:75e6a2e50519 554 // Do a barrel roll.
mafischl 0:75e6a2e50519 555 else if (!strncmp(messageBufferIncoming, "Do a barrel roll.", sizeof("Do a barrel roll.")-1)) {
mafischl 0:75e6a2e50519 556 serialIO.printf("Why don't you do a barrel roll?\r\n");
mafischl 0:75e6a2e50519 557 }
mafischl 0:75e6a2e50519 558 // Who created you?
mafischl 0:75e6a2e50519 559 else if (!strncmp(messageBufferIncoming, "Who created you?", sizeof("Who created you?")-1)) {
mafischl 0:75e6a2e50519 560 serialIO.printf("I was created by George P. Burdell.\r\n");
mafischl 0:75e6a2e50519 561 }
mafischl 0:75e6a2e50519 562 // Who are you?
mafischl 0:75e6a2e50519 563 else if (!strncmp(messageBufferIncoming, "Who are you?", sizeof("Who are you?")-1)) {
mafischl 0:75e6a2e50519 564 serialIO.printf("I am the Van Leer Weather Monitoring Station. I am truely a one-of-a-kind piece of technology.\r\n");
mafischl 0:75e6a2e50519 565 }
mafischl 0:75e6a2e50519 566 // Ping
mafischl 0:75e6a2e50519 567 else if (!strncmp(messageBufferIncoming, "Ping", sizeof("Ping")-1)) {
mafischl 0:75e6a2e50519 568 serialIO.printf("I am not a submarine.\r\n");
mafischl 0:75e6a2e50519 569 }
mafischl 0:75e6a2e50519 570 // Find Chuck Norris
mafischl 0:75e6a2e50519 571 else if (!strncmp(messageBufferIncoming, "Find Chuck Norris.", sizeof("Find Chuck Norris.")-1)) {
mafischl 0:75e6a2e50519 572 serialIO.printf("I am sorry but I cannot find Chuck Norris because I know that Chuck Norris cannot be found. Chuck Norris finds you.\r\n");
mafischl 0:75e6a2e50519 573 }
mafischl 0:75e6a2e50519 574 // Tell Me A Joke.
mafischl 0:75e6a2e50519 575 else if (!strncmp(messageBufferIncoming, "Tell me a joke.", sizeof("Tell me a joke.")-1)) {
mafischl 0:75e6a2e50519 576 serialIO.printf("Humans.\r\n");
mafischl 0:75e6a2e50519 577 }
mafischl 0:75e6a2e50519 578 // I need help !
mafischl 0:75e6a2e50519 579 else if (!strncmp(messageBufferIncoming, "I need help!.", sizeof("I need help!")-1)) {
mafischl 0:75e6a2e50519 580 serialIO.printf("I cannot help you, but you can get help from the one they call Ewout.\r\n");
mafischl 0:75e6a2e50519 581 }
mafischl 0:75e6a2e50519 582
mafischl 0:75e6a2e50519 583 // Invalid Command
mafischl 0:75e6a2e50519 584 else serialIO.printf("I am sorry but I do not understand your message. Please try again.\r\n");
mafischl 0:75e6a2e50519 585
mafischl 0:75e6a2e50519 586 serialIO.rxBufferFlush(); //Flush the Buffer
mafischl 0:75e6a2e50519 587 return;
mafischl 0:75e6a2e50519 588 }
mafischl 0:75e6a2e50519 589
mafischl 0:75e6a2e50519 590
mafischl 0:75e6a2e50519 591 int main() {
mafischl 0:75e6a2e50519 592
mafischl 0:75e6a2e50519 593 if ((LPC_WDT->WDMOD >> 2) & 1)
mafischl 0:75e6a2e50519 594 wdtLED = 1;
mafischl 0:75e6a2e50519 595 else wdtLED = 0;
mafischl 0:75e6a2e50519 596
mafischl 0:75e6a2e50519 597 // 30 second timeout on watchdog timer hardware
mafischl 0:75e6a2e50519 598 wdt.kick(10.0);
mafischl 0:75e6a2e50519 599
mafischl 0:75e6a2e50519 600 serialIO.baud(9600);
mafischl 0:75e6a2e50519 601 serialIO.format(8, Serial::None, 1);
mafischl 0:75e6a2e50519 602 serialIO.attach(&messageReceive, MODSERIAL::RxAutoDetect); //Attaches Interrupts
mafischl 0:75e6a2e50519 603 serialIO.autoDetectChar('\n'); //Set Detection to Line Feed
mafischl 0:75e6a2e50519 604
mafischl 0:75e6a2e50519 605 lcd.locate(0,0);
mafischl 0:75e6a2e50519 606 lcd.printf("Acquiring signal");
mafischl 0:75e6a2e50519 607 lcd.locate(0,1);
mafischl 0:75e6a2e50519 608 lcd.printf("from satellite..");
mafischl 0:75e6a2e50519 609
mafischl 0:75e6a2e50519 610 cs=1;
mafischl 0:75e6a2e50519 611 spi.frequency(500000); // the fastest of the sensor
mafischl 0:75e6a2e50519 612 spi.format(8, 0); // Use 8 bits of data
mafischl 0:75e6a2e50519 613 wait(0.5);
mafischl 0:75e6a2e50519 614
mafischl 0:75e6a2e50519 615 write_register(0x06,0x01);
mafischl 0:75e6a2e50519 616 wait(0.5);
mafischl 0:75e6a2e50519 617
mafischl 0:75e6a2e50519 618 write_register(0x03,0x0A);
mafischl 0:75e6a2e50519 619 wait(0.5);
mafischl 0:75e6a2e50519 620
mafischl 0:75e6a2e50519 621
mafischl 0:75e6a2e50519 622 wait(1.0);
mafischl 0:75e6a2e50519 623
mafischl 0:75e6a2e50519 624 //Continuous mode, periodic set/reset, 20Hz measurement rate.
mafischl 0:75e6a2e50519 625 compass.setOpMode(HMC6352_CONTINUOUS, 1, 20);
mafischl 0:75e6a2e50519 626
mafischl 0:75e6a2e50519 627 windSpeed.rise(&w_speed);
mafischl 0:75e6a2e50519 628 rainGauge.rise(&w_rain);
mafischl 0:75e6a2e50519 629
mafischl 0:75e6a2e50519 630 t.start(); // Heartbeat sensor time
mafischl 0:75e6a2e50519 631 state = 1; // Sets state to active
mafischl 0:75e6a2e50519 632
mafischl 0:75e6a2e50519 633 serialIO.printf("systemReady\r\n"); // Send System Ready Message
mafischl 0:75e6a2e50519 634
mafischl 0:75e6a2e50519 635
mafischl 0:75e6a2e50519 636 while (1) {
mafischl 0:75e6a2e50519 637
mafischl 0:75e6a2e50519 638
mafischl 0:75e6a2e50519 639 timeCount = t.read();
mafischl 0:75e6a2e50519 640
mafischl 0:75e6a2e50519 641 if (timeCount > 0) {
mafischl 0:75e6a2e50519 642 if (state == 1) {
mafischl 0:75e6a2e50519 643 // Detect a unusual reading from the GPS Antenna. If consecutive readings are not roughly the same, then it should be assumed that the GPS does not have a confident reading and to signal that it does not have a lock.
mafischl 0:75e6a2e50519 644 if ((((gps.latitude - prevLatitude > 5.0)||(gps.latitude - prevLatitude < -5.0))&&((gps.longitude - prevLongitude > 5.0)||(gps.longitude - prevLongitude < -5.0)))||(gps.numSat < 2)) {
mafischl 0:75e6a2e50519 645 GPSlock = 0;
mafischl 0:75e6a2e50519 646 // lcd.cls();
mafischl 0:75e6a2e50519 647 // lcd.locate(0,0);
mafischl 0:75e6a2e50519 648 // lcd.printf("GPS NOT LOCKED");
mafischl 0:75e6a2e50519 649 } else {
mafischl 0:75e6a2e50519 650 GPSlock = 1;
mafischl 0:75e6a2e50519 651 // lcd.cls();
mafischl 0:75e6a2e50519 652 // lcd.locate(0,0);
mafischl 0:75e6a2e50519 653 // lcd.printf("GPS LOCKED");
mafischl 0:75e6a2e50519 654 }
mafischl 0:75e6a2e50519 655
mafischl 0:75e6a2e50519 656 if (gps.latitude != prevLatitude) prevLatitude = gps.latitude;
mafischl 0:75e6a2e50519 657 if (gps.longitude != prevLongitude) prevLongitude = gps.longitude;
mafischl 0:75e6a2e50519 658
mafischl 0:75e6a2e50519 659 if (gps.sample()&&(gps.lock)&&(GPSlock)) {
mafischl 0:75e6a2e50519 660 lcd.cls();
mafischl 0:75e6a2e50519 661 lcd.locate(0,0);
mafischl 0:75e6a2e50519 662 lcd.printf("LAT: %f", gps.latitude);
mafischl 0:75e6a2e50519 663 lcd.locate(0,1);
mafischl 0:75e6a2e50519 664 lcd.printf("LON: %f", gps.longitude);
mafischl 0:75e6a2e50519 665 progressBarCount = 0;
mafischl 0:75e6a2e50519 666 } else {
mafischl 0:75e6a2e50519 667 lcd.cls();
mafischl 0:75e6a2e50519 668 lcd.locate(0,0);
mafischl 0:75e6a2e50519 669 if (progressBarCount > 19) {
mafischl 0:75e6a2e50519 670 progressBarCount = 0;
mafischl 0:75e6a2e50519 671 }
mafischl 0:75e6a2e50519 672 if (progressBarCount < 3) lcd.printf("Acquiring Signal");
mafischl 0:75e6a2e50519 673 else if (progressBarCount < 6) lcd.printf("from satellite ");
mafischl 0:75e6a2e50519 674 else if (progressBarCount < 8) lcd.printf("Are you inside? ");
mafischl 0:75e6a2e50519 675 else if (progressBarCount < 10) lcd.printf("Any skyscrapers?");
mafischl 0:75e6a2e50519 676 else if (progressBarCount < 13) lcd.printf("Acquiring Signal");
mafischl 0:75e6a2e50519 677 else if (progressBarCount < 16) lcd.printf("from satellite ");
mafischl 0:75e6a2e50519 678 else if (progressBarCount < 18) lcd.printf("Clouds overhead?");
mafischl 0:75e6a2e50519 679 else lcd.printf("Lost satellite? ");
mafischl 0:75e6a2e50519 680 lcd.locate(0,1);
mafischl 0:75e6a2e50519 681 switch (progressBarCount) {
mafischl 0:75e6a2e50519 682 case 0:
mafischl 0:75e6a2e50519 683 lcd.printf(" ");
mafischl 0:75e6a2e50519 684 break;
mafischl 0:75e6a2e50519 685 case 1:
mafischl 0:75e6a2e50519 686 lcd.printf("> ");
mafischl 0:75e6a2e50519 687 break;
mafischl 0:75e6a2e50519 688 case 2:
mafischl 0:75e6a2e50519 689 lcd.printf(">> ");
mafischl 0:75e6a2e50519 690 break;
mafischl 0:75e6a2e50519 691 case 3:
mafischl 0:75e6a2e50519 692 lcd.printf("<>> ");
mafischl 0:75e6a2e50519 693 break;
mafischl 0:75e6a2e50519 694 case 4:
mafischl 0:75e6a2e50519 695 lcd.printf("<<>> ");
mafischl 0:75e6a2e50519 696 break;
mafischl 0:75e6a2e50519 697 case 5:
mafischl 0:75e6a2e50519 698 lcd.printf(" <<>> ");
mafischl 0:75e6a2e50519 699 break;
mafischl 0:75e6a2e50519 700 case 6:
mafischl 0:75e6a2e50519 701 lcd.printf(" <<>> ");
mafischl 0:75e6a2e50519 702 break;
mafischl 0:75e6a2e50519 703 case 7:
mafischl 0:75e6a2e50519 704 lcd.printf(" <<>> ");
mafischl 0:75e6a2e50519 705 break;
mafischl 0:75e6a2e50519 706 case 8:
mafischl 0:75e6a2e50519 707 lcd.printf(" <<>> ");
mafischl 0:75e6a2e50519 708 break;
mafischl 0:75e6a2e50519 709 case 9:
mafischl 0:75e6a2e50519 710 lcd.printf(" <<>> ");
mafischl 0:75e6a2e50519 711 break;
mafischl 0:75e6a2e50519 712 case 10:
mafischl 0:75e6a2e50519 713 lcd.printf(" <<>> ");
mafischl 0:75e6a2e50519 714 break;
mafischl 0:75e6a2e50519 715 case 11:
mafischl 0:75e6a2e50519 716 lcd.printf(" <<>> ");
mafischl 0:75e6a2e50519 717 break;
mafischl 0:75e6a2e50519 718 case 12:
mafischl 0:75e6a2e50519 719 lcd.printf(" <<>> ");
mafischl 0:75e6a2e50519 720 break;
mafischl 0:75e6a2e50519 721 case 13:
mafischl 0:75e6a2e50519 722 lcd.printf(" <<>> ");
mafischl 0:75e6a2e50519 723 break;
mafischl 0:75e6a2e50519 724 case 14:
mafischl 0:75e6a2e50519 725 lcd.printf(" <<>> ");
mafischl 0:75e6a2e50519 726 break;
mafischl 0:75e6a2e50519 727 case 15:
mafischl 0:75e6a2e50519 728 lcd.printf(" <<>> ");
mafischl 0:75e6a2e50519 729 break;
mafischl 0:75e6a2e50519 730 case 16:
mafischl 0:75e6a2e50519 731 lcd.printf(" <<>>");
mafischl 0:75e6a2e50519 732 break;
mafischl 0:75e6a2e50519 733 case 17:
mafischl 0:75e6a2e50519 734 lcd.printf(" <<>");
mafischl 0:75e6a2e50519 735 break;
mafischl 0:75e6a2e50519 736 case 18:
mafischl 0:75e6a2e50519 737 lcd.printf(" <<");
mafischl 0:75e6a2e50519 738 break;
mafischl 0:75e6a2e50519 739 case 19:
mafischl 0:75e6a2e50519 740 lcd.printf(" <");
mafischl 0:75e6a2e50519 741 break;
mafischl 0:75e6a2e50519 742 }
mafischl 0:75e6a2e50519 743 progressBarCount++;
mafischl 0:75e6a2e50519 744
mafischl 0:75e6a2e50519 745 }
mafischl 0:75e6a2e50519 746 // Calculate the Wind Speed
mafischl 0:75e6a2e50519 747 wSpeed = speedCount * 1.492f;
mafischl 0:75e6a2e50519 748 speedCount = 0;
mafischl 0:75e6a2e50519 749
mafischl 0:75e6a2e50519 750 // Create a rolling average over 10 minutes to determine light and rain level
mafischl 0:75e6a2e50519 751 lightValue = light;
mafischl 0:75e6a2e50519 752 if (lightTimeCount < 600) {
mafischl 0:75e6a2e50519 753 lightCount = (((lightCount*lightTimeCount)+lightValue)/(lightTimeCount+1));
mafischl 0:75e6a2e50519 754
mafischl 0:75e6a2e50519 755 lightTimeCount++;
mafischl 0:75e6a2e50519 756 } else {
mafischl 0:75e6a2e50519 757 lightCount = (((lightCount*599)+lightValue)/600);
mafischl 0:75e6a2e50519 758 }
mafischl 0:75e6a2e50519 759 if (lightCount >= 0.7) {
mafischl 0:75e6a2e50519 760 lightLevel = 3;
mafischl 0:75e6a2e50519 761 } else if ((lightCount >= 0.5) && (lightCount < 0.7)) {
mafischl 0:75e6a2e50519 762 lightLevel = 2;
mafischl 0:75e6a2e50519 763 } else if ((lightCount >= 0.3) && (lightCount < 0.5)) {
mafischl 0:75e6a2e50519 764 lightLevel = 1;
mafischl 0:75e6a2e50519 765 } else {
mafischl 0:75e6a2e50519 766 lightLevel = 0;
mafischl 0:75e6a2e50519 767 }
mafischl 0:75e6a2e50519 768
mafischl 0:75e6a2e50519 769 // Increase Rain Interval
mafischl 0:75e6a2e50519 770 rainInterval++;
mafischl 0:75e6a2e50519 771
mafischl 0:75e6a2e50519 772 } else {
mafischl 0:75e6a2e50519 773 lcd.cls();
mafischl 0:75e6a2e50519 774 lcd.locate(0,0);
mafischl 0:75e6a2e50519 775 lcd.printf("PAUSED");
mafischl 0:75e6a2e50519 776 }
mafischl 0:75e6a2e50519 777
mafischl 0:75e6a2e50519 778 heartBeat = !heartBeat;
mafischl 0:75e6a2e50519 779 t.stop();
mafischl 0:75e6a2e50519 780 t.reset();
mafischl 0:75e6a2e50519 781 t.start();
mafischl 0:75e6a2e50519 782 }
mafischl 0:75e6a2e50519 783
mafischl 0:75e6a2e50519 784 wait(0.05);
mafischl 0:75e6a2e50519 785 wdt.kick();
mafischl 0:75e6a2e50519 786 }
mafischl 0:75e6a2e50519 787 return 0;
mafischl 0:75e6a2e50519 788 }