DS1820 DS18B20 ESP01 Webserver STM32NucleoL152RE basiert auf dem DS1820-Example von Paul Staron
Dependencies: LCD_i2c_GSOE ESP8266WebserverGSOE DS1820
main.cpp@0:a3c589406dc4, 2020-12-29 (annotated)
- Committer:
- star297
- Date:
- Tue Dec 29 13:08:22 2020 +0000
- Revision:
- 0:a3c589406dc4
- Child:
- 2:2c947079aae0
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
star297 | 0:a3c589406dc4 | 1 | #include "mbed.h" |
star297 | 0:a3c589406dc4 | 2 | #include "DS1820.h" |
star297 | 0:a3c589406dc4 | 3 | |
star297 | 0:a3c589406dc4 | 4 | DigitalOut led(LED1); |
star297 | 0:a3c589406dc4 | 5 | |
star297 | 0:a3c589406dc4 | 6 | #define DATA_PIN A0 |
star297 | 0:a3c589406dc4 | 7 | #define MAX_PROBES 16 |
star297 | 0:a3c589406dc4 | 8 | DS1820* probe[MAX_PROBES]; |
star297 | 0:a3c589406dc4 | 9 | |
star297 | 0:a3c589406dc4 | 10 | Timer t; |
star297 | 0:a3c589406dc4 | 11 | |
star297 | 0:a3c589406dc4 | 12 | int DS; |
star297 | 0:a3c589406dc4 | 13 | float Temp[16]; |
star297 | 0:a3c589406dc4 | 14 | |
star297 | 0:a3c589406dc4 | 15 | int main(){ |
star297 | 0:a3c589406dc4 | 16 | |
star297 | 0:a3c589406dc4 | 17 | printf("\033[0m\033[2J\033[HInitialise...!\n\n"); |
star297 | 0:a3c589406dc4 | 18 | |
star297 | 0:a3c589406dc4 | 19 | while (DS1820::unassignedProbe(DATA_PIN)) { |
star297 | 0:a3c589406dc4 | 20 | probe[DS] = new DS1820(DATA_PIN); |
star297 | 0:a3c589406dc4 | 21 | DS++; |
star297 | 0:a3c589406dc4 | 22 | if (DS == MAX_PROBES) { |
star297 | 0:a3c589406dc4 | 23 | break; |
star297 | 0:a3c589406dc4 | 24 | } |
star297 | 0:a3c589406dc4 | 25 | } |
star297 | 0:a3c589406dc4 | 26 | |
star297 | 0:a3c589406dc4 | 27 | if (!DS) { |
star297 | 0:a3c589406dc4 | 28 | printf("No Sensors found!\n\n"); |
star297 | 0:a3c589406dc4 | 29 | ThisThread::sleep_for(chrono::milliseconds(1000)); |
star297 | 0:a3c589406dc4 | 30 | NVIC_SystemReset(); |
star297 | 0:a3c589406dc4 | 31 | } |
star297 | 0:a3c589406dc4 | 32 | |
star297 | 0:a3c589406dc4 | 33 | // set each probe resolution, default is 12bit (750ms) |
star297 | 0:a3c589406dc4 | 34 | probe[0]->setResolution(9); |
star297 | 0:a3c589406dc4 | 35 | // probe[0]->setResolution(10); |
star297 | 0:a3c589406dc4 | 36 | // probe[0]->setResolution(11); |
star297 | 0:a3c589406dc4 | 37 | // probe[0]->setResolution(12); |
star297 | 0:a3c589406dc4 | 38 | // probe[1]->setResolution(9); |
star297 | 0:a3c589406dc4 | 39 | // probe[2]->setResolution(10); |
star297 | 0:a3c589406dc4 | 40 | |
star297 | 0:a3c589406dc4 | 41 | t.start(); |
star297 | 0:a3c589406dc4 | 42 | |
star297 | 0:a3c589406dc4 | 43 | while(1) { |
star297 | 0:a3c589406dc4 | 44 | |
star297 | 0:a3c589406dc4 | 45 | printf("\033[0m\033[2J\033[HDS Sensor data..\n\n"); |
star297 | 0:a3c589406dc4 | 46 | |
star297 | 0:a3c589406dc4 | 47 | int DS_error = 0; |
star297 | 0:a3c589406dc4 | 48 | for (int i = 0; i < DS; i++) { |
star297 | 0:a3c589406dc4 | 49 | Temp[i] = probe[i]->temperature(); |
star297 | 0:a3c589406dc4 | 50 | if(Temp[i]==-1000) { |
star297 | 0:a3c589406dc4 | 51 | Temp[i] = probe[i]->temperature(); // get read temp again if error |
star297 | 0:a3c589406dc4 | 52 | DS_error++; |
star297 | 0:a3c589406dc4 | 53 | } |
star297 | 0:a3c589406dc4 | 54 | printf("Probe %d: %3.2f %cc\r\n",i,Temp[i],0xb0); |
star297 | 0:a3c589406dc4 | 55 | } |
star297 | 0:a3c589406dc4 | 56 | printf("\nDS errors: %d\n\n", DS_error); |
star297 | 0:a3c589406dc4 | 57 | |
star297 | 0:a3c589406dc4 | 58 | printf("Start conversion\n"); |
star297 | 0:a3c589406dc4 | 59 | t.reset(); |
star297 | 0:a3c589406dc4 | 60 | // don't wait for conversion, but do something that takes at least 750ms before reading the sensors |
star297 | 0:a3c589406dc4 | 61 | //if(DS>0){probe[0]->convertTemperature(0, DS1820::all_devices);} |
star297 | 0:a3c589406dc4 | 62 | |
star297 | 0:a3c589406dc4 | 63 | // wait for conversion, can take up to 750ms(12 bit mode) |
star297 | 0:a3c589406dc4 | 64 | if(DS>0){probe[0]->convertTemperature(1, DS1820::all_devices);} |
star297 | 0:a3c589406dc4 | 65 | |
star297 | 0:a3c589406dc4 | 66 | printf("\nConvert process time: %0.6f Seconds\n", chrono::duration<float>(t.elapsed_time()).count()); |
star297 | 0:a3c589406dc4 | 67 | |
star297 | 0:a3c589406dc4 | 68 | ThisThread::sleep_for(chrono::milliseconds(1000)); |
star297 | 0:a3c589406dc4 | 69 | } |
star297 | 0:a3c589406dc4 | 70 | } |