Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- IPAddress ip(1, 1, 1, 125);
- IPAddress gateway(1, 1, 1, 1);
- IPAddress subnet(255, 255, 255, 0);
- String ssid = "NAG";
- String heslo = "";
- int led1 = D5;
- int tlacitko = D8;
- int pinMotor[] = {16, 5, 4, 0};
- int delkaKroku = 500;
- int buttonState = 0; // current state of the button
- int lastButtonState = 0; // previous state of the button
- int startPressed = 0; // the time button was pressed
- int endPressed = 0; // the time button was released
- int timeHold = 0; // the time button is hold
- int timeReleased = 0; // the time button is released
- bool stavLed = false;
- bool pripojOdpoj = false;
- int cas1;
- int cas2;
- int cas3;
- int poziceMot[8][4] = {
- {0,0,0,1},
- {0,0,1,1},
- {0,0,1,0},
- {0,1,1,0},
- {0,1,0,0},
- {1,1,0,0},
- {1,0,0,0},
- {1,0,0,1},
- };
- // Server na portu 80
- WiFiServer server(80);
- // Proměnná pro uložení HTTP requestu
- String header;
- // Auxiliar variables to store the current output state
- String output5State = "off";
- String output4State = "off";
- // Assign output variables to GPIO pins
- const int output5 = D0;
- const int output4 = D1;
- void otaceni(int smer) {
- while(smer > 0) {
- for(int i = 0; i < 8; i++) {
- for(int d = 0; d < 4; d++) {
- digitalWrite(pinMotor[d], poziceMot[i][d]);
- }
- delay(1);
- }
- smer--;
- }
- while(smer < 0){
- for(int i = 7; i >= 0; i--) {
- for(int d = 0; d < 4; d++) {
- digitalWrite(pinMotor[d], poziceMot[i][d]);
- }
- delay(1);
- }
- smer++;
- }
- }
- void pripojSeNaWIFI() {
- WiFi.config(ip, gateway, subnet);
- // Připojení k wifi síti
- WiFi.begin(ssid,heslo);
- Serial.print("Připojuji se k wifi síti");
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- Serial.println();
- Serial.print("Připojeno k WIFI, IP: : ");
- Serial.println(WiFi.localIP());
- }
- void kontrolaTlacitka() {
- buttonState = digitalRead(tlacitko);
- // button state changed
- if (buttonState != lastButtonState) {
- // the button was just pressed
- if (buttonState == HIGH) {
- startPressed = millis();
- timeReleased = startPressed - endPressed;
- if (timeReleased >= 1000) {
- Serial.print("Tlačítko stisknuto na více než 1 sekundu, ");
- pripojOdpoj = !pripojOdpoj;
- if(pripojOdpoj == true) {
- pripojSeNaWIFI();
- server.begin();
- }
- else {
- Serial.println("Odpojuji od wifi sítě");
- WiFi.disconnect();
- }
- }
- // the button was just released
- } else {
- endPressed = millis();
- timeHold = endPressed - startPressed;
- }
- }
- // save the current state as the last state,
- //for next time through the loop
- lastButtonState = buttonState;
- }
- void handleKlient() {
- WiFiClient client = server.available(); // Listen for incoming clients
- if (client) { // If a new client connects,
- Serial.println("New Client."); // print a message out in the serial port
- String currentLine = ""; // make a String to hold incoming data from the client
- while (client.connected()) { // loop while the client's connected
- if (client.available()) { // if there's bytes to read from the client,
- char c = client.read(); // read a byte, then
- Serial.write(c); // print it out the serial monitor
- header += c;
- if (c == '\n') { // if the byte is a newline character
- // if the current line is blank, you got two newline characters in a row.
- // that's the end of the client HTTP request, so send a response:
- if (currentLine.length() == 0) {
- client.println("HTTP/1.1 200 OK");
- client.println("Content-type:text/html");
- client.println("Connection: close");
- client.println();
- // turns the GPIOs on and off
- if (header.indexOf("GET /5/on") >= 0) {
- Serial.println("GPIO 5 on");
- output5State = "on";
- output4State = "off";
- digitalWrite(output5, HIGH);
- } else if (header.indexOf("GET /5/off") >= 0) {
- Serial.println("GPIO 5 off");
- output5State = "off";
- digitalWrite(output5, LOW);
- } else if (header.indexOf("GET /4/on") >= 0) {
- Serial.println("GPIO 4 on");
- output4State = "on";
- output5State = "off";
- digitalWrite(output4, HIGH);
- } else if (header.indexOf("GET /4/off") >= 0) {
- Serial.println("GPIO 4 off");
- output4State = "off";
- digitalWrite(output4, LOW);
- }
- // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
- // and a content-type so the client knows what's coming, then a blank line:
- client.println("<!DOCTYPE HTML><html>");
- client.println(" <head><meta charset='utf-8'><meta name='generator' content='PSPad editor, www.pspad.com'><title>VDA</title></head>");
- client.println("<body><h1><b>NAG-IOT-VDA-Ioutaci</b></h1><div><form '#' method='POST'><label for='CAS'>Trvani jednoho kroku</label>(ms)<input type='number' name='parametr' id='CAS'>");
- client.println("<input type='submit' value='SEND'></form><div>");
- if (output4State=="off") {
- client.println("<p><a href=\'/4/on\'><button class=\'button\'>Doleva</button></a></p>");
- } else {
- client.println("<p><a href=\'/4/off\'><button class=\'button button2\'>Doleva jede</button></a></p>");
- }
- if (output5State=="off") {
- client.println("<p><a href=\'/5/on\'><button class=\'button\'>Doprava</button></a></p>");
- } else {
- client.println("<p><a href=\'/5/off\'><button class=\'button button2\'>Doprava jede</button></a></p>");
- }
- client.println("</body></html>");
- // The HTTP response ends with another blank line
- client.println();
- // Break out of the while loop
- break;
- } else { // if you got a newline, then clear currentLine
- currentLine = "";
- }
- } else if (c != '\r') { // if you got anything else but a carriage return character,
- currentLine += c; // add it to the end of the currentLine
- }
- }
- }
- // Clear the header variable
- header = "";
- // Close the connection
- client.stop();
- Serial.println("Client disconnected.");
- Serial.println("");
- }
- }
- void setup() {
- // Inicializace Sériové linky
- Serial.begin(115200);
- Serial.println();
- WiFi.disconnect();
- for(int i = 0; i < 4; i++) {
- pinMode(pinMotor[i], OUTPUT);
- }
- pinMode(led1,OUTPUT);
- pinMode(tlacitko,INPUT_PULLUP);
- output4State == "on";
- }
- void loop() {
- cas1 = millis();
- if(cas1-cas2 > delkaKroku && output4State == "on") {
- otaceni(1);
- cas2 = millis();
- Serial.println("Doleva");
- }
- if(cas1-cas2 > delkaKroku && output5State == "on") {
- otaceni(-1);
- Serial.println("Dprava");
- cas2 = millis();
- }
- if(WiFi.status() == WL_CONNECTED && cas1-cas3 > 500) {
- stavLed = !stavLed;
- digitalWrite(led1,stavLed);
- cas3 = millis();
- }
- else if(WiFi.status() != WL_CONNECTED) {
- digitalWrite(led1,LOW);
- }
- kontrolaTlacitka();
- handleKlient();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement