Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <esp_wiFi.h>
- #include <WiFiUdp.h>
- #include <NTPClient.h>
- #include <WebServer.h>
- #include <DNSServer.h>
- #include <WiFiManager.h>
- #include <cstdlib>
- #include <Wire.h>
- #include <AsyncTCP.h>
- #include <ESPAsyncWebServer.h>
- #include <Update.h>
- #include <ESPmDNS.h>
- #define U_PART U_SPIFFS
- #include <NeoPixelBrightnessBus.h> //https://github.com/Makuna/NeoPixelBus
- #define LED_PIN 2
- #define NUM_LEDS 60
- int PixelCount = NUM_LEDS;
- byte PixelPin = LED_PIN; //on ESP32 usable any pin below 32
- NeoPixelBrightnessBus<NeoGrbFeature, NeoEsp32I2s1800KbpsMethod> strip(PixelCount,PixelPin); //instead of NeoEsp3
- RgbColor Black(0,0,0);
- RgbColor Orange(255,140,0);
- String tel_1 = "+36xxxxxxxxx"; //Gábor saját
- String tel_2 = "+36xxxxxxxxx"; // Erika saját
- String tel_3 = "+36xxxxxxxxx"; // Gábor Graphax
- String tel_4 = "+36xxxxxxxxx"; // Erika céges
- String tel_5 = "+36xxxxxxxxx"; // Apu
- String tel_6 = "+36xxxxxxxxx"; // Anyu
- String tel_7 = "+36xxxxxxxxx"; // Szabolcs
- String tel_8 = "+36xxxxxxxxx"; // Ildi
- String tel_9 = "+36xxxxxxxxx"; // Norbi
- String tel_10 = "+36xxxxxxxxx"; // Gábor Vodafone
- String tel_11 = "+36xxxxxxxxx"; // Száz Kati
- int opendoor, lastopendoor = 0;
- int direction = 1; //0=bezárás, 1=nyitás
- int led = 2;
- volatile byte state = LOW;
- hw_timer_t * timer = NULL;
- const int output = 2;
- AsyncWebServer server(80);
- const char* host = "GATEOPENER";
- String temp,str,command;
- String sliderValue = "100";
- size_t content_len;
- const char* PARAM_INPUT = "state";
- const char* PARAM_INPUT_1 = "output";
- const char* PARAM_INPUT_2 = "value";
- int gateposition = 2775;
- unsigned long startMillis; //some global variables available anywhere in the program
- unsigned long currentMillis;
- unsigned long startMillis2;
- const char index_html[] PROGMEM = R"rawliteral(
- <!DOCTYPE HTML><html>
- <head>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>GATEOPENER</title>
- <style>
- html {font-family: Arial; display: inline-block; text-align: center;}
- h2 {font-size: 2.4rem;}
- p {font-size: 2.2rem;}
- body {max-width: 600px; margin:0px auto; padding-bottom: 25px;}
- .switch {position: relative; display: inline-block; width: 120px; height: 68px}
- .switch input {display: none}
- .slider {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; border-radius: 34px}
- .slider:before {position: absolute; content: ""; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #fff; -webkit-transition: .4s; transition: .4s; border-radius: 68px}
- input:checked+.slider {background-color: #2196F3}
- input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)}
- .slider2 { -webkit-appearance: none; margin: 14px; width: 300px; height: 20px; background: #ccc;
- outline: none; -webkit-transition: .2s; transition: opacity .2s;}
- .slider2::-webkit-slider-thumb {-webkit-appearance: none; appearance: none; width: 30px; height: 30px; background: #2f4468; cursor: pointer;}
- .slider2::-moz-range-thumb { width: 30px; height: 30px; background: #2f4468; cursor: pointer; }
- </style>
- </head>
- <body>
- <h2>GATEOPENER</h2>
- <p>Pozicio:<span id="timerValue">%TIMERVALUE%</span>%</p>
- <p><input type="range" onchange="updateSliderTimer(this)" id="timerSlider" min="1" max="100" value="%TIMERVALUE%" step="1" class="slider2"></p>
- <p>%BUTTONPLACEHOLDER%</p>
- <p>V5.2</p>
- <p><a href="/updateFW">FW upgrade</p>
- <script>
- function toggleCheckbox(element) {
- var xhr = new XMLHttpRequest();
- if(element.checked){ xhr.open("GET", "/update?output="+element.id+"&state=1", true); xhr.send();}
- else { xhr.open("GET", "/update?output="+element.id+"&state=0", true); xhr.send(); }
- }
- function updateSliderTimer(element) {
- var sliderValue = document.getElementById("timerSlider").value;
- document.getElementById("timerValue").innerHTML = sliderValue;
- var xhr = new XMLHttpRequest();
- xhr.open("GET", "/slider?value="+sliderValue, true);
- xhr.send();
- }
- </script>
- </body>
- </html>
- )rawliteral";
- void resetWiFi() {
- static unsigned long lastTest = millis();
- if ((millis() - lastTest) < 15000)
- return; // check in every 15 sec
- lastTest = millis();
- WiFi.reconnect();
- Serial.println("reconnect..");
- return;
- }
- void setupLeds() {
- strip.Begin();
- strip.SetBrightness(255);
- strip.Show();
- playLeds();
- }
- void playLeds() {
- static unsigned long lastRun = 0;
- static int counter = 0; //elcsúsztatjuk körönként az induló pozíciót
- int ptr; //pointer to led
- if ((millis()-lastRun)<50) return;
- lastRun = millis();
- ptr = counter;
- for (int i=0;i<PixelCount;i++) {
- ptr = i + ptr;
- if (ptr>=PixelCount) ptr -= PixelCount; //végére értünk, visszaugrik az elejére
- strip.SetPixelColor(ptr, (i/10)%2 ? Orange : Black);
- }
- /*
- leds[59] = leds[40];
- leds[39] = leds[20];
- leds[19] = leds[0];
- for(int dot = 0; dot < 20; dot++) {
- leds[dot+40] = leds[dot+41];
- leds[dot+20] = leds[dot+21];
- leds[dot] = leds[dot+1];
- */
- counter++;
- if (counter>=PixelCount) counter = 0;
- strip.Show(); //megjeleníti.
- }
- void delay2(unsigned long d) {
- unsigned long started = millis();
- while ((millis()-started)<d) {
- playLeds();
- yield(); //elengedi a procit mást csinálni
- }
- }
- void modemcomm(String command) {
- Serial2.println(command);
- Serial.print("momdemre kuldott parancs:");
- Serial.println(command);
- Serial.print("modem valasza:");
- Serial.println(Serial2.readString());
- }
- void controllcomm (String command, int data) {
- Serial1.println(command);
- Serial.print("SKR-re kuldott parancs:");
- Serial.println(command);
- if (data ==0) {
- Serial.print("SKR valasza:");
- delay2(200);
- Serial.println(Serial1.readString());
- }
- }
- void dooropen() {
- startMillis2 = currentMillis;
- //controllcomm ("M410",0);
- controllcomm ("M114",1);
- str = Serial1.readString();
- temp = (str.substring(2,6));
- Serial.print("kiolvasott koordinata:");
- Serial.println(temp);
- if (temp == "2775") {
- controllcomm("M106 S255", 0); //Bekapcsolja a mágnest
- controllcomm("M906 X2250", 0);
- delay2(150);
- controllcomm("G1 X0 F12000", 0); //Becsukja a kaput
- //controllcomm("M906 X500", 0);
- opendoor = 0; //a figyelő értéket 0-za (zárt a kapu)
- lastopendoor =0; //a figyelő értéket 0-za (zárt a kapu)
- }
- if (temp =="0.00") {
- digitalWrite(output, HIGH);
- controllcomm("M106 S00", 0); //kikapcsolja amágnest
- delay2(150); //vár 50ms-ot
- controllcomm("M906 X2250", 0);
- delay2(150);
- controllcomm("G1 X2775 F12000", 0); //kinyitja a kaput
- controllcomm("M106 S255", 0);
- //controllcomm("M906 X500", 0);
- opendoor = 1; //a figyelő értéket 1-re állítja (nyitott a kapu)
- lastopendoor =1; //a figyelő értéket 1-re állítja (nyitott a kapu)
- }
- }
- String opencheck(){ //a kapu állapotát megjelenetití a web oldalon
- if(opendoor){
- return "checked";
- }
- else {
- return "";
- }
- return "";
- }
- String processor(const String& var){
- if(var == "BUTTONPLACEHOLDER"){
- String buttons = "";
- String openValue = opencheck();
- buttons+= "<p>ZAR/NYIT:</p><p><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"1\" " + openValue + "><span class=\"slider\"></span></label></p>";
- return buttons;
- }
- else if(var == "TIMERVALUE"){
- return sliderValue;
- }
- return String();
- }
- void handleUpdate(AsyncWebServerRequest *request) {
- char* html = "<form method='POST' action='/doUpdate' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>";
- request->send(200, "text/html", html);
- }
- void handleDoUpdate(AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final) {
- if (!index){
- Serial.println("Update");
- content_len = request->contentLength();
- // if filename includes spiffs, update the spiffs partition
- int cmd = (filename.indexOf("spiffs") > -1) ? U_PART : U_FLASH;
- if (!Update.begin(UPDATE_SIZE_UNKNOWN, cmd)) {
- Update.printError(Serial);
- }
- }
- if (Update.write(data, len) != len) {
- Update.printError(Serial);
- Serial.printf("Progress: %d%%\n", (Update.progress()*100)/Update.size());
- }
- if (final) {
- AsyncWebServerResponse *response = request->beginResponse(302, "text/plain", "Please wait while the device reboots");
- response->addHeader("Refresh", "20");
- response->addHeader("Location", "/");
- request->send(response);
- if (!Update.end(true)){
- Update.printError(Serial);
- } else {
- Serial.println("Update complete");
- Serial.flush();
- ESP.restart();
- }
- }
- }
- void printProgress(size_t prg, size_t sz) {
- Serial.printf("Progress: %d%%\n", (prg*100)/content_len);
- }
- void webInit() {
- server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {request->redirect("/update");});
- server.on("/updateFW", HTTP_GET, [](AsyncWebServerRequest *request){handleUpdate(request);});
- server.on("/doUpdate", HTTP_POST,
- [](AsyncWebServerRequest *request) {},
- [](AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data,
- size_t len, bool final) {handleDoUpdate(request, filename, index, data, len, final);}
- );
- server.onNotFound([](AsyncWebServerRequest *request){request->send(404);});
- server.begin();
- Update.onProgress(printProgress);
- }
- boolean wifiConnect(char* host) {
- WiFi.begin();
- WiFi.waitForConnectResult();
- Serial.println(WiFi.localIP());
- return (WiFi.status() == WL_CONNECTED);
- }
- void notFound(AsyncWebServerRequest *request) {
- request->send(404, "text/plain", "Not found");
- }
- void setup() {
- pinMode(output, OUTPUT);
- digitalWrite(output, LOW);
- setupLeds();
- delay(10000);
- Serial1.begin(57600,SERIAL_8N1,16,17); //SKR
- Serial2.begin(57600,SERIAL_8N1,26,27); //Modem
- Serial.begin(57600); //belső soros monitor
- WiFi.setHostname("GATEOPENER");
- WiFi.begin("CLOCK", "xxxxxxxx");
- //WiFiManager wifiManager;
- //wifiManager.setDebugOutput(true);
- //wifiManager.autoConnect("GATEOPENER", "12345678");
- //wifiManager.setConnectTimeout (700);
- while (WiFi.status() != WL_CONNECTED) {
- delay2(500);
- Serial.println("Connecting to WiFi..");
- }
- server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send_P(200, "text/html", index_html, processor);
- });
- server.on("/updateFW", HTTP_GET, [](AsyncWebServerRequest *request){handleUpdate(request);});
- server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) {
- String inputMessage;
- String inputMessage2;
- if (request->hasParam(PARAM_INPUT_1) && request->hasParam(PARAM_INPUT)) {
- inputMessage = request->getParam(PARAM_INPUT_1)->value();
- inputMessage2 = request->getParam(PARAM_INPUT)->value();
- }
- else {
- inputMessage = "No message sent";
- inputMessage2 = "No message sent";
- }
- if (inputMessage.toInt() == 1) opendoor = inputMessage2.toInt();
- Serial.print ("opendoor");
- Serial.println(opendoor);
- request->send(200, "text/plain", "OK");
- });
- server.on("/slider", HTTP_GET, [] (AsyncWebServerRequest *request) {
- String inputMessage;
- if (request->hasParam(PARAM_INPUT_2)) {
- inputMessage = request->getParam(PARAM_INPUT_2)->value();
- sliderValue = inputMessage;
- gateposition = (sliderValue.toInt()*2.55);
- }
- else {
- inputMessage = "No message sent";
- }
- request->send(200, "text/plain", "OK");
- });
- server.onNotFound(notFound);
- server.begin();
- char host[40];
- uint8_t baseMac[6];
- esp_read_mac(baseMac, ESP_MAC_WIFI_STA);
- sprintf(host, "GATEOPENER%02X%02X%02X%02X%02X%02X", baseMac[0], baseMac[1], baseMac[2], baseMac[3], baseMac[4], baseMac[5]);
- if(!wifiConnect(host)) {
- Serial.println("Connection failed");
- return;
- }
- MDNS.begin(host);
- webInit();
- MDNS.addService("http", "tcp", 80);
- Serial.printf("Ready! Open http://%s.local in your browser\n", host);
- Serial.println("Elindult");
- modemcomm("AT");
- /*
- while ((Serial2.readString()).substring(0,2) != "AT") {
- delay2(500);
- Serial2.println("AT");
- Serial.print('.');
- Serial.print(Serial2.readString().substring(0,2));
- Serial2.println("AT");
- }
- */
- modemcomm("AT+CLIP=1");
- Serial.println("loop");
- controllcomm("M906 X2250", 0); //2250mA állítja a motor áramot
- //delay2(150);
- controllcomm("G28 X", 0); //bezárja a kaput
- controllcomm("M106 S255", 0); //bekapcsolja a mágnest
- //controllcomm("M906 X500", 0);
- Serial.print(Serial1.readString());
- Serial.print(Serial1.readString());
- Serial.println(Serial1.readString());
- }
- void loop() {
- playLeds();
- String ipString = WiFi.localIP().toString();
- if (Serial2.available()) {
- str = Serial2.readString();
- Serial.println(str);
- temp = (str.substring(18,30));
- Serial.print("kiolvasott szam:");
- Serial.println(str.substring(18,30));
- if (temp == tel_1)
- {
- Serial.println("Gabor sajat");
- modemcomm("AT H");
- modemcomm("AT+CLIP=1");
- dooropen();
- }
- if (temp == tel_2)
- {
- Serial.println("Erika sajat");
- modemcomm("AT H");
- modemcomm("AT+CLIP=1");
- dooropen();
- }
- if (temp == tel_4)
- {
- Serial.println("Erika ceges");
- modemcomm("AT H");
- modemcomm("AT+CLIP=1");
- dooropen();
- }
- if (temp == tel_5)
- {
- Serial.println("Apu");
- modemcomm("AT H");
- modemcomm("AT+CLIP=1");
- dooropen();
- }
- if (temp == tel_6)
- {
- Serial.println("Anyu");
- modemcomm("AT H");
- modemcomm("AT+CLIP=1");
- dooropen();
- }
- if (temp == tel_7)
- {
- Serial.println("Szabolcs");
- modemcomm("AT H");
- modemcomm("AT+CLIP=1");
- dooropen();
- }
- if (temp == tel_8)
- {
- Serial.println("Ildi");
- modemcomm("AT H");
- modemcomm("AT+CLIP=1");
- dooropen();
- }
- if (temp == tel_9)
- {
- Serial.println("Norbi");
- modemcomm("AT H");
- modemcomm("AT+CLIP=1");
- dooropen();
- }
- if (temp == tel_10)
- {
- Serial.println("Gabor Vodafone");
- modemcomm("AT H");
- modemcomm("AT+CLIP=1");
- dooropen();
- }
- if (temp == tel_11)
- {
- Serial.println("Szaz Kati");
- modemcomm("AT H");
- modemcomm("AT+CLIP=1");
- dooropen();
- }
- else {
- modemcomm("AT H"); //egyébként leteszi
- modemcomm("AT+CLIP=1"); //kéri a hívószámkijelzést
- }
- }
- if (lastopendoor != opendoor) {
- modemcomm("AT H");
- modemcomm("AT+CLIP=1");
- dooropen();
- lastopendoor = opendoor;
- }
- currentMillis = millis();
- if (currentMillis - startMillis >= 30000) //30mp-enként lefut
- {
- controllcomm("M906 X2250", 0);
- //delay2(150);
- controllcomm("M17 X", 0); //tartásba rakja a motort
- //controllcomm("M710 A1 S255", 0); //beállítja a ventit
- modemcomm("AT"); //reseteli a modemet
- modemcomm("AT+CLIP=1"); //kéri a hívószámkijezést
- startMillis = currentMillis;
- }
- if (currentMillis - startMillis2 >= 180000) //3percenként lefut
- {
- controllcomm ("M114",1);
- str = Serial1.readString();
- temp = (str.substring(2,6));
- Serial.print("nyitott kapu timeout:");
- Serial.println(temp.toInt());
- if (temp.toInt() > 0 ) //ha nagyobb az X értéke 0-nál akkor
- {
- controllcomm("M106 S255", 0); //bekapcsolja a mágnest
- controllcomm("M906 X2250", 0);
- //delay2(150);
- controllcomm("G1 X0 F12000", 0); //becsukja a kaput
- //controllcomm("M906 X500", 0);
- }
- startMillis2 = currentMillis;
- opendoor = 0;
- lastopendoor =0;
- }
- if (opendoor == 0) digitalWrite(output, LOW);
- else digitalWrite(output, HIGH);
- if ((WiFi.status() != WL_CONNECTED) || (ipString == "0.0.0.0")) {
- Serial.println("WiFi problema");
- resetWiFi();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement