DS1820 DS18B20 ESP01 Webserver STM32NucleoL152RE basiert auf dem DS1820-Example von Paul Staron

Dependencies:   LCD_i2c_GSOE ESP8266WebserverGSOE DS1820

Committer:
jack1930
Date:
Sat Aug 07 18:33:20 2021 +0000
Revision:
3:33bd03e2244f
Parent:
2:2c947079aae0
DS1820 mit Webserver

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jack1930 2:2c947079aae0 1 //basiert auf dem DS1820-Example von Paul Staron
jack1930 2:2c947079aae0 2
star297 0:a3c589406dc4 3 #include "mbed.h"
star297 0:a3c589406dc4 4 #include "DS1820.h"
jack1930 2:2c947079aae0 5 #include "LCD.h"
jack1930 3:33bd03e2244f 6 #include "ESP8266Webserver.h"
jack1930 3:33bd03e2244f 7
star297 0:a3c589406dc4 8
star297 0:a3c589406dc4 9 DigitalOut led(LED1);
jack1930 3:33bd03e2244f 10 lcd myLCD;
star297 0:a3c589406dc4 11
jack1930 2:2c947079aae0 12 #define DATA_PIN PB_0
star297 0:a3c589406dc4 13 #define MAX_PROBES 16
star297 0:a3c589406dc4 14 DS1820* probe[MAX_PROBES];
star297 0:a3c589406dc4 15
jack1930 3:33bd03e2244f 16 ESP8266Webserver myWebserver;
jack1930 3:33bd03e2244f 17
star297 0:a3c589406dc4 18 Timer t;
star297 0:a3c589406dc4 19
star297 0:a3c589406dc4 20 int DS;
star297 0:a3c589406dc4 21 float Temp[16];
star297 0:a3c589406dc4 22
jack1930 3:33bd03e2244f 23 string getRootPage()
jack1930 3:33bd03e2244f 24 {
jack1930 3:33bd03e2244f 25 string webpage;
jack1930 3:33bd03e2244f 26 webpage="<!DOCTYPE html>";
jack1930 3:33bd03e2244f 27 //HTML
jack1930 3:33bd03e2244f 28 webpage+="<html>";
jack1930 3:33bd03e2244f 29 webpage+="<head>";
jack1930 3:33bd03e2244f 30 webpage+="<title>STM32 HTTP</title>";
jack1930 3:33bd03e2244f 31 webpage+="</head>";
jack1930 3:33bd03e2244f 32 webpage+="<body>";
jack1930 3:33bd03e2244f 33 webpage+="<h1>WIFI mit STM32 ESP01</h1>\n";
jack1930 3:33bd03e2244f 34 webpage+="<p>Temperatur= "+to_string(Temp[0])+"C</p>\n";
jack1930 3:33bd03e2244f 35 webpage+="</body>";
jack1930 3:33bd03e2244f 36 webpage+="</html>";
jack1930 3:33bd03e2244f 37 return webpage;
jack1930 3:33bd03e2244f 38 }
jack1930 3:33bd03e2244f 39
jack1930 3:33bd03e2244f 40 void testfunc()
jack1930 3:33bd03e2244f 41 {
jack1930 3:33bd03e2244f 42 myWebserver.send(200,"text/html",getRootPage());
jack1930 3:33bd03e2244f 43 }
jack1930 3:33bd03e2244f 44
star297 0:a3c589406dc4 45 int main(){
jack1930 3:33bd03e2244f 46 myWebserver.on("/",&testfunc);
jack1930 3:33bd03e2244f 47 myWebserver.begin();
jack1930 3:33bd03e2244f 48 myLCD.clear();
jack1930 3:33bd03e2244f 49 myLCD.cursorpos(0);
jack1930 3:33bd03e2244f 50 myLCD.printf("%s",myWebserver.gibIP());
jack1930 3:33bd03e2244f 51
star297 0:a3c589406dc4 52 printf("\033[0m\033[2J\033[HInitialise...!\n\n");
star297 0:a3c589406dc4 53
star297 0:a3c589406dc4 54 while (DS1820::unassignedProbe(DATA_PIN)) {
star297 0:a3c589406dc4 55 probe[DS] = new DS1820(DATA_PIN);
star297 0:a3c589406dc4 56 DS++;
star297 0:a3c589406dc4 57 if (DS == MAX_PROBES) {
star297 0:a3c589406dc4 58 break;
star297 0:a3c589406dc4 59 }
star297 0:a3c589406dc4 60 }
star297 0:a3c589406dc4 61
star297 0:a3c589406dc4 62 if (!DS) {
star297 0:a3c589406dc4 63 printf("No Sensors found!\n\n");
star297 0:a3c589406dc4 64 ThisThread::sleep_for(chrono::milliseconds(1000));
star297 0:a3c589406dc4 65 NVIC_SystemReset();
star297 0:a3c589406dc4 66 }
star297 0:a3c589406dc4 67
star297 0:a3c589406dc4 68 // set each probe resolution, default is 12bit (750ms)
star297 0:a3c589406dc4 69 probe[0]->setResolution(9);
star297 0:a3c589406dc4 70 // probe[0]->setResolution(10);
star297 0:a3c589406dc4 71 // probe[0]->setResolution(11);
star297 0:a3c589406dc4 72 // probe[0]->setResolution(12);
star297 0:a3c589406dc4 73 // probe[1]->setResolution(9);
star297 0:a3c589406dc4 74 // probe[2]->setResolution(10);
star297 0:a3c589406dc4 75
star297 0:a3c589406dc4 76 t.start();
star297 0:a3c589406dc4 77
star297 0:a3c589406dc4 78 while(1) {
jack1930 3:33bd03e2244f 79 myWebserver.handleClient();
star297 0:a3c589406dc4 80 printf("\033[0m\033[2J\033[HDS Sensor data..\n\n");
star297 0:a3c589406dc4 81
star297 0:a3c589406dc4 82 int DS_error = 0;
star297 0:a3c589406dc4 83 for (int i = 0; i < DS; i++) {
star297 0:a3c589406dc4 84 Temp[i] = probe[i]->temperature();
star297 0:a3c589406dc4 85 if(Temp[i]==-1000) {
star297 0:a3c589406dc4 86 Temp[i] = probe[i]->temperature(); // get read temp again if error
star297 0:a3c589406dc4 87 DS_error++;
star297 0:a3c589406dc4 88 }
star297 0:a3c589406dc4 89 printf("Probe %d: %3.2f %cc\r\n",i,Temp[i],0xb0);
jack1930 3:33bd03e2244f 90
jack1930 3:33bd03e2244f 91 myLCD.cursorpos(0x40);
jack1930 3:33bd03e2244f 92 myLCD.printf("%3.1f ",Temp[0]);
star297 0:a3c589406dc4 93 }
star297 0:a3c589406dc4 94 printf("\nDS errors: %d\n\n", DS_error);
star297 0:a3c589406dc4 95
star297 0:a3c589406dc4 96 printf("Start conversion\n");
star297 0:a3c589406dc4 97 t.reset();
star297 0:a3c589406dc4 98 // don't wait for conversion, but do something that takes at least 750ms before reading the sensors
star297 0:a3c589406dc4 99 //if(DS>0){probe[0]->convertTemperature(0, DS1820::all_devices);}
star297 0:a3c589406dc4 100
star297 0:a3c589406dc4 101 // wait for conversion, can take up to 750ms(12 bit mode)
star297 0:a3c589406dc4 102 if(DS>0){probe[0]->convertTemperature(1, DS1820::all_devices);}
star297 0:a3c589406dc4 103
star297 0:a3c589406dc4 104 printf("\nConvert process time: %0.6f Seconds\n", chrono::duration<float>(t.elapsed_time()).count());
star297 0:a3c589406dc4 105
star297 0:a3c589406dc4 106 ThisThread::sleep_for(chrono::milliseconds(1000));
star297 0:a3c589406dc4 107 }
star297 0:a3c589406dc4 108 }