delay 10s
Dependencies: HX711 DHT DS1820
main.cpp@0:2e0fc2c1e24c, 2020-10-12 (annotated)
- Committer:
- ismatel77
- Date:
- Mon Oct 12 10:37:18 2020 +0000
- Revision:
- 0:2e0fc2c1e24c
- Child:
- 1:b811fd6ee6f5
projet b-hive;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ismatel77 | 0:2e0fc2c1e24c | 1 | #include "mbed.h" |
ismatel77 | 0:2e0fc2c1e24c | 2 | #include "DHT.h" |
ismatel77 | 0:2e0fc2c1e24c | 3 | #include "DS1820.h" |
ismatel77 | 0:2e0fc2c1e24c | 4 | |
ismatel77 | 0:2e0fc2c1e24c | 5 | #define DHT_DATA_PIN D4 |
ismatel77 | 0:2e0fc2c1e24c | 6 | |
ismatel77 | 0:2e0fc2c1e24c | 7 | DS1820 ds1820(D3); // substitute D8 with the actual pin name connected to the DS1820 sensor |
ismatel77 | 0:2e0fc2c1e24c | 8 | float temp = 0; |
ismatel77 | 0:2e0fc2c1e24c | 9 | int result = 0; |
ismatel77 | 0:2e0fc2c1e24c | 10 | |
ismatel77 | 0:2e0fc2c1e24c | 11 | DHT sensor(DHT_DATA_PIN, DHT22); //DHT(PinName pin, eType DHTtype) |
ismatel77 | 0:2e0fc2c1e24c | 12 | Serial pc(USBTX, USBRX); // tx, rx |
ismatel77 | 0:2e0fc2c1e24c | 13 | |
ismatel77 | 0:2e0fc2c1e24c | 14 | Serial device(D1, D0); // tx, rx |
ismatel77 | 0:2e0fc2c1e24c | 15 | |
ismatel77 | 0:2e0fc2c1e24c | 16 | int main() |
ismatel77 | 0:2e0fc2c1e24c | 17 | { |
ismatel77 | 0:2e0fc2c1e24c | 18 | int error = 0; |
ismatel77 | 0:2e0fc2c1e24c | 19 | float h = 0.0f, c = 0.0f; |
ismatel77 | 0:2e0fc2c1e24c | 20 | pc.baud(9600); |
ismatel77 | 0:2e0fc2c1e24c | 21 | device.baud(9600); |
ismatel77 | 0:2e0fc2c1e24c | 22 | |
ismatel77 | 0:2e0fc2c1e24c | 23 | if (!ds1820.begin()){ |
ismatel77 | 0:2e0fc2c1e24c | 24 | pc.printf("No DS1820 sensor found!\r\n"); |
ismatel77 | 0:2e0fc2c1e24c | 25 | } |
ismatel77 | 0:2e0fc2c1e24c | 26 | else{ |
ismatel77 | 0:2e0fc2c1e24c | 27 | pc.printf("DS1820 sensor found!\r\n"); |
ismatel77 | 0:2e0fc2c1e24c | 28 | } |
ismatel77 | 0:2e0fc2c1e24c | 29 | |
ismatel77 | 0:2e0fc2c1e24c | 30 | while(1) { |
ismatel77 | 0:2e0fc2c1e24c | 31 | |
ismatel77 | 0:2e0fc2c1e24c | 32 | ds1820.startConversion(); // start temperature conversion from analog to digital |
ismatel77 | 0:2e0fc2c1e24c | 33 | ThisThread::sleep_for(1000);// let DS1820 complete the temperature conversion |
ismatel77 | 0:2e0fc2c1e24c | 34 | result = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC) |
ismatel77 | 0:2e0fc2c1e24c | 35 | error = sensor.readData(); //read error value |
ismatel77 | 0:2e0fc2c1e24c | 36 | |
ismatel77 | 0:2e0fc2c1e24c | 37 | c = sensor.ReadTemperature(CELCIUS); |
ismatel77 | 0:2e0fc2c1e24c | 38 | h = sensor.ReadHumidity(); |
ismatel77 | 0:2e0fc2c1e24c | 39 | pc.printf("temperature = %4.2f\r\n", c); // on affiche les valeurs sur teraTerm pour debug |
ismatel77 | 0:2e0fc2c1e24c | 40 | pc.printf("Humidite = %4.2f\r\n", h); |
ismatel77 | 0:2e0fc2c1e24c | 41 | |
ismatel77 | 0:2e0fc2c1e24c | 42 | switch (result) { |
ismatel77 | 0:2e0fc2c1e24c | 43 | case 0: // no errors -> 'temp' contains the value of measured temperature |
ismatel77 | 0:2e0fc2c1e24c | 44 | pc.printf("temp DS18B20 = %3.1f\r\n", temp); |
ismatel77 | 0:2e0fc2c1e24c | 45 | break; |
ismatel77 | 0:2e0fc2c1e24c | 46 | |
ismatel77 | 0:2e0fc2c1e24c | 47 | case 1: // no sensor present -> 'temp' was not updated |
ismatel77 | 0:2e0fc2c1e24c | 48 | pc.printf("no sensor present\n\r"); |
ismatel77 | 0:2e0fc2c1e24c | 49 | break; |
ismatel77 | 0:2e0fc2c1e24c | 50 | |
ismatel77 | 0:2e0fc2c1e24c | 51 | case 2: // CRC error -> 'temp' was not updated |
ismatel77 | 0:2e0fc2c1e24c | 52 | pc.printf("CRC error\r\n"); |
ismatel77 | 0:2e0fc2c1e24c | 53 | } |
ismatel77 | 0:2e0fc2c1e24c | 54 | device.printf("AT$SF=%02X%02X%02X\r\n", (char) c,(char) h, (char) temp); // on envoie les données sur l'antenne |
ismatel77 | 0:2e0fc2c1e24c | 55 | wait(2.0f); |
ismatel77 | 0:2e0fc2c1e24c | 56 | } |
ismatel77 | 0:2e0fc2c1e24c | 57 | } |