PD: I ignored the part of the temperature thing, is not on the code.
Code Here:
Code: Select all
#include <WiFi.h>
#include <PubSubClient.h>
/* change it with your ssid-password */
const char* ssid = "CLAROQNK37";
const char* password = "9j98g4NuEaq496Yk";
/* this is the IP of PC/raspberry where you installed MQTT Server
on Wins use "ipconfig"
on Linux use "ifconfig" to get its IP address */
const char* mqtt_server = "10.0.0.5";
/* create an instance of PubSubClient client */
WiFiClient espClient;
PubSubClient client(espClient);
/*LED GPIO pin*/
const char led = 4;
/* topics */
#define LED_TOPIC "Bulb/Room1"/*Bulb/Room1*/ /* 1=on, 0=off */
long lastMsg = 0;
char msg[20];
void receivedCallback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message received: ");
Serial.println(topic);
Serial.print("payload: ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
/* we got '1' -> on */
if ((char)payload[0] == '1') {
digitalWrite(led, HIGH);
} else {
/* we got '0' -> on */
digitalWrite(led, LOW);
}
}
void mqttconnect() {
/* Loop until reconnected */
while (!client.connected()) {
Serial.print("MQTT connecting ...");
/* client ID */
String clientId = "ESP32Client";
/* connect now */
if (client.connect(clientId.c_str())) {
Serial.println("connected");
/* subscribe topic with default QoS 0*/
client.subscribe(LED_TOPIC);
} else {
Serial.print("failed, status code =");
Serial.print(client.state());
Serial.println("try again in 5 seconds");
/* Wait 5 seconds before retrying */
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
/* set led as output to control led on-off */
pinMode(led, OUTPUT);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
/* configure the MQTT server with IPaddress and port */
client.setServer(mqtt_server, 1883);
/* this receivedCallback function will be invoked
when client received subscribed topic */
client.setCallback(receivedCallback);
}
void loop() {
/* if client was disconnected then try to reconnect again */
if (!client.connected()) {
mqttconnect();
}
/* this function will listen for incomming
subscribed topic-process-invoke receivedCallback */
client.loop();
}