HighInBC

Untitled

Oct 9th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.55 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <OneWire.h>
  5. #include <DallasTemperature.h>
  6. #include <Adafruit_NeoPixel.h>
  7. #include <FS.h>
  8. #include <PID_v1.h>
  9.  
  10. #define RELAY D2
  11. #define ONE_WIRE_BUS D3
  12. #define LED_PIN D4
  13.  
  14. #define MODE_T 0
  15. #define MODE_P 1
  16.  
  17. const char* ssid     = "HighInBC";
  18. const char* password = "";
  19.  
  20. ESP8266WebServer server(80);
  21.  
  22. OneWire oneWire(ONE_WIRE_BUS);
  23. DallasTemperature sensors(&oneWire);
  24. DeviceAddress thermometer = { 0x28, 0xFF, 0x96, 0x0A, 0x63, 0x16, 0x04, 0x1E };
  25.  
  26. Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, LED_PIN, NEO_GRB + NEO_KHZ800);
  27. double white  = strip.Color( 255, 255, 255 );
  28. double red    = strip.Color( 255,   0,   0 );
  29. double green  = strip.Color(   0, 255,   0 );
  30. double blue   = strip.Color(   0,   0, 255 );
  31. double orange = strip.Color( 255, 165,   0 );
  32. double black  = strip.Color(   0,   0,   0 );
  33.  
  34. double Setpoint = 23.5;
  35. double Input, Output;
  36. PID myPID(&Input, &Output, &Setpoint,1.5,5,1, DIRECT);
  37.  
  38. unsigned int WindowSize = 20000;
  39. unsigned long windowStartTime;
  40.  
  41. float temp = -127.00;
  42.  
  43. int logic_mode = MODE_T;
  44.  
  45. void printStatus() {
  46.   String response;
  47.   response += String(temp)+"C";
  48.   response += " ("+String(Setpoint)+"C) - ";
  49.   response += String(Output)+"ms ";
  50.   float percent = Output * 100 / WindowSize;
  51.   response += "("+String(percent)+"%) Mode: ";
  52.   response += logic_mode == MODE_T ? "T" : "P";
  53.   response += '\n';
  54.   server.send(200, "text/plain", response );
  55. }
  56.  
  57. void printJSON() {
  58.   String json;
  59.   String modeString = logic_mode ? "P" : "T";
  60.   json += "{\"target\":"+String( Setpoint )+",";
  61.   json += "\"windowSize\":"+String( WindowSize )+",";
  62.   json += "\"rate\":"+String( Output )+",";
  63.   json += "\"temperature\":"+String( temp )+",";
  64.   json += "\"mode\":\""+modeString+"\"}";
  65.   server.send(200, "application/json", json );
  66. }
  67.  
  68. String getContentType(String filename){
  69.   if(server.hasArg("download")) return "application/octet-stream";
  70.   else if(filename.endsWith(".htm")) return "text/html";
  71.   else if(filename.endsWith(".html")) return "text/html";
  72.   else if(filename.endsWith(".css")) return "text/css";
  73.   else if(filename.endsWith(".js")) return "application/javascript";
  74.   else if(filename.endsWith(".png")) return "image/png";
  75.   else if(filename.endsWith(".gif")) return "image/gif";
  76.   else if(filename.endsWith(".jpg")) return "image/jpeg";
  77.   else if(filename.endsWith(".ico")) return "image/x-icon";
  78.   else if(filename.endsWith(".xml")) return "text/xml";
  79.   else if(filename.endsWith(".pdf")) return "application/x-pdf";
  80.   else if(filename.endsWith(".zip")) return "application/x-zip";
  81.   else if(filename.endsWith(".gz")) return "application/x-gzip";
  82.   return "text/plain";
  83. }
  84.  
  85. bool handleFileRead(String path){
  86.   if(path.endsWith("/")) path += "index.html";
  87.   String contentType = getContentType(path);
  88.   String pathWithGz = path + ".gz";
  89.   if(SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)){
  90.     if(SPIFFS.exists(pathWithGz))
  91.       path += ".gz";
  92.     File file = SPIFFS.open(path, "r");
  93.     size_t sent = server.streamFile(file, contentType);
  94.     file.close();
  95.     return true;
  96.   }
  97.   return false;
  98. }
  99.  
  100. void logic_mode_pid( float temperature ) {
  101.   Input = temperature;
  102.   myPID.Compute();
  103.  
  104.   unsigned long now = millis();
  105.   if(now - windowStartTime>WindowSize)
  106.   {
  107.     windowStartTime += WindowSize;
  108.   }
  109.   if(Output > now - windowStartTime) {
  110.     strip.setPixelColor( 0, red );
  111.     digitalWrite(RELAY,HIGH);
  112.   } else {
  113.     strip.setPixelColor( 0, green );
  114.     digitalWrite(RELAY,LOW);
  115.   }
  116.   strip.show();
  117. }
  118.  
  119. void logic_mode_thermostat( float temperature ) {
  120.   if( temperature > Setpoint ) {
  121.     digitalWrite(RELAY, LOW);
  122.     strip.setPixelColor( 0, blue );
  123.     strip.show();
  124.   } else if( temperature < Setpoint ) {
  125.     digitalWrite(RELAY, HIGH);
  126.     strip.setPixelColor( 0, orange );
  127.     strip.show();
  128.   }
  129. }
  130.  
  131. void setup()
  132. {
  133.   Serial.begin(9600);
  134.  
  135.   SPIFFS.begin();
  136.  
  137.   uint8_t i = 0;
  138.   while (WiFi.status() != WL_CONNECTED && i++ < 20) {//wait 10 seconds
  139.     delay(500);
  140.   }
  141.   if(i == 21){
  142.     Serial.print("Could not connect to");
  143.     Serial.println(ssid);
  144.     while(1) delay(500);
  145.   }
  146.   Serial.print("Connected! IP address: ");
  147.   Serial.println(WiFi.localIP());
  148.  
  149.   server.on("/status",  HTTP_GET, printStatus);
  150.   server.on("/json",    HTTP_GET, printJSON);  
  151.   server.onNotFound([](){
  152.     if(!handleFileRead(server.uri()))
  153.       server.send(404, "text/plain", "FileNotFound");
  154.   });
  155.   server.begin();
  156.   Serial.println("HTTP server started");
  157.  
  158.   pinMode(RELAY, OUTPUT);
  159.  
  160.   windowStartTime = millis();
  161.  
  162.   myPID.SetOutputLimits(0, WindowSize);
  163.   myPID.SetMode(AUTOMATIC);
  164.  
  165.   sensors.begin();
  166.   sensors.setResolution(thermometer, 12);
  167.   sensors.setWaitForConversion(false);
  168.   sensors.requestTemperatures();
  169.  
  170.   strip.begin();
  171.   strip.setPixelColor( 0, black );
  172.   strip.show();
  173. }
  174.  
  175. void loop()
  176. {
  177.   server.handleClient();
  178.  
  179.   if( sensors.isConversionAvailable(thermometer) ) {
  180.     temp = sensors.getTempC(thermometer);
  181.     sensors.requestTemperatures();
  182.     Serial.println(temp);
  183.   }
  184.  
  185.   if( temp > 40 || temp < 0 ) { // sanity check
  186.     digitalWrite(RELAY, LOW);
  187.     strip.setPixelColor( 0, white );
  188.     strip.show();
  189.     return;
  190.   }
  191.   if( logic_mode == MODE_T && abs(Setpoint - temp) < .1 ) {
  192.     logic_mode = MODE_P;
  193.   }
  194.   if( logic_mode == MODE_P && abs(Setpoint - temp) >= .5 ) {
  195.     logic_mode = MODE_T;
  196.   }
  197.   switch( logic_mode ) {
  198.     case MODE_T:
  199.       logic_mode_thermostat( temp );
  200.       break;
  201.     case MODE_P:
  202.       logic_mode_pid( temp );
  203.       break;
  204.   }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment