Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

3 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <Arduino.h>
  4. #include <WiFi.h>
  5. #include "ESPAsyncWebServer.h"
  6. #include <ESPDash.h>
  7. #include <ESPmDNS.h>
  8. #include <Button2.h>
  9. #include <Wire.h>
  10. #include <dht.h>
  11. #include <Adafruit_BME280.h>
  12. #include <WiFiMulti.h>
  13. #include "esp_wifi.h"
  14. #define ARDUINOJSON_USE_LONG_LONG 0
  15. //#define SOFTAP_MODE
  16. // #define USE_18B20_TEMP_SENSOR
  17. // #define USE_CHINESE_WEB
  18. // Simple ds18b20 class
  19. class DS18B20
  20. {
  21. public:
  22. DS18B20(int gpio)
  23. {
  24. pin = gpio;
  25. }
  26. float temp()
  27. {
  28. uint8_t arr[2] = {0};
  29. if (reset()) {
  30. wByte(0xCC);
  31. wByte(0x44);
  32. delay(750);
  33. reset();
  34. wByte(0xCC);
  35. wByte(0xBE);
  36. arr[0] = rByte();
  37. arr[1] = rByte();
  38. reset();
  39. return (float)(arr[0] + (arr[1] * 256)) / 16;
  40. }
  41. return 0;
  42. }
  43. private:
  44. int pin;
  45. void write(uint8_t bit)
  46. {
  47. pinMode(pin, OUTPUT);
  48. digitalWrite(pin, LOW);
  49. delayMicroseconds(5);
  50. if (bit)digitalWrite(pin, HIGH);
  51. delayMicroseconds(80);
  52. digitalWrite(pin, HIGH);
  53. }
  54. uint8_t read()
  55. {
  56. pinMode(pin, OUTPUT);
  57. digitalWrite(pin, LOW);
  58. delayMicroseconds(2);
  59. digitalWrite(pin, HIGH);
  60. delayMicroseconds(15);
  61. pinMode(pin, INPUT);
  62. return digitalRead(pin);
  63. }
  64. void wByte(uint8_t bytes)
  65. {
  66. for (int i = 0; i < 8; ++i) {
  67. write((bytes >> i) & 1);
  68. }
  69. delayMicroseconds(100);
  70. }
  71. uint8_t rByte()
  72. {
  73. uint8_t r = 0;
  74. for (int i = 0; i < 8; ++i) {
  75. if (read()) r |= 1 << i;
  76. delayMicroseconds(15);
  77. }
  78. return r;
  79. }
  80. bool reset()
  81. {
  82. pinMode(pin, OUTPUT);
  83. digitalWrite(pin, LOW);
  84. delayMicroseconds(500);
  85. digitalWrite(pin, HIGH);
  86. pinMode(pin, INPUT);
  87. delayMicroseconds(500);
  88. return digitalRead(pin);
  89. }
  90. };
  91. #define I2C_SDA 25
  92. #define I2C_SCL 26
  93. #define dht11_PIN 16
  94. #define BAT_ADC 33
  95. #define SALT_PIN 34
  96. #define SOIL_PIN 32
  97. #define BOOT_PIN 0
  98. #define POWER_CTRL 4
  99. #define USER_BUTTON 35
  100. #define DS18B20_PIN 21 //18b20 data pin
  101. //BH1750 lightMeter(0x23); //0x23
  102. Adafruit_BME280 bmp; //0x77
  103. DHT dht11(dht11_PIN, DHT11);
  104. AsyncWebServer server(80);
  105. Button2 button(BOOT_PIN);
  106. Button2 useButton(USER_BUTTON);
  107. WiFiMulti multi;
  108. DS18B20 temp18B20(DS18B20_PIN);
  109. #define WIFI_SSID "iot"
  110. #define WIFI_PASSWD "iot4Grabner"
  111. bool bme_found = false;
  112. void smartConfigStart(Button2 &b)
  113. {
  114. Serial.println("smartConfigStart...");
  115. WiFi.disconnect();
  116. WiFi.beginSmartConfig();
  117. while (!WiFi.smartConfigDone()) {
  118. Serial.print(".");
  119. delay(200);
  120. }
  121. WiFi.stopSmartConfig();
  122. Serial.println();
  123. Serial.print("smartConfigStop Connected:");
  124. Serial.print(WiFi.SSID());
  125. Serial.print("PSW: ");
  126. Serial.println(WiFi.psk());
  127. }
  128. void sleepHandler(Button2 &b)
  129. {
  130. Serial.println("Enter Deepsleep ...");
  131. esp_sleep_enable_ext1_wakeup(GPIO_SEL_35, ESP_EXT1_WAKEUP_ALL_LOW);
  132. delay(1000);
  133. esp_deep_sleep_start();
  134. }
  135. bool serverBegin()
  136. {
  137. static bool isBegin = false;
  138. if (isBegin) {
  139. return true;
  140. }
  141. ESPDash.init(server);
  142. isBegin = true;
  143. if (MDNS.begin("soil")) {
  144. Serial.println("MDNS responder started");
  145. }
  146. // Add Respective Cards
  147. if (bme_found) {
  148. #ifdef USE_CHINESE_WEB
  149. ESPDash.addTemperatureCard("temp", "BME传感器温度/C", 0, 0);
  150. ESPDash.addNumberCard("press", "BME传感器压力/hPa", 0);
  151. ESPDash.addNumberCard("alt", "BME传感器高度/m", 0);
  152. #else
  153. ESPDash.addTemperatureCard("temp", "BME Temperature/C", 0, 0);
  154. ESPDash.addNumberCard("press", "BME Pressure/hPa", 0);
  155. ESPDash.addNumberCard("alt", "BME Altitude/m", 0);
  156. #endif
  157. }
  158. #ifdef USE_CHINESE_WEB
  159. ESPDash.addTemperatureCard("temp2", "dht11传感器温度/C", 0, 0);
  160. ESPDash.addHumidityCard("hum2", "dht11传感器湿度/%", 0);
  161. ESPDash.addNumberCard("lux", "BH1750传感器亮度/lx", 0);
  162. ESPDash.addHumidityCard("soil", "土壤湿度", 0);
  163. ESPDash.addNumberCard("salt", "水分百分比", 0);
  164. ESPDash.addNumberCard("batt", "电池电压/mV", 0);
  165. #else
  166. ESPDash.addTemperatureCard("temp2", "DHT Temperature/C", 0, 0);
  167. ESPDash.addHumidityCard("hum2", "DHT Humidity/%", 0);
  168. ESPDash.addNumberCard("lux", "BH1750/lx", 0);
  169. ESPDash.addHumidityCard("soil", "Soil", 0);
  170. ESPDash.addNumberCard("salt", "Salt", 0);
  171. ESPDash.addNumberCard("batt", "Battery/mV", 0);
  172. #endif
  173. #ifdef USE_18B20_TEMP_SENSOR
  174. ESPDash.addTemperatureCard("temp3", "18B20温度/C", 0, 0);
  175. #endif
  176. ESPDash.addTemperatureCard("temp3", "18B20 Temperature/C", 0, 0);
  177. server.begin();
  178. MDNS.addService("http", "tcp", 80);
  179. return true;
  180. }
  181. void setup()
  182. {
  183. Serial.begin(115200);
  184. #ifdef SOFTAP_MODE
  185. Serial.println("Configuring access point...");
  186. uint8_t mac[6];
  187. char buff[128];
  188. esp_wifi_get_mac(WIFI_IF_AP, mac);
  189. sprintf(buff, "T-Higrow-%02X:%02X", mac[4], mac[5]);
  190. WiFi.softAP(buff);
  191. #else
  192. WiFi.mode(WIFI_STA);
  193. wifi_config_t current_conf;
  194. esp_wifi_get_config(WIFI_IF_STA, &current_conf);
  195. int ssidlen = strlen((char *)(current_conf.sta.ssid));
  196. int passlen = strlen((char *)(current_conf.sta.password));
  197. if (ssidlen == 0 || passlen == 0) {
  198. multi.addAP(WIFI_SSID, WIFI_PASSWD);
  199. Serial.println("Connect to defalut ssid, you can long press BOOT button enter smart config mode");
  200. while (multi.run() != WL_CONNECTED) {
  201. Serial.print('.');
  202. }
  203. } else {
  204. WiFi.begin();
  205. }
  206. if (WiFi.waitForConnectResult() != WL_CONNECTED) {
  207. Serial.printf("WiFi connect fail!,please restart retry,or long press BOOT button enter smart config mode\n");
  208. }
  209. if (WiFi.status() == WL_CONNECTED) {
  210. Serial.print("IP Address: ");
  211. Serial.println(WiFi.localIP());
  212. }
  213. #endif
  214. button.setLongClickHandler(smartConfigStart);
  215. useButton.setLongClickHandler(sleepHandler);
  216. Wire.begin(I2C_SDA, I2C_SCL);
  217. dht11.begin();
  218. //! Sensor power control pin , use deteced must set high
  219. pinMode(POWER_CTRL, OUTPUT);
  220. digitalWrite(POWER_CTRL, 1);
  221. delay(1000);
  222. if (!bmp.begin()) {
  223. Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
  224. bme_found = false;
  225. } else {
  226. bme_found = true;
  227. }
  228. }
  229. uint32_t readSalt()
  230. {
  231. uint8_t samples = 120;
  232. uint32_t humi = 0;
  233. uint16_t array[120];
  234. for (int i = 0; i < samples; i++) {
  235. array[i] = analogRead(SALT_PIN);
  236. delay(2);
  237. }
  238. std::sort(array, array + samples);
  239. for (int i = 0; i < samples; i++) {
  240. if (i == 0 || i == samples - 1)continue;
  241. humi += array[i];
  242. }
  243. humi /= samples - 2;
  244. return humi;
  245. }
  246. uint16_t readSoil()
  247. {
  248. uint16_t soil = analogRead(SOIL_PIN);
  249. return map(soil, 0, 4095, 100, 0);
  250. }
  251. float readBattery()
  252. {
  253. int vref = 1100;
  254. uint16_t volt = analogRead(BAT_ADC);
  255. float battery_voltage = ((float)volt / 4095.0) * 2.0 * 3.3 * (vref);
  256. return battery_voltage;
  257. }
  258. void loop()
  259. {
  260. static uint64_t timestamp;
  261. button.loop();
  262. useButton.loop();
  263. if (millis() - timestamp > 1000 ) {
  264. timestamp = millis();
  265. // if (WiFi.status() == WL_CONNECTED) {
  266. if (serverBegin()) {
  267. if (bme_found) {
  268. float bme_temp = bmp.readTemperature();
  269. float bme_pressure = (bmp.readPressure() / 100.0F);
  270. float bme_altitude = bmp.readAltitude(1013.25);
  271. ESPDash.updateTemperatureCard("temp", (int)bme_temp);
  272. ESPDash.updateNumberCard("press", (int)bme_pressure);
  273. ESPDash.updateNumberCard("alt", (int)bme_altitude);
  274. }
  275. float t12 = dht11.readTemperature();
  276. // Read temperature as Fahrenheit (isFahrenheit = true)
  277. float h12 = dht11.readHumidity();
  278. if (!isnan(t12) && !isnan(h12) ) {
  279. ESPDash.updateTemperatureCard("temp2", (int)t12);
  280. ESPDash.updateHumidityCard("hum2", (int)h12);
  281. }
  282. uint16_t soil = readSoil();
  283. uint32_t salt = readSalt();
  284. float bat = readBattery();
  285. ESPDash.updateHumidityCard("soil", (int)soil);
  286. ESPDash.updateNumberCard("salt", (int)salt);
  287. ESPDash.updateNumberCard("batt", (int)bat);
  288. #ifdef USE_18B20_TEMP_SENSOR
  289. //Single data stream upload
  290. float temp = temp18B20.temp();
  291. ESPDash.updateTemperatureCard("temp3", (int)temp);
  292. #endif
  293. }
  294. }
  295. }