ESP8266 SHT21 sensor
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ESP-Weather.ino 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * ESP-Weather.ino
  3. *
  4. * This is the main program for our distributed temperature and humidity logger
  5. * with WiFi interface, based on the ESP8266 ESP-01 module and an SHT21 sensor.
  6. *
  7. * ----------------------------------------------------------------------------
  8. * "THE BEER-WARE LICENSE" (Revision 42):
  9. * <xythobuz@xythobuz.de> & <ghost-ghost@web.de> wrote this file. As long as
  10. * you retain this notice you can do whatever you want with this stuff. If we
  11. * meet some day, and you think this stuff is worth it, you can buy us a beer
  12. * in return. Thomas Buck & Christian Högerle
  13. * ----------------------------------------------------------------------------
  14. */
  15. #include <ESP8266WiFi.h>
  16. #include <WiFiClient.h>
  17. #include <ESP8266WebServer.h>
  18. #include <WiFiUdp.h>
  19. #include <SHT21.h>
  20. #include <vector>
  21. #include <WebSocketServer.h>
  22. #include <WiFiServer.h>
  23. #include <DNSServer.h>
  24. #include <WiFiManager.h>
  25. #include "config.h"
  26. #include "ntp.h"
  27. #include "static.h"
  28. #include "storage.h"
  29. #define DEBUG
  30. SHT21 sensor;
  31. ESP8266WebServer server(WEB_PORT);
  32. WiFiServer serverSocket(WEBSOCKET_PORT);
  33. WebSocketServer webSocketServer;
  34. IPAddress broadcastIP;
  35. WiFiUDP udp;
  36. PersistentStorage storage;
  37. std::vector<IPAddress> vecClients;
  38. char packetBuffer[UDP_PACKET_BUFFER_SIZE];
  39. unsigned long lastStorageTime = 0;
  40. byte storeAtBoot = 1;
  41. unsigned long lastTime = 0;
  42. bool waitingForReplies = false;
  43. static void handleRoot() {
  44. #ifdef DEBUG
  45. Serial.println("Sending UDP Broadcast...");
  46. #endif // DEBUG
  47. // Send UDP broadcast to other modules
  48. udp.beginPacket(broadcastIP, BROADCAST_PORT);
  49. udp.write(UDP_PING_CONTENTS);
  50. udp.endPacket();
  51. // Start reply wait timer
  52. lastTime = millis();
  53. waitingForReplies = true;
  54. }
  55. static void handleJS() {
  56. String message = F(JS_FILE);
  57. server.send(200, "text/javascript", message);
  58. }
  59. static void handleCSS() {
  60. String message = F(CSS_FILE);
  61. server.send(200, "text/css", message);
  62. }
  63. static void handleFavicon() {
  64. server.send_P(200, faviconMimeType, (PGM_P)favicon, faviconSize);
  65. }
  66. static void handleNotFound() {
  67. String message = "File Not Found\n\n";
  68. message += "URI: ";
  69. message += server.uri();
  70. message += "\nMethod: ";
  71. message += (server.method() == HTTP_GET)?"GET":"POST";
  72. message += "\nArguments: ";
  73. message += server.args();
  74. message += "\n";
  75. for (uint8_t i = 0; i < server.args(); i++){
  76. message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  77. }
  78. server.send(404, "text/plain", message);
  79. }
  80. void setup(void) {
  81. Serial.begin(DEBUG_BAUDRATE);
  82. #ifdef DEBUG
  83. Serial.println();
  84. Serial.println("ESP-Weather init...");
  85. #endif // DEBUG
  86. //sensor.begin();
  87. // The SHT library is simpy calling Wire.begin(), but the default
  88. // config does not match the pins i'm using (sda - 2; scl - 0)
  89. Wire.begin(2, 0);
  90. // Here you can override the WiFiManager configuration. Leave
  91. // the default autoConnect in use for the default behaviour.
  92. // To force the config portal, even though the module was connected before,
  93. // comment-out autoConnect and use startConfigPortal instead.
  94. WiFiManager wifiManager;
  95. wifiManager.autoConnect(DEFAULT_SSID, DEFAULT_PASS);
  96. //wifiManager.startConfigPortal(DEFAULT_SSID, DEFAULT_PASS);
  97. initMemory();
  98. storage = readMemory();
  99. // Wait for connection
  100. if (WiFi.status() != WL_CONNECTED) {
  101. while (WiFi.status() != WL_CONNECTED) {
  102. delay(500);
  103. #ifdef DEBUG
  104. Serial.print(".");
  105. #endif // DEBUG
  106. }
  107. #ifdef DEBUG
  108. Serial.println("");
  109. #endif // DEBUG
  110. }
  111. #ifdef DEBUG
  112. Serial.print("IP address: ");
  113. Serial.println(WiFi.localIP());
  114. #endif // DEBUG
  115. broadcastIP = ~WiFi.subnetMask() | WiFi.gatewayIP();
  116. server.on("/", handleRoot);
  117. server.on("/index.html", handleRoot);
  118. server.on("/view.js", handleJS);
  119. server.on("/style.css", handleCSS);
  120. server.on("/favicon.ico", handleFavicon);
  121. server.onNotFound(handleNotFound);
  122. server.begin();
  123. serverSocket.begin();
  124. ntpInit();
  125. udp.begin(BROADCAST_PORT);
  126. #ifdef DEBUG
  127. Serial.println("ESP-Weather ready!");
  128. #endif // DEBUG
  129. }
  130. void loop(void) {
  131. ntpRun();
  132. server.handleClient();
  133. // Websocket fuer Browser
  134. WiFiClient client = serverSocket.available();
  135. if (client.connected() && webSocketServer.handshake(client)) {
  136. Serial.println("Building WebSocket Response...");
  137. String json = "{\"H\":";
  138. json += String(sensor.getHumidity());
  139. json += ",";
  140. json += "\"T\":";
  141. json += String(sensor.getTemperature());
  142. json += ", \"EEPROM\" : [";
  143. for (int i = 0; i < storage.header.count; i++) {
  144. json += "{\"H\":";
  145. json += String(storage.data[i].humidity);
  146. json += ",";
  147. json += "\"T\":";
  148. json += String(storage.data[i].temperature);
  149. json += "}";
  150. if (i < storage.header.count - 1) {
  151. json += ",";
  152. }
  153. }
  154. json += "]}";
  155. #ifdef DEBUG
  156. Serial.println("WebSocket Response:");
  157. Serial.println(json);
  158. #endif // DEBUG
  159. webSocketServer.sendData(json);
  160. client.flush();
  161. client.stop();
  162. }
  163. // EEPROM-Schreiben jede Stunde
  164. if ((((((millis() - timeReceived) / 1000) + timestamp) % 3600) == 0)
  165. && (timestamp != 0) && (((millis() - lastStorageTime) > 100000) || storeAtBoot) ) {
  166. #ifdef DEBUG
  167. Serial.println("Storing new data packet...");
  168. #endif // DEBUG
  169. lastStorageTime = millis();
  170. storeAtBoot = 0;
  171. if (storage.header.count < MAX_STORAGE) {
  172. storage.header.count++;
  173. } else {
  174. for(int i = 0; i < MAX_STORAGE - 1; i++) {
  175. storage.data[i] = storage.data[i+1];
  176. }
  177. }
  178. storage.data[storage.header.count - 1].temperature = sensor.getTemperature();
  179. storage.data[storage.header.count - 1].humidity = sensor.getHumidity();
  180. writeMemory(storage);
  181. }
  182. // UDP
  183. int packetSize = udp.parsePacket();
  184. if (packetSize) {
  185. IPAddress remoteIp = udp.remoteIP();
  186. // read the packet into packetBufffer
  187. int len = udp.read(packetBuffer, UDP_PACKET_BUFFER_SIZE);
  188. if (len > 0) {
  189. packetBuffer[len] = 0;
  190. }
  191. #ifdef DEBUG
  192. Serial.print("Got UDP packet: ");
  193. Serial.println(packetBuffer);
  194. #endif // DEBUG
  195. if (strcmp(packetBuffer, UDP_PING_CONTENTS) == 0) {
  196. #ifdef DEBUG
  197. Serial.println("Broadcast");
  198. #endif // DEBUG
  199. udp.beginPacket(udp.remoteIP(), udp.remotePort());
  200. udp.print(UDP_ECHO_CONTENTS);
  201. udp.endPacket();
  202. } else if((strcmp(packetBuffer, UDP_ECHO_CONTENTS) == 0) && (waitingForReplies == true)) {
  203. vecClients.push_back(udp.remoteIP());
  204. }
  205. }
  206. if (((millis() - lastTime) >= MAX_BROADCAST_WAIT_TIME) && (waitingForReplies == true)) {
  207. #ifdef DEBUG
  208. Serial.println("Timeout, sending response...");
  209. #endif // DEBUG
  210. waitingForReplies = false;
  211. String message = F(HTML_BEGIN);
  212. message += "var clients = Array(";
  213. message += "\"" + WiFi.localIP().toString() + "\"";
  214. if (vecClients.size() > 0) {
  215. message += ", ";
  216. }
  217. for (int i = 0; i < vecClients.size(); i++) {
  218. message += "\"" + vecClients[i].toString() + "\"";
  219. if (i < (vecClients.size() - 1)) {
  220. message += ", ";
  221. }
  222. }
  223. message += ");";
  224. message += F(HTML_END);
  225. vecClients.clear();
  226. server.send(200, "text/html", message);
  227. }
  228. }