Comments (3)
After some efforts I was able to connect to my broker. Following is my new code:
* uMQTTBroker demo for Arduino (C++-style)
*
* The program defines a custom broker class with callbacks,
* starts it, subscribes locally to anything, and publishs a topic every second.
* Try to connect from a remote client and publish something - the console will show this as well.
*/
#define MQTTBROKER true
#include <ESP8266WiFi.h>
#include "uMQTTBroker.h"
/*
* Your WiFi config here
*/
char apssid[] = "ESP";
char ssid[] = "myssid"; // your network SSID (name)
char pass[] = "mypassword"; // your network password
//bool WiFiAP = false; // Do yo want the ESP as AP?
int device;
int voltage;
int temperature;
int humidity;
int pressure;
int light;
/*
Predefined sensor type table is below:
volatage = 16, temperature = 26, humidity= 36, pressure= 46, light= 56, OpenClose = 66,
level = 76, presence = 86, motion = 96 etc.
*/
int sensorType1;
int sensorType2;
int sensorType3;
int sensorType4;
int sensorType5;
//uint8_t probeData[] = {temperature, humidity, pressure, voltage, light, unit};
char topic1[50];
char topic2[50];
char topic3[50];
char topic4[50];
char topic5[50];
char topic6[50];
int command1;
int command2;
int command3;
int command4;
int command5;
int command6;
//uint8_t mac[6] = {19,01,25,30,01,01};
//uint8_t mac[6] = {19,01,250,value4,value5,01};
uint8_t mac[6] = {command1, command2, command3, command4, command5, command6};
// ==================== end of TUNEABLE PARAMETERS ====================
extern "C" void preinit() {
wifi_set_opmode(STATIONAP_MODE);
wifi_set_macaddr(SOFTAP_IF, mac);
}
/*
* Custom broker class with overwritten callback functions
*/
class myMQTTBroker: public uMQTTBroker
{
public:
virtual bool onConnect(IPAddress addr, uint16_t client_count) {
Serial.println(addr.toString()+" connected");
return true;
}
virtual bool onAuth(String username, String password) {
Serial.println("Username/Password: "+username+"/"+password);
return true;
}
virtual void onData(String topic, const char *data, uint32_t length) {
char data_str[length+1];
os_memcpy(data_str, data, length);
data_str[length] = '\0';
Serial.println("received topic '" + topic + (String)data_str);
}
};
myMQTTBroker myBroker;
WiFiEventHandler probeRequestPrintHandler;
/*
* WiFi init stuff
*/
void startWiFiClient()
{
Serial.println("Connecting to "+(String)ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: " + WiFi.localIP().toString());
}
void startWiFiAP()
{
WiFi.softAP("ESP", "<notused>", 7, 0, 0); //(gateway, "<notused>", 7, 1, 0) for hidden SSID.
Serial.println("AP started");
Serial.println("IP address: " + WiFi.softAPIP().toString());
}
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println();
startWiFiClient();
startWiFiAP();
probeRequestPrintHandler = WiFi.onSoftAPModeProbeRequestReceived(&onProbeRequest);
delay(1);
// wifi_set_macaddr(SOFTAP_IF, mac);
// Start the broker
Serial.println("Starting MQTT broker");
myBroker.init();
/*
* Subscribe to anything
*/
myBroker.subscribe("#");
}
void loop()
{
}
void mqttPublish() {
/*
Predefined sensor type table is below:
volatage = 16, temperature = 26, humidity= 36, pressure= 46, light= 56, OpenClose = 66,
level = 76, presence = 86, motion = 96 etc.
*/
sprintf(topic1,"%s%i%s%i%s", "Sensordata/", device, "/", device, "/");
sprintf(topic2,"%s%i%s%i%s", "Sensordata/", device, "/", sensorType1, "/");
sprintf(topic3,"%s%i%s%i%s", "Sensordata/", device, "/", sensorType2, "/");
sprintf(topic4,"%s%i%s%i%s", "Sensordata/", device, "/", sensorType3, "/");
sprintf(topic5,"%s%i%s%i%s", "Sensordata/", device, "/", sensorType4, "/");
sprintf(topic6,"%s%i%s%i%s", "Sensordata/", device, "/", sensorType5, "/");
// myBroker.publish(topic1, (String)device);
// myBroker.publish("SensorData/device/", (String)device);
myBroker.publish(topic2, (String)voltage);
myBroker.publish(topic3, (String)temperature);
myBroker.publish(topic4, (String)humidity);
myBroker.publish(topic5, (String)pressure);
myBroker.publish(topic6, (String)light);
// wait a second
delay(5000);
}
void onProbeRequest(const WiFiEventSoftAPModeProbeRequestReceived& dataReceived) {
sendCommand();
/*
for(int i = 0; i < 6; i++ )
{
Serial.println(dataReceived.mac[i]);
}
*/
if (dataReceived.mac[1] == 16) // only accept data from device with voltage as a sensor type at byte 1.
{
sensorType1 = (dataReceived.mac[1]);
sensorType2 = (dataReceived.mac[2]);
sensorType3 = (dataReceived.mac[3]);
sensorType4 = (dataReceived.mac[4]);
sensorType5 = (dataReceived.mac[5]);
}
if (dataReceived.mac[0] == 6 || dataReceived.mac[0] == 16 || dataReceived.mac[0] == 26 || dataReceived.mac[0] == 36 || dataReceived.mac[0] == 46 || dataReceived.mac[0] == 56 || dataReceived.mac[0] == 66 || dataReceived.mac[0] == 76 || dataReceived.mac[0] == 86 || dataReceived.mac[0] == 96 || dataReceived.mac[0] == 106 || dataReceived.mac[0] == 116 || dataReceived.mac[0] == 126 || dataReceived.mac[0] == 136 || dataReceived.mac[0] == 146 || dataReceived.mac[0] == 156 || dataReceived.mac[0] == 166 || dataReceived.mac[0] == 176 || dataReceived.mac[0] == 186 || dataReceived.mac[0] == 196 || dataReceived.mac[0] == 206 || dataReceived.mac[0] == 216 || dataReceived.mac[0] == 226 || dataReceived.mac[0] == 236 || dataReceived.mac[0] == 246) // only accept data from certain devices.
{
Serial.print("Probe Request:- ");
Serial.print(" Device ID: ");
Serial.print(dataReceived.mac[0], DEC);
device = dataReceived.mac[0];
Serial.print(" Voltage: ");
Serial.print(dataReceived.mac[1], DEC);
voltage = dataReceived.mac[1];
voltage = voltage * 2;
Serial.print(" Temperature: ");
Serial.print(dataReceived.mac[2], DEC);
temperature = dataReceived.mac[2];
Serial.print(" Humidity: ");
Serial.print(dataReceived.mac[3], DEC);
humidity = dataReceived.mac[3];
Serial.print(" Pressure: ");
Serial.print(dataReceived.mac[4], DEC);
pressure = dataReceived.mac[4];
pressure = pressure * 4;
Serial.print(" Light: ");
Serial.println(dataReceived.mac[5], DEC);
light = dataReceived.mac[5];
if (voltage < 295) // if voltage of battery gets to low, print the warning below.
{
#if MQTTBROKER
myBroker.publish("SensorData/warning", "Battery Low");
#endif
Serial.println("**************Warning :- Battery Voltage low please change batteries********************" );
Serial.println();
}
if (dataReceived.mac[1] > 115 && dataReceived.mac[1] < 180) {
#if MQTTBROKER
mqttPublish();
#endif
}
} else {
//Serial.println("Waiting for Data............");
}
}
void sendCommand() {
command1 = 10; //random(25);
command2 = 20; //random(256);
command3 = 30; //random(256);
command4 = 40; //random(256);
command5 = random(256);
command6 = 01;
mac[0] = command1;
mac[1] = command2;
mac[2] = command3;
mac[3] = command4;
mac[4] = command5;
mac[5] = command6;
wifi_set_macaddr(SOFTAP_IF, mac);
}````
Thanks
from umqttbroker.
Thanks for the Info. Can you give me a hint on what makes the difference?
from umqttbroker.
I spent few hours to solve this issue. To pin point where the problem was I used your example code plus added some code 1 step at a time to make it simple and I came to conclusion that:
I have to remove this line from startWiFiAP() function as I had set wifi mode to STA_AP earlier in the code :
WiFi.mode(WIFI_AP);
Then in Setup loop I used:
startWiFiClient();
startWiFiAP();
//startWiFiClient();
Instead of:
//startWiFiClient();
startWiFiAP();
startWiFiClient();
And it started working properly.
Code I used to resolve the issue:
`/*
- uMQTTBroker demo for Arduino (C++-style)
- The program defines a custom broker class with callbacks,
- starts it, subscribes locally to anything, and publishs a topic every second.
- Try to connect from a remote client and publish something - the console will show this as well.
*/
#include <ESP8266WiFi.h>
#include "uMQTTBroker.h"
/*
- Your WiFi config here
*/
char ssid[] = "ssid"; // your network SSID (name)
char pass[] = "password"; // your network password
//bool WiFiAP = false; // Do yo want the ESP as AP?
int command1; int command2; int command3; int command4; int command5; int command6;
uint8_t mac[6] = {command1, command2, command3, command4, command5, command6};
extern "C" void preinit() {
wifi_set_opmode(STATIONAP_MODE);
wifi_set_macaddr(SOFTAP_IF, mac);
}
/*
-
Custom broker class with overwritten callback functions
*/
class myMQTTBroker: public uMQTTBroker
{
public:
virtual bool onConnect(IPAddress addr, uint16_t client_count) {
Serial.println(addr.toString()+" connected");
return true;
}virtual bool onAuth(String username, String password) {
Serial.println("Username/Password: "+username+"/"+password);
return true;
}virtual void onData(String topic, const char *data, uint32_t length) {
char data_str[length+1];
os_memcpy(data_str, data, length);
data_str[length] = '\0';Serial.println("received topic '"+topic+"' with data '"+(String)data_str+"'");
}
};
myMQTTBroker myBroker;
/*
- WiFi init stuff
*/
void startWiFiClient()
{
Serial.println("Connecting to "+(String)ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: " + WiFi.localIP().toString());
}
void startWiFiAP()
{
// WiFi.mode(WIFI_AP);
WiFi.softAP("ESP", "", 7, 0, 0); //(gateway, "", 7, 1, 0) for hidden SSID.
Serial.println("AP started");
Serial.println("IP address: " + WiFi.softAPIP().toString());
}
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println();
//Start WiFi
startWiFiClient();
startWiFiAP();
//startWiFiClient();
// Start the broker
Serial.println("Starting MQTT broker");
myBroker.init();
/*
- Subscribe to anything
*/
myBroker.subscribe("#");
}
int counter = 0;
void loop()
{
/*
- Publish the counter value as String
*/
myBroker.publish("broker/counter", (String)counter++);
// wait a second
delay(1000);
}`
Thanks.
from umqttbroker.
Related Issues (20)
- disconnect and reconnect 6-10 time,the broker will cant't be connected HOT 1
- Client count HOT 8
- How to restart the broker?
- Ignored espconn_send() return (error) value.
- Broker Crashing when wifi disconnects HOT 2
- Accessing via mosquitto_sub/pub. How? HOT 1
- Broker doesn't accept new connections after 24h running? HOT 4
- Locally published topics do not appear to retain
- Compiling Error HOT 1
- ESP32 Port? HOT 1
- CRASH BROKER ((( WHY ?
- Ip address MQTT broker
- Compiling error using uMQTTBroker HOT 1
- Partial message / disconnections HOT 1
- Connection problem
- problem with using lwip:v2
- mqtt no user and pass HOT 1
- Broker cannot be connected after client disconnect
- Cannot send HTTPClient POST with MQTT
- Using ESP8266 as MQTT broker and datalogger HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from umqttbroker.