Advertisement
Double_G

Untitled

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