NMF Atlantis
/
NMFAtlantis_Final
Code required to drive the microcontroller of the NMF Atlantis
main.cpp@0:dbaef09f6d82, 2020-01-20 (annotated)
- Committer:
- nmf_atlantis
- Date:
- Mon Jan 20 15:47:07 2020 +0000
- Revision:
- 0:dbaef09f6d82
NMF Atlantis final version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nmf_atlantis | 0:dbaef09f6d82 | 1 | #include "mbed.h" |
nmf_atlantis | 0:dbaef09f6d82 | 2 | #include "DHT.h" |
nmf_atlantis | 0:dbaef09f6d82 | 3 | #include "Pulse.h" |
nmf_atlantis | 0:dbaef09f6d82 | 4 | #include "RangeFinder.h" |
nmf_atlantis | 0:dbaef09f6d82 | 5 | #include "WakeUp.h" |
nmf_atlantis | 0:dbaef09f6d82 | 6 | |
nmf_atlantis | 0:dbaef09f6d82 | 7 | |
nmf_atlantis | 0:dbaef09f6d82 | 8 | RangeFinder rf(D12, 10, 5800, 100000); //(Pin, duree du pulse en us, distance [m] = pulse width [us] / 5800 [us / m] , temps de fonctionnement) |
nmf_atlantis | 0:dbaef09f6d82 | 9 | |
nmf_atlantis | 0:dbaef09f6d82 | 10 | DigitalOut led(LED1); |
nmf_atlantis | 0:dbaef09f6d82 | 11 | Serial serial(D1, D0); |
nmf_atlantis | 0:dbaef09f6d82 | 12 | Serial pc(USBTX, USBRX); |
nmf_atlantis | 0:dbaef09f6d82 | 13 | DHT HTSensor(D10,22); |
nmf_atlantis | 0:dbaef09f6d82 | 14 | AnalogIn battery(A1); |
nmf_atlantis | 0:dbaef09f6d82 | 15 | |
nmf_atlantis | 0:dbaef09f6d82 | 16 | |
nmf_atlantis | 0:dbaef09f6d82 | 17 | int main() |
nmf_atlantis | 0:dbaef09f6d82 | 18 | { |
nmf_atlantis | 0:dbaef09f6d82 | 19 | |
nmf_atlantis | 0:dbaef09f6d82 | 20 | pc.printf("Starting...\n\r\n\r"); |
nmf_atlantis | 0:dbaef09f6d82 | 21 | |
nmf_atlantis | 0:dbaef09f6d82 | 22 | float d; |
nmf_atlantis | 0:dbaef09f6d82 | 23 | int dInt; |
nmf_atlantis | 0:dbaef09f6d82 | 24 | |
nmf_atlantis | 0:dbaef09f6d82 | 25 | float temperature; |
nmf_atlantis | 0:dbaef09f6d82 | 26 | int tInt; |
nmf_atlantis | 0:dbaef09f6d82 | 27 | float humidity; |
nmf_atlantis | 0:dbaef09f6d82 | 28 | int hInt; |
nmf_atlantis | 0:dbaef09f6d82 | 29 | float b; |
nmf_atlantis | 0:dbaef09f6d82 | 30 | int bInt; |
nmf_atlantis | 0:dbaef09f6d82 | 31 | int cpt = 0; |
nmf_atlantis | 0:dbaef09f6d82 | 32 | |
nmf_atlantis | 0:dbaef09f6d82 | 33 | |
nmf_atlantis | 0:dbaef09f6d82 | 34 | // Depending on the waterway |
nmf_atlantis | 0:dbaef09f6d82 | 35 | float sensor_limit = 4.0*100.0; |
nmf_atlantis | 0:dbaef09f6d82 | 36 | float danger_limit = 50.0; |
nmf_atlantis | 0:dbaef09f6d82 | 37 | int danger_time = 15; |
nmf_atlantis | 0:dbaef09f6d82 | 38 | int default_time = 30; |
nmf_atlantis | 0:dbaef09f6d82 | 39 | int sending_interval = 3600; //every hour (1h = 3600 s) |
nmf_atlantis | 0:dbaef09f6d82 | 40 | |
nmf_atlantis | 0:dbaef09f6d82 | 41 | while (1) |
nmf_atlantis | 0:dbaef09f6d82 | 42 | { |
nmf_atlantis | 0:dbaef09f6d82 | 43 | //reduced battery level measurement (between 0 V et 1.85 V) |
nmf_atlantis | 0:dbaef09f6d82 | 44 | b = battery.read()*3.3f; |
nmf_atlantis | 0:dbaef09f6d82 | 45 | bInt = (int)(b*100/3.3f); |
nmf_atlantis | 0:dbaef09f6d82 | 46 | |
nmf_atlantis | 0:dbaef09f6d82 | 47 | /*pc.printf("Voltage = %f volts\r\n\r\n", b); |
nmf_atlantis | 0:dbaef09f6d82 | 48 | pc.printf("Voltage code = %d percentage\r\n\r\n", bInt);*/ |
nmf_atlantis | 0:dbaef09f6d82 | 49 | |
nmf_atlantis | 0:dbaef09f6d82 | 50 | //Humidity and temperature measurement |
nmf_atlantis | 0:dbaef09f6d82 | 51 | |
nmf_atlantis | 0:dbaef09f6d82 | 52 | HTSensor.readData(); |
nmf_atlantis | 0:dbaef09f6d82 | 53 | wait_us(500); |
nmf_atlantis | 0:dbaef09f6d82 | 54 | |
nmf_atlantis | 0:dbaef09f6d82 | 55 | temperature = HTSensor.ReadTemperature(KELVIN) - 273,15; |
nmf_atlantis | 0:dbaef09f6d82 | 56 | tInt = (int) (temperature + 40); |
nmf_atlantis | 0:dbaef09f6d82 | 57 | humidity = HTSensor.ReadHumidity(); |
nmf_atlantis | 0:dbaef09f6d82 | 58 | hInt = (int) humidity; |
nmf_atlantis | 0:dbaef09f6d82 | 59 | |
nmf_atlantis | 0:dbaef09f6d82 | 60 | //level measurement |
nmf_atlantis | 0:dbaef09f6d82 | 61 | d = rf.read_m(); |
nmf_atlantis | 0:dbaef09f6d82 | 62 | d = d*100; |
nmf_atlantis | 0:dbaef09f6d82 | 63 | if (d < 0) |
nmf_atlantis | 0:dbaef09f6d82 | 64 | { |
nmf_atlantis | 0:dbaef09f6d82 | 65 | d = 0; |
nmf_atlantis | 0:dbaef09f6d82 | 66 | //Timeout Error |
nmf_atlantis | 0:dbaef09f6d82 | 67 | WakeUp::set(default_time); //time in second |
nmf_atlantis | 0:dbaef09f6d82 | 68 | deepsleep(); |
nmf_atlantis | 0:dbaef09f6d82 | 69 | cpt += default_time; |
nmf_atlantis | 0:dbaef09f6d82 | 70 | |
nmf_atlantis | 0:dbaef09f6d82 | 71 | } |
nmf_atlantis | 0:dbaef09f6d82 | 72 | else if (d >= sensor_limit) |
nmf_atlantis | 0:dbaef09f6d82 | 73 | { |
nmf_atlantis | 0:dbaef09f6d82 | 74 | // Seeed's sensor has a maximum range of 4m, it returns |
nmf_atlantis | 0:dbaef09f6d82 | 75 | // something like 7m if the ultrasound pulse isn't reflected. |
nmf_atlantis | 0:dbaef09f6d82 | 76 | d = sensor_limit; |
nmf_atlantis | 0:dbaef09f6d82 | 77 | WakeUp::set(default_time); //time in second |
nmf_atlantis | 0:dbaef09f6d82 | 78 | deepsleep(); |
nmf_atlantis | 0:dbaef09f6d82 | 79 | cpt += Temps_default; |
nmf_atlantis | 0:dbaef09f6d82 | 80 | } |
nmf_atlantis | 0:dbaef09f6d82 | 81 | else if (d <= danger_limit) //Inondation |
nmf_atlantis | 0:dbaef09f6d82 | 82 | { |
nmf_atlantis | 0:dbaef09f6d82 | 83 | dInt = (int)d; |
nmf_atlantis | 0:dbaef09f6d82 | 84 | //Data sending |
nmf_atlantis | 0:dbaef09f6d82 | 85 | serial.printf("AT$SF=%04x%02x%02x%02x\r\n",dInt,tInt,hInt, bInt); |
nmf_atlantis | 0:dbaef09f6d82 | 86 | |
nmf_atlantis | 0:dbaef09f6d82 | 87 | WakeUp::set(danger_time); //time in second |
nmf_atlantis | 0:dbaef09f6d82 | 88 | deepsleep(); |
nmf_atlantis | 0:dbaef09f6d82 | 89 | cpt += danger_time; |
nmf_atlantis | 0:dbaef09f6d82 | 90 | } |
nmf_atlantis | 0:dbaef09f6d82 | 91 | else |
nmf_atlantis | 0:dbaef09f6d82 | 92 | { |
nmf_atlantis | 0:dbaef09f6d82 | 93 | //Data sending |
nmf_atlantis | 0:dbaef09f6d82 | 94 | WakeUp::set(default_time); //time in second |
nmf_atlantis | 0:dbaef09f6d82 | 95 | deepsleep(); |
nmf_atlantis | 0:dbaef09f6d82 | 96 | cpt += default_time; |
nmf_atlantis | 0:dbaef09f6d82 | 97 | } |
nmf_atlantis | 0:dbaef09f6d82 | 98 | |
nmf_atlantis | 0:dbaef09f6d82 | 99 | if (cpt >= sending_interval) |
nmf_atlantis | 0:dbaef09f6d82 | 100 | { |
nmf_atlantis | 0:dbaef09f6d82 | 101 | dInt = (int)d; |
nmf_atlantis | 0:dbaef09f6d82 | 102 | serial.printf("AT$SF=%04x%02x%02x%02x\r\n",dInt,tInt,hInt, bInt); |
nmf_atlantis | 0:dbaef09f6d82 | 103 | cpt = 0; |
nmf_atlantis | 0:dbaef09f6d82 | 104 | } |
nmf_atlantis | 0:dbaef09f6d82 | 105 | |
nmf_atlantis | 0:dbaef09f6d82 | 106 | } |
nmf_atlantis | 0:dbaef09f6d82 | 107 | } |