Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <DNSServer.h>
- #include <ESP8266WebServer.h>
- #include <WiFiManager.h>
- #include "utils.h"
- ESP8266WebServer server(80);
- // button pin to open blind
- int up_in = 13;
- // button down to close blind
- int down_in = 16;
- // relay to open blind
- int up_out = 5;
- // relay to close blind
- int down_out = 12;
- Day week [31];
- String calendar;
- // time to close blind
- int workTimeDown = 0;
- // time to open blind
- int workTimeUp = 0;
- // blind current percent
- int currentPercent = 0;
- int onePercentDown = 0;
- int onePercentUp = 0;
- bool calibrated = false;
- String deviceID = "0x11";
- void setup() {
- setupPins();
- setupTime();
- Serial.begin(115200);
- WiFiManager wifiManager;
- wifiManager.autoConnect("SmartHome 0x11" , "P@ssw0rd");
- Serial.println("connected...yeey :)");
- server.on("/position", getProgress);
- server.on("/calibrated", isCalibrated);
- server.on("/moveBlind", moveBlind);
- server.on("/getPlan", getPlan);
- server.on("/setPlan", setPlan);
- server.on("/setTimes", setTimes);
- server.on("/getData", getData);
- server.begin();
- Serial.println("HTTP server started");
- }
- void loop() {
- // put your main code here, to run repeatedly:
- server.handleClient();
- checkDay();
- if (!calibrated) {
- if (digitalRead(up_in) == LOW) {
- digitalWrite(up_out, HIGH);
- } else if (digitalRead(up_in) == HIGH) {
- digitalWrite(up_out, LOW);
- }
- if (digitalRead(down_in) == LOW) {
- digitalWrite(down_out, HIGH);
- } else if (digitalRead(down_in) == HIGH) {
- digitalWrite(down_out, LOW);
- }
- }
- else if (calibrated) {
- buttonUp();
- buttonDown();
- }
- }
- void getProgress() {
- String progressConverted = String(currentPercent);
- server.send(200, "text / plain", progressConverted);
- }
- void isCalibrated() {
- server.send(200, "text / plain", String(calibrated));
- }
- void moveBlind() {
- String message;
- int receivedPercent;
- if (server.arg("level") != "") {
- message = server.arg("level");
- // covert message to int
- receivedPercent = atoi(message.c_str());
- }
- else if (server.hasArg("plain") == true) {
- String message = server.arg("plain");
- // convert json to value
- receivedPercent = convertFromJson(message);
- }
- // o ile procent trzeba wysunąć
- int roznica = currentPercent - receivedPercent;
- if (roznica < 0) { // to opuszczaj
- roznica = -roznica; // zamień na liczbę dodatnią
- digitalWrite(down_out, HIGH);
- delay (roznica * onePercentDown); // uśpij na czas wysuwu
- digitalWrite(down_out, LOW);
- }
- else if (roznica > 0) { // to podnosc
- digitalWrite(up_out, HIGH);
- delay (roznica * onePercentUp); // uśpij na czas wysuwu
- digitalWrite(up_out, LOW);
- }
- currentPercent = receivedPercent;
- }
- void getPlan() {
- server.send(200, "text / plain", calendar);
- }
- void getData() {
- String progressConverted = "";
- progressConverted += "czas pracy w gore ";
- progressConverted += String(workTimeUp);
- progressConverted += "<br>czas pracy w dol ";
- progressConverted += String(workTimeDown);
- progressConverted += "<br>percent Up ";
- progressConverted += String(onePercentUp);
- progressConverted += "<br>percent down ";
- progressConverted += String(onePercentDown);
- progressConverted += "<br>currentPercent ";
- progressConverted += String(currentPercent);
- progressConverted += "<br>calibrated ";
- progressConverted += String(calibrated);
- progressConverted += "<br>Calendar <br><br>";
- progressConverted += calendar;
- server.send(200, "text/html", progressConverted);
- }
- void setPlan() {
- if (server.hasArg("plain") == true) {
- String message = server.arg("plain");
- calendar = message;
- convertPlan(message);
- }
- server.send(200, "text/html", "odpowiedz");
- }
- void setTimes() {
- String message;
- if (server.arg("down") != "") {
- message = server.arg("down");
- // covert message to int
- workTimeDown = atoi(message.c_str());
- roundUp(workTimeDown);
- onePercentDown = workTimeDown / 100;
- }
- if (server.arg("up") != "") {
- message = server.arg("up");
- // covert message to int
- workTimeUp = atoi(message.c_str());
- roundDown(workTimeUp);
- onePercentUp = workTimeUp / 100;
- }
- calibrated = true;
- currentPercent = 0;
- server.send(200, "text / plain", "calibrated");
- }
- void setupTime() {
- // konfiguracja czasu z internetu
- configTime(2 * 3600, 0, "pool.ntp.org", "time.nist.gov");
- while (!time(nullptr)) {
- delay(1000);
- }
- time_t now = time(nullptr);
- }
- void setupPins() {
- // ustawienie pinów jako wejścia
- pinMode(up_in, INPUT);
- pinMode(down_in, INPUT);
- // ustawienie pinoów jako wyjścia
- pinMode(up_out, OUTPUT);
- pinMode(down_out, OUTPUT);
- // wyzerowanie wszystkich pinów
- digitalWrite(up_in, LOW);
- digitalWrite(down_in, LOW);
- digitalWrite(up_out, LOW);
- digitalWrite(down_out, LOW);
- }
- void checkDay() {
- time_t now;
- struct tm * timeinfo;
- time(&now);
- timeinfo = localtime(&now);
- /* check days */
- Day day = week[(timeinfo->tm_mday) - 1];
- if (day.hourUp == 0 && day.minuteUp == 0) {
- }
- else {
- if ((day.hourUp != 0) && (day.hourUp == timeinfo->tm_hour) && (day.minuteUp == timeinfo->tm_min) && (currentPercent != 0)) {
- /*podnoś*/
- digitalWrite(up_out, HIGH);
- delay (currentPercent * onePercentUp); // uśpij na czas wysuwu
- digitalWrite(up_out, LOW);
- currentPercent = 0;
- }
- }
- if (day.hourDown == 0 && day.minuteDown == 0) {
- } else {
- if ((day.hourDown != 0) && (day.hourDown == timeinfo->tm_hour) && (day.minuteDown == timeinfo->tm_min) && (currentPercent != 100)) {
- /*opuszczaj*/
- digitalWrite(down_out, HIGH);
- delay ((100 - currentPercent) * onePercentDown); // uśpij na czas wysuwu
- digitalWrite(down_out, LOW);
- currentPercent = 100;
- }
- }
- }
- /* button up calibrated */
- void buttonDown() {
- // wykryto stan wysoki na wejściu sprawdź kolejne warunki
- if (digitalRead(down_in) == LOW && currentPercent < 100) {
- digitalWrite(down_out, HIGH);
- delay((100 - currentPercent)*onePercentDown);
- digitalWrite(down_out, LOW);
- currentPercent = 100;
- }
- }
- /* button down calibrated */
- void buttonUp() {
- // wykryto stan wysoki na wejściu
- if (digitalRead(up_in) == LOW && currentPercent > 0) {
- digitalWrite(up_out, HIGH);
- delay(currentPercent * onePercentUp);
- digitalWrite(up_out, LOW);
- currentPercent = 0;
- }
- }
- #include "utils.h"
- DynamicJsonBuffer jsonBuffer;
- extern Day week [31];
- int convertFromJson(String & json) {
- JsonObject& root = jsonBuffer.parseObject(json);
- int progress = root["level"];
- return progress;
- }
- void convertPlan(String & json) {
- JsonArray& root = jsonBuffer.parseArray(json);
- int i = 0;
- for (auto& request : root) {
- week[i].dayNumber = request["dayNumber"]; // numer tygodnia
- week[i].hourUp = request["hourUp"]; // godzina podniesienia
- week[i].minuteUp = request["minuteUp"]; // minuta podniesienia
- week[i].hourDown = request["hourDown"]; // godzina zapuszczenia
- week[i].minuteDown = request["minuteDown"]; // minuta zapuszczenia
- i++;
- }
- }
- void roundDown(int& val) {
- int r = val % 1000;
- int addVal = 0;
- if (r != 0 && r != 500) {
- if (r > 500) {
- addVal = r - 500;
- val -= addVal;
- } else {
- val -= r;
- }
- }
- }
- void roundUp(int& val) {
- int r = val % 1000;
- int addVal = 0;
- if (r != 0 && r != 500) {
- addVal = 500 - r;
- if (addVal > 0) {
- val += addVal;
- } else {
- addVal += 500;
- val += addVal;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement