/*********
* Adaptação: Marcos Chaves outubro/2022
Fontes:
Complete project details at http://randomnerdtutorials.com
https://www.arduino.cc/reference/en/libraries/wifi/
*********/
// Carregar biblioteca Wifi
#include <WiFi.h>
#include <PubSubClient.h>
#include <iostream>
#include <sstream>
String frase1;
const char *ssid = "WI-FI Xuxa";
const char *password = "xuxa2022";
const char *mqtt_broker = "broker.hivemq.com";
const char *topic = "IFSP/bodavolis";
const char *topic2;
const char *mqtt_username = "mchavesferreira";
const char *mqtt_password = "Embar2022";
const int mqtt_port = 1883;
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
// configurar porta para webserver 80
WiFiServer server(80);
// Variavle de armazenamento HTTP request
String header;
// variavel auxiliar para armzanenr o estado das saidas
String output26State = "off";
String temp = "0";
String output27State = "off";
// varivel para pinos GPIO
const int output26 = 26;
const int output27 = 27;
int number1;
int number2;
int number3;
int number4;
int tempo = 1;
int estado = 0;
String valor1;
String valor2;
String valor3;
//char inicio = "IFSP/";
String saida;
// Tempo atual
unsigned long currentTime = millis();
// Proximo tempo
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
void setup() {
// inicia porta comunicação serial
Serial.begin(115200); // velocidade 115200 bauds/segundo
// Configura os pinos GPIO
pinMode(output26, OUTPUT);
pinMode(output27, OUTPUT);
// configura saida em Baixo
digitalWrite(output26, LOW);
digitalWrite(output27, LOW);
// Conecta a rede wifi, SSID e password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
// aguarda conexao wifi
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Imprime na serial endereço da IP e web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); /// ip do esp conectado a sua rede
Serial.print("ESP Board MAC Address: ");
Serial.println(WiFi.macAddress());
Serial.print("RRSI: ");
Serial.println(WiFi.RSSI());
server.begin();
mqttClient.setServer(mqtt_broker, mqtt_port);
mqttClient.setCallback(callback);
while (!mqttClient.connected()) {
String client_id = String(WiFi.macAddress());
if (mqttClient.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
Serial.println("Connected to Public MQTT Broker");
} else {
Serial.print("Failed to connect with MQTT Broker");
Serial.print(mqttClient.state());
delay(2000);
}
}
}
void loop(){
// Cria um cliente que pode se conectar a um endereço IP
WiFiClient client = server.available(); // Listen for incoming clients
mqttClient.loop();
if (client) { // If a new client connects,
currentTime = millis();
previousTime = currentTime;
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() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
currentTime = millis();
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) {
// 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("Connection: close");
client.println();
LoopRandom();
// turns the GPIOs on and off
if (header.indexOf("GET /26/on") >= 0) {
Serial.println("GPIO 26 on");
mqttClient.subscribe(topic);
mqttClient.subscribe(topic2);
mqttClient.publish(topic, frase1.c_str());
delay(1000);
mqttClient.publish(topic2, "on");
output26State = "Ligado";
digitalWrite(output26, HIGH);
} else if (header.indexOf("GET /26/off") >= 0) {
Serial.println("GPIO 26 off");
output26State = "Desligado";
mqttClient.subscribe(topic);
mqttClient.subscribe(topic2);
mqttClient.publish(topic, frase1.c_str());
delay(1000);
mqttClient.publish(topic2, "off");
digitalWrite(output26, LOW);
} else if (header.indexOf("GET /27/on") >= 0) {
Serial.println("GPIO 27 on");
output27State = "on";
digitalWrite(output27, HIGH);
} else if (header.indexOf("GET /27/off") >= 0) {
Serial.println("GPIO 27 off");
output27State = "off";
digitalWrite(output27, LOW);
}
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #555555; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #4CAF50;}</style></head>");
// Web Page Heading
//client.println("<body><h1>Micro-Ondas Bodavolis</h1>");
//client.println("<label for=\"temperatura\">Temperatura </label>0 <a href=\"/temp\"><input type=\"text\" id=\"/temp\" name=\"temperatura\"></a\>250<br>");
/*if (header.indexOf("/temp") >= 0 ){
temp = header.indexOf("/temp");
Serial.println(temp);
}*/
// Display current state, and ON/OFF buttons for GPIO 26
client.println("<p>Ligar/Desligar - Estado " + output26State + "</p>");
// If the output26State is off, it displays the ON button
if (output26State=="Desligado") {
client.println("<p><a href=\"/26/on\"><button class=\"button\">OFF</button></a></p>");
} else {
client.println("<p><a href=\"/26/off\"><button class=\"button button2\">ON</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 27
/*client.println("<p>GPIO 27 - State " + output27State + "</p>");
// If the output27State is off, it displays the ON button
if (output27State=="off") {
client.println("<p><a href=\"/27/on\"><button class=\"button\">OFF</button></a></p>");
} else {
client.println("<p><a href=\"/27/off\"><button class=\"button button2\">ON</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("");
frase1 = "";
tempo = 1;
}
}
void callback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char) payload[i]);
}
Serial.println();
Serial.println("************");
}
void LoopRandom(){
while(tempo <= 25){
number1 = random(1,26);
number2 = random(1,26);
number3 = random(1,4);
number4 = random(5);
if (number1 == 1) {valor1 = "a";}
if (number1 == 2) {valor1 = "b";}
if (number1 == 3) {valor1 = "c";}
if (number1 == 4) {valor1 = "d";}
if (number1 == 5) {valor1 = "e";}
if (number1 == 6) {valor1 = "f";}
if (number1 == 7) {valor1 = "g";}
if (number1 == 8) {valor1 = "h";}
if (number1 == 9) {valor1 = "i";}
if (number1 == 10) {valor1 = "j";}
if (number1 == 11) {valor1 = "k";}
if (number1 == 12) {valor1 = "l";}
if (number1 == 13) {valor1 = "m";}
if (number1 == 14) {valor1 = "n";}
if (number1 == 15) {valor1 = "o";}
if (number1 == 16) {valor1 = "p";}
if (number1 == 17) {valor1 = "q";}
if (number1 == 18) {valor1 = "r";}
if (number1 == 19) {valor1 = "s";}
if (number1 == 20) {valor1 = "t";}
if (number1 == 21) {valor1 = "u";}
if (number1 == 22) {valor1 = "v";}
if (number1 == 23) {valor1 = "w";}
if (number1 == 24) {valor1 = "x";}
if (number1 == 25) {valor1 = "y";}
if (number1 == 26) {valor1 = "z";}
if (number2 == 1) {valor2 = "A";}
if (number2 == 2) {valor2 = "B";}
if (number2 == 3) {valor2 = "C";}
if (number2 == 4) {valor2 = "D";}
if (number2 == 5) {valor2 = "E";}
if (number2 == 6) {valor2 = "F";}
if (number2 == 7) {valor2 = "G";}
if (number2 == 8) {valor2 = "H";}
if (number2 == 9) {valor2 = "I";}
if (number2 == 10) {valor2 = "J";}
if (number2 == 11) {valor2 = "K";}
if (number2 == 12) {valor2 = "L";}
if (number2 == 13) {valor2 = "M";}
if (number2 == 14) {valor2 = "N";}
if (number2 == 15) {valor2 = "O";}
if (number2 == 16) {valor2 = "P";}
if (number2 == 17) {valor2 = "Q";}
if (number2 == 18) {valor2 = "R";}
if (number2 == 19) {valor2 = "S";}
if (number2 == 20) {valor2 = "T";}
if (number2 == 21) {valor2 = "U";}
if (number2 == 22) {valor2 = "V";}
if (number2 == 23) {valor2 = "W";}
if (number2 == 24) {valor2 = "X";}
if (number2 == 25) {valor2 = "Y";}
if (number2 == 26) {valor2 = "Z";}
if (number3 == 1) {valor3 = "!";}
if (number3 == 2) {valor3 = "@";}
if (number3 == 3) {valor3 = "%";}
if (number3 == 4) {valor3 = "#";}
if (number4 == 0) {frase1 = frase1 + valor1 + valor2 + valor3;}
if (number4 == 1) {frase1 = frase1 + valor1 + valor3 + valor2;}
if (number4 == 2) {frase1 = frase1 + valor2 + valor1 + valor3;}
if (number4 == 3) {frase1 = frase1 + valor2 + valor3 + valor1;}
if (number4 == 4) {frase1 = frase1 + valor3 + valor1 + valor2;}
if (number4 == 5) {frase1 = frase1 + valor3 + valor2 + valor1;}
tempo++;
}
frase1 = "IFSP/" + frase1;
topic2 = frase1.c_str();
}