Sei sulla pagina 1di 10

Codigo:

#include <ESP32Servo.h>

#include <analogWrite.h>

Servo myservo;

/*

WiFi Web Server LED Blink

A simple web server that lets you blink an LED via the web.

This sketch will print the IP address of your WiFi Shield (once connected)

to the Serial monitor. From there, you can open that address in a web browser

to turn on and off the LED on pin 5.

If the IP address of your shield is yourAddress:

http://yourAddress/H turns the LED on

http://yourAddress/L turns it off

This example is written for a network using WPA encryption. For

WEP or WPA, change the Wifi.begin() call accordingly.

Circuit:

* WiFi shield attached

* LED attached to pin 5

created for arduino 25 Nov 2012

by Tom Igoe

ported for sparkfun esp32

31.01.2017 by Jan Hendrik Berlin

*/
#include <WiFi.h>

const char* ssid = "Redmi Note 11";

const char* password = "gabrieldasilva";

WiFiServer server(80);

static const int servo = 26;

class DCMotor {

int spd = 255, pin1, pin2;

public:

void Pinout(int in1, int in2){ // Pinout é o método para a declaração dos pinos que vão
controlar o objeto motor

pin1 = in1;

pin2 = in2;

pinMode(pin1, OUTPUT);

pinMode(pin2, OUTPUT);

void Speed(int in1){ // Speed é o método que irá ser responsável por salvar a velocidade de
atuação do motor

spd = in1;

void Forward(){ // Forward é o método para fazer o motor girar para frente

analogWrite(pin1, spd);

digitalWrite(pin2, LOW);

void Backward(){ // Backward é o método para fazer o motor girar para trás

digitalWrite(pin1, LOW);

analogWrite(pin2, spd);

}
void Stop(){ // Stop é o metodo para fazer o motor ficar parado.

digitalWrite(pin1, LOW);

digitalWrite(pin2, LOW);

};

DCMotor Motor1, Motor2; // Criação de dois objetos motores, já que usaremos dois
motores, e eles já estão prontos para receber os comandos já configurados acima.

unsigned long currentTime = millis();

unsigned long previousTime = 0;

const long timeoutTime = 2000;

int angulofechar = 180;

int anguloabrir = 10;

int i;

void setup() {

Motor1.Pinout(22,21); // Seleção dos pinos que cada motor usará, como descrito na classe.

Motor2.Pinout(13,14);

Serial.begin(115200);

pinMode(5, OUTPUT); // set the LED pin mode

pinMode(15, OUTPUT);

pinMode(4, OUTPUT);

pinMode(2, OUTPUT);

pinMode(18, OUTPUT);

myservo.attach(servo);

myservo.write(180);

delay(10);
// We start by connecting to a WiFi network

Serial.println();

Serial.println();

Serial.print("Connecting to ");

Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

Serial.println("");

Serial.println("WiFi connected.");

Serial.println("IP address: ");

Serial.println(WiFi.localIP());

server.begin();

int value = 0;

void loop(){

Motor1.Speed(200); // A velocidade do motor pode variar de 0 a 255, onde 255 é a velocidade


máxima.

Motor2.Speed(200);

WiFiClient client = server.available(); // listen for incoming clients


if (client) { // if you get a client,

Serial.println("New Client."); // print a message out 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

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) {

// 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("HTTP/1.1 200 OK");

client.println("Content-type:text/html");

client.println();

client.print("<html>");

client.print("<head><meta content=\"width=device-width, initial-scale=1\";


charset=\"UTF-8\">");

client.print("<style> html { margin: 0px auto; text-align: center;}");

client.print(".Vaga1 { background: #52FF00; border: 3px solid #000000;border-radius:


20px; width: 162px;height: 85px;left: 155px;top: 346px;}");

client.print(".Vaga1off { background: red; border: 3px solid #000000;border-radius:


20px; width: 162px;height: 85px;left: 165px;top: 346px;}");

client.print(".Vaga2 { background: #52FF00; border: 3px solid #000000;border-radius:


20px; width: 162px;height: 85px;left: 155px;top: 459px;}");

client.print(".Vaga2off { background: red; border: 3px solid #000000;border-radius:


20px; width: 162px;height: 85px;left: 165px;top: 459px;}");

client.print(".Vaga3 { background: #52FF00; border: 3px solid #000000;border-radius:


20px; width: 162px;height: 85px;left: 155px;top: 571px;}");

client.print(".Vaga3off { background: red; border: 3px solid #000000;border-radius:


20px; width: 162px;height: 85px;left: 165px;top: 571px;}");
client.print(".Vaga4 { background: #52FF00; border: 3px solid #000000;border-radius:
20px; width: 162px;height: 85px;left: 155px;top: 684px;}");

client.print(".Vaga4off { background: red; border: 3px solid #000000;border-radius:


20px; width: 162px;height: 85px;left: 165px;top: 684px;}");

client.print(".Vaga5 { background: #52FF00; border: 3px solid #000000;border-radius:


20px; width: 162px;height: 85px;left: 155px;top: 797px;}");

client.print(".Vaga5off { background: red; border: 3px solid #000000;border-radius:


20px; width: 162px;height: 85px;left: 165px;top: 797px;}");

client.print(".RodaLiga { background: #52FF00; border: 3px solid #000000;border-radius:


20px; width: 162px;height: 85px;left: 155px;top: 910px;}");

client.print(".Rodaoff { background: red; border: 3px solid #000000;border-radius: 20px;


width: 162px;height: 85px;left: 165px;top: 910px;}");

client.print(".ChapeuLiga { background: #52FF00; border: 3px solid #000000;border-


radius: 20px; width: 162px;height: 85px;left: 155px;top: 1023px;}");

client.print(".Chapeuoff { background: red; border: 3px solid #000000;border-radius:


20px; width: 162px;height: 85px;left: 165px;top: 1023px;}</style>");

client.print("<body>");

// the content of the HTTP response follows the header:

client.print("<h3>A1</h3>");

client.print("<a href=\"/a1/H\"><button class=\"Vaga1\">Selecionar</button></a>");

client.print("<a href=\"/a1/L\"><button class=\"Vaga1off\">Liberar</button></a>");

client.print("<h3>A2</h3>");

client.print("<a href=\"/a2/H\"><button class=\"Vaga2\">Selecionar</button></a>");

client.print("<a href=\"/a2/L\"><button class=\"Vaga2off\">Liberar</button></a>");

client.print("<h3>A3</h3>");

client.print("<a href=\"/a3/H\"><button class=\"Vaga3\">Selecionar</button></a>");

client.print("<a href=\"/a3/L\"><button class=\"Vaga3off\">Liberar</button></a>");

client.print("<h3>A4</h3>");

client.print("<a href=\"/a4/H\"><button class=\"Vaga4\">Selecionar</button></a>");

client.print("<a href=\"/a4/L\"><button class=\"Vaga4off\">Liberar</button></a>");

client.print("<h3>A5</h3>");

client.print("<a href=\"/a5/H\"><button class=\"Vaga5\">Selecionar</button></a>");

client.print("<a href=\"/a5/L\"><button class=\"Vaga5off\">Liberar</button></a>");

client.print("<h3>Roda Gigante</h3>");
client.print("<a href=\"/rg/H\"><button class=\"RodaLiga\">Ligar</button></a>");

client.print("<a href=\"/rg/L\"><button class=\"Rodaoff\">Desligar</button></a>");

client.print("<h3>Chapéu Mexicano</h3>");

client.print("<a href=\"/cm/H\"><button class=\"ChapeuLiga\">Ligar</button></a>");

client.print("<a href=\"/cm/L\"><button class=\"Chapeuoff\">Desligar</button></a>");

client.print("</body>");

// 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

// Check to see if the client request was "GET /H" or "GET /L":

if (currentLine.endsWith("GET /a1/H")) {

digitalWrite(15, HIGH); // GET /H turns the LED on

myservo.write(10);

delay(10000);

myservo.write(180);

if (currentLine.endsWith("GET /a1/L")) {

digitalWrite(15, LOW); // GET /L turns the LED off

myservo.write(10);

delay(10000);

myservo.write(180);

if (currentLine.endsWith("GET /a2/H")) {
digitalWrite(2, HIGH); // GET /H turns the LED on

myservo.write(10);

delay(10000);

myservo.write(180);

if (currentLine.endsWith("GET /a2/L")) {

digitalWrite(2, LOW); // GET /L turns the LED off

myservo.write(10);

delay(10000);

myservo.write(180);

if (currentLine.endsWith("GET /a3/H")) {

digitalWrite(4, HIGH); // GET /H turns the LED on

myservo.write(10);

delay(10000);

myservo.write(180);

if (currentLine.endsWith("GET /a3/L")) {

digitalWrite(4, LOW); // GET /L turns the LED off

myservo.write(10);

delay(10000);

myservo.write(180);

if (currentLine.endsWith("GET /a4/H")) {

digitalWrite(5, HIGH); // GET /H turns the LED on

myservo.write(10);

delay(10000);

myservo.write(180);

if (currentLine.endsWith("GET /a4/L")) {

digitalWrite(5, LOW); // GET /L turns the LED off


myservo.write(10);

delay(10000);

myservo.write(180);

if (currentLine.endsWith("GET /a5/H")) {

digitalWrite(18, HIGH); // GET /H turns the LED on

myservo.write(10);

delay(10000);

myservo.write(180);

if (currentLine.endsWith("GET /a5/L")) {

digitalWrite(18, LOW); // GET /L turns the LED off

myservo.write(10);

delay(10000);

myservo.write(180);

if (currentLine.endsWith("GET /rg/H")) {

Motor1.Forward(); // GET /L turns the LED off

if (currentLine.endsWith("GET /rg/L")) {

Motor1.Stop(); // GET /L turns the LED off

if (currentLine.endsWith("GET /cm/H")) {

Motor2.Forward(); // GET /L turns the LED off

if (currentLine.endsWith("GET /cm/L")) {

Motor2.Stop(); // GET /L turns the LED off

// close the connection:


client.stop();

Serial.println("Client Disconnected.");

Link com o vídeo:


https://drive.google.com/drive/folders/1pgfMNyL6EX-
e9MRHYU7WEq7mRYDhF5jr?usp=sharing

Potrebbero piacerti anche