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 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 <EEPROM.h>
  10. #include <DNSServer.h>
  11. #include <WiFiManager.h>
  12. #define NTP_PORT 2392
  13. #define MAX_WAIT_TIME 550
  14. #define EEPROM_SIZE 512
  15. // NTP-Client
  16. IPAddress timeServerIP;
  17. const char* ntpServerName = "time.nist.gov";
  18. const int NTP_PACKET_SIZE = 48;
  19. byte ntpPacketBuffer[NTP_PACKET_SIZE];
  20. WiFiUDP ntp;
  21. unsigned long timestamp = 0;
  22. unsigned long timeReceived = 0;
  23. unsigned long lastStorageTime = 0;
  24. byte storeAtBoot = 1;
  25. unsigned long lastNTP = 0;
  26. SHT21 SHT21;
  27. ESP8266WebServer server(80);
  28. WiFiServer serverSocket(2391);
  29. WebSocketServer webSocketServer;
  30. #define DEFAULT_SSID "ESP-Weather"
  31. #define DEFAULT_PASS "testtest"
  32. IPAddress broadcastIP;
  33. struct __attribute__((__packed__)) Measurement {
  34. float temperature;
  35. float humidity;
  36. };
  37. struct __attribute__((__packed__)) Header {
  38. uint16_t count;
  39. uint16_t checksum;
  40. };
  41. #define MAX_STORAGE (EEPROM_SIZE - sizeof(Header)) / sizeof(Measurement)
  42. struct __attribute__((__packed__)) PersistentStorage {
  43. Measurement data[MAX_STORAGE];
  44. Header header;
  45. };
  46. PersistentStorage storage;
  47. void writeMemory(PersistentStorage &s) {
  48. Serial.println("write Memory");
  49. unsigned char* r = (unsigned char*) &s;
  50. uint16_t a = 0, b = 0;
  51. for(int i = 0; i < sizeof(PersistentStorage) - 2; i++) {
  52. a = (a + r[i]) % 255;
  53. b = (b + a) % 255;
  54. }
  55. s.header.checksum = (b << 8) | a;
  56. for(int i = 0; i < sizeof(PersistentStorage); i++) {
  57. EEPROM.write(i, r[i]);
  58. }
  59. EEPROM.commit();
  60. }
  61. PersistentStorage readMemory() {
  62. PersistentStorage s;
  63. unsigned char* r = (unsigned char*) &s;
  64. for(int i = 0; i < sizeof(PersistentStorage); i++) {
  65. r[i] = EEPROM.read(i);
  66. }
  67. uint16_t a = 0, b = 0;
  68. for(int i = 0; i < sizeof(PersistentStorage) - 2; i++) {
  69. a = (a + r[i]) % 255;
  70. b = (b + a) % 255;
  71. }
  72. if(s.header.checksum != ((b << 8) | a)) {
  73. Serial.print("Checksum error ");
  74. Serial.print(s.header.checksum);
  75. Serial.print(" ");
  76. Serial.println((b << 8) | a);
  77. s.header.count = 0;
  78. } else {
  79. Serial.println("Checksum ok");
  80. }
  81. return s;
  82. }
  83. std::vector<IPAddress> vecClients;
  84. // UDP-Config
  85. unsigned int localPort = 2390;
  86. char packetBuffer[255];
  87. char pingBuffer[] = "pingESP8266v0.1";
  88. char echoBuffer[] = "echoESP8266v0.1";
  89. WiFiUDP Udp;
  90. unsigned long lastTime;
  91. bool waitingForReplies = false;
  92. const char* htmlBegin = "<html>\
  93. <head>\
  94. <title>Sysadmin</title>\
  95. <script src=\"http://hoegerle-home.de/sysAdmin/js/jquery-3.1.1.min.js\"></script>\
  96. <script src=\"http://hoegerle-home.de/sysAdmin/js/bootstrap.min.js\"></script>\
  97. <script src=\"http://hoegerle-home.de/sysAdmin/js/Chart.bundle.min.js\"></script>\
  98. <script src=\"http://hoegerle-home.de/sysAdmin/js/script.js\"></script>\
  99. <link rel=\"stylesheet\" href=\"http://hoegerle-home.de/sysAdmin/css/bootstrap.min.css\" />\
  100. </head>\
  101. <body>\
  102. <script type=\"text/javascript\">";
  103. const char* htmlEnd = "</script>\
  104. </body>\
  105. </html>";
  106. // root-URL
  107. void handleRoot() {
  108. // send a reply, to the IP address and port that sent us the packet we received
  109. Udp.beginPacket(broadcastIP, 2390);
  110. Udp.write(pingBuffer);
  111. Udp.endPacket();
  112. // Timer starten
  113. lastTime = millis();
  114. waitingForReplies = true;
  115. Serial.println("Sending UDP Broadcast...");
  116. }
  117. // URL nicht vorhanden
  118. void handleNotFound(){
  119. String message = "File Not Found\n\n";
  120. message += "URI: ";
  121. message += server.uri();
  122. message += "\nMethod: ";
  123. message += (server.method() == HTTP_GET)?"GET":"POST";
  124. message += "\nArguments: ";
  125. message += server.args();
  126. message += "\n";
  127. for (uint8_t i=0; i<server.args(); i++){
  128. message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  129. }
  130. server.send(404, "text/plain", message);
  131. }
  132. void setup(void) {
  133. EEPROM.begin(EEPROM_SIZE);
  134. // Debugging
  135. Serial.begin(115200);
  136. Serial.println();
  137. Serial.println("ESP-Weather init...");
  138. //SHT21.begin();
  139. // The SHT library is simpy calling Wire.begin(), but the default
  140. // config does not match the pins i'm using (sda - 2; scl - 0)
  141. Wire.begin(2, 0);
  142. WiFiManager wifiManager;
  143. // use one or the other, never both!
  144. wifiManager.autoConnect(DEFAULT_SSID, DEFAULT_PASS);
  145. //wifiManager.startConfigPortal(DEFAULT_SSID, DEFAULT_PASS);
  146. storage = readMemory();
  147. // Wait for connection
  148. while (WiFi.status() != WL_CONNECTED) {
  149. delay(500);
  150. Serial.print(".");
  151. }
  152. Serial.println("");
  153. Serial.print("IP address: ");
  154. Serial.println(WiFi.localIP());
  155. broadcastIP = ~WiFi.subnetMask() | WiFi.gatewayIP();
  156. server.on("/", handleRoot);
  157. server.onNotFound(handleNotFound);
  158. server.begin();
  159. serverSocket.begin();
  160. // NTP-Client
  161. ntp.begin(NTP_PORT);
  162. WiFi.hostByName(ntpServerName, timeServerIP);
  163. lastNTP = millis();
  164. sendNTPpacket(timeServerIP);
  165. Udp.begin(localPort);
  166. Serial.println("ESP-Weather ready!");
  167. }
  168. void loop(void){
  169. server.handleClient();
  170. // Websocket fuer Browser
  171. WiFiClient client = serverSocket.available();
  172. if (client.connected() && webSocketServer.handshake(client)) {
  173. Serial.println("Building WebSocket Response...");
  174. String json = "{\"H\":";
  175. json += String(SHT21.getHumidity());
  176. json += ",";
  177. json += "\"T\":";
  178. json += String(SHT21.getTemperature());
  179. json += ", \"EEPROM\" : [";
  180. for (int i = 0; i < storage.header.count; i++) {
  181. json += "{\"H\":";
  182. json += String(storage.data[i].humidity);
  183. json += ",";
  184. json += "\"T\":";
  185. json += String(storage.data[i].temperature);
  186. json += "}";
  187. if (i < storage.header.count - 1) {
  188. json += ",";
  189. }
  190. }
  191. json += "]}";
  192. Serial.println("WebSocket Response:");
  193. Serial.println(json);
  194. webSocketServer.sendData(json);
  195. client.flush();
  196. client.stop();
  197. }
  198. // NTP wiederholen falls keine Antwort
  199. if ((timestamp == 0) && ((millis() - lastNTP) > 2000)) {
  200. Serial.println("NTP packet retry...");
  201. WiFi.hostByName(ntpServerName, timeServerIP);
  202. lastNTP = millis();
  203. sendNTPpacket(timeServerIP);
  204. }
  205. // NTP Paket vom Server erhalten
  206. if (ntp.parsePacket() >= NTP_PACKET_SIZE) {
  207. ntp.read(ntpPacketBuffer, NTP_PACKET_SIZE);
  208. unsigned long highWord = word(ntpPacketBuffer[40], ntpPacketBuffer[41]);
  209. unsigned long lowWord = word(ntpPacketBuffer[42], ntpPacketBuffer[43]);
  210. unsigned long secsSince1900 = highWord << 16 | lowWord;
  211. const unsigned long seventyYears = 2208988800UL;
  212. unsigned long epoch = secsSince1900 - seventyYears;
  213. timestamp = epoch;
  214. timeReceived = millis();
  215. Serial.print("Got NTP time: ");
  216. Serial.println(epoch);
  217. }
  218. // EEPROM-Schreiben jede Stunde
  219. if ((((((millis() - timeReceived) / 1000) + timestamp) % 3600) == 0)
  220. && (timestamp != 0) && (((millis() - lastStorageTime) > 100000) || storeAtBoot) ) {
  221. Serial.println("Storing new data packet...");
  222. lastStorageTime = millis();
  223. storeAtBoot = 0;
  224. if (storage.header.count < MAX_STORAGE) {
  225. storage.header.count++;
  226. } else {
  227. for(int i = 0; i < MAX_STORAGE - 1; i++) {
  228. storage.data[i] = storage.data[i+1];
  229. }
  230. }
  231. storage.data[storage.header.count-1].temperature = SHT21.getTemperature();
  232. storage.data[storage.header.count-1].humidity = SHT21.getHumidity();
  233. writeMemory(storage);
  234. }
  235. // UDP
  236. int packetSize = Udp.parsePacket();
  237. if (packetSize) {
  238. IPAddress remoteIp = Udp.remoteIP();
  239. // read the packet into packetBufffer
  240. int len = Udp.read(packetBuffer, 255);
  241. if (len > 0) {
  242. packetBuffer[len] = 0;
  243. }
  244. Serial.print("Got UDP packet: ");
  245. Serial.println(packetBuffer);
  246. if (strcmp(packetBuffer, pingBuffer) == 0) {
  247. Serial.println("Broadcast");
  248. Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  249. Udp.print(echoBuffer);
  250. Udp.endPacket();
  251. } else if((strcmp(packetBuffer, echoBuffer) == 0) && (waitingForReplies == true)) {
  252. vecClients.push_back(Udp.remoteIP());
  253. }
  254. }
  255. if (((millis() - lastTime) >= MAX_WAIT_TIME) && (waitingForReplies == true)) {
  256. Serial.println("Timeout, sending response...");
  257. waitingForReplies = false;
  258. String message = htmlBegin;
  259. message += "var clients = Array(";
  260. message += "\"" + WiFi.localIP().toString() + "\"";
  261. if(vecClients.size() > 0) {
  262. message += ", ";
  263. }
  264. for(int i = 0; i < vecClients.size(); i++) {
  265. message += "\"" + vecClients[i].toString() + "\"";
  266. if(i < (vecClients.size() - 1)) {
  267. message += ", ";
  268. }
  269. }
  270. message += ");";
  271. message += htmlEnd;
  272. vecClients.clear();
  273. server.send(200, "text/html", message);
  274. }
  275. }
  276. void sendNTPpacket(IPAddress& address) {
  277. Serial.println("Sending NTP packet...");
  278. memset(ntpPacketBuffer, 0, NTP_PACKET_SIZE);
  279. ntpPacketBuffer[0] = 0b11100011; // LI, Version, Mode
  280. ntpPacketBuffer[1] = 0;
  281. ntpPacketBuffer[2] = 6;
  282. ntpPacketBuffer[3] = 0xEC;
  283. ntpPacketBuffer[12] = 49;
  284. ntpPacketBuffer[13] = 0x4E;
  285. ntpPacketBuffer[14] = 49;
  286. ntpPacketBuffer[15] = 52;
  287. ntp.beginPacket(address, 123);
  288. ntp.write(ntpPacketBuffer, NTP_PACKET_SIZE);
  289. ntp.endPacket();
  290. }