SHARE
TWEET

Untitled

a guest Jan 20th, 2020 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // mapping of Waveshare ESP32 Driver Board
  3. // BUSY -> 25, RST -> 26, DC -> 27, CS-> 15, CLK -> 13, DIN -> 14
  4.  
  5. #include <ArduinoJson.h>
  6. #include <GxEPD2_BW.h>
  7. #include <Adafruit_GFX.h>
  8. #include <Fonts/FreeMonoBold9pt7b.h>
  9. #include <Fonts/FreeSansBold24pt7b.h>
  10. #include <Fonts/Tahomabd9pt8b.h>
  11. #include <Fonts/Tahomabd45pt8b.h>
  12. #include <Fonts/FreeSans9pt7b.h>
  13. #include <Fonts/FreeSerif9pt7b.h>
  14.  
  15. //#if defined(ESP32)
  16. GxEPD2_BW<GxEPD2_270, GxEPD2_270::HEIGHT> display(GxEPD2_270(/*CS=*/ 15, /*DC=*/ 27, /*RST=*/ 26, /*BUSY=*/ 25));
  17. //#endif
  18.  
  19. #if defined(ESP8266) || defined(ESP32)
  20. #include <StreamString.h>
  21. #define PrintString StreamString
  22. #else
  23. class PrintString : public Print, public String
  24. {
  25.   public:
  26.     size_t write(uint8_t data) override
  27.     {
  28.       return concat(char(data));
  29.     };
  30. };
  31. #endif
  32.  
  33. #define EPD_BLACK 0x0000
  34. #define EPD_WHITE 0xFFFF
  35. // Wifi
  36. #include <WiFi.h>
  37. #include <WiFiClient.h>
  38. #include <WebServer.h>
  39. #include <ESPmDNS.h>
  40. #include <Update.h>
  41.  
  42. //Veriables
  43. const char* ssid     = "Test";
  44. const char* password = "Test123456";
  45. const char* host = "monitor-web";
  46.  
  47. int16_t tbx, tby; uint16_t tbw, tbh , x , y;
  48.  
  49. float tempC = 5;
  50. int timer_set = 7200;
  51. int timer_left = 0;
  52. int retry = 0;
  53. unsigned long previousMillis = 0;
  54. String timedis;
  55.  
  56. // constants won't change :
  57. const long interval = 5000;           // interval at which to blink (milliseconds)
  58.  
  59. WiFiClient espClient;
  60. WebServer server(80);
  61.  
  62. void handleRoot() {
  63.   Serial.println("start Handle root");
  64.   //String s = MAIN_page; //Read HTML contents
  65.   String html = "<html> <head> <style> html { font-family: Arial; display: inline-block; margin: 0px auto; text-align: center; } h1 { color: #0F3376; padding: 2vh; } p { font-size: 1.5rem; } .button { display: inline-block; background-color: #008CBA; border: none; border-radius: 4px; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer; } .button2 { background-color: #f44336; } .form { background-color: #f44336; color: white; } .units { font-size: 1.2rem; } .sensor-labels { font-size: 1.5rem; vertical-align:middle; padding-bottom: 15px; } </style> <title>E-Paper Temperature Monitor</title> <meta name='viewport' content='width=device-width, initial-scale=1'> </head> <body> <h1>E-Paper Temperature Monitor</h1><p> Firmware Upgrade</strong></p> <p> <form method='POST' action='/update' enctype='multipart/form-data'> <input class='button' type='file' name='update'> <a href='/update'><button class='button button2'>Upgrade</button></a> </form> <p></p> <a href='/restart'><button class='button button2'>Restart</button></a> </p> </body></html>";
  66.   server.send(200, "text/html", html); //Send web page
  67.   Serial.println("Handle root");
  68.  
  69. }
  70.  
  71.  
  72. void printSensors();
  73. void initDisplay();
  74.  
  75. String getData(String url) {
  76.   String line,payload;
  77.   if (espClient.connect("192.168.0.50", 80)) {
  78.     Serial.print("requesting URL: ");
  79.     Serial.println(url);
  80.     espClient.print(String("GET ") + url + " HTTP/1.1\r\n" +
  81.                     "Host: " + host + "\r\n" +
  82.                     "Connection: close\r\n\r\n");
  83.     Serial.println("request sent");
  84.     int count = 10;
  85.     while (count > 0) {
  86.       line = espClient.readStringUntil('\n');
  87.       if (line == "\r") {
  88.         Serial.println("headers received");
  89.         break;
  90.       }
  91.       count--;
  92.     }
  93.     payload = espClient.readStringUntil('\n');
  94.     Serial.println("reply was:");
  95.     Serial.println("==========");
  96.     Serial.println(payload);
  97.     Serial.println("==========");
  98.     Serial.println("closing connection");
  99.   }
  100.   else{
  101.     Serial.println("error");
  102.   }
  103.   return payload;
  104. }
  105. void getSensorData() {
  106.   DynamicJsonBuffer jsonBuffer;
  107.   JsonObject& json_temp = jsonBuffer.parseObject(getData("/cm?cmnd=status%208"));
  108.   JsonObject& json_timer = jsonBuffer.parseObject(getData("/cm?cmnd=pulsetime1"));
  109.   float test = tempC;
  110.   tempC = (float)json_temp["StatusSNS"]["DS18B20"]["Temperature"];
  111.   if ( test > 0 && tempC == 0) {
  112.     tempC = test;
  113.   }
  114.   timer_set = (int)(json_timer["PulseTime1"]["Set"]);
  115.   timer_left = (int)(json_timer["PulseTime1"]["Remaining"]);
  116.   Serial.print("Temp: ");
  117.   Serial.println(tempC);
  118.  
  119. }
  120.  
  121. void setup_wifi()
  122. {
  123.   WiFi.begin(ssid, password);
  124.   if (WiFi.waitForConnectResult() == WL_CONNECTED) {
  125.     MDNS.begin(host);
  126.     Serial.printf("Ready! Open http://%s.local in your browser\n", host);
  127.     Serial.println(WiFi.localIP());
  128.   } else {
  129.     Serial.println("WiFi Failed");
  130.   }
  131. }
  132.  
  133. void setup()
  134. {
  135.   display.init(115200); // uses standard SPI pins, e.g. SCK(18), MISO(19), MOSI(23), SS(5)
  136.   // *** special handling for Waveshare ESP32 Driver board *** //
  137.   // ********************************************************* //
  138.   SPI.end(); // release standard SPI pins, e.g. SCK(18), MISO(19), MOSI(23), SS(5)
  139.   //SPI: void begin(int8_t sck=-1, int8_t miso=-1, int8_t mosi=-1, int8_t ss=-1);
  140.   SPI.begin(13, 12, 14, 15); // map and init SPI pins SCK(13), MISO(12), MOSI(14), SS(15)
  141.   // *** end of special handling for Waveshare ESP32 Driver board *** //
  142.   // **************************************************************** //
  143.   Serial.begin(115200);
  144.   Serial.println();
  145.   Serial.println("Starting");
  146.   WiFi.mode(WIFI_STA);
  147.   setup_wifi();
  148.   Serial.println();
  149.   Serial.println("WiFi connected");
  150.  
  151.   // Print the IP address
  152.   Serial.println("IP address: ");
  153.   Serial.println(WiFi.localIP());
  154.   delay(100);
  155.  
  156.   if (WiFi.waitForConnectResult() == WL_CONNECTED) {
  157.     MDNS.begin(host);
  158.     server.on("/", handleRoot);
  159.     server.on("/restart", HTTP_GET, []() {
  160.       ESP.restart();
  161.     });
  162.     server.on("/update", HTTP_POST, []() {
  163.       server.sendHeader("Connection", "close");
  164.       server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
  165.       ESP.restart();
  166.     }, []() {
  167.       HTTPUpload& upload = server.upload();
  168.       if (upload.status == UPLOAD_FILE_START) {
  169.         Serial.setDebugOutput(true);
  170.         Serial.printf("Update: %s\n", upload.filename.c_str());
  171.         if (!Update.begin()) { //start with max available size
  172.           Update.printError(Serial);
  173.         }
  174.       } else if (upload.status == UPLOAD_FILE_WRITE) {
  175.         if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
  176.           Update.printError(Serial);
  177.         }
  178.       } else if (upload.status == UPLOAD_FILE_END) {
  179.         if (Update.end(true)) { //true to set the size to the current progress
  180.           Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
  181.         } else {
  182.           Update.printError(Serial);
  183.         }
  184.         Serial.setDebugOutput(false);
  185.       } else {
  186.         Serial.printf("Update Failed Unexpectedly (likely broken connection): status=%d\n", upload.status);
  187.       }
  188.     });
  189.     server.begin();
  190.     MDNS.addService("http", "tcp", 80);
  191.   }
  192.   else{
  193.     Serial.println("error");
  194.   }
  195. }
  196.  
  197. void loop() {
  198.   unsigned long currentMillis = millis();
  199.   server.handleClient();
  200.  
  201.   if (currentMillis - previousMillis >= interval) {
  202.     previousMillis = currentMillis;
  203.     getSensorData();
  204.     printSensors();
  205.   }
  206. }
  207.  
  208. //initialise e-paper display
  209. void initDisplay() {
  210.   display.init(115200);
  211.   display.setFullWindow();
  212.   display.setPartialWindow(0, 0, display.width(), display.height());
  213. }
  214.  
  215. //print to e-paper display
  216. void printSensors() {
  217.   delay(100);
  218.   display.setRotation(1);
  219.   display.setTextColor(display.epd2.hasColor ? GxEPD_RED : GxEPD_BLACK);
  220.   display.fillScreen(GxEPD_WHITE);
  221.   PrintString valueTemp;
  222.   PrintString valueStatus;
  223.   PrintString valueWater;
  224.   display.firstPage();
  225.   do
  226.   {
  227.     display.setFont(&tahomabd45pt8b);
  228.     String tempo = String((int)tempC) + "°";
  229.     valueTemp.print((int)tempC);
  230.     display.getTextBounds(valueTemp, 0, 0, &tbx, &tby, &tbw, &tbh);
  231.     x = ((display.width() - tbw) / 2) - tbx;
  232.     y = (display.height() * 2 / 6) + tbh / 2; // y is base line!
  233.     display.setCursor(x, y);
  234.     display.print(tempo);
  235.     display.setFont(&Tahomabd9pt8b);
  236.     valueStatus.print(timer_set);
  237.     display.getTextBounds(valueStatus, 0, 0, &tbx, &tby, &tbw, &tbh);
  238.     x = ((display.width() - tbw) / 2) - tbx;
  239.     y = (display.height() * 7 / 10) + tbh / 2; // y is base line!
  240.     display.setCursor(x, y);
  241.     display.print((String)valueStatus);
  242.     valueWater.print("");
  243.     display.getTextBounds(valueWater, 0, 0, &tbx, &tby, &tbw, &tbh);
  244.     x = ((display.width() - tbw) / 2) - tbx;
  245.     y = (display.height() * 8 / 10) + tbh / 2; // y is base line!
  246.     display.setCursor(x, y);
  247.     display.print(valueWater);
  248.     display.setFont(&FreeSerif9pt7b);
  249.     display.setCursor(1, 173);
  250.     String wifi_status = "";
  251.     if ((WiFi.status() == WL_CONNECTED)) {
  252.       wifi_status = "Wifi: OK, IP: 192.168.0.24";
  253.     }
  254.     else {
  255.       wifi_status = "Wifi: Error";
  256.     }
  257.     display.print(wifi_status);
  258.   }
  259.   while (display.nextPage());
  260. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top