ESP8266 SHT21 sensor
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ESP-Weather.ino 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <WiFiUdp.h>
  5. #include <SHT21.h>
  6. #include <vector>
  7. #include <WebSocketServer.h>
  8. #include <WiFiServer.h>
  9. #include <DNSServer.h>
  10. #include <WiFiManager.h>
  11. #include "config.h"
  12. #include "ntp.h"
  13. #include "storage.h"
  14. SHT21 sensor;
  15. ESP8266WebServer server(WEB_PORT);
  16. WiFiServer serverSocket(WEBSOCKET_PORT);
  17. WebSocketServer webSocketServer;
  18. IPAddress broadcastIP;
  19. WiFiUDP udp;
  20. PersistentStorage storage;
  21. std::vector<IPAddress> vecClients;
  22. char packetBuffer[UDP_PACKET_BUFFER_SIZE];
  23. unsigned long lastStorageTime = 0;
  24. byte storeAtBoot = 1;
  25. unsigned long lastTime = 0;
  26. bool waitingForReplies = false;
  27. void handleRoot() {
  28. Serial.println("Sending UDP Broadcast...");
  29. // Send UDP broadcast to other modules
  30. udp.beginPacket(broadcastIP, BROADCAST_PORT);
  31. udp.write(UDP_PING_CONTENTS);
  32. udp.endPacket();
  33. // Start reply wait timer
  34. lastTime = millis();
  35. waitingForReplies = true;
  36. }
  37. void handleNotFound() {
  38. String message = "File Not Found\n\n";
  39. message += "URI: ";
  40. message += server.uri();
  41. message += "\nMethod: ";
  42. message += (server.method() == HTTP_GET)?"GET":"POST";
  43. message += "\nArguments: ";
  44. message += server.args();
  45. message += "\n";
  46. for (uint8_t i = 0; i < server.args(); i++){
  47. message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  48. }
  49. server.send(404, "text/plain", message);
  50. }
  51. void setup(void) {
  52. // Debugging
  53. Serial.begin(115200);
  54. Serial.println();
  55. Serial.println("ESP-Weather init...");
  56. //sensor.begin();
  57. // The SHT library is simpy calling Wire.begin(), but the default
  58. // config does not match the pins i'm using (sda - 2; scl - 0)
  59. Wire.begin(2, 0);
  60. // Here you can override the WiFiManager configuration. Leave
  61. // the default autoConnect in use for the default behaviour.
  62. // To force the config portal, even though the module was connected before,
  63. // comment-out autoConnect and use startConfigPortal instead.
  64. WiFiManager wifiManager;
  65. wifiManager.autoConnect(DEFAULT_SSID, DEFAULT_PASS);
  66. //wifiManager.startConfigPortal(DEFAULT_SSID, DEFAULT_PASS);
  67. initMemory();
  68. storage = readMemory();
  69. // Wait for connection
  70. if (WiFi.status() != WL_CONNECTED) {
  71. while (WiFi.status() != WL_CONNECTED) {
  72. delay(500);
  73. Serial.print(".");
  74. }
  75. Serial.println("");
  76. }
  77. Serial.print("IP address: ");
  78. Serial.println(WiFi.localIP());
  79. broadcastIP = ~WiFi.subnetMask() | WiFi.gatewayIP();
  80. server.on("/", handleRoot);
  81. server.onNotFound(handleNotFound);
  82. server.begin();
  83. serverSocket.begin();
  84. ntpInit();
  85. udp.begin(BROADCAST_PORT);
  86. Serial.println("ESP-Weather ready!");
  87. }
  88. void loop(void) {
  89. ntpRun();
  90. server.handleClient();
  91. // Websocket fuer Browser
  92. WiFiClient client = serverSocket.available();
  93. if (client.connected() && webSocketServer.handshake(client)) {
  94. Serial.println("Building WebSocket Response...");
  95. String json = "{\"H\":";
  96. json += String(sensor.getHumidity());
  97. json += ",";
  98. json += "\"T\":";
  99. json += String(sensor.getTemperature());
  100. json += ", \"EEPROM\" : [";
  101. for (int i = 0; i < storage.header.count; i++) {
  102. json += "{\"H\":";
  103. json += String(storage.data[i].humidity);
  104. json += ",";
  105. json += "\"T\":";
  106. json += String(storage.data[i].temperature);
  107. json += "}";
  108. if (i < storage.header.count - 1) {
  109. json += ",";
  110. }
  111. }
  112. json += "]}";
  113. Serial.println("WebSocket Response:");
  114. Serial.println(json);
  115. webSocketServer.sendData(json);
  116. client.flush();
  117. client.stop();
  118. }
  119. // EEPROM-Schreiben jede Stunde
  120. if ((((((millis() - timeReceived) / 1000) + timestamp) % 3600) == 0)
  121. && (timestamp != 0) && (((millis() - lastStorageTime) > 100000) || storeAtBoot) ) {
  122. Serial.println("Storing new data packet...");
  123. lastStorageTime = millis();
  124. storeAtBoot = 0;
  125. if (storage.header.count < MAX_STORAGE) {
  126. storage.header.count++;
  127. } else {
  128. for(int i = 0; i < MAX_STORAGE - 1; i++) {
  129. storage.data[i] = storage.data[i+1];
  130. }
  131. }
  132. storage.data[storage.header.count - 1].temperature = sensor.getTemperature();
  133. storage.data[storage.header.count - 1].humidity = sensor.getHumidity();
  134. writeMemory(storage);
  135. }
  136. // UDP
  137. int packetSize = udp.parsePacket();
  138. if (packetSize) {
  139. IPAddress remoteIp = udp.remoteIP();
  140. // read the packet into packetBufffer
  141. int len = udp.read(packetBuffer, UDP_PACKET_BUFFER_SIZE);
  142. if (len > 0) {
  143. packetBuffer[len] = 0;
  144. }
  145. Serial.print("Got UDP packet: ");
  146. Serial.println(packetBuffer);
  147. if (strcmp(packetBuffer, UDP_PING_CONTENTS) == 0) {
  148. Serial.println("Broadcast");
  149. udp.beginPacket(udp.remoteIP(), udp.remotePort());
  150. udp.print(UDP_ECHO_CONTENTS);
  151. udp.endPacket();
  152. } else if((strcmp(packetBuffer, UDP_ECHO_CONTENTS) == 0) && (waitingForReplies == true)) {
  153. vecClients.push_back(udp.remoteIP());
  154. }
  155. }
  156. if (((millis() - lastTime) >= MAX_BROADCAST_WAIT_TIME) && (waitingForReplies == true)) {
  157. Serial.println("Timeout, sending response...");
  158. waitingForReplies = false;
  159. String message = HTML_BEGIN;
  160. message += "var clients = Array(";
  161. message += "\"" + WiFi.localIP().toString() + "\"";
  162. if (vecClients.size() > 0) {
  163. message += ", ";
  164. }
  165. for (int i = 0; i < vecClients.size(); i++) {
  166. message += "\"" + vecClients[i].toString() + "\"";
  167. if (i < (vecClients.size() - 1)) {
  168. message += ", ";
  169. }
  170. }
  171. message += ");";
  172. message += HTML_END;
  173. vecClients.clear();
  174. server.send(200, "text/html", message);
  175. }
  176. }