Closed ghost closed 6 years ago
Port 1883 is usally a port for the MQTT protocol via TCP/IP. Since you want to use MQTT in a browser, this isn't possible and websockets are the only solution for this. The common brokers I know like hummingbird
or mosquitto
support websockets, but I don't know if the ESP8266 does.
Besides this, there are plenty reasons against using an ESP8266 as a broker. I would suggest to use it as a client in a setup like this instead:
I want to use ESP8266 as broker because it's cheap and I only send very simple datas, memory should not be an issue
I changed the programs of my ESP8266 Broker and client to use port 9001. It work fine. So I should be able to have a broker working on port 9001 and clients (phone and ESP8266) subscribing to it.
I kept the MQTT_SERVICE_OPTIONS as below.
export const MQTT_SERVICE_OPTIONS: IMqttServiceOptions = { hostname: '192.168.4.1', port: 1883, path: '/mqtt' };
But I still have a problem that should come from those lines :
this.sub = this._mqttService.observe('/Server/led').subscribe((message: IMqttMessage) => { this.msg= message.payload.toString(); });
I think this function does not execute or I'm not able to receive datas from the topic
If it can help, here is the full code of the page :
import { Component } from '@angular/core';
import { NavController,NavParams } from 'ionic-angular';
import {IMqttMessage, MqttModule, MqttService, IMqttServiceOptions} from 'ngx-mqtt';
import {Observable} from 'rxjs/Observable';
@Component({
selector: 'page-mqtt',
templateUrl: 'mqtt.html'
})
export class MqttPage {
msg:string;
sub:any;
constructor(public navCtrl: NavController, public navParams: NavParams, private _mqttService: MqttService) {
this.msg= "constructor...";
this.sub = this._mqttService.observe('/Server/led').subscribe((message: IMqttMessage) => {
this.msg= message.payload.toString();
});
}
public unsafePublish(topic: string, message: string): void {
this._mqttService.unsafePublish(topic, message, {qos: 1, retain: true});
}
public ngOnDestroy() {
this.sub.unsubscribe();
}
}```
And I didn't precise but I'm using Ionic 3.20, and as editor Sublime Text 3 (don't know if it's helpful)
And you're sure your ESP broker really works with webockets? Ports are just numbers, be sure it's the right protocol. What error do you exactly get?
I search a bit and ESP broker seems to work with websockets
I don't get any error, just nothing happen.
Please post the code of the ESP and the full frontend code (e.g. on https://stackblitz.com). Based on this information I can't even guess what you misconfiguerd.
I might know where the problem is comming from. I think the library I'm using doesn't use webscockets so the message is not understood by my phone. This is the library used by my ESPs https://github.com/martin-ger/uMQTTBroker
ESP Broker
/*
* uMQTTBroker demo for Arduino
*
* The program starts a broker, subscribes 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[] = "pass"; // your network password
char ip[] = "192.168.4.1";
unsigned int mqttPort = 9001; // the standard MQTT broker port
unsigned int max_subscriptions = 10;
unsigned int max_retained_topics = 10;
void data_callback(uint32_t *client /* we can ignore this */, const char* topic, uint32_t topic_len, const char *data, uint32_t lengh) {
char topic_str[topic_len+1];
os_memcpy(topic_str, topic, topic_len);
topic_str[topic_len] = '\0';
char data_str[lengh+1];
os_memcpy(data_str, data, lengh);
data_str[lengh] = '\0';
Serial.print("received topic '");
Serial.print(topic_str);
Serial.print("' with data '");
Serial.print(data_str);
Serial.println("'");
}
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println();
// We start by connecting to a WiFi network
IPAddress selfIP(192,168,4,1);
IPAddress gateway(192,168,4,0);
IPAddress subnet(255,255,255,0);
WiFi.config(selfIP, gateway, subnet);
Serial.print("Creating : ");
Serial.println(ssid);
WiFi.softAP(ssid, pass);
Serial.println("WiFi ready");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
/*
* Register the callback
*/
MQTT_server_onData(data_callback);
/*
* Start the broker
*/
Serial.println("Starting MQTT broker");
MQTT_server_start(mqttPort, max_subscriptions, max_retained_topics);
/*
* Subscribe to anything
*/
MQTT_local_subscribe((unsigned char *)"#", 0);
}
int counter = 0;
void loop()
{
String myData(random(2));
/*
* Publish the counter value as String
*/
MQTT_local_publish((unsigned char *)"/Server/led", (unsigned char *)myData.c_str(), myData.length(), 0, 0);
// wait a second
delay(1000);
}
`
ESP Client
`#include <ESP8266WiFi.h>
#include <MQTT.h>
void myDataCb(String& topic, String& data);
void myPublishedCb();
void myDisconnectedCb();
void myConnectedCb();
#define CLIENT_ID "client3"
#define TOPIC "/Server/led"
// create MQTT
MQTT myMqtt(CLIENT_ID, "192.168.4.1", 9001);
const char* ssid = "ssid";
const char* password = "pass";
//
void setup() {
Serial.begin(115200);
delay(1000);
pinMode(2,OUTPUT);
digitalWrite(2,HIGH);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Connecting to MQTT server");
// setup callbacks
myMqtt.onConnected(myConnectedCb);
myMqtt.onDisconnected(myDisconnectedCb);
myMqtt.onPublished(myPublishedCb);
myMqtt.onData(myDataCb);
Serial.println("connect mqtt...");
myMqtt.connect();
Serial.println("subscribe to topic...");
myMqtt.subscribe(TOPIC);
delay(10);
}
//
void loop() {
}
/*
*
*/
void myConnectedCb()
{
Serial.println("connected to MQTT server");
}
void myDisconnectedCb()
{
Serial.println("disconnected. try to reconnect...");
delay(500);
myMqtt.connect();
}
void myPublishedCb()
{
//Serial.println("published.");
}
void myDataCb(String& topic, String& data)
{
if(topic=="/Server/led"){
if(data=="1"){
digitalWrite(2,HIGH);
}
if(data=="0"){
digitalWrite(2,LOW);
}
}
}
Exactly as I initally guessed. Your broker doesn't support websockets.
There also exists an issue for this feature request in uMQTTBroker: https://github.com/martin-ger/uMQTTBroker/issues/8
Using this library https://github.com/xDWart/MQTTbroker/blob/master/examples/MQTTbroker/MQTTbroker.ino should solve my problem ?
EDIT : Sorry same library that in the issue you posted above
I'm not an expert in Cyrillic, but the title says so. I'm afraid you simply have to try and work through. General questions about protocols and the MQTT ecosystem exceed the scope of this project. Eventually this is nothing to discuss in an issue tracker, but something for stackoverflow and the like.
Hello,
I'm submitting a...
Current behavior
Actually It seem like my phone fail to subscribe to the topic. I'm trying to use an ESP8266 as MQTT Broker (this part works, I tried it with another EPS8266 as client) and have my phone (Android) subscribed to a topic. I'm using the port 1883 and the name of my topic is "/Server/led " The part of the library that I don't fully understand is the following :
export const MQTT_SERVICE_OPTIONS: IMqttServiceOptions = { hostname: '192.168.4.1', port: 1883, path: '/mqtt' };
So I set hostname to the IP address of my broker, the port to 1883 (port used by the ESP8266) and the path is where I have questions. I set it to '/mqtt' like in the example but I'm not sure that's what I should do. So I would like to know what is this paramater. (I was wondering if path is not related to the topic and for example I could set the path to '/Server' and my topic to '/led')
Expected behavior
Receive message from the subscribed topic
Environment
The code is the same as the example except the "hostname" and "port" mentioned above.
Thank you