- [code]
- #include <HardwareSerial.h>
- #include <PubSubClient.h> //Connect and publish data to the MQTT broker
- #include <WiFi.h> //Enables Esp32 to connect to the local network(via WiFi)
- #define RXD2 16
- #define TXD2 17
- //char myRcvChar;
- //WiFi
- const char* ssid = "Bilal-Asad-310";
- const char* wifi_password = "31019309";
- //MQTT
- const char* mqtt_server = "192.168.0.106";
- const char* message_topic = "test/esp32/message";
- //const char* bending_topic = "exoskeleton/walking/bending";
- //const char* streching_topic = "exoskeleton/walking/streching";
- const char* mqtt_username = "pi"; //MQTT username
- const char* mqtt_password = "raspberry"; //MQTT password
- const char* clientID = "first message";
- //Initialise the Wifi and MQTT Client objects
- WiFiClient wifiClient;
- //1883 is the listener port for the broker
- PubSubClient client(mqtt_server,1883,wifiClient);
- //Custom function to connect to the MQTT broker via WiFi
- void connect_MQTT(){
- Serial.print("Connecting to ");
- Serial.println(ssid);
- //Connect to the WiFi
- WiFi.begin(ssid, wifi_password);
- //wait until the connection has been confirmed before continuing
- while(WiFi.status()!= WL_CONNECTED){
- delay(500);
- Serial.print(".");
- }
- //Debugging-Output the IP Address of the ESP32
- Serial.println("WiFi connected");
- Serial.println("IP Address");
- Serial.println(WiFi.localIP());
- //Connect to MQTT Broker
- //client.connect returns a boolean value to let us know if the connection was successful
- //if the connection is failing, make sure you are using the correct MQTT Username and Password
- if(client.connect(clientID,mqtt_username,mqtt_password)){
- Serial.println("Connected to MQTT Broker");
- }
- else{
- Serial.println("Connection to MQTT Broker failed...");
- }
- }
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(115200);
- Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
- //char myRcvchar;
- }
- void loop() {
- // put your main code here, to run repeatedly:
- connect_MQTT();
- Serial.setTimeout(2000);
- Serial.println("Data Received: ");
- Serial.println(Serial2.readStringUntil('\n'));
- const char* message = "Hello";
- Serial.print("Message Received: ");
- Serial.print(message);
- //MQTT can only transmit strings
- String ms = "Mess: "+String((char*)message);
- //Publish to MQTT broker
- if(client.publish(message_topic, String(message).c_str())){
- Serial.println("Message sent");
- }
- //Again client.publish will return boolean value depending on whether it succeded or not.
- //if the message failed to send, try again, as the connection might have broken
- else{
- Serial.println("Message failed to send. Reconnecting to MQTT Broker and trying again");
- client.connect(clientID,mqtt_username,mqtt_password);
- delay(10); //This delay ensures that client.publish does not clash with client.connect call
- client.publish(message_topic,String(message).c_str());
- }
- client.disconnect(); //disconnect from the MQTT broker
- delay(1000*60); //print new values every 2 minutes
- }
- [/code]
Esp32 and MQTT (Raspberry pi) connection issue
Esp32 and MQTT (Raspberry pi) connection issue
Hello, I am trying to connect Esp32 to MQTT broker(Raspberry Pi) via Wi-Fi but I am facing issues. I am trying to send "Hello" from Esp32 to MQTT. Upon checking, Esp32 IP Address is: "192.168.0.105", Raspberry Pi IP: "192.169.0.106" and Wi-Fi IP is: "192.168.0.1". I don't know if this is the issue or something else. I am also posting the code below, kindly tell me what am I doing wrong or how can I solve this issue? Thank you.
Who is online
Users browsing this forum: Baidu [Spider], Bing [Bot], weoiss and 151 guests