Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: LCD_i2c_GSOE ESP8266MQTT
main.cpp
00001 00002 #include "mbed.h" 00003 #include "PubSubClient.h" //MQTT- Bibliothek 00004 #include "LCD.h" 00005 00006 00007 00008 #define DHTLIB_OK 0 00009 #define DHTLIB_ERROR_CHECKSUM -1 00010 #define DHTLIB_ERROR_TIMEOUT -2 00011 00012 Timer tmr; 00013 lcd mylcd; 00014 00015 00016 PubSubClient client; //Deklaration des MQTT-Clients 00017 DigitalInOut data_pin(PB_0); //Anschluss S (Data) 00018 //Anschluss - an GND 00019 //mittlerer Anschluss an 3,3V 00020 00021 int humidity; 00022 int temperature; 00023 00024 //######################################## 00025 // DHT11 Library 00026 //######################################## 00027 int dht_read(void){ 00028 00029 // BUFFER TO RECEIVE 00030 uint8_t bits[5]; 00031 uint8_t cnt = 7; 00032 uint8_t idx = 0; 00033 00034 tmr.stop(); 00035 tmr.reset(); 00036 00037 // EMPTY BUFFER 00038 for(int i=0; i< 5; i++) bits[i] = 0; 00039 00040 // REQUEST SAMPLE 00041 data_pin.output(); 00042 data_pin.write(0); 00043 wait_us(18000); 00044 data_pin.write(1); 00045 wait_us(40); 00046 data_pin.input(); 00047 00048 // ACKNOWLEDGE or TIMEOUT 00049 unsigned int loopCnt = 20000; 00050 00051 while(!data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT; 00052 00053 loopCnt = 20000; 00054 00055 while(data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT; 00056 00057 // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT 00058 for(int i=0; i<40; i++){ 00059 00060 loopCnt = 20000; 00061 00062 while(!data_pin.read())if(loopCnt-- == 0)return DHTLIB_ERROR_TIMEOUT; 00063 00064 //unsigned long t = micros(); 00065 tmr.start(); 00066 00067 loopCnt = 20000; 00068 00069 while(data_pin.read())if(!loopCnt--)return DHTLIB_ERROR_TIMEOUT; 00070 00071 if(tmr.read_us() > 40) bits[idx] |= (1 << cnt); 00072 00073 tmr.stop(); 00074 tmr.reset(); 00075 00076 if(cnt == 0){ // next byte? 00077 00078 cnt = 7; // restart at MSB 00079 idx++; // next byte! 00080 00081 }else cnt--; 00082 00083 } 00084 00085 // WRITE TO RIGHT VARS 00086 // as bits[1] and bits[3] are allways zero they are omitted in formulas. 00087 humidity = bits[0]; 00088 temperature = bits[2]; 00089 00090 uint8_t sum = bits[0] + bits[2]; 00091 00092 if(bits[4] != sum)return DHTLIB_ERROR_CHECKSUM; 00093 00094 return DHTLIB_OK; 00095 00096 } 00097 00098 char buffer[20]; 00099 00100 //######################################## 00101 // End of DHT11 Library 00102 //######################################## 00103 00104 //Subscribe-Callbacks 00105 void subscribeCallback(MessageData& mymessage) 00106 { 00107 mylcd.cursorpos(0); 00108 //Der Inhalt einer Botschaft (payload) kann als String mit gibPayload 00109 //abgerufen werden 00110 mylcd.printf("%s ",client.gibPayload(mymessage).c_str()); 00111 } 00112 00113 void subscribeCallback2(MessageData& mymessage) 00114 { 00115 mylcd.cursorpos(0x40); 00116 mylcd.printf("%s ",client.gibPayload(mymessage).c_str()); 00117 } 00118 00119 00120 int main(void){ 00121 00122 mylcd.clear(); 00123 mylcd.cursorpos(0); 00124 00125 mylcd.printf("Nucleo - DHT11"); 00126 00127 client.connect((char*)"joerg"); //Verbinde Client mit ID 00128 //Client für topic einschreiben 00129 client.subscribe("MBED/oehringen/temp", QOS0, subscribeCallback); 00130 client.subscribe("MBED/oehringen/hum", QOS1, subscribeCallback2); 00131 00132 for(;;){ 00133 client.loop(); //Auf neue Botschaften prüfen 00134 int fehler=dht_read(); 00135 //Temperatur veröffentlichen 00136 sprintf(buffer, "Temp=%2dC", temperature); //Temperaturwert in buffer speichern 00137 client.publish("MBED/oehringen/temp", buffer, QOS1,true); //unter Topic veröffentlichen 00138 //HAL_Delay(200); 00139 //Luftfeuchte veröffentlichen 00140 sprintf(buffer, "Luftfeuchte=%d", humidity); //diag in buffer speichern 00141 client.publish("MBED/oehringen/hum", buffer,QOS2,true); //unter Topic veröffentlichen 00142 00143 00144 00145 wait_us(500000); 00146 00147 00148 00149 } 00150 00151 }
Generated on Thu Aug 4 2022 22:27:59 by
1.7.2