Advertisement
Double_G

Untitled

Sep 12th, 2021
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.09 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <esp_wiFi.h>        
  3. #include <WiFiUdp.h>
  4. #include <NTPClient.h>
  5. #include <WebServer.h>
  6. #include <DNSServer.h>
  7. #include <WiFiManager.h>
  8. #include <cstdlib>
  9. #include <Wire.h>
  10. #include <AsyncTCP.h>
  11. #include <ESPAsyncWebServer.h>
  12. #include <Update.h>
  13. #include <ESPmDNS.h>
  14. #define U_PART U_SPIFFS
  15.  
  16. String tel_1  = "+36xxxxxxxxx";  //Gábor saját
  17. String tel_2  = "+36xxxxxxxxx"; // Erika saját
  18. String tel_3  = "+36xxxxxxxxx"; // Gábor Graphax
  19. String tel_4  = "+36xxxxxxxxx"; // Erika céges
  20. String tel_5  = "+36xxxxxxxxx"; // Apu
  21. String tel_6  = "+36xxxxxxxxx"; // Anyu
  22. String tel_7  = "+36xxxxxxxxx"; // Szabolcs
  23. String tel_8  = "+36xxxxxxxxx"; // Ildi
  24. String tel_9  = "+36xxxxxxxxx"; // Norbi
  25. String tel_10 = "+36xxxxxxxxx"; // Gábor Vodafone
  26. String tel_11 = "+36xxxxxxxxx"; // Száz Kati
  27.  
  28. int opendoor, lastopendoor = 0;
  29. int direction = 1; //0=bezárás, 1=nyitás
  30. int led = 2;
  31. volatile byte state = LOW;
  32. hw_timer_t * timer = NULL;
  33. const int output = 2;
  34.  
  35. AsyncWebServer server(80);
  36. const char* host = "GATEOPENER";
  37. String temp,str,command;
  38. String sliderValue = "100";
  39. size_t content_len;
  40. const char* PARAM_INPUT = "state";
  41. const char* PARAM_INPUT_1 = "output";
  42. const char* PARAM_INPUT_2 = "value";
  43. int gateposition = 2775;
  44.  
  45.  
  46.  
  47. unsigned long startMillis;  //some global variables available anywhere in the program
  48. unsigned long currentMillis;
  49. unsigned long startMillis2;
  50.  
  51. const char index_html[] PROGMEM = R"rawliteral(
  52. <!DOCTYPE HTML><html>
  53. <head>
  54.  <meta name="viewport" content="width=device-width, initial-scale=1">
  55.  <title>GATEOPENER</title>
  56.  <style>
  57.    html {font-family: Arial; display: inline-block; text-align: center;}
  58.    h2 {font-size: 2.4rem;}
  59.    p {font-size: 2.2rem;}
  60.    body {max-width: 600px; margin:0px auto; padding-bottom: 25px;}
  61.    .switch {position: relative; display: inline-block; width: 120px; height: 68px}
  62.    .switch input {display: none}
  63.    .slider {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; border-radius: 34px}
  64.    .slider:before {position: absolute; content: ""; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 68px}
  65.    input:checked+.slider {background-color: #2196F3}
  66.    input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)}
  67.    .slider2 { -webkit-appearance: none; margin: 14px; width: 300px; height: 20px; background: #ccc;
  68.      outline: none; -webkit-transition: .2s; transition: opacity .2s;}
  69.    .slider2::-webkit-slider-thumb {-webkit-appearance: none; appearance: none; width: 30px; height: 30px; background: #2f4468; cursor: pointer;}
  70.    .slider2::-moz-range-thumb { width: 30px; height: 30px; background: #2f4468; cursor: pointer; }
  71.  </style>
  72. </head>
  73. <body>
  74.  <h2>GATEOPENER</h2>
  75.  <p>Pozicio:<span id="timerValue">%TIMERVALUE%</span>&#37</p>
  76.  <p><input type="range" onchange="updateSliderTimer(this)" id="timerSlider" min="1" max="100" value="%TIMERVALUE%" step="1" class="slider2"></p>
  77.  <p>%BUTTONPLACEHOLDER%</p>
  78.  <p>V5.2</p>
  79.  <p><a href="/updateFW">FW upgrade</p>
  80. <script>
  81. function toggleCheckbox(element) {
  82.  var xhr = new XMLHttpRequest();
  83.  if(element.checked){ xhr.open("GET", "/update?output="+element.id+"&state=1", true); xhr.send();}
  84.   else {  xhr.open("GET", "/update?output="+element.id+"&state=0", true); xhr.send(); }
  85. }
  86. function updateSliderTimer(element) {
  87.  var sliderValue = document.getElementById("timerSlider").value;
  88.  document.getElementById("timerValue").innerHTML = sliderValue;
  89.  var xhr = new XMLHttpRequest();
  90.  xhr.open("GET", "/slider?value="+sliderValue, true);
  91.  xhr.send();
  92. }
  93. </script>
  94. </body>
  95. </html>
  96. )rawliteral";
  97.  
  98. void resetWiFi() {
  99.   static unsigned long lastTest = millis();
  100.  
  101.   if ((millis() - lastTest) < 15000)
  102.     return; // check in every 15 sec
  103.   lastTest = millis();
  104.  
  105.   WiFi.reconnect();
  106.   Serial.println("reconnect..");
  107.   return;
  108. }
  109.  
  110. void modemcomm(String command) {
  111.   Serial2.println(command);
  112.   Serial.print("momdemre kuldott parancs:");
  113.   Serial.println(command);
  114.   Serial.print("modem valasza:");
  115.   Serial.println(Serial2.readString());
  116. }
  117.  
  118. void controllcomm (String command, int data) {
  119.   Serial1.println(command);
  120.   Serial.print("SKR-re kuldott parancs:");
  121.   Serial.println(command);
  122.   if (data ==0) {
  123.     Serial.print("SKR valasza:");
  124.     delay(200);
  125.     Serial.println(Serial1.readString());
  126.   }
  127.  
  128. }
  129.  
  130. void dooropen() {
  131. startMillis2 = currentMillis;
  132. //controllcomm ("M410",0);
  133. controllcomm ("M114",1);
  134. str = Serial1.readString();
  135. temp = (str.substring(2,6));
  136. Serial.print("kiolvasott koordinata:");
  137. Serial.println(temp);
  138.  
  139. if (temp == "2775") {
  140.   controllcomm("M106 S255", 0);         //Bekapcsolja a mágnest
  141.   controllcomm("M906 X2250", 0);
  142.   delay(150);  
  143.   controllcomm("G1 X0 F12000", 0);       //Becsukja a kaput
  144.   //controllcomm("M906 X500", 0);
  145.   opendoor = 0;                         //a figyelő értéket 0-za (zárt a kapu)
  146.   lastopendoor =0;                      //a figyelő értéket 0-za (zárt a kapu)
  147. }
  148. if (temp =="0.00") {                    
  149.   digitalWrite(output, HIGH);
  150.   controllcomm("M106 S00", 0);          //kikapcsolja amágnest
  151.   delay(150);                            //vár 50ms-ot
  152.   controllcomm("M906 X2250", 0);
  153.   delay(150);  
  154.   controllcomm("G1 X2775 F12000", 0);   //kinyitja a kaput
  155.   controllcomm("M106 S255", 0);
  156.   //controllcomm("M906 X500", 0);
  157.   opendoor = 1;                         //a figyelő értéket 1-re állítja (nyitott a kapu)
  158.   lastopendoor =1;                      //a figyelő értéket 1-re állítja (nyitott a kapu)
  159. }
  160.  
  161. }
  162.  
  163.  
  164. String opencheck(){                     //a kapu állapotát megjelenetití a web oldalon
  165.   if(opendoor){
  166.     return "checked";
  167.   }
  168.   else {
  169.     return "";
  170.   }
  171.   return "";
  172. }
  173.  
  174.  
  175.  
  176. String processor(const String& var){
  177.   if(var == "BUTTONPLACEHOLDER"){
  178.     String buttons = "";
  179.     String openValue = opencheck();
  180.     buttons+= "<p>ZAR/NYIT:</p><p><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"1\" " + openValue + "><span class=\"slider\"></span></label></p>";
  181.     return buttons;
  182.   }
  183.  
  184.   else if(var == "TIMERVALUE"){
  185.     return sliderValue;
  186.   }
  187.   return String();
  188. }
  189.  
  190. void handleUpdate(AsyncWebServerRequest *request) {
  191.   char* html = "<form method='POST' action='/doUpdate' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>";
  192.   request->send(200, "text/html", html);
  193. }
  194.  
  195.  
  196. void handleDoUpdate(AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final) {
  197.   if (!index){
  198.     Serial.println("Update");
  199.     content_len = request->contentLength();
  200.     // if filename includes spiffs, update the spiffs partition
  201.     int cmd = (filename.indexOf("spiffs") > -1) ? U_PART : U_FLASH;
  202.  
  203.   if (!Update.begin(UPDATE_SIZE_UNKNOWN, cmd)) {  
  204.       Update.printError(Serial);
  205.     }
  206.   }
  207.   if (Update.write(data, len) != len) {
  208.     Update.printError(Serial);
  209.     Serial.printf("Progress: %d%%\n", (Update.progress()*100)/Update.size());
  210.       }
  211. if (final) {
  212.     AsyncWebServerResponse *response = request->beginResponse(302, "text/plain", "Please wait while the device reboots");
  213.     response->addHeader("Refresh", "20");  
  214.     response->addHeader("Location", "/");
  215.     request->send(response);
  216.     if (!Update.end(true)){
  217.       Update.printError(Serial);
  218.     } else {
  219.       Serial.println("Update complete");
  220.       Serial.flush();
  221.       ESP.restart();
  222.     }
  223.   }
  224. }
  225.  
  226. void printProgress(size_t prg, size_t sz) {
  227.   Serial.printf("Progress: %d%%\n", (prg*100)/content_len);
  228. }
  229.  
  230. boolean webInit() {
  231.   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {request->redirect("/update");});
  232.   server.on("/updateFW", HTTP_GET, [](AsyncWebServerRequest *request){handleUpdate(request);});
  233.   server.on("/doUpdate", HTTP_POST,
  234.     [](AsyncWebServerRequest *request) {},
  235.     [](AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data,
  236.                   size_t len, bool final) {handleDoUpdate(request, filename, index, data, len, final);}
  237.   );
  238.   server.onNotFound([](AsyncWebServerRequest *request){request->send(404);});
  239.   server.begin();
  240.   Update.onProgress(printProgress);
  241.   }
  242.  
  243. boolean wifiConnect(char* host) {
  244.   WiFi.begin();
  245.   WiFi.waitForConnectResult();
  246.   Serial.println(WiFi.localIP());
  247.   return (WiFi.status() == WL_CONNECTED);
  248. }
  249.  
  250.  
  251. void notFound(AsyncWebServerRequest *request) {
  252.   request->send(404, "text/plain", "Not found");
  253. }
  254.  
  255. void setup() {
  256.   pinMode(output, OUTPUT);
  257.   digitalWrite(output, LOW);
  258.   delay(10000);
  259.  
  260.   Serial1.begin(57600,SERIAL_8N1,16,17);  //SKR
  261.   Serial2.begin(57600,SERIAL_8N1,26,27);  //Modem
  262.   Serial.begin(57600);                    //belső soros monitor
  263.   WiFi.setHostname("GATEOPENER");
  264.  
  265.  
  266.   WiFi.begin("CLOCK", "xxxxxxxx");
  267.   //WiFiManager wifiManager;
  268.   //wifiManager.setDebugOutput(true);
  269.   //wifiManager.autoConnect("GATEOPENER", "12345678");
  270.   //wifiManager.setConnectTimeout (700);
  271.  
  272.   while (WiFi.status() != WL_CONNECTED) {
  273.   delay(500);
  274.   Serial.println("Connecting to WiFi..");
  275. }
  276.  
  277.     server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  278.     request->send_P(200, "text/html", index_html, processor);
  279.   });
  280.  
  281.   server.on("/updateFW", HTTP_GET, [](AsyncWebServerRequest *request){handleUpdate(request);});
  282.  
  283.   server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) {
  284.     String inputMessage;
  285.     String inputMessage2;
  286.     if (request->hasParam(PARAM_INPUT_1) && request->hasParam(PARAM_INPUT)) {
  287.       inputMessage = request->getParam(PARAM_INPUT_1)->value();
  288.       inputMessage2 = request->getParam(PARAM_INPUT)->value();
  289.       }
  290.     else {
  291.       inputMessage = "No message sent";
  292.       inputMessage2 = "No message sent";
  293.     }
  294.    
  295.     if (inputMessage.toInt() == 1)  opendoor = inputMessage2.toInt();
  296.     Serial.print ("opendoor");
  297.     Serial.println(opendoor);
  298.        
  299.     request->send(200, "text/plain", "OK");
  300.   });
  301.  
  302.   server.on("/slider", HTTP_GET, [] (AsyncWebServerRequest *request) {
  303.     String inputMessage;
  304.     if (request->hasParam(PARAM_INPUT_2)) {
  305.       inputMessage = request->getParam(PARAM_INPUT_2)->value();
  306.       sliderValue = inputMessage;
  307.       gateposition = (sliderValue.toInt()*2.55);
  308.     }
  309.     else {
  310.       inputMessage = "No message sent";
  311.     }
  312.    
  313.     request->send(200, "text/plain", "OK");
  314.   });
  315.  
  316.   server.onNotFound(notFound);
  317.  
  318.   server.begin();
  319.   char host[40];
  320.   snprintf(host, 16, "GATEOPENER", ESP.getEfuseMac());
  321.   if(!wifiConnect(host)) {
  322.     Serial.println("Connection failed");
  323.     return;
  324.   }
  325.  
  326.   MDNS.begin(host);
  327.   webInit();
  328.   MDNS.addService("http", "tcp", 80);
  329.   Serial.printf("Ready! Open http://%s.local in your browser\n", host);
  330.  
  331.  Serial.println("Elindult");
  332. modemcomm("AT");
  333. /*
  334. while ((Serial2.readString()).substring(0,2) != "AT") {
  335. delay(500);
  336. Serial2.println("AT");
  337. Serial.print('.');
  338. Serial.print(Serial2.readString().substring(0,2));
  339. Serial2.println("AT");
  340. }
  341. */
  342.  
  343. modemcomm("AT+CLIP=1");
  344. Serial.println("loop");
  345. controllcomm("M906 X2250", 0);                //2250mA állítja a motor áramot
  346. //delay(150);  
  347. controllcomm("G28 X", 0);                     //bezárja a kaput
  348. controllcomm("M106 S255", 0);                 //bekapcsolja a mágnest
  349. //controllcomm("M906 X500", 0);
  350. Serial.print(Serial1.readString());
  351. Serial.print(Serial1.readString());
  352. Serial.println(Serial1.readString());
  353.  
  354.  
  355.  
  356. }
  357.  
  358. void loop() {
  359.   String ipString = WiFi.localIP().toString();
  360.      if (Serial2.available()) {
  361.       str = Serial2.readString();
  362.       Serial.println(str);
  363.       temp = (str.substring(18,30));
  364.       Serial.print("kiolvasott szam:");
  365.       Serial.println(str.substring(18,30));
  366.  
  367.     if (temp == tel_1)
  368.      {
  369.         Serial.println("Gabor sajat");
  370.         modemcomm("AT H");
  371.         modemcomm("AT+CLIP=1");
  372.         dooropen();
  373.      }
  374.  
  375.     if (temp == tel_2)
  376.      {
  377.         Serial.println("Erika sajat");
  378.         modemcomm("AT H");
  379.         modemcomm("AT+CLIP=1");
  380.         dooropen();
  381.      }
  382.    
  383.          if (temp == tel_4)
  384.      {
  385.         Serial.println("Erika ceges");
  386.         modemcomm("AT H");
  387.         modemcomm("AT+CLIP=1");
  388.         dooropen();
  389.      }
  390.  
  391.          if (temp == tel_5)
  392.      {
  393.         Serial.println("Apu");
  394.         modemcomm("AT H");
  395.         modemcomm("AT+CLIP=1");
  396.         dooropen();
  397.      }
  398.  
  399.     if (temp == tel_6)
  400.      {
  401.         Serial.println("Anyu");
  402.         modemcomm("AT H");
  403.         modemcomm("AT+CLIP=1");
  404.         dooropen();
  405.      }
  406.  
  407.     if (temp == tel_7)
  408.      {
  409.         Serial.println("Szabolcs");
  410.         modemcomm("AT H");
  411.         modemcomm("AT+CLIP=1");
  412.         dooropen();
  413.      }
  414.  
  415.  
  416.     if (temp == tel_8)
  417.      {
  418.         Serial.println("Ildi");
  419.         modemcomm("AT H");
  420.         modemcomm("AT+CLIP=1");
  421.         dooropen();
  422.      }
  423.  
  424.      
  425.     if (temp == tel_9)
  426.      {
  427.         Serial.println("Norbi");
  428.         modemcomm("AT H");
  429.         modemcomm("AT+CLIP=1");
  430.         dooropen();
  431.      }
  432.  
  433.      if (temp == tel_10)
  434.      {
  435.         Serial.println("Gabor Vodafone");
  436.         modemcomm("AT H");
  437.         modemcomm("AT+CLIP=1");
  438.         dooropen();
  439.      }
  440.  
  441. if (temp == tel_11)
  442.      {
  443.         Serial.println("Szaz Kati");
  444.         modemcomm("AT H");
  445.         modemcomm("AT+CLIP=1");
  446.         dooropen();
  447.      }
  448.  
  449.      else {
  450.        modemcomm("AT H");                 //egyébként leteszi
  451.        modemcomm("AT+CLIP=1");            //kéri a hívószámkijelzést
  452.      }
  453.    
  454.   }
  455.  
  456.   if (lastopendoor != opendoor) {
  457.     modemcomm("AT H");
  458.     modemcomm("AT+CLIP=1");
  459.     dooropen();
  460.     lastopendoor = opendoor;
  461.   }
  462.  
  463.   currentMillis = millis();  
  464.   if (currentMillis - startMillis >= 30000)       //30mp-enként lefut
  465.   {
  466.     controllcomm("M906 X2250", 0);
  467.     //delay(150);
  468.     controllcomm("M17 X", 0);                     //tartásba rakja a motort
  469.     //controllcomm("M710 A1 S255", 0);            //beállítja a ventit
  470.     modemcomm("AT");                              //reseteli a modemet
  471.     modemcomm("AT+CLIP=1");                       //kéri a hívószámkijezést
  472.     startMillis = currentMillis;            
  473.   }
  474. if (currentMillis - startMillis2 >= 180000)       //3percenként lefut
  475.   {
  476.     controllcomm ("M114",1);                      
  477.     str = Serial1.readString();
  478.     temp = (str.substring(2,6));
  479.     Serial.print("nyitott kapu timeout:");
  480.     Serial.println(temp.toInt());
  481.     if (temp.toInt() > 0 )                        //ha nagyobb az X értéke 0-nál akkor
  482.     {
  483.       controllcomm("M106 S255", 0);               //bekapcsolja a mágnest
  484.       controllcomm("M906 X2250", 0);
  485.       //delay(150);  
  486.       controllcomm("G1 X0 F12000", 0);             //becsukja a kaput
  487.       //controllcomm("M906 X500", 0);    
  488.     }
  489.     startMillis2 = currentMillis;    
  490.     opendoor = 0;
  491.     lastopendoor =0;
  492.  
  493.   }
  494.  
  495. if (opendoor == 0) digitalWrite(output, LOW);
  496.   else digitalWrite(output, HIGH);
  497.  
  498.   if ((WiFi.status() != WL_CONNECTED) || (ipString == "0.0.0.0")) {
  499.     Serial.println("WiFi problema");
  500.     resetWiFi();
  501.   }
  502. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement