MQTT + UDP code Arduino ID ESP32


#1

image

Guide to install software necessary to program with arduino ID the esp32

Code

                #include <WiFi.h>
				#include <PubSubClient.h>
				#include <WiFiUdp.h>
				 
				const char* ssid = "yourSSID";
				const char* password =  "yourPASSWORD";
				const char* mqttServer = "broker.mqtt-dashboard.com";
				const int mqttPort = 1883;
				const char* mqttUser = "";
				const char* mqttPassword = "";

				WiFiUDP Udp;
				unsigned int localUdpPort = 12345;  // local port to listen on
				char incomingPacket[255];  // buffer for incoming packets
				char  replyPacekt[] = "Hi there! Got the message :-)";  // a reply string to send back

				 
				WiFiClient espClient;
				PubSubClient client(espClient);

				void callback(char* topic, byte* payload, unsigned int length) {
				  Serial.print("Message arrived [");
				  Serial.print(topic);
				  Serial.print("] ");
				  String stringTwo = "";
				  for (int i=0;i<length;i++) {
					Serial.print((char)payload[i]);
					stringTwo.concat((char)payload[i]); 
				  }
				if(stringTwo == "HIGH"){

				  digitalWrite(2, HIGH); 
				  }
				  if(stringTwo== "LOW"){

				  digitalWrite(2, LOW); 
				  }
				  Serial.print(stringTwo);
				  Serial.println();
				}

				 
				void setup() {
				 
				  Serial.begin(115200);
				  WiFi.begin(ssid, password);
				 
				  while (WiFi.status() != WL_CONNECTED) {
					delay(500);
					Serial.println("Connecting to WiFi..");
				  }
				 
				  Serial.println("Connected to the WiFi network");
				 
				  client.setServer(mqttServer, mqttPort);
				 
				  while (!client.connected()) {
					Serial.println("Connecting to MQTT...");
				 
					if (client.connect("ESP32Client", mqttUser, mqttPassword )) {
						   client.setCallback(callback);
							client.subscribe("test/esp32/andres");
					  Serial.println("connected");
					  pinMode(2, OUTPUT);
					} else {
				 
					  Serial.print("failed with state ");
					  Serial.print(client.state());
					  delay(2000);
				 
					}
				  }
				 
				  client.publish("esp/test", "Hello from ESP32");
				  Udp.begin(localUdpPort);
				  Serial.printf("Now listening at IP %s, UDP port %d\n", espClient.localIP().toString().c_str(), localUdpPort);
				 
				}
				 
				void loop() {
				  client.loop();

					   int packetSize = Udp.parsePacket();
						  if (packetSize) {
							  // receive incoming UDP packets
							  Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
							  int len = Udp.read(incomingPacket, 255);
							  if (len > 0){
							  incomingPacket[len] = 0;
							}
						   Serial.printf("UDP packet contents: %s\n", incomingPacket);
						   client.publish("esp/test/udp", incomingPacket);
						  }
				  
				}

UDP Extension

MQTT Extension

Saludos

Andres Cotes


#2

Hello there Andre.
Nice to learn about your MQTT library but it is not clear for me how to use it on Appybuilder.
I do have several ESP32 apps that I would like to integrate with my Android app.
Please give more information on how should I do it.
Thanks
Paulo