I can't send data from mqtt-server to esp8266
I can connect, and send data from esp8266 to broker(MQTT-server). But in many example on Internet I have tried, all of this is use available server (like ws://broker.hivemq.com:8000/mqtt ), I need make a broker by myself and i wanna send data from broker to ESP8266.Sorry, my english is bad, thanks you so much
ESP8266
const char* ssid = "redmi";
const char* password = "123456789";
const char* mqtt_server = "192.168.43.206";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
bool comefromserver;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is active low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str(),"","")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("clientConnected", "clientConnected");
// ... and resubscribe
Serial.println(msg);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, 50, "hello world #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("outTopic", msg);
}
Server
var mosca = require('mosca');
var settings = {
port: 1883,
};
var server = new mosca.Server(settings);
server.on('clientConnected', function(client) {
console.log('client connected', client);
});
server.on('published', function(packet, client) {
console.log('Published', packet.payload.toString('utf8'));
});
server.on('ready', setup);
function setup() {
console.log('Mosca server is up and running');
}
mqtt esp8266
add a comment |
I can connect, and send data from esp8266 to broker(MQTT-server). But in many example on Internet I have tried, all of this is use available server (like ws://broker.hivemq.com:8000/mqtt ), I need make a broker by myself and i wanna send data from broker to ESP8266.Sorry, my english is bad, thanks you so much
ESP8266
const char* ssid = "redmi";
const char* password = "123456789";
const char* mqtt_server = "192.168.43.206";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
bool comefromserver;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is active low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str(),"","")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("clientConnected", "clientConnected");
// ... and resubscribe
Serial.println(msg);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, 50, "hello world #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("outTopic", msg);
}
Server
var mosca = require('mosca');
var settings = {
port: 1883,
};
var server = new mosca.Server(settings);
server.on('clientConnected', function(client) {
console.log('client connected', client);
});
server.on('published', function(packet, client) {
console.log('Published', packet.payload.toString('utf8'));
});
server.on('ready', setup);
function setup() {
console.log('Mosca server is up and running');
}
mqtt esp8266
1
You need to explain what doesn't work. Just posting code with no description of what goes wrong or what you've tried to debug the problem gives us nothing to work with
– hardillb
Jan 19 at 19:38
This code work fine, but i wanna to send data from ESP8266 to Server Nodejs, I tried to read document but I didn't understand clearly
– phanhien1701
Jan 21 at 1:27
No where in your code do you subscribe to any topics
– hardillb
Jan 21 at 7:10
add a comment |
I can connect, and send data from esp8266 to broker(MQTT-server). But in many example on Internet I have tried, all of this is use available server (like ws://broker.hivemq.com:8000/mqtt ), I need make a broker by myself and i wanna send data from broker to ESP8266.Sorry, my english is bad, thanks you so much
ESP8266
const char* ssid = "redmi";
const char* password = "123456789";
const char* mqtt_server = "192.168.43.206";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
bool comefromserver;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is active low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str(),"","")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("clientConnected", "clientConnected");
// ... and resubscribe
Serial.println(msg);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, 50, "hello world #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("outTopic", msg);
}
Server
var mosca = require('mosca');
var settings = {
port: 1883,
};
var server = new mosca.Server(settings);
server.on('clientConnected', function(client) {
console.log('client connected', client);
});
server.on('published', function(packet, client) {
console.log('Published', packet.payload.toString('utf8'));
});
server.on('ready', setup);
function setup() {
console.log('Mosca server is up and running');
}
mqtt esp8266
I can connect, and send data from esp8266 to broker(MQTT-server). But in many example on Internet I have tried, all of this is use available server (like ws://broker.hivemq.com:8000/mqtt ), I need make a broker by myself and i wanna send data from broker to ESP8266.Sorry, my english is bad, thanks you so much
ESP8266
const char* ssid = "redmi";
const char* password = "123456789";
const char* mqtt_server = "192.168.43.206";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
bool comefromserver;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is active low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str(),"","")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("clientConnected", "clientConnected");
// ... and resubscribe
Serial.println(msg);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, 50, "hello world #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("outTopic", msg);
}
Server
var mosca = require('mosca');
var settings = {
port: 1883,
};
var server = new mosca.Server(settings);
server.on('clientConnected', function(client) {
console.log('client connected', client);
});
server.on('published', function(packet, client) {
console.log('Published', packet.payload.toString('utf8'));
});
server.on('ready', setup);
function setup() {
console.log('Mosca server is up and running');
}
mqtt esp8266
mqtt esp8266
asked Jan 19 at 6:24
phanhien1701phanhien1701
13
13
1
You need to explain what doesn't work. Just posting code with no description of what goes wrong or what you've tried to debug the problem gives us nothing to work with
– hardillb
Jan 19 at 19:38
This code work fine, but i wanna to send data from ESP8266 to Server Nodejs, I tried to read document but I didn't understand clearly
– phanhien1701
Jan 21 at 1:27
No where in your code do you subscribe to any topics
– hardillb
Jan 21 at 7:10
add a comment |
1
You need to explain what doesn't work. Just posting code with no description of what goes wrong or what you've tried to debug the problem gives us nothing to work with
– hardillb
Jan 19 at 19:38
This code work fine, but i wanna to send data from ESP8266 to Server Nodejs, I tried to read document but I didn't understand clearly
– phanhien1701
Jan 21 at 1:27
No where in your code do you subscribe to any topics
– hardillb
Jan 21 at 7:10
1
1
You need to explain what doesn't work. Just posting code with no description of what goes wrong or what you've tried to debug the problem gives us nothing to work with
– hardillb
Jan 19 at 19:38
You need to explain what doesn't work. Just posting code with no description of what goes wrong or what you've tried to debug the problem gives us nothing to work with
– hardillb
Jan 19 at 19:38
This code work fine, but i wanna to send data from ESP8266 to Server Nodejs, I tried to read document but I didn't understand clearly
– phanhien1701
Jan 21 at 1:27
This code work fine, but i wanna to send data from ESP8266 to Server Nodejs, I tried to read document but I didn't understand clearly
– phanhien1701
Jan 21 at 1:27
No where in your code do you subscribe to any topics
– hardillb
Jan 21 at 7:10
No where in your code do you subscribe to any topics
– hardillb
Jan 21 at 7:10
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54264623%2fi-cant-send-data-from-mqtt-server-to-esp8266%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54264623%2fi-cant-send-data-from-mqtt-server-to-esp8266%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You need to explain what doesn't work. Just posting code with no description of what goes wrong or what you've tried to debug the problem gives us nothing to work with
– hardillb
Jan 19 at 19:38
This code work fine, but i wanna to send data from ESP8266 to Server Nodejs, I tried to read document but I didn't understand clearly
– phanhien1701
Jan 21 at 1:27
No where in your code do you subscribe to any topics
– hardillb
Jan 21 at 7:10