Advertisement
Guest User

Untitled

a guest
Jul 16th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.07 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WiFiMulti.h>
  3. #include <WiFiUdp.h>
  4. #include <ArduinoOTA.h>
  5. #include <ESP8266WebServer.h>
  6. #include <ESP8266mDNS.h>
  7. #include <FS.h>
  8. #include <dht.h>
  9.  
  10.  
  11. #define ONE_HOUR 3600000UL
  12.  
  13. #define DHTPIN 5
  14.  
  15. #define DHTTYPE DHT11   // DHT 11
  16.  
  17. dht DHT;
  18.  
  19. ESP8266WebServer server(80);             // create a web server on port 80
  20.  
  21. File fsUploadFile;                                    // a File variable to temporarily store the received file
  22.  
  23. ESP8266WiFiMulti wifiMulti;    // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
  24.  
  25. const char *OTAName = "ESP8266";         // A name and a password for the OTA service
  26. const char *OTAPassword = "esp8266";
  27.  
  28. const char* mdnsName = "esp8266";        // Domain name for the mDNS responder
  29.  
  30. WiFiUDP UDP;                   // Create an instance of the WiFiUDP class to send and receive UDP messages
  31.  
  32. IPAddress timeServerIP;        // The time.nist.gov NTP server's IP address
  33. const char* ntpServerName = "time.nist.gov";
  34.  
  35. const int NTP_PACKET_SIZE = 48;          // NTP time stamp is in the first 48 bytes of the message
  36.  
  37. byte packetBuffer[NTP_PACKET_SIZE];      // A buffer to hold incoming and outgoing packets
  38.  
  39. /*__________________________________________________________SETUP__________________________________________________________*/
  40.  
  41. void setup() {
  42.   Serial.begin(115200);        // Start the Serial communication to send messages to the computer
  43.   delay(10);
  44.   Serial.println("\r\n");
  45.  
  46.   //dht.begin();
  47.  
  48.   startWiFi();                 // Start a Wi-Fi access point, and try to connect to some given access points. Then wait for either an AP or STA connection
  49.  
  50.   startOTA();                  // Start the OTA service
  51.  
  52.   startSPIFFS();               // Start the SPIFFS and list all contents
  53.  
  54.   startMDNS();                 // Start the mDNS responder
  55.  
  56.   startServer();               // Start a HTTP server with a file read handler and an upload handler
  57.  
  58.   startUDP();                  // Start listening for UDP messages to port 123
  59.  
  60.   WiFi.hostByName(ntpServerName, timeServerIP); // Get the IP address of the NTP server
  61.   Serial.print("Time server IP:\t");
  62.   Serial.println(timeServerIP);
  63.  
  64.   sendNTPpacket(timeServerIP);
  65.   delay(500);
  66. }
  67.  
  68. /*__________________________________________________________LOOP__________________________________________________________*/
  69.  
  70. const unsigned long intervalNTP = ONE_HOUR; // Update the time every hour
  71. unsigned long prevNTP = 0;
  72. unsigned long lastNTPResponse = millis();
  73.  
  74. const unsigned long intervalTemp = 60000;   // Do a temperature measurement every minute
  75. unsigned long prevTemp = 0;
  76. bool tmpRequested = false;
  77. const unsigned long DS_delay = 750;         // Reading the temperature from the DS18x20 can take up to 750ms
  78.  
  79. uint32_t timeUNIX = 0;                      // The most recent timestamp received from the time server
  80.  
  81. void loop() {
  82.   unsigned long currentMillis = millis();
  83.  
  84.   if (currentMillis - prevNTP > intervalNTP) { // Request the time from the time server every hour
  85.     prevNTP = currentMillis;
  86.     sendNTPpacket(timeServerIP);
  87.   }
  88.  
  89.   uint32_t time = getTime();                   // Check if the time server has responded, if so, get the UNIX time
  90.   if (time) {
  91.     timeUNIX = time;
  92.     Serial.print("NTP response:\t");
  93.     Serial.println(timeUNIX);
  94.     lastNTPResponse = millis();
  95.   } else if ((millis() - lastNTPResponse) > 24UL * ONE_HOUR) {
  96.     Serial.println("More than 24 hours since last NTP response. Rebooting.");
  97.     Serial.flush();
  98.     ESP.reset();
  99.   }
  100.  
  101.   if (timeUNIX != 0) {
  102.     if (currentMillis - prevTemp > intervalTemp) {  // Every minute, request the temperature
  103.       //tempSensors.requestTemperatures(); // Request the temperature from the sensor (it takes some time to read it)
  104.       tmpRequested = true;
  105.       prevTemp = currentMillis;
  106.       Serial.println("Data requested");
  107.     }
  108.     if (currentMillis - prevTemp > DS_delay && tmpRequested) { // 750 ms after requesting the temperature
  109.       uint32_t actualTime = timeUNIX + (currentMillis - lastNTPResponse) / 1000;
  110.       // The actual time is the last NTP time plus the time that has elapsed since the last NTP response
  111.       tmpRequested = false;
  112.       //float temp = tempSensors.getTempCByIndex(0); // Get the temperature from the sensor
  113.       //temp = round(temp * 100.0) / 100.0; // round temperature to 2 digits
  114.       
  115.  
  116.       int chk = DHT.read11(DHTPIN);
  117.       delay(2000);
  118.       switch (chk)
  119.       {
  120.         case DHTLIB_OK:  
  121.         Serial.print("OK,\t");
  122.         break;
  123.         case DHTLIB_ERROR_CHECKSUM:
  124.         Serial.print("Checksum error,\t");
  125.         break;
  126.         case DHTLIB_ERROR_TIMEOUT:
  127.         Serial.print("Time out error,\t");
  128.         break;
  129.         default:
  130.         Serial.print("Unknown error,\t");
  131.         break;
  132.       }
  133.       // DISPLAY DATA
  134.       float hum = DHT.humidity;
  135.       Serial.print(F("Humidity = "));
  136.       Serial.print(hum, 1);
  137.       Serial.print(DHT.humidity);
  138.       Serial.println(F(" %"));
  139.  
  140.       Serial.printf("Appending data to file: %lu,", actualTime);
  141.       Serial.println(hum);
  142.       File humLog = SPIFFS.open("/hum.csv", "a");
  143.       humLog.print(actualTime);
  144.       humLog.print(',');
  145.       humLog.println(hum);
  146.       humLog.close();
  147.       //File tempLog = SPIFFS.open("/temp.csv", "a"); // Write the time and the temperature to the csv file
  148.       //tempLog.print(actualTime);
  149.       //tempLog.print(',');
  150.       //tempLog.println(temp);
  151.       //tempLog.close();
  152.     }
  153.   } else {                                    // If we didn't receive an NTP response yet, send another request
  154.     sendNTPpacket(timeServerIP);
  155.     delay(500);
  156.   }
  157.  
  158.   server.handleClient();                      // run the server
  159.   ArduinoOTA.handle();                        // listen for OTA events
  160. }
  161.  
  162. /*__________________________________________________________SETUP_FUNCTIONS__________________________________________________________*/
  163.  
  164. void startWiFi() { // Try to connect to some given access points. Then wait for a connection
  165.   wifiMulti.addAP("OG", "27282930");   // add Wi-Fi networks you want to connect to
  166.   wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
  167.   wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
  168.  
  169.   Serial.println("Connecting");
  170.   while (wifiMulti.run() != WL_CONNECTED) {  // Wait for the Wi-Fi to connect
  171.     delay(250);
  172.     Serial.print('.');
  173.   }
  174.   Serial.println("\r\n");
  175.   Serial.print("Connected to ");
  176.   Serial.println(WiFi.SSID());             // Tell us what network we're connected to
  177.   Serial.print("IP address:\t");
  178.   Serial.print(WiFi.localIP());            // Send the IP address of the ESP8266 to the computer
  179.   Serial.println("\r\n");
  180. }
  181.  
  182. void startUDP() {
  183.   Serial.println("Starting UDP");
  184.   UDP.begin(123);                          // Start listening for UDP messages to port 123
  185.   Serial.print("Local port:\t");
  186.   Serial.println(UDP.localPort());
  187. }
  188.  
  189. void startOTA() { // Start the OTA service
  190.   ArduinoOTA.setHostname(OTAName);
  191.   ArduinoOTA.setPassword(OTAPassword);
  192.  
  193.   ArduinoOTA.onStart([]() {
  194.     Serial.println("Start");
  195.   });
  196.   ArduinoOTA.onEnd([]() {
  197.     Serial.println("\r\nEnd");
  198.   });
  199.   ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  200.     Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  201.   });
  202.   ArduinoOTA.onError([](ota_error_t error) {
  203.     Serial.printf("Error[%u]: ", error);
  204.     if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  205.     else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  206.     else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  207.     else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  208.     else if (error == OTA_END_ERROR) Serial.println("End Failed");
  209.   });
  210.   ArduinoOTA.begin();
  211.   Serial.println("OTA ready\r\n");
  212. }
  213.  
  214. void startSPIFFS() { // Start the SPIFFS and list all contents
  215.   SPIFFS.begin();                             // Start the SPI Flash File System (SPIFFS)
  216.   Serial.println("SPIFFS started. Contents:");
  217.   {
  218.     Dir dir = SPIFFS.openDir("/");
  219.     while (dir.next()) {                      // List the file system contents
  220.       String fileName = dir.fileName();
  221.       size_t fileSize = dir.fileSize();
  222.       Serial.printf("\tFS File: %s, size: %s\r\n", fileName.c_str(), formatBytes(fileSize).c_str());
  223.     }
  224.     Serial.printf("\n");
  225.   }
  226. }
  227.  
  228. void startMDNS() { // Start the mDNS responder
  229.   MDNS.begin(mdnsName);                        // start the multicast domain name server
  230.   Serial.print("mDNS responder started: http://");
  231.   Serial.print(mdnsName);
  232.   Serial.println(".local");
  233. }
  234.  
  235. void startServer() { // Start a HTTP server with a file read handler and an upload handler
  236.   server.on("/edit.html",  HTTP_POST, []() {  // If a POST request is sent to the /edit.html address,
  237.     server.send(200, "text/plain", "");
  238.   }, handleFileUpload);                       // go to 'handleFileUpload'
  239.  
  240.   server.onNotFound(handleNotFound);          // if someone requests any other file or page, go to function 'handleNotFound'
  241.   // and check if the file exists
  242.  
  243.   server.begin();                             // start the HTTP server
  244.   Serial.println("HTTP server started.");
  245. }
  246.  
  247. /*__________________________________________________________SERVER_HANDLERS__________________________________________________________*/
  248.  
  249. void handleNotFound() { // if the requested file or page doesn't exist, return a 404 not found error
  250.   if (!handleFileRead(server.uri())) {        // check if the file exists in the flash memory (SPIFFS), if so, send it
  251.     server.send(404, "text/plain", "404: File Not Found");
  252.   }
  253. }
  254.  
  255. bool handleFileRead(String path) { // send the right file to the client (if it exists)
  256.   Serial.println("handleFileRead: " + path);
  257.   if (path.endsWith("/")) path += "index.html";          // If a folder is requested, send the index file
  258.   String contentType = getContentType(path);             // Get the MIME type
  259.   String pathWithGz = path + ".gz";
  260.   if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) { // If the file exists, either as a compressed archive, or normal
  261.     if (SPIFFS.exists(pathWithGz))                         // If there's a compressed version available
  262.       path += ".gz";                                         // Use the compressed verion
  263.     File file = SPIFFS.open(path, "r");                    // Open the file
  264.     size_t sent = server.streamFile(file, contentType);    // Send it to the client
  265.     file.close();                                          // Close the file again
  266.     Serial.println(String("\tSent file: ") + path);
  267.     return true;
  268.   }
  269.   Serial.println(String("\tFile Not Found: ") + path);   // If the file doesn't exist, return false
  270.   return false;
  271. }
  272.  
  273. void handleFileUpload() { // upload a new file to the SPIFFS
  274.   HTTPUpload& upload = server.upload();
  275.   String path;
  276.   if (upload.status == UPLOAD_FILE_START) {
  277.     path = upload.filename;
  278.     if (!path.startsWith("/")) path = "/" + path;
  279.     if (!path.endsWith(".gz")) {                         // The file server always prefers a compressed version of a file
  280.       String pathWithGz = path + ".gz";                  // So if an uploaded file is not compressed, the existing compressed
  281.       if (SPIFFS.exists(pathWithGz))                     // version of that file must be deleted (if it exists)
  282.         SPIFFS.remove(pathWithGz);
  283.     }
  284.     Serial.print("handleFileUpload Name: "); Serial.println(path);
  285.     fsUploadFile = SPIFFS.open(path, "w");               // Open the file for writing in SPIFFS (create if it doesn't exist)
  286.     path = String();
  287.   } else if (upload.status == UPLOAD_FILE_WRITE) {
  288.     if (fsUploadFile)
  289.       fsUploadFile.write(upload.buf, upload.currentSize); // Write the received bytes to the file
  290.   } else if (upload.status == UPLOAD_FILE_END) {
  291.     if (fsUploadFile) {                                   // If the file was successfully created
  292.       fsUploadFile.close();                               // Close the file again
  293.       Serial.print("handleFileUpload Size: "); Serial.println(upload.totalSize);
  294.       server.sendHeader("Location", "/success.html");     // Redirect the client to the success page
  295.       server.send(303);
  296.     } else {
  297.       server.send(500, "text/plain", "500: couldn't create file");
  298.     }
  299.   }
  300. }
  301.  
  302. /*__________________________________________________________HELPER_FUNCTIONS__________________________________________________________*/
  303.  
  304. String formatBytes(size_t bytes) { // convert sizes in bytes to KB and MB
  305.   if (bytes < 1024) {
  306.     return String(bytes) + "B";
  307.   } else if (bytes < (1024 * 1024)) {
  308.     return String(bytes / 1024.0) + "KB";
  309.   } else if (bytes < (1024 * 1024 * 1024)) {
  310.     return String(bytes / 1024.0 / 1024.0) + "MB";
  311.   }
  312. }
  313.  
  314. String getContentType(String filename) { // determine the filetype of a given filename, based on the extension
  315.   if (filename.endsWith(".html")) return "text/html";
  316.   else if (filename.endsWith(".css")) return "text/css";
  317.   else if (filename.endsWith(".js")) return "application/javascript";
  318.   else if (filename.endsWith(".ico")) return "image/x-icon";
  319.   else if (filename.endsWith(".gz")) return "application/x-gzip";
  320.   return "text/plain";
  321. }
  322.  
  323. unsigned long getTime() { // Check if the time server has responded, if so, get the UNIX time, otherwise, return 0
  324.   if (UDP.parsePacket() == 0) { // If there's no response (yet)
  325.     return 0;
  326.   }
  327.   UDP.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
  328.   // Combine the 4 timestamp bytes into one 32-bit number
  329.   uint32_t NTPTime = (packetBuffer[40] << 24) | (packetBuffer[41] << 16) | (packetBuffer[42] << 8) | packetBuffer[43];
  330.   // Convert NTP time to a UNIX timestamp:
  331.   // Unix time starts on Jan 1 1970. That's 2208988800 seconds in NTP time:
  332.   const uint32_t seventyYears = 2208988800UL;
  333.   // subtract seventy years:
  334.   uint32_t UNIXTime = NTPTime - seventyYears;
  335.   return UNIXTime;
  336. }
  337.  
  338.  
  339. void sendNTPpacket(IPAddress& address) {
  340.   Serial.println("Sending NTP request");
  341.   memset(packetBuffer, 0, NTP_PACKET_SIZE);  // set all bytes in the buffer to 0
  342.   // Initialize values needed to form NTP request
  343.   packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  344.  
  345.   // send a packet requesting a timestamp:
  346.   UDP.beginPacket(address, 123); // NTP requests are to port 123
  347.   UDP.write(packetBuffer, NTP_PACKET_SIZE);
  348.   UDP.endPacket();
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement