//#include <ESP8266WiFi.h>
#include <WiFi.h>
#include <PubSubClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "test.mosquitto.org";
Adafruit_NeoPixel pixels(16, 32, NEO_GRB + NEO_KHZ800);
WiFiClient WOKWI_client;
PubSubClient client(WOKWI_client);
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
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());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("WOKWI_client")) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
pinMode(2, OUTPUT);
pinMode(15, OUTPUT);
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
//client.setCallback(callback);
}
void Conectado_Wifi(){
if (WiFi.status())
digitalWrite(2, HIGH);
else
digitalWrite(2, LOW);
}
void Conectado_Mosquito(){
if (client.connected())
digitalWrite(15, HIGH);
else
digitalWrite(15, LOW);
}
void Publica_Dados(){
int Valor_Pot;
Valor_Pot = analogRead(34);
client.publish("TOPICO_WOKWI_hivqs79",String(Valor_Pot).c_str());
Serial.print("Data send: ");
Serial.println(Valor_Pot);
delay(1000);
}
void loop() {
reconnect();
Conectado_Wifi();
Conectado_Mosquito();
Publica_Dados();
}
// void loop() {
// pixels.clear(); // Set all pixel colors to 'off'
// // The first NeoPixel in a strand is #0, second is 1, all the way up
// // to the count of pixels minus one.
// for(int i=0; i<16; i++) { // For each pixel...
// int value = analogRead(pot);
// int DELAYVAL = map(value, 0, 4095, 500, 0);
// Serial.println(DELAYVAL);
// // pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
// // Here we're using a moderately bright green color:
// pixels.setPixelColor(i, pixels.Color(150, 0, 0));
// pixels.show(); // Send the updated pixel colors to the hardware.
// delay(DELAYVAL);
// }
// }