![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
SOFT253 Coursework Weather Reader
Fork of Soft253-WeatherReader by
main.cpp@5:ba160e9778d0, 2017-05-02 (annotated)
- Committer:
- Jdumpleton3
- Date:
- Tue May 02 09:36:46 2017 +0000
- Revision:
- 5:ba160e9778d0
- Parent:
- 4:edef041d2094
- Child:
- 6:0cc980145515
Change time to use time_t object rather than flat string;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jdumpleton3 | 0:b490b8486404 | 1 | #include "mbed.h" |
Jdumpleton3 | 0:b490b8486404 | 2 | #include "rtos.h" |
Jdumpleton3 | 0:b490b8486404 | 3 | #include "hts221.h" |
Jdumpleton3 | 0:b490b8486404 | 4 | #include "LPS25H.h" |
Jdumpleton3 | 1:8585a8d417dc | 5 | #include "DataGenerator.h" |
Jdumpleton3 | 4:edef041d2094 | 6 | #include <string> |
Jdumpleton3 | 5:ba160e9778d0 | 7 | #include <time.h> |
Jdumpleton3 | 0:b490b8486404 | 8 | |
Jdumpleton3 | 2:27b92722246b | 9 | //Complex.Variables |
Jdumpleton3 | 0:b490b8486404 | 10 | DigitalOut myled(LED1); |
Jdumpleton3 | 0:b490b8486404 | 11 | I2C i2c2(I2C_SDA, I2C_SCL); |
Jdumpleton3 | 2:27b92722246b | 12 | Ticker dataTimer; |
Jdumpleton3 | 0:b490b8486404 | 13 | |
Jdumpleton3 | 2:27b92722246b | 14 | //Method Headers |
Jdumpleton3 | 2:27b92722246b | 15 | void readData(); |
Jdumpleton3 | 2:27b92722246b | 16 | void createDataThread(); |
Jdumpleton3 | 2:27b92722246b | 17 | void printData(); |
jedward | 3:d17f07a0d08d | 18 | void storeNewRecord(); |
Jdumpleton3 | 2:27b92722246b | 19 | |
Jdumpleton3 | 2:27b92722246b | 20 | //Prim.Variables |
Jdumpleton3 | 0:b490b8486404 | 21 | float tempCelsius = 25.50; |
Jdumpleton3 | 0:b490b8486404 | 22 | float humi = 55; |
Jdumpleton3 | 2:27b92722246b | 23 | float barometerPressure = 0; |
Jdumpleton3 | 2:27b92722246b | 24 | float barometerTemperature = 0; |
Jdumpleton3 | 4:edef041d2094 | 25 | float dataReadTime = 2.0; |
Jdumpleton3 | 0:b490b8486404 | 26 | int humiMax = 100; |
Jdumpleton3 | 0:b490b8486404 | 27 | char cmd=0; |
Jdumpleton3 | 0:b490b8486404 | 28 | uint32_t seconds = 0, minutes=0, hours=0; |
jedward | 3:d17f07a0d08d | 29 | int headNode = 0; |
jedward | 3:d17f07a0d08d | 30 | int currentNode = 0; |
jedward | 3:d17f07a0d08d | 31 | const int maxNumRecords = 120; |
Jdumpleton3 | 5:ba160e9778d0 | 32 | |
Jdumpleton3 | 2:27b92722246b | 33 | //Can't print data in a ticker method (not interrupt safe) |
Jdumpleton3 | 2:27b92722246b | 34 | //This is set once we have got new data, so it can be printed safely later |
Jdumpleton3 | 2:27b92722246b | 35 | bool isNewData=false; |
Jdumpleton3 | 0:b490b8486404 | 36 | |
Jdumpleton3 | 2:27b92722246b | 37 | // Test Data generator setup |
Jdumpleton3 | 1:8585a8d417dc | 38 | DataGenerator dataGen; |
Jdumpleton3 | 0:b490b8486404 | 39 | |
jedward | 3:d17f07a0d08d | 40 | struct dataEntry{ |
jedward | 3:d17f07a0d08d | 41 | float airTemp; |
jedward | 3:d17f07a0d08d | 42 | float airPress; |
jedward | 3:d17f07a0d08d | 43 | float barTemp; |
jedward | 3:d17f07a0d08d | 44 | float barPress; |
Jdumpleton3 | 5:ba160e9778d0 | 45 | time_t dt; |
jedward | 3:d17f07a0d08d | 46 | }tempEntry; |
jedward | 3:d17f07a0d08d | 47 | |
jedward | 3:d17f07a0d08d | 48 | dataEntry storedDataArray [maxNumRecords]; |
jedward | 3:d17f07a0d08d | 49 | |
Jdumpleton3 | 1:8585a8d417dc | 50 | int main(){ |
Jdumpleton3 | 1:8585a8d417dc | 51 | printf("SOFT253 simple Temperature Humidity and Pressure Sensor Monitor\n\r"); |
Jdumpleton3 | 1:8585a8d417dc | 52 | printf("Using the X-NUCLEO-IKS01A1 shield and MBED Libraries\n\r"); |
Jdumpleton3 | 1:8585a8d417dc | 53 | |
Jdumpleton3 | 2:27b92722246b | 54 | dataTimer.attach(&readData, dataReadTime); //Attach timer to data reader. |
Jdumpleton3 | 0:b490b8486404 | 55 | |
Jdumpleton3 | 4:edef041d2094 | 56 | |
Jdumpleton3 | 1:8585a8d417dc | 57 | while(1) |
Jdumpleton3 | 0:b490b8486404 | 58 | { |
Jdumpleton3 | 2:27b92722246b | 59 | if (isNewData == true){ |
jedward | 3:d17f07a0d08d | 60 | storeNewRecord(); |
Jdumpleton3 | 2:27b92722246b | 61 | printData(); |
Jdumpleton3 | 2:27b92722246b | 62 | } |
Jdumpleton3 | 1:8585a8d417dc | 63 | |
Jdumpleton3 | 4:edef041d2094 | 64 | |
Jdumpleton3 | 1:8585a8d417dc | 65 | /* Flicker the LED. */ |
Jdumpleton3 | 0:b490b8486404 | 66 | myled = 1; // LED is ON |
Jdumpleton3 | 0:b490b8486404 | 67 | Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds! |
Jdumpleton3 | 0:b490b8486404 | 68 | myled = 0; // LED is OFF |
Jdumpleton3 | 0:b490b8486404 | 69 | Thread::wait(100); // 100 ms |
Jdumpleton3 | 2:27b92722246b | 70 | } |
Jdumpleton3 | 2:27b92722246b | 71 | } |
Jdumpleton3 | 2:27b92722246b | 72 | |
jedward | 3:d17f07a0d08d | 73 | void storeNewRecord(){ |
jedward | 3:d17f07a0d08d | 74 | |
jedward | 3:d17f07a0d08d | 75 | if(headNode>=120){ |
jedward | 3:d17f07a0d08d | 76 | headNode = 0; |
jedward | 3:d17f07a0d08d | 77 | } |
jedward | 3:d17f07a0d08d | 78 | |
Jdumpleton3 | 5:ba160e9778d0 | 79 | tempEntry.dt = time(0); |
Jdumpleton3 | 4:edef041d2094 | 80 | |
jedward | 3:d17f07a0d08d | 81 | storedDataArray[headNode] = tempEntry; |
jedward | 3:d17f07a0d08d | 82 | currentNode = headNode; |
jedward | 3:d17f07a0d08d | 83 | headNode++; |
jedward | 3:d17f07a0d08d | 84 | } |
jedward | 3:d17f07a0d08d | 85 | |
Jdumpleton3 | 2:27b92722246b | 86 | /* Reads all the data in whenever the ticker interrupt is called */ |
Jdumpleton3 | 2:27b92722246b | 87 | void readData(){ |
Jdumpleton3 | 2:27b92722246b | 88 | //Check if old data has been printed if it hasn't don't do anything |
Jdumpleton3 | 2:27b92722246b | 89 | //As you have interrupted printdata section. |
Jdumpleton3 | 2:27b92722246b | 90 | if (isNewData == false){ |
jedward | 3:d17f07a0d08d | 91 | //Random data CHANGE ME |
Jdumpleton3 | 4:edef041d2094 | 92 | |
Jdumpleton3 | 2:27b92722246b | 93 | dataGen.ReadTempHumi(&tempCelsius, &humi); |
Jdumpleton3 | 2:27b92722246b | 94 | |
Jdumpleton3 | 2:27b92722246b | 95 | barometerPressure = dataGen.pressure(); |
Jdumpleton3 | 2:27b92722246b | 96 | barometerTemperature = dataGen.temperature(); |
Jdumpleton3 | 2:27b92722246b | 97 | |
jedward | 3:d17f07a0d08d | 98 | tempEntry.airTemp = tempCelsius; |
jedward | 3:d17f07a0d08d | 99 | tempEntry.airPress = humi; |
jedward | 3:d17f07a0d08d | 100 | tempEntry.barTemp = barometerTemperature; |
Jdumpleton3 | 4:edef041d2094 | 101 | tempEntry.barPress = barometerPressure; |
jedward | 3:d17f07a0d08d | 102 | |
Jdumpleton3 | 2:27b92722246b | 103 | isNewData = true; |
Jdumpleton3 | 2:27b92722246b | 104 | } |
Jdumpleton3 | 2:27b92722246b | 105 | } |
Jdumpleton3 | 2:27b92722246b | 106 | /* Prints the data that was read */ |
Jdumpleton3 | 2:27b92722246b | 107 | void printData(){ |
Jdumpleton3 | 5:ba160e9778d0 | 108 | printf("%s", ctime(&tempEntry.dt)); |
Jdumpleton3 | 5:ba160e9778d0 | 109 | printf("%i: %4.2fC %3.1f%%", headNode, tempEntry.airTemp, tempEntry.airPress); |
jedward | 3:d17f07a0d08d | 110 | printf(" %6.1f %4.1f\r\n", tempEntry.barPress, tempEntry.barTemp); |
Jdumpleton3 | 2:27b92722246b | 111 | isNewData = false; |
Jdumpleton3 | 5:ba160e9778d0 | 112 | } |
Jdumpleton3 | 0:b490b8486404 | 113 | |
Jdumpleton3 | 0:b490b8486404 | 114 | |
Jdumpleton3 | 0:b490b8486404 | 115 | |
Jdumpleton3 | 2:27b92722246b | 116 | /* Old Code |
Jdumpleton3 | 2:27b92722246b | 117 | LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR); |
Jdumpleton3 | 2:27b92722246b | 118 | HTS221 humidity(I2C_SDA, I2C_SCL); |
Jdumpleton3 | 0:b490b8486404 | 119 | |
Jdumpleton3 | 2:27b92722246b | 120 | Humidity sensor setup. |
Jdumpleton3 | 2:27b92722246b | 121 | humidity.init(); |
Jdumpleton3 | 2:27b92722246b | 122 | humidity.calib(); |
Jdumpleton3 | 2:27b92722246b | 123 | |
Jdumpleton3 | 2:27b92722246b | 124 | //printf("%#x\n\r",barometer.read_id()); |
Jdumpleton3 | 2:27b92722246b | 125 | |
Jdumpleton3 | 2:27b92722246b | 126 | barometer.get(); |
Jdumpleton3 | 2:27b92722246b | 127 | printf(" %6.1f %4.1f\r\n", barometer.pressure(), barometer.temperature()); |
Jdumpleton3 | 2:27b92722246b | 128 | humidity.ReadTempHumi(&tempCelsius, &humi); |
Jdumpleton3 | 2:27b92722246b | 129 | printf("%4.2fC %3.1f%%", tempCelsius, humi); |
Jdumpleton3 | 2:27b92722246b | 130 | */ |