Network, SD card, Serial, LCD and sensors all work! :) ** Don't Press the User Button without an SD Card inserted!! **
Dependencies: BMP280
Fork of Thread_Communication_V2 by
main.cpp@9:b838c5787ed7, 2017-12-30 (annotated)
- Committer:
- dnonoo
- Date:
- Sat Dec 30 15:12:09 2017 +0000
- Revision:
- 9:b838c5787ed7
- Parent:
- 8:ab6322afa341
- Child:
- 10:c10d1337d754
Network, SD card, Serial, LCD and sensors all work! :); ; ** Don't Press the User Button without an SD Card inserted!! **
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benparkes | 0:cb3a5c15b01e | 1 | #include "mbed.h" |
benparkes | 0:cb3a5c15b01e | 2 | #include "main.h" |
dnonoo | 7:f017a37bcf1b | 3 | #include "stdio.h" |
GeorgeJourneaux | 4:93d6d13d4de3 | 4 | |
dnonoo | 8:ab6322afa341 | 5 | #define FallingEdge 0 |
dnonoo | 8:ab6322afa341 | 6 | #define RisingEdge 1 |
dnonoo | 8:ab6322afa341 | 7 | #define USER_BUTTON_PRESSED 1 |
dnonoo | 8:ab6322afa341 | 8 | |
GeorgeJourneaux | 3:73497379c0cb | 9 | LCD lcd(PD_15, PF_12, PF_13, PE_9, PF_14, PF_15); |
GeorgeJourneaux | 3:73497379c0cb | 10 | BMP280 Sensor(D14, D15); |
benparkes | 0:cb3a5c15b01e | 11 | |
GeorgeJourneaux | 5:ea3ec65cbf5f | 12 | //Define Functions |
benparkes | 0:cb3a5c15b01e | 13 | void PrintLCD (); |
GeorgeJourneaux | 2:28d12a3db239 | 14 | void Rx_interrupt(); |
dnonoo | 7:f017a37bcf1b | 15 | void serialCMD(); |
dnonoo | 7:f017a37bcf1b | 16 | void sensorRead(); |
dnonoo | 7:f017a37bcf1b | 17 | void readISR(); |
dnonoo | 7:f017a37bcf1b | 18 | void circBuff(); |
dnonoo | 8:ab6322afa341 | 19 | void writeRemove_SD(); |
dnonoo | 9:b838c5787ed7 | 20 | void Network1(); |
dnonoo | 8:ab6322afa341 | 21 | |
dnonoo | 8:ab6322afa341 | 22 | // USER_BUTTON ISRs (Debounce) |
dnonoo | 8:ab6322afa341 | 23 | |
dnonoo | 8:ab6322afa341 | 24 | void userButtonRise(); |
dnonoo | 8:ab6322afa341 | 25 | void userButtonFall(); |
dnonoo | 8:ab6322afa341 | 26 | void userButtonTimeoutHandler(); |
dnonoo | 8:ab6322afa341 | 27 | |
dnonoo | 8:ab6322afa341 | 28 | // Tickers & Timeouts |
dnonoo | 8:ab6322afa341 | 29 | Timeout userButtonTimeout; // FOR debouncing User Switch |
dnonoo | 8:ab6322afa341 | 30 | Ticker read; // ***Sets sampling period!*** (ISR Signals sampling Thread) |
GeorgeJourneaux | 2:28d12a3db239 | 31 | |
dnonoo | 7:f017a37bcf1b | 32 | /* LOCKS */ |
GeorgeJourneaux | 6:64d346936f0e | 33 | Mutex DataBuffer; |
dnonoo | 8:ab6322afa341 | 34 | Mutex dataLock; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 35 | |
dnonoo | 7:f017a37bcf1b | 36 | /* THREADS */ |
dnonoo | 7:f017a37bcf1b | 37 | Thread _PrintLCD, _serialCMD, _circBuff; |
dnonoo | 8:ab6322afa341 | 38 | Thread _sensorRead (osPriorityRealtime); //dataLock Thread |
dnonoo | 8:ab6322afa341 | 39 | Thread _writeRemove_SD; |
dnonoo | 9:b838c5787ed7 | 40 | Thread _Network1; |
GeorgeJourneaux | 3:73497379c0cb | 41 | |
dnonoo | 7:f017a37bcf1b | 42 | /* GLOBAL DATA */ |
dnonoo | 7:f017a37bcf1b | 43 | volatile float LDR = 0; |
dnonoo | 7:f017a37bcf1b | 44 | volatile double PRES = 0; |
dnonoo | 7:f017a37bcf1b | 45 | volatile double TEMP = 0; |
benparkes | 1:bca9993a0df3 | 46 | |
dnonoo | 8:ab6322afa341 | 47 | // int to hold current switch state |
dnonoo | 8:ab6322afa341 | 48 | int userButtonState = FallingEdge; |
dnonoo | 8:ab6322afa341 | 49 | |
dnonoo | 8:ab6322afa341 | 50 | /* DEBOUNCER INTERRUPTS */ |
dnonoo | 8:ab6322afa341 | 51 | InterruptIn userButton(USER_BUTTON); |
dnonoo | 8:ab6322afa341 | 52 | |
dnonoo | 8:ab6322afa341 | 53 | void userButtonRise () { |
dnonoo | 8:ab6322afa341 | 54 | userButton.rise(NULL); |
dnonoo | 8:ab6322afa341 | 55 | userButtonState = RisingEdge; |
dnonoo | 8:ab6322afa341 | 56 | userButtonTimeout.attach(&userButtonTimeoutHandler, 0.1); |
dnonoo | 8:ab6322afa341 | 57 | } |
benparkes | 0:cb3a5c15b01e | 58 | |
dnonoo | 8:ab6322afa341 | 59 | void userButtonFall () { |
dnonoo | 8:ab6322afa341 | 60 | userButton.fall(NULL); |
dnonoo | 8:ab6322afa341 | 61 | _writeRemove_SD.signal_set(USER_BUTTON_PRESSED); |
dnonoo | 8:ab6322afa341 | 62 | userButtonState = FallingEdge; |
dnonoo | 8:ab6322afa341 | 63 | userButtonTimeout.attach(&userButtonTimeoutHandler, 0.1); |
dnonoo | 8:ab6322afa341 | 64 | } |
dnonoo | 7:f017a37bcf1b | 65 | |
dnonoo | 8:ab6322afa341 | 66 | void userButtonTimeoutHandler() { |
dnonoo | 8:ab6322afa341 | 67 | userButtonTimeout.detach(); |
dnonoo | 8:ab6322afa341 | 68 | |
dnonoo | 8:ab6322afa341 | 69 | switch (userButtonState) { |
dnonoo | 8:ab6322afa341 | 70 | case RisingEdge: |
dnonoo | 8:ab6322afa341 | 71 | userButton.fall(&userButtonFall); |
dnonoo | 8:ab6322afa341 | 72 | break; |
dnonoo | 8:ab6322afa341 | 73 | case FallingEdge: |
dnonoo | 8:ab6322afa341 | 74 | userButton.rise(userButtonRise); |
dnonoo | 8:ab6322afa341 | 75 | break; |
dnonoo | 8:ab6322afa341 | 76 | }// End Switch |
dnonoo | 8:ab6322afa341 | 77 | } //End ISR |
GeorgeJourneaux | 3:73497379c0cb | 78 | /*--------------------------------MAIN--------------------------------*/ |
GeorgeJourneaux | 3:73497379c0cb | 79 | int main() { |
GeorgeJourneaux | 3:73497379c0cb | 80 | |
GeorgeJourneaux | 3:73497379c0cb | 81 | pc.baud(9600); |
GeorgeJourneaux | 3:73497379c0cb | 82 | pc.attach(&Rx_interrupt, Serial::RxIrq); |
dnonoo | 7:f017a37bcf1b | 83 | POST(); |
dnonoo | 7:f017a37bcf1b | 84 | |
dnonoo | 7:f017a37bcf1b | 85 | _serialCMD.start(serialCMD); |
dnonoo | 7:f017a37bcf1b | 86 | _PrintLCD.start(PrintLCD); |
dnonoo | 7:f017a37bcf1b | 87 | _sensorRead.start(sensorRead); |
dnonoo | 7:f017a37bcf1b | 88 | _circBuff.start(circBuff); |
dnonoo | 8:ab6322afa341 | 89 | _writeRemove_SD.start(writeRemove_SD); |
dnonoo | 9:b838c5787ed7 | 90 | _Network1.start(Network1); |
dnonoo | 7:f017a37bcf1b | 91 | |
dnonoo | 8:ab6322afa341 | 92 | userButton.rise(&userButtonRise); |
dnonoo | 7:f017a37bcf1b | 93 | read.attach(readISR, SAMPLING_PERIOD); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 94 | |
dnonoo | 7:f017a37bcf1b | 95 | |
dnonoo | 8:ab6322afa341 | 96 | while (1) { |
dnonoo | 8:ab6322afa341 | 97 | Yellow_ext = ON; |
dnonoo | 8:ab6322afa341 | 98 | Thread::wait (200); |
dnonoo | 8:ab6322afa341 | 99 | Yellow_ext = OFF; |
dnonoo | 8:ab6322afa341 | 100 | Thread::wait(200); |
dnonoo | 8:ab6322afa341 | 101 | }// End While |
dnonoo | 8:ab6322afa341 | 102 | } // End Main |
dnonoo | 7:f017a37bcf1b | 103 | /*--------------------------------------------------------------------*/ |
dnonoo | 7:f017a37bcf1b | 104 | void circBuff () { |
dnonoo | 7:f017a37bcf1b | 105 | |
dnonoo | 7:f017a37bcf1b | 106 | while(1) { |
dnonoo | 7:f017a37bcf1b | 107 | Thread::signal_wait(DATA_READY); // wait for signal from sensorRead |
GeorgeJourneaux | 5:ea3ec65cbf5f | 108 | |
dnonoo | 7:f017a37bcf1b | 109 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 110 | DataBuffer.lock(); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 111 | |
dnonoo | 7:f017a37bcf1b | 112 | //Format samples, send to FIFO buffer head |
GeorgeJourneaux | 6:64d346936f0e | 113 | memset(data_buffer[sample_h],NULL,64); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 114 | time( &raw_time ); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 115 | sample_epoch = localtime( &raw_time ); |
GeorgeJourneaux | 6:64d346936f0e | 116 | char sample_time[20]; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 117 | strftime(sample_time,20,"%d/%m/%Y %X",sample_epoch); |
dnonoo | 7:f017a37bcf1b | 118 | |
dnonoo | 8:ab6322afa341 | 119 | dataLock.lock(); //lock critical section |
dnonoo | 7:f017a37bcf1b | 120 | sprintf(data_buffer[sample_h],"%s, %2.2f, %4.2f, %.4f\n\r", sample_time, TEMP, PRES, LDR); |
dnonoo | 8:ab6322afa341 | 121 | dataLock.unlock(); //unlock critical section |
dnonoo | 7:f017a37bcf1b | 122 | |
GeorgeJourneaux | 5:ea3ec65cbf5f | 123 | memset(sample_time,NULL,20); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 124 | |
dnonoo | 7:f017a37bcf1b | 125 | //Set seperate FIFO head and tail for printing data |
GeorgeJourneaux | 6:64d346936f0e | 126 | data_h = sample_h; |
GeorgeJourneaux | 6:64d346936f0e | 127 | data_t = sample_t; |
GeorgeJourneaux | 6:64d346936f0e | 128 | |
dnonoo | 7:f017a37bcf1b | 129 | //Move sample FIFO buffer head to next row in buffer |
GeorgeJourneaux | 6:64d346936f0e | 130 | sample_h++; |
dnonoo | 7:f017a37bcf1b | 131 | //Check sample FIFO buffer head |
GeorgeJourneaux | 6:64d346936f0e | 132 | if(sample_h >= MAX_SAMPLES){ |
GeorgeJourneaux | 6:64d346936f0e | 133 | sample_h = 0; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 134 | } |
dnonoo | 7:f017a37bcf1b | 135 | //Check sample FIFO buffer tail |
GeorgeJourneaux | 6:64d346936f0e | 136 | if(sample_t == sample_h){ |
GeorgeJourneaux | 6:64d346936f0e | 137 | sample_t++; |
GeorgeJourneaux | 6:64d346936f0e | 138 | if(sample_t >= (MAX_SAMPLES)){ |
GeorgeJourneaux | 6:64d346936f0e | 139 | sample_t = 0; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 140 | } |
GeorgeJourneaux | 5:ea3ec65cbf5f | 141 | } |
dnonoo | 7:f017a37bcf1b | 142 | //Unlock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 143 | DataBuffer.unlock(); |
dnonoo | 7:f017a37bcf1b | 144 | } |
dnonoo | 7:f017a37bcf1b | 145 | } |
dnonoo | 7:f017a37bcf1b | 146 | |
dnonoo | 7:f017a37bcf1b | 147 | /*---------------------Read Sensors ---------------------------*/ |
dnonoo | 7:f017a37bcf1b | 148 | |
dnonoo | 7:f017a37bcf1b | 149 | void readISR () { // Ticker interrupt defined in main |
dnonoo | 7:f017a37bcf1b | 150 | |
dnonoo | 7:f017a37bcf1b | 151 | _sensorRead.signal_set(SENSOR_UPDATE); |
dnonoo | 7:f017a37bcf1b | 152 | } |
dnonoo | 8:ab6322afa341 | 153 | // Keep short |
dnonoo | 7:f017a37bcf1b | 154 | void sensorRead () { |
dnonoo | 7:f017a37bcf1b | 155 | |
dnonoo | 7:f017a37bcf1b | 156 | while (1) { |
dnonoo | 7:f017a37bcf1b | 157 | |
dnonoo | 8:ab6322afa341 | 158 | dataLock.lock(); // Entering Critial Section |
dnonoo | 7:f017a37bcf1b | 159 | |
dnonoo | 7:f017a37bcf1b | 160 | // Store Data in global Variables |
dnonoo | 7:f017a37bcf1b | 161 | LDR = LDR_In.read(); |
dnonoo | 7:f017a37bcf1b | 162 | TEMP = Sensor.getTemperature(); |
dnonoo | 7:f017a37bcf1b | 163 | PRES = Sensor.getPressure(); |
dnonoo | 7:f017a37bcf1b | 164 | |
dnonoo | 8:ab6322afa341 | 165 | dataLock.unlock(); // Exiting Critical Section |
dnonoo | 7:f017a37bcf1b | 166 | |
dnonoo | 7:f017a37bcf1b | 167 | Green_int = !Green_int; // debugging |
GeorgeJourneaux | 5:ea3ec65cbf5f | 168 | |
dnonoo | 7:f017a37bcf1b | 169 | //Read sensors, send to mail-queue |
dnonoo | 7:f017a37bcf1b | 170 | mail_t *mail = mail_box.alloc(); |
dnonoo | 7:f017a37bcf1b | 171 | mail->LDR_Value = LDR; |
dnonoo | 7:f017a37bcf1b | 172 | mail->temp_Value = TEMP; |
dnonoo | 7:f017a37bcf1b | 173 | mail->press_Value = PRES; |
dnonoo | 7:f017a37bcf1b | 174 | mail_box.put(mail); |
dnonoo | 7:f017a37bcf1b | 175 | |
dnonoo | 7:f017a37bcf1b | 176 | _circBuff.signal_set(DATA_READY); // Set signal to buffer to store updated values |
dnonoo | 7:f017a37bcf1b | 177 | Thread::signal_wait(SENSOR_UPDATE); // Wait for the Timer interrupt |
dnonoo | 7:f017a37bcf1b | 178 | } |
GeorgeJourneaux | 3:73497379c0cb | 179 | } |
dnonoo | 7:f017a37bcf1b | 180 | |
GeorgeJourneaux | 3:73497379c0cb | 181 | |
GeorgeJourneaux | 3:73497379c0cb | 182 | /*--------------------------------LCD---------------------------------*/ |
benparkes | 0:cb3a5c15b01e | 183 | void PrintLCD () { |
benparkes | 0:cb3a5c15b01e | 184 | |
benparkes | 0:cb3a5c15b01e | 185 | int i = 0; |
benparkes | 0:cb3a5c15b01e | 186 | while(1){ |
benparkes | 1:bca9993a0df3 | 187 | char lightString[16]; |
benparkes | 1:bca9993a0df3 | 188 | char tempString[16]; |
benparkes | 1:bca9993a0df3 | 189 | char pressString[16]; |
benparkes | 1:bca9993a0df3 | 190 | |
benparkes | 0:cb3a5c15b01e | 191 | lcd.Clear(); |
benparkes | 0:cb3a5c15b01e | 192 | lcd.RowSelect(0); |
benparkes | 1:bca9993a0df3 | 193 | |
benparkes | 0:cb3a5c15b01e | 194 | switch (i){ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 195 | case 0:{ |
GeorgeJourneaux | 2:28d12a3db239 | 196 | osEvent evt = mail_box.get(); |
benparkes | 1:bca9993a0df3 | 197 | |
GeorgeJourneaux | 2:28d12a3db239 | 198 | if (evt.status == osEventMail) { |
GeorgeJourneaux | 2:28d12a3db239 | 199 | mail_t *mail = (mail_t*)evt.value.p; |
benparkes | 0:cb3a5c15b01e | 200 | |
GeorgeJourneaux | 2:28d12a3db239 | 201 | sprintf(lightString,"%.4f", mail->LDR_Value); |
GeorgeJourneaux | 2:28d12a3db239 | 202 | sprintf(tempString,"%2.2f", mail->temp_Value); |
GeorgeJourneaux | 2:28d12a3db239 | 203 | sprintf(pressString,"%4.2f", mail->press_Value); |
benparkes | 1:bca9993a0df3 | 204 | |
GeorgeJourneaux | 2:28d12a3db239 | 205 | mail_box.free(mail); |
GeorgeJourneaux | 2:28d12a3db239 | 206 | } |
benparkes | 1:bca9993a0df3 | 207 | |
GeorgeJourneaux | 2:28d12a3db239 | 208 | lcd.Write("Light Level:"); |
GeorgeJourneaux | 2:28d12a3db239 | 209 | lcd.RowSelect(1); |
GeorgeJourneaux | 2:28d12a3db239 | 210 | lcd.Write(lightString); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 211 | i++; |
benparkes | 0:cb3a5c15b01e | 212 | break; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 213 | } |
GeorgeJourneaux | 2:28d12a3db239 | 214 | case 1: |
GeorgeJourneaux | 2:28d12a3db239 | 215 | lcd.Write("Temperature:"); |
GeorgeJourneaux | 2:28d12a3db239 | 216 | lcd.RowSelect(1); |
GeorgeJourneaux | 2:28d12a3db239 | 217 | lcd.Write(tempString); |
GeorgeJourneaux | 2:28d12a3db239 | 218 | i++; |
benparkes | 0:cb3a5c15b01e | 219 | break; |
benparkes | 0:cb3a5c15b01e | 220 | |
GeorgeJourneaux | 2:28d12a3db239 | 221 | case 2: |
GeorgeJourneaux | 2:28d12a3db239 | 222 | lcd.Write("Pressure:"); |
GeorgeJourneaux | 2:28d12a3db239 | 223 | lcd.RowSelect(1); |
GeorgeJourneaux | 2:28d12a3db239 | 224 | lcd.Write(pressString); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 225 | i =0; |
benparkes | 0:cb3a5c15b01e | 226 | break; |
GeorgeJourneaux | 2:28d12a3db239 | 227 | |
GeorgeJourneaux | 5:ea3ec65cbf5f | 228 | default: |
GeorgeJourneaux | 5:ea3ec65cbf5f | 229 | i = 0; |
benparkes | 0:cb3a5c15b01e | 230 | break; |
benparkes | 0:cb3a5c15b01e | 231 | } |
benparkes | 0:cb3a5c15b01e | 232 | |
GeorgeJourneaux | 2:28d12a3db239 | 233 | Red_int = !Red_int; |
benparkes | 1:bca9993a0df3 | 234 | |
benparkes | 0:cb3a5c15b01e | 235 | Thread::wait (5000); |
GeorgeJourneaux | 2:28d12a3db239 | 236 | } |
benparkes | 0:cb3a5c15b01e | 237 | } |
GeorgeJourneaux | 3:73497379c0cb | 238 | /*--------------------------------------------------------------------*/ |
benparkes | 1:bca9993a0df3 | 239 | |
GeorgeJourneaux | 3:73497379c0cb | 240 | /*------------------------------SERIAL_CMD----------------------------*/ |
GeorgeJourneaux | 4:93d6d13d4de3 | 241 | //Interrupt when recieving from serial port |
GeorgeJourneaux | 2:28d12a3db239 | 242 | void Rx_interrupt() { |
GeorgeJourneaux | 2:28d12a3db239 | 243 | |
dnonoo | 7:f017a37bcf1b | 244 | //Wait for serial input |
GeorgeJourneaux | 2:28d12a3db239 | 245 | while (pc.readable()) { |
GeorgeJourneaux | 6:64d346936f0e | 246 | |
dnonoo | 7:f017a37bcf1b | 247 | //Return input to serial |
GeorgeJourneaux | 5:ea3ec65cbf5f | 248 | rx_buffer[rx_in] = pc.getc(); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 249 | pc.putc(rx_buffer[rx_in]); |
GeorgeJourneaux | 6:64d346936f0e | 250 | |
dnonoo | 7:f017a37bcf1b | 251 | //If enter key is pressed, set serial thread signal |
GeorgeJourneaux | 5:ea3ec65cbf5f | 252 | if(rx_buffer[rx_in] == 0xD){ |
dnonoo | 7:f017a37bcf1b | 253 | _serialCMD.signal_set(ENTER_KEY); |
GeorgeJourneaux | 2:28d12a3db239 | 254 | } |
GeorgeJourneaux | 6:64d346936f0e | 255 | |
dnonoo | 7:f017a37bcf1b | 256 | //Increment buffer head |
GeorgeJourneaux | 2:28d12a3db239 | 257 | else{ |
GeorgeJourneaux | 2:28d12a3db239 | 258 | rx_in = (rx_in + 1); |
GeorgeJourneaux | 2:28d12a3db239 | 259 | } |
GeorgeJourneaux | 2:28d12a3db239 | 260 | } |
GeorgeJourneaux | 2:28d12a3db239 | 261 | } |
benparkes | 0:cb3a5c15b01e | 262 | |
GeorgeJourneaux | 4:93d6d13d4de3 | 263 | //Check what command what recieved and execute |
dnonoo | 7:f017a37bcf1b | 264 | void serialCMD(){ |
GeorgeJourneaux | 2:28d12a3db239 | 265 | |
GeorgeJourneaux | 2:28d12a3db239 | 266 | while(1){ |
dnonoo | 7:f017a37bcf1b | 267 | //Wait for thread signal |
GeorgeJourneaux | 4:93d6d13d4de3 | 268 | Thread::signal_wait(ENTER_KEY); |
GeorgeJourneaux | 6:64d346936f0e | 269 | |
dnonoo | 7:f017a37bcf1b | 270 | //Detach serial interrupt |
GeorgeJourneaux | 2:28d12a3db239 | 271 | pc.attach(NULL, Serial::RxIrq); |
GeorgeJourneaux | 6:64d346936f0e | 272 | |
GeorgeJourneaux | 3:73497379c0cb | 273 | struct tm * s_time; |
GeorgeJourneaux | 3:73497379c0cb | 274 | char tm_n[4]; |
GeorgeJourneaux | 3:73497379c0cb | 275 | |
GeorgeJourneaux | 6:64d346936f0e | 276 | /*----CARRAGE RETURN-------------*/ |
GeorgeJourneaux | 6:64d346936f0e | 277 | if(rx_buffer[0] == 0xD){ |
GeorgeJourneaux | 6:64d346936f0e | 278 | pc.puts("\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 279 | } |
GeorgeJourneaux | 6:64d346936f0e | 280 | /*----READ ALL----------------------------------*/ |
GeorgeJourneaux | 6:64d346936f0e | 281 | else if(strstr(rx_buffer, "READ ALL")){ |
GeorgeJourneaux | 6:64d346936f0e | 282 | pc.puts(" READ ALL\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 283 | |
dnonoo | 7:f017a37bcf1b | 284 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 285 | DataBuffer.lock(); |
GeorgeJourneaux | 6:64d346936f0e | 286 | |
dnonoo | 7:f017a37bcf1b | 287 | //Print all samples to serial |
GeorgeJourneaux | 6:64d346936f0e | 288 | for(int n=data_t; n<=MAX_SAMPLES; n++){ |
GeorgeJourneaux | 6:64d346936f0e | 289 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 290 | } |
GeorgeJourneaux | 6:64d346936f0e | 291 | if(data_t>data_h){ |
GeorgeJourneaux | 6:64d346936f0e | 292 | for(int n=0; n<=(data_t-1); n++){ |
GeorgeJourneaux | 6:64d346936f0e | 293 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 294 | } |
GeorgeJourneaux | 6:64d346936f0e | 295 | } |
GeorgeJourneaux | 5:ea3ec65cbf5f | 296 | |
dnonoo | 7:f017a37bcf1b | 297 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 298 | DataBuffer.unlock(); |
GeorgeJourneaux | 2:28d12a3db239 | 299 | } |
GeorgeJourneaux | 6:64d346936f0e | 300 | /*----DELETE ALL----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 301 | else if(strstr(rx_buffer, "DELETE ALL")){ |
GeorgeJourneaux | 6:64d346936f0e | 302 | pc.puts(" DELETE ALL\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 303 | |
GeorgeJourneaux | 6:64d346936f0e | 304 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 305 | DataBuffer.lock(); |
GeorgeJourneaux | 6:64d346936f0e | 306 | |
GeorgeJourneaux | 6:64d346936f0e | 307 | //Delete all sampled data |
GeorgeJourneaux | 6:64d346936f0e | 308 | for(int n=0; n<=MAX_SAMPLES; n++){ |
GeorgeJourneaux | 6:64d346936f0e | 309 | memset(data_buffer[n], NULL, 64); |
GeorgeJourneaux | 6:64d346936f0e | 310 | } |
GeorgeJourneaux | 6:64d346936f0e | 311 | data_h = data_t; |
GeorgeJourneaux | 6:64d346936f0e | 312 | sample_h = sample_t; |
GeorgeJourneaux | 6:64d346936f0e | 313 | |
GeorgeJourneaux | 6:64d346936f0e | 314 | //Unlock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 315 | DataBuffer.unlock(); |
GeorgeJourneaux | 2:28d12a3db239 | 316 | } |
GeorgeJourneaux | 6:64d346936f0e | 317 | /*----READ----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 318 | else if(strstr(rx_buffer, "READ")){ |
GeorgeJourneaux | 6:64d346936f0e | 319 | pc.puts(" READ \n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 320 | int N = atoi(strncpy(tm_n,&rx_buffer[5],4)); |
GeorgeJourneaux | 6:64d346936f0e | 321 | int S = 0; |
GeorgeJourneaux | 6:64d346936f0e | 322 | pc.printf("N = %d\n\r",N); |
GeorgeJourneaux | 6:64d346936f0e | 323 | |
GeorgeJourneaux | 6:64d346936f0e | 324 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 325 | DataBuffer.lock(); |
GeorgeJourneaux | 6:64d346936f0e | 326 | |
GeorgeJourneaux | 6:64d346936f0e | 327 | //Check if N is greater than buffer size |
GeorgeJourneaux | 6:64d346936f0e | 328 | if(N >= MAX_SAMPLES){ |
GeorgeJourneaux | 6:64d346936f0e | 329 | N = MAX_SAMPLES; |
GeorgeJourneaux | 6:64d346936f0e | 330 | } |
GeorgeJourneaux | 6:64d346936f0e | 331 | |
GeorgeJourneaux | 6:64d346936f0e | 332 | //Read N samples from FIFO buffer |
GeorgeJourneaux | 6:64d346936f0e | 333 | if(N <= 0){ |
GeorgeJourneaux | 6:64d346936f0e | 334 | pc.puts("####ERROR####\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 335 | } |
GeorgeJourneaux | 6:64d346936f0e | 336 | else{ |
GeorgeJourneaux | 6:64d346936f0e | 337 | for(int n=data_t; n<=MAX_SAMPLES-1; n++){ |
GeorgeJourneaux | 6:64d346936f0e | 338 | if(S>=N){} |
GeorgeJourneaux | 6:64d346936f0e | 339 | else{ |
GeorgeJourneaux | 6:64d346936f0e | 340 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 341 | S++; |
GeorgeJourneaux | 6:64d346936f0e | 342 | } |
GeorgeJourneaux | 6:64d346936f0e | 343 | } |
GeorgeJourneaux | 6:64d346936f0e | 344 | for(int n=0; n<=data_t; n++){ |
GeorgeJourneaux | 6:64d346936f0e | 345 | if(S>=N){} |
GeorgeJourneaux | 6:64d346936f0e | 346 | else{ |
GeorgeJourneaux | 6:64d346936f0e | 347 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 348 | S++; |
GeorgeJourneaux | 6:64d346936f0e | 349 | } |
GeorgeJourneaux | 6:64d346936f0e | 350 | } |
GeorgeJourneaux | 6:64d346936f0e | 351 | } |
GeorgeJourneaux | 6:64d346936f0e | 352 | |
GeorgeJourneaux | 6:64d346936f0e | 353 | //Unlock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 354 | DataBuffer.unlock(); |
GeorgeJourneaux | 2:28d12a3db239 | 355 | } |
GeorgeJourneaux | 6:64d346936f0e | 356 | /*----DELETE----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 357 | else if(strstr(rx_buffer, "DELETE")){ |
GeorgeJourneaux | 6:64d346936f0e | 358 | pc.puts(" DELETE \n\r"); |
GeorgeJourneaux | 3:73497379c0cb | 359 | } |
GeorgeJourneaux | 6:64d346936f0e | 360 | /*----SETDATE----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 361 | else if(strstr(rx_buffer, "SETDATE")){ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 362 | time(&raw_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 363 | s_time = localtime(&raw_time); |
GeorgeJourneaux | 3:73497379c0cb | 364 | |
GeorgeJourneaux | 6:64d346936f0e | 365 | //Update day in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 366 | int dd = atoi(strncpy(tm_n,&rx_buffer[8],2)); |
GeorgeJourneaux | 3:73497379c0cb | 367 | s_time->tm_mday = dd; |
GeorgeJourneaux | 3:73497379c0cb | 368 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 369 | |
GeorgeJourneaux | 6:64d346936f0e | 370 | //Update month in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 371 | int mm = atoi(strncpy(tm_n,&rx_buffer[11],2)); |
GeorgeJourneaux | 3:73497379c0cb | 372 | s_time->tm_mon = mm-1; |
GeorgeJourneaux | 3:73497379c0cb | 373 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 374 | |
GeorgeJourneaux | 6:64d346936f0e | 375 | //Update year in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 376 | int yyyy = atoi(strncpy(tm_n,&rx_buffer[14],4)); |
GeorgeJourneaux | 3:73497379c0cb | 377 | s_time->tm_year = yyyy-1900; |
GeorgeJourneaux | 3:73497379c0cb | 378 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 379 | |
GeorgeJourneaux | 6:64d346936f0e | 380 | //Set date from updated time structure |
GeorgeJourneaux | 3:73497379c0cb | 381 | set_time(mktime(s_time)); |
GeorgeJourneaux | 6:64d346936f0e | 382 | strftime(serial_buffer, 80, "\n\r Set Date: %d/%m/%Y\n\r", s_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 383 | pc.puts(serial_buffer); |
GeorgeJourneaux | 2:28d12a3db239 | 384 | } |
GeorgeJourneaux | 6:64d346936f0e | 385 | /*----SETTIME---------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 386 | else if(strstr(rx_buffer, "SETTIME")){ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 387 | time(&raw_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 388 | s_time = localtime(&raw_time); |
GeorgeJourneaux | 3:73497379c0cb | 389 | |
GeorgeJourneaux | 6:64d346936f0e | 390 | //Update seconds in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 391 | int ss = atoi(strncpy(tm_n,&rx_buffer[14],2)); |
GeorgeJourneaux | 3:73497379c0cb | 392 | s_time->tm_sec = ss; |
GeorgeJourneaux | 3:73497379c0cb | 393 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 6:64d346936f0e | 394 | |
GeorgeJourneaux | 6:64d346936f0e | 395 | //Update minutes in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 396 | int mm = atoi(strncpy(tm_n,&rx_buffer[11],2)); |
GeorgeJourneaux | 3:73497379c0cb | 397 | s_time->tm_min = mm; |
GeorgeJourneaux | 3:73497379c0cb | 398 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 399 | |
GeorgeJourneaux | 6:64d346936f0e | 400 | //Update hour in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 401 | int hh = atoi(strncpy(tm_n,&rx_buffer[8],2)); |
GeorgeJourneaux | 3:73497379c0cb | 402 | s_time->tm_hour = hh; |
GeorgeJourneaux | 3:73497379c0cb | 403 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 6:64d346936f0e | 404 | |
GeorgeJourneaux | 6:64d346936f0e | 405 | //Set time from updated time structure |
GeorgeJourneaux | 3:73497379c0cb | 406 | set_time(mktime(s_time)); |
GeorgeJourneaux | 6:64d346936f0e | 407 | strftime(serial_buffer, 80, "\n\r Set Time: %X\n\r", s_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 408 | pc.puts(serial_buffer); |
GeorgeJourneaux | 2:28d12a3db239 | 409 | } |
GeorgeJourneaux | 6:64d346936f0e | 410 | /*----SETT----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 411 | else if(strstr(rx_buffer, "SETT")){ |
GeorgeJourneaux | 6:64d346936f0e | 412 | pc.puts(" SETT\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 413 | } |
GeorgeJourneaux | 6:64d346936f0e | 414 | /*----STATE----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 415 | else if(strstr(rx_buffer, "STATE")){ |
GeorgeJourneaux | 6:64d346936f0e | 416 | pc.puts(" STATE\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 417 | } |
GeorgeJourneaux | 6:64d346936f0e | 418 | /*----LOGGING----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 419 | else if(strstr(rx_buffer, "LOGGING")){ |
GeorgeJourneaux | 6:64d346936f0e | 420 | pc.puts(" LOGGING\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 421 | } |
GeorgeJourneaux | 6:64d346936f0e | 422 | /*----ERROR---*/ |
GeorgeJourneaux | 2:28d12a3db239 | 423 | else{ |
GeorgeJourneaux | 6:64d346936f0e | 424 | pc.puts("####ERROR####\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 425 | } |
GeorgeJourneaux | 6:64d346936f0e | 426 | /*----------------------------------------------*/ |
GeorgeJourneaux | 6:64d346936f0e | 427 | |
GeorgeJourneaux | 6:64d346936f0e | 428 | //Clear serial buffers |
GeorgeJourneaux | 5:ea3ec65cbf5f | 429 | memset(serial_buffer, NULL, 80); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 430 | memset(rx_buffer, NULL, 32); |
GeorgeJourneaux | 2:28d12a3db239 | 431 | rx_in = 0; |
GeorgeJourneaux | 2:28d12a3db239 | 432 | |
GeorgeJourneaux | 6:64d346936f0e | 433 | //Attach serial interrupt |
GeorgeJourneaux | 2:28d12a3db239 | 434 | pc.attach(&Rx_interrupt, Serial::RxIrq); |
GeorgeJourneaux | 2:28d12a3db239 | 435 | } |
GeorgeJourneaux | 2:28d12a3db239 | 436 | } |
dnonoo | 7:f017a37bcf1b | 437 | /*------------------------------------------------*/ |
dnonoo | 7:f017a37bcf1b | 438 | |
dnonoo | 7:f017a37bcf1b | 439 | void POST () { |
dnonoo | 7:f017a37bcf1b | 440 | |
dnonoo | 7:f017a37bcf1b | 441 | pc.printf(" ALL Leds should be flashing\n\r"); |
dnonoo | 7:f017a37bcf1b | 442 | |
dnonoo | 7:f017a37bcf1b | 443 | for(unsigned int n = 0; n<10; n++) { |
dnonoo | 7:f017a37bcf1b | 444 | Green_int = ON; |
dnonoo | 7:f017a37bcf1b | 445 | Blue_int = ON; |
dnonoo | 7:f017a37bcf1b | 446 | Red_int = ON; |
dnonoo | 7:f017a37bcf1b | 447 | Green_ext = ON; |
dnonoo | 7:f017a37bcf1b | 448 | Yellow_ext = ON; |
dnonoo | 7:f017a37bcf1b | 449 | Red_ext = ON; |
dnonoo | 7:f017a37bcf1b | 450 | |
dnonoo | 7:f017a37bcf1b | 451 | wait (0.2); |
dnonoo | 7:f017a37bcf1b | 452 | Green_int = OFF; |
dnonoo | 7:f017a37bcf1b | 453 | Blue_int = OFF; |
dnonoo | 7:f017a37bcf1b | 454 | Red_int = OFF; |
dnonoo | 7:f017a37bcf1b | 455 | Green_ext = OFF; |
dnonoo | 7:f017a37bcf1b | 456 | Yellow_ext = OFF; |
dnonoo | 7:f017a37bcf1b | 457 | Red_ext = OFF; |
dnonoo | 7:f017a37bcf1b | 458 | wait (0.2); |
dnonoo | 7:f017a37bcf1b | 459 | } |
dnonoo | 7:f017a37bcf1b | 460 | |
dnonoo | 7:f017a37bcf1b | 461 | pc.printf("Switch states:\n\r"); |
dnonoo | 7:f017a37bcf1b | 462 | pc.printf("\tSW_L: %d\n\r\tSW_R %d\n\r", SW_L.read(), SW_R.read()); |
dnonoo | 7:f017a37bcf1b | 463 | |
dnonoo | 7:f017a37bcf1b | 464 | float Temp = Sensor.getTemperature(); |
dnonoo | 7:f017a37bcf1b | 465 | float Pres = Sensor.getPressure(); |
dnonoo | 7:f017a37bcf1b | 466 | float ldrs = LDR_In.read(); |
dnonoo | 7:f017a37bcf1b | 467 | |
dnonoo | 7:f017a37bcf1b | 468 | pc.printf("Sensor test:\n\r"); |
dnonoo | 7:f017a37bcf1b | 469 | pc.printf("T: %f\tP: %f\tL: %f\n\r",Temp,Pres,ldrs); |
dnonoo | 7:f017a37bcf1b | 470 | |
dnonoo | 7:f017a37bcf1b | 471 | pc.printf("LCD Test\n\r"); |
dnonoo | 7:f017a37bcf1b | 472 | |
dnonoo | 7:f017a37bcf1b | 473 | lcd.Clear(); |
dnonoo | 7:f017a37bcf1b | 474 | lcd.RowSelect(1); |
dnonoo | 7:f017a37bcf1b | 475 | lcd.Write("*******LCD******"); |
dnonoo | 7:f017a37bcf1b | 476 | lcd.RowSelect(2); |
dnonoo | 7:f017a37bcf1b | 477 | lcd.Write("******TEST******"); |
dnonoo | 7:f017a37bcf1b | 478 | wait(1); |
dnonoo | 7:f017a37bcf1b | 479 | lcd.Clear(); |
dnonoo | 7:f017a37bcf1b | 480 | } |
dnonoo | 8:ab6322afa341 | 481 | |
dnonoo | 8:ab6322afa341 | 482 | void writeRemove_SD() { |
dnonoo | 7:f017a37bcf1b | 483 | |
dnonoo | 8:ab6322afa341 | 484 | while(1) { |
dnonoo | 8:ab6322afa341 | 485 | Thread::signal_wait(USER_BUTTON_PRESSED); //wait for debounce signal |
dnonoo | 8:ab6322afa341 | 486 | pc.printf("Initalising SD Card\n\r"); |
dnonoo | 8:ab6322afa341 | 487 | |
dnonoo | 8:ab6322afa341 | 488 | //check init |
dnonoo | 8:ab6322afa341 | 489 | if (sd.init() != 0) { |
dnonoo | 8:ab6322afa341 | 490 | pc.printf("******SD Initialise FAIL*******\n\r"); |
dnonoo | 8:ab6322afa341 | 491 | } |
dnonoo | 8:ab6322afa341 | 492 | |
dnonoo | 8:ab6322afa341 | 493 | // Create Filing system for SD Card |
dnonoo | 8:ab6322afa341 | 494 | FATFileSystem fs("sd", &sd); |
dnonoo | 8:ab6322afa341 | 495 | |
dnonoo | 8:ab6322afa341 | 496 | //OpenFiles to write/append to |
dnonoo | 8:ab6322afa341 | 497 | pc.printf("Writing to SDC\n\r"); |
dnonoo | 8:ab6322afa341 | 498 | |
dnonoo | 8:ab6322afa341 | 499 | FILE* fp = fopen("/sd/TheChamberOfSecrets.txt", "w"); //"w" to overwrite file ftb |
dnonoo | 8:ab6322afa341 | 500 | |
dnonoo | 8:ab6322afa341 | 501 | // Check for error in opening file |
dnonoo | 8:ab6322afa341 | 502 | if (fp == NULL) { |
dnonoo | 8:ab6322afa341 | 503 | pc.printf("*****ERROR - Could not open file for write*****\n\r"); |
dnonoo | 8:ab6322afa341 | 504 | } |
dnonoo | 8:ab6322afa341 | 505 | //HERE IS WHERE TO PRINT DATA TO SD CARD FROM BUFFER (REMEMBER TO EMPTY BUFFER???) |
dnonoo | 8:ab6322afa341 | 506 | //Lock data buffer |
dnonoo | 8:ab6322afa341 | 507 | DataBuffer.lock(); |
dnonoo | 8:ab6322afa341 | 508 | dataLock.lock(); |
dnonoo | 8:ab6322afa341 | 509 | //Print all samples to SD |
dnonoo | 8:ab6322afa341 | 510 | for(int n=data_t; n<=MAX_SAMPLES; n++){ |
dnonoo | 8:ab6322afa341 | 511 | fputs(data_buffer[n], fp); |
dnonoo | 8:ab6322afa341 | 512 | fprintf(fp, "\n\r"); |
dnonoo | 8:ab6322afa341 | 513 | } |
dnonoo | 8:ab6322afa341 | 514 | if(data_t>data_h){ |
dnonoo | 8:ab6322afa341 | 515 | for(int n=0; n<=(data_t-1); n++){ |
dnonoo | 8:ab6322afa341 | 516 | fputs(data_buffer[n], fp); |
dnonoo | 8:ab6322afa341 | 517 | |
dnonoo | 8:ab6322afa341 | 518 | } |
dnonoo | 8:ab6322afa341 | 519 | } |
dnonoo | 8:ab6322afa341 | 520 | |
dnonoo | 8:ab6322afa341 | 521 | //Lock data buffer |
dnonoo | 8:ab6322afa341 | 522 | DataBuffer.unlock(); |
dnonoo | 8:ab6322afa341 | 523 | dataLock.unlock(); |
dnonoo | 8:ab6322afa341 | 524 | //fprintf(fp, "dd/mm/yy hh:mm:ss, TEMPERATURE, PRESSURE, LIGHT\n\r"); |
dnonoo | 8:ab6322afa341 | 525 | |
dnonoo | 8:ab6322afa341 | 526 | fclose(fp); |
dnonoo | 8:ab6322afa341 | 527 | |
dnonoo | 8:ab6322afa341 | 528 | pc.printf("Write Sucessful!\n\r"); |
dnonoo | 8:ab6322afa341 | 529 | |
dnonoo | 8:ab6322afa341 | 530 | sd.deinit(); |
dnonoo | 8:ab6322afa341 | 531 | pc.printf("SD Card Ready to Remove\n\r"); |
dnonoo | 8:ab6322afa341 | 532 | Green_ext = 1; |
dnonoo | 8:ab6322afa341 | 533 | Thread::wait(500); |
dnonoo | 8:ab6322afa341 | 534 | Green_ext = 0; |
dnonoo | 8:ab6322afa341 | 535 | Thread::wait(500); |
dnonoo | 8:ab6322afa341 | 536 | Green_ext = 1; |
dnonoo | 8:ab6322afa341 | 537 | Thread::wait(500); |
dnonoo | 8:ab6322afa341 | 538 | Green_ext = 0; |
dnonoo | 8:ab6322afa341 | 539 | Thread::wait(500); |
dnonoo | 8:ab6322afa341 | 540 | Green_ext = 1; |
dnonoo | 8:ab6322afa341 | 541 | Thread::wait(500); |
dnonoo | 8:ab6322afa341 | 542 | Green_ext = 0; |
dnonoo | 8:ab6322afa341 | 543 | Thread::wait(500); |
dnonoo | 8:ab6322afa341 | 544 | Green_ext = 1; |
dnonoo | 8:ab6322afa341 | 545 | Thread::wait(500); |
dnonoo | 8:ab6322afa341 | 546 | Green_ext = 0; |
dnonoo | 8:ab6322afa341 | 547 | Thread::wait(500); |
dnonoo | 8:ab6322afa341 | 548 | }// End While |
dnonoo | 8:ab6322afa341 | 549 | }// End Thread |
dnonoo | 8:ab6322afa341 | 550 | |
dnonoo | 9:b838c5787ed7 | 551 | void Network1 () { |
dnonoo | 9:b838c5787ed7 | 552 | |
dnonoo | 9:b838c5787ed7 | 553 | printf("Basic HTTP server example\n"); |
dnonoo | 9:b838c5787ed7 | 554 | |
dnonoo | 9:b838c5787ed7 | 555 | //Configure an ethernet connection |
dnonoo | 9:b838c5787ed7 | 556 | EthernetInterface eth; |
dnonoo | 9:b838c5787ed7 | 557 | eth.set_network(IP, NETMASK, GATEWAY); |
dnonoo | 9:b838c5787ed7 | 558 | eth.connect(); |
dnonoo | 9:b838c5787ed7 | 559 | printf("The target IP address is '%s'\n", eth.get_ip_address()); |
dnonoo | 9:b838c5787ed7 | 560 | |
dnonoo | 9:b838c5787ed7 | 561 | //Now setup a web server |
dnonoo | 9:b838c5787ed7 | 562 | TCPServer srv; //TCP/IP Server |
dnonoo | 9:b838c5787ed7 | 563 | |
dnonoo | 9:b838c5787ed7 | 564 | SocketAddress clt_addr; //Address of incoming connection |
dnonoo | 9:b838c5787ed7 | 565 | |
dnonoo | 9:b838c5787ed7 | 566 | /* Open the server on ethernet stack */ |
dnonoo | 9:b838c5787ed7 | 567 | srv.open(ð); |
dnonoo | 9:b838c5787ed7 | 568 | |
dnonoo | 9:b838c5787ed7 | 569 | /* Bind the HTTP port (TCP 80) to the server */ |
dnonoo | 9:b838c5787ed7 | 570 | srv.bind(eth.get_ip_address(), 80); |
dnonoo | 9:b838c5787ed7 | 571 | |
dnonoo | 9:b838c5787ed7 | 572 | /* Can handle 5 simultaneous connections */ |
dnonoo | 9:b838c5787ed7 | 573 | srv.listen(5); |
dnonoo | 9:b838c5787ed7 | 574 | |
dnonoo | 9:b838c5787ed7 | 575 | while (true) { |
dnonoo | 9:b838c5787ed7 | 576 | |
dnonoo | 9:b838c5787ed7 | 577 | TCPSocket clt_sock; //Socket for communication |
dnonoo | 9:b838c5787ed7 | 578 | using namespace std; |
dnonoo | 9:b838c5787ed7 | 579 | //Block and wait on an incoming connection |
dnonoo | 9:b838c5787ed7 | 580 | srv.accept(&clt_sock, &clt_addr); |
dnonoo | 9:b838c5787ed7 | 581 | printf("accept %s:%d\n\r", clt_addr.get_ip_address(), clt_addr.get_port()); |
dnonoo | 9:b838c5787ed7 | 582 | |
dnonoo | 9:b838c5787ed7 | 583 | //Uses a C++ string to make it easier to concatinate |
dnonoo | 9:b838c5787ed7 | 584 | string response; |
dnonoo | 9:b838c5787ed7 | 585 | string strL = "LDR:"; |
dnonoo | 9:b838c5787ed7 | 586 | string strP = ", Pressure(mBar): "; |
dnonoo | 9:b838c5787ed7 | 587 | string strT = ", Temp(C): "; |
dnonoo | 9:b838c5787ed7 | 588 | //This is a C string |
dnonoo | 9:b838c5787ed7 | 589 | char l_str[64]; |
dnonoo | 9:b838c5787ed7 | 590 | char p_str[64]; |
dnonoo | 9:b838c5787ed7 | 591 | char t_str[64]; |
dnonoo | 9:b838c5787ed7 | 592 | |
dnonoo | 9:b838c5787ed7 | 593 | //Read the LDR value |
dnonoo | 9:b838c5787ed7 | 594 | dataLock.lock(); |
dnonoo | 9:b838c5787ed7 | 595 | float L = LDR ; |
dnonoo | 9:b838c5787ed7 | 596 | float T = TEMP; |
dnonoo | 9:b838c5787ed7 | 597 | float P = PRES; |
dnonoo | 9:b838c5787ed7 | 598 | dataLock.unlock(); |
dnonoo | 9:b838c5787ed7 | 599 | |
dnonoo | 9:b838c5787ed7 | 600 | //Convert to a C String |
dnonoo | 9:b838c5787ed7 | 601 | sprintf(l_str, "%1.3f", L ); |
dnonoo | 9:b838c5787ed7 | 602 | sprintf(t_str, "%2.2f", T); |
dnonoo | 9:b838c5787ed7 | 603 | sprintf(p_str, "%4.2f", P); |
dnonoo | 9:b838c5787ed7 | 604 | |
dnonoo | 9:b838c5787ed7 | 605 | |
dnonoo | 9:b838c5787ed7 | 606 | //Build the C++ string response |
dnonoo | 9:b838c5787ed7 | 607 | response = HTTP_MESSAGE_BODY1; |
dnonoo | 9:b838c5787ed7 | 608 | // response += strL; |
dnonoo | 9:b838c5787ed7 | 609 | response += l_str; |
dnonoo | 9:b838c5787ed7 | 610 | response += strT; |
dnonoo | 9:b838c5787ed7 | 611 | response += t_str; |
dnonoo | 9:b838c5787ed7 | 612 | response += strP; |
dnonoo | 9:b838c5787ed7 | 613 | response += p_str; |
dnonoo | 9:b838c5787ed7 | 614 | response += HTTP_MESSAGE_BODY2; |
dnonoo | 9:b838c5787ed7 | 615 | |
dnonoo | 9:b838c5787ed7 | 616 | //Send static HTML response (as a C string) |
dnonoo | 9:b838c5787ed7 | 617 | clt_sock.send(response.c_str(), response.size()+6); |
dnonoo | 9:b838c5787ed7 | 618 | } |
dnonoo | 9:b838c5787ed7 | 619 | } |