You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
3.3 KiB
134 lines
3.3 KiB
// MQTT server + client for ESP32
|
|
#include"sMQTTBroker.h"
|
|
#include <WiFi.h>
|
|
#include <PubSubClient.h>
|
|
#include <WiFiClientSecure.h>
|
|
#include <MQTT.h>
|
|
|
|
const char* ssid = "NOS-3B26"; // Setubal
|
|
const char* password = "RMKSX2GL"; // Setubal
|
|
//const char* ssid = "MEO-AA9030"; // Andre
|
|
//const char* password = "81070ce635"; // Andre
|
|
|
|
// Set your Static IP address
|
|
IPAddress local_IP(192, 168, 1, 123);
|
|
// Set your Gateway IP address
|
|
IPAddress gateway(192, 168, 1, 1);
|
|
|
|
IPAddress subnet(255, 255, 0, 0);
|
|
IPAddress primaryDNS(8, 8, 8, 8); // optional
|
|
IPAddress secondaryDNS(8, 8, 4, 4); // optional
|
|
|
|
sMQTTBroker broker;
|
|
|
|
WiFiClient net;
|
|
//WiFiClientSecure net;
|
|
MQTTClient mqtt_client(1024);
|
|
|
|
//WiFiClient espClient;
|
|
//PubSubClient client(espClient);
|
|
//long lastMsg = 0;
|
|
//char msg[50];
|
|
//int value = 0;
|
|
|
|
unsigned long lastMillis = 0;
|
|
|
|
//////////////////////////////////
|
|
void WiFiconnect(){
|
|
int nn = 0;
|
|
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
|
|
delay(500);
|
|
if(nn<9){
|
|
Serial.print(".");
|
|
nn++;
|
|
}else{
|
|
nn=0;
|
|
Serial.println(".");
|
|
}
|
|
}
|
|
|
|
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
|
|
Serial.println("STA Failed to configure");
|
|
}
|
|
|
|
Serial.println("Connection established!");
|
|
Serial.print("IP address:\t");
|
|
Serial.println(WiFi.localIP());
|
|
|
|
|
|
|
|
// Serial.print("\nconnecting...");
|
|
//// while (!mqtt_client.connect("bsidebotham", "", "")) {
|
|
// while (!mqtt_client.connect("tezmqlientx2323", "", "")) {
|
|
// Serial.print(".");
|
|
// delay(1000);
|
|
// }
|
|
//
|
|
// Serial.println("\nconnected!");
|
|
//
|
|
// mqtt_client.subscribe("/hello");
|
|
//
|
|
// // MQTT brokers usually use port 8883 for secure connections.
|
|
//// client.begin("broker.shiftr.io", 8883, net);
|
|
// mqtt_client.begin("192.168.1.123", 1883, net);
|
|
// mqtt_client.onMessage(messageReceived);
|
|
|
|
}
|
|
|
|
//////////////////////////////
|
|
void messageReceived(String &topic, String &payload) {
|
|
Serial.println("incoming: " + topic + " - " + payload);
|
|
|
|
// Note: Do not use the client in the callback to publish, subscribe or
|
|
// unsubscribe as it may cause deadlocks when other things arrive while
|
|
// sending and receiving acknowledgments. Instead, change a global variable,
|
|
// or push to a queue and handle it in the loop after calling `client.loop()`.
|
|
}
|
|
|
|
//////////////////////////////
|
|
void setup()
|
|
{
|
|
Serial.begin(115200);
|
|
// const char* ssid = "MEO-AA9030"; // The SSID (name) of the Wi-Fi network you want to connect to
|
|
// const char* password = "81070ce635"; // The password of the Wi-Fi network
|
|
|
|
Serial.print("Connecting to ");
|
|
Serial.println(ssid);
|
|
WiFi.begin(ssid, password);
|
|
|
|
const unsigned short mqttPort=1883;
|
|
broker.init(mqttPort);
|
|
// all done
|
|
|
|
|
|
WiFiconnect();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
//////////////////////////////
|
|
void loop()
|
|
{
|
|
broker.update();
|
|
|
|
// mqtt_client.loop();
|
|
|
|
delay(10); // <- fixes some issues with WiFi stability
|
|
|
|
// publish a message roughly every second.
|
|
// if (millis() - lastMillis > 1000) {
|
|
// if (!mqtt_client.connected()) {
|
|
// Serial.print("lastError: ");
|
|
// Serial.println(mqtt_client.lastError());
|
|
// WiFiconnect();
|
|
// }
|
|
// lastMillis = millis();
|
|
// int rnum = random(1,23);
|
|
// mqtt_client.publish("/hello", "ciccio" + String(rnum));
|
|
// }
|
|
//
|
|
}
|