Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <PubSubClient.h>
- const char* sand = "false";
- // Replace the next variables with your SSID/Password combination
- const char* ssid = "Sde-Guest";
- const char* password = "";
- //MQTT USERNAME, PASSWORD AND TOPIC
- const char* mqtt_username = "admin";
- const char* mqtt_password = "ciscogang123";
- const char* clientID = "Alarm";
- // Add your MQTT Broker IP address, example:
- const char* mqtt_server = "10.142.110.108";
- WiFiClient espClient;
- PubSubClient client(espClient);
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.println("Attempting MQTT connection...");
- // Attempt to connect
- if (client.connect(clientID, mqtt_username, mqtt_password)) {
- Serial.println("Connected");
- sand = "true";
- // ... and subscribe to topic
- client.subscribe("test");
- } 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() {
- // put your setup code here, to run once:
- Serial.begin(115200);
- setup_wifi();
- client.setServer(mqtt_server, 1883);
- }
- void setup_wifi() {
- delay(1000);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- delay (5000);
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- client.subscribe("test");
- }
- void loop() {
- // put your main code here, to run repeatedly:
- if (sand == "true"){
- client.publish("test", "Hello from ESP");
- }
- client.publish("test", "Hello from ESP");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement