SHARE
TWEET

Untitled

a guest Apr 20th, 2019 104 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2.  
  3. #include <DNSServer.h>
  4. #include <ESP8266WebServer.h>
  5. #include <WiFiManager.h>
  6.  
  7. #include "utils.h"
  8.  
  9. ESP8266WebServer server(80);
  10.  
  11. // button pin to open blind
  12. int up_in = 13;
  13.  
  14. // button down to close blind
  15. int down_in = 16;
  16.  
  17. // relay to open blind
  18. int up_out = 5;
  19.  
  20. // relay to close blind
  21. int down_out = 12;
  22.  
  23. Day week [31];
  24.  
  25. String calendar;
  26.  
  27. // time to close blind
  28. int workTimeDown = 0;
  29.  
  30. // time to open blind
  31. int workTimeUp = 0;
  32.  
  33.  
  34. // blind current percent
  35. int currentPercent = 0;
  36.  
  37.  
  38. int onePercentDown = 0;
  39.  
  40.  
  41. int onePercentUp = 0;
  42.  
  43.  
  44. bool calibrated = false;
  45.  
  46. String deviceID = "0x11";
  47.  
  48. void setup() {
  49.   setupPins();
  50.   setupTime();
  51.   Serial.begin(115200);
  52.  
  53.   WiFiManager wifiManager;
  54.  
  55.  
  56.   wifiManager.autoConnect("SmartHome 0x11" , "P@ssw0rd");
  57.  
  58.   Serial.println("connected...yeey :)");
  59.  
  60.   server.on("/position", getProgress);
  61.   server.on("/calibrated", isCalibrated);
  62.   server.on("/moveBlind", moveBlind);
  63.   server.on("/getPlan", getPlan);
  64.   server.on("/setPlan", setPlan);
  65.   server.on("/setTimes", setTimes);
  66.   server.on("/getData", getData);
  67.  
  68.   server.begin();
  69.  
  70.   Serial.println("HTTP server started");
  71. }
  72.  
  73.  
  74.  
  75. void loop() {
  76.   // put your main code here, to run repeatedly:
  77.   server.handleClient();
  78.  
  79.   checkDay();
  80.  
  81.   if (!calibrated) {
  82.     if (digitalRead(up_in) == LOW) {
  83.       digitalWrite(up_out, HIGH);
  84.     } else if (digitalRead(up_in) == HIGH) {
  85.       digitalWrite(up_out, LOW);
  86.     }
  87.  
  88.  
  89.     if (digitalRead(down_in) == LOW) {
  90.       digitalWrite(down_out, HIGH);
  91.     } else if (digitalRead(down_in) == HIGH) {
  92.       digitalWrite(down_out, LOW);
  93.     }
  94.   }
  95.   else if (calibrated) {
  96.     buttonUp();
  97.     buttonDown();
  98.   }
  99.  
  100. }
  101.  
  102.  
  103.  
  104. void getProgress() {
  105.   String progressConverted = String(currentPercent);
  106.   server.send(200, "text / plain", progressConverted);
  107. }
  108.  
  109.  
  110.  
  111. void isCalibrated() {
  112.   server.send(200, "text / plain", String(calibrated));
  113. }
  114.  
  115.  
  116.  
  117. void moveBlind() {
  118.   String message;
  119.   int receivedPercent;
  120.   if (server.arg("level") != "") {
  121.     message = server.arg("level");
  122.  
  123.     // covert message to int
  124.     receivedPercent = atoi(message.c_str());
  125.  
  126.   }
  127.   else if (server.hasArg("plain") == true) {
  128.     String message = server.arg("plain");
  129.  
  130.  
  131.     // convert json to value
  132.     receivedPercent = convertFromJson(message);
  133.  
  134.   }
  135.   // o ile procent trzeba wysunąć
  136.   int roznica = currentPercent - receivedPercent;
  137.  
  138.  
  139.   if (roznica < 0) { // to opuszczaj
  140.     roznica = -roznica; // zamień na liczbę dodatnią
  141.     digitalWrite(down_out, HIGH);
  142.     delay (roznica * onePercentDown); // uśpij na czas wysuwu
  143.     digitalWrite(down_out, LOW);
  144.   }
  145.  
  146.   else if (roznica > 0) { // to podnosc
  147.     digitalWrite(up_out, HIGH);
  148.     delay (roznica * onePercentUp); // uśpij na czas wysuwu
  149.     digitalWrite(up_out, LOW);
  150.   }
  151.   currentPercent = receivedPercent;
  152. }
  153.  
  154.  
  155.  
  156. void getPlan() {
  157.   server.send(200, "text / plain", calendar);
  158. }
  159.  
  160. void getData() {
  161.   String progressConverted = "";
  162.  
  163.   progressConverted += "czas pracy w gore ";
  164.   progressConverted += String(workTimeUp);
  165.  
  166.   progressConverted += "<br>czas pracy w dol ";
  167.   progressConverted += String(workTimeDown);
  168.  
  169.   progressConverted += "<br>percent Up ";
  170.   progressConverted += String(onePercentUp);
  171.  
  172.   progressConverted += "<br>percent down ";
  173.   progressConverted += String(onePercentDown);
  174.  
  175.   progressConverted += "<br>currentPercent ";
  176.   progressConverted += String(currentPercent);
  177.  
  178.   progressConverted += "<br>calibrated ";
  179.   progressConverted += String(calibrated);
  180.  
  181.   progressConverted += "<br>Calendar <br><br>";
  182.   progressConverted += calendar;
  183.  
  184.   server.send(200, "text/html", progressConverted);
  185. }
  186.  
  187.  
  188.  
  189. void setPlan() {
  190.  
  191.   if (server.hasArg("plain") == true) {
  192.     String message = server.arg("plain");
  193.     calendar = message;
  194.     convertPlan(message);
  195.   }
  196.   server.send(200, "text/html", "odpowiedz");
  197. }
  198.  
  199.  
  200.  
  201. void setTimes() {
  202.   String message;
  203.  
  204.   if (server.arg("down") != "") {
  205.     message = server.arg("down");
  206.  
  207.     // covert message to int
  208.     workTimeDown = atoi(message.c_str());
  209.  
  210.     roundUp(workTimeDown);
  211.     onePercentDown = workTimeDown / 100;
  212.  
  213.   }
  214.  
  215.   if (server.arg("up") != "") {
  216.     message = server.arg("up");
  217.  
  218.     // covert message to int
  219.     workTimeUp = atoi(message.c_str());
  220.  
  221.     roundDown(workTimeUp);
  222.     onePercentUp = workTimeUp / 100;
  223.   }
  224.   calibrated = true;
  225.  
  226.   currentPercent = 0;
  227.  
  228.   server.send(200, "text / plain", "calibrated");
  229. }
  230.  
  231.  
  232.  
  233.  
  234.  
  235. void setupTime() {
  236.   // konfiguracja czasu z internetu
  237.   configTime(2 * 3600, 0, "pool.ntp.org", "time.nist.gov");
  238.   while (!time(nullptr)) {
  239.     delay(1000);
  240.   }
  241.   time_t now = time(nullptr);
  242. }
  243.  
  244.  
  245.  
  246. void setupPins() {
  247.   // ustawienie pinów jako wejścia
  248.   pinMode(up_in, INPUT);
  249.   pinMode(down_in, INPUT);
  250.  
  251.   // ustawienie pinoów jako wyjścia
  252.   pinMode(up_out, OUTPUT);
  253.   pinMode(down_out, OUTPUT);
  254.  
  255.  
  256.   // wyzerowanie wszystkich pinów
  257.   digitalWrite(up_in, LOW);
  258.   digitalWrite(down_in, LOW);
  259.  
  260.   digitalWrite(up_out, LOW);
  261.   digitalWrite(down_out, LOW);
  262. }
  263.  
  264.  
  265.  
  266. void checkDay() {
  267.   time_t now;
  268.   struct tm * timeinfo;
  269.   time(&now);
  270.   timeinfo = localtime(&now);
  271.  
  272.  
  273.   /* check days */
  274.  
  275.   Day day = week[(timeinfo->tm_mday) - 1];
  276.   if (day.hourUp == 0 && day.minuteUp == 0) {
  277.   }
  278.   else {
  279.     if ((day.hourUp != 0) && (day.hourUp == timeinfo->tm_hour) && (day.minuteUp == timeinfo->tm_min) && (currentPercent != 0)) {
  280.       /*podnoś*/
  281.       digitalWrite(up_out, HIGH);
  282.       delay (currentPercent * onePercentUp); // uśpij na czas wysuwu
  283.       digitalWrite(up_out, LOW);
  284.       currentPercent = 0;
  285.     }
  286.   }
  287.   if (day.hourDown == 0 && day.minuteDown == 0) {
  288.  
  289.   } else {
  290.     if ((day.hourDown != 0) && (day.hourDown == timeinfo->tm_hour) && (day.minuteDown == timeinfo->tm_min) && (currentPercent != 100)) {
  291.       /*opuszczaj*/
  292.       digitalWrite(down_out, HIGH);
  293.       delay ((100 - currentPercent) * onePercentDown); // uśpij na czas wysuwu
  294.       digitalWrite(down_out, LOW);
  295.       currentPercent = 100;
  296.     }
  297.   }
  298. }
  299.  
  300.  
  301.  
  302. /* button up calibrated */
  303.  
  304. void buttonDown() {
  305.  
  306.   // wykryto stan wysoki na wejściu sprawdź kolejne warunki
  307.   if (digitalRead(down_in) == LOW && currentPercent < 100) {
  308.     digitalWrite(down_out, HIGH);
  309.     delay((100 - currentPercent)*onePercentDown);
  310.     digitalWrite(down_out, LOW);
  311.     currentPercent = 100;
  312.   }
  313. }
  314.  
  315.  
  316. /* button down calibrated */
  317.  
  318. void buttonUp() {
  319.  
  320.   // wykryto stan wysoki na wejściu
  321.   if (digitalRead(up_in) == LOW && currentPercent > 0) {
  322.     digitalWrite(up_out, HIGH);
  323.     delay(currentPercent * onePercentUp);
  324.     digitalWrite(up_out, LOW);
  325.     currentPercent = 0;
  326.   }
  327. }
  328.  
  329.  
  330.  
  331.  
  332. #include "utils.h"
  333.  
  334. DynamicJsonBuffer jsonBuffer;
  335. extern Day week [31];
  336.  
  337.  
  338. int convertFromJson(String & json) {
  339.  
  340.   JsonObject& root = jsonBuffer.parseObject(json);
  341.   int progress = root["level"];
  342.   return progress;
  343. }
  344.  
  345.  
  346.  
  347. void convertPlan(String & json) {
  348.  
  349.   JsonArray& root = jsonBuffer.parseArray(json);
  350.   int i = 0;
  351.   for (auto& request : root) {
  352.     week[i].dayNumber = request["dayNumber"];  // numer tygodnia
  353.     week[i].hourUp = request["hourUp"];     // godzina podniesienia
  354.     week[i].minuteUp = request["minuteUp"];   // minuta podniesienia
  355.     week[i].hourDown = request["hourDown"];   // godzina zapuszczenia
  356.     week[i].minuteDown =   request["minuteDown"];  // minuta zapuszczenia
  357.     i++;
  358.   }
  359.  
  360. }
  361.  
  362.  
  363. void  roundDown(int& val) {
  364.   int r = val % 1000;
  365.   int addVal = 0;
  366.   if (r != 0 && r != 500) {
  367.     if (r > 500) {
  368.       addVal = r - 500;
  369.       val -= addVal;
  370.     } else {
  371.       val -= r;
  372.     }
  373.   }
  374. }
  375.  
  376. void  roundUp(int& val) {
  377.   int r = val % 1000;
  378.   int addVal = 0;
  379.   if (r != 0 && r != 500) {
  380.     addVal = 500 - r;
  381.     if (addVal > 0) {
  382.       val += addVal;
  383.     } else {
  384.       addVal += 500;
  385.       val += addVal;
  386.     }
  387.   }
  388. }
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