Skip to content

PubSub missing #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
scargill opened this issue Apr 2, 2015 · 8 comments
Closed

PubSub missing #38

scargill opened this issue Apr 2, 2015 · 8 comments

Comments

@scargill
Copy link

scargill commented Apr 2, 2015

PubSub appears to be missing from the libraries and the original pubsub won't compile.

@igrr
Copy link
Member

igrr commented Apr 2, 2015

The original one was reported to work fine.
Please try this sample for a start.

@igrr igrr closed this as completed in 2c7a024 Apr 3, 2015
@scargill
Copy link
Author

scargill commented Apr 3, 2015

I thought I had it there - compiles ok, publishes ok, subscribes ok but will not take any notice of subscribed topics - and if you keep firing subscribed topics at it, the program eventually dies.

Here's my modified test - I even used millis() in case the 5 second delay was causing problems - made no difference...

#include <PubSubClient.h>
#include <ESP8266WiFi.h>

void* __dso_handle;

const char* ssid = "loft-east";
const char* password = "mypass";

char* topic = "blah";
char* server = "192.168.0.15";

unsigned long mymillis;

WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
Serial.print("Got message "); Serial.print(topic); Serial.print(" length "); Serial.println(length);
}

String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}

void setup() {
Serial.begin(115200);
delay(10);

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());

// Generate client name based on MAC address and last 8 bits of microsecond counter
String clientName;
clientName += "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
clientName += "-";
clientName += String(micros() & 0xff, 16);

Serial.print("Connecting to ");
Serial.print(server);
Serial.print(" as ");
Serial.println(clientName);

if (client.connect((char*) clientName.c_str()),"admin","mypass") {
Serial.println("Connected to MQTT broker");
Serial.print("Topic is: ");
Serial.println(topic);

if (client.publish(topic, "hello from ESP8266")) {
  Serial.println("Publish ok");
  if (client.subscribe("hmm")) Serial.println("Subscribed ok"); else ("Subscribed failed");
}
else {
  Serial.println("Publish failed");
}

}
else {
Serial.println("MQTT connect failed");
Serial.println("Will reset and try again...");
abort();
}
mymillis=millis()+5000;
}

void loop() {
static int counter = 0;

if (mymillis<millis())
{
mymillis=millis()+5000;
String payload = "{"micros":";
payload += micros();
payload += ","counter":";
payload += counter;
payload += "}";

if (client.connected()){
Serial.print("Sending payload: ");
Serial.println(payload);

if (client.publish(topic, (char*) payload.c_str())) {
  Serial.println("Publish ok");
}
else {
  Serial.println("Publish failed");
}

}
++counter;
}
}

Any ideas?

@igrr
Copy link
Member

igrr commented Apr 3, 2015

oh, you need to call client.loop(); from your loop function for the
callback to fire.
will update the gist.
On Apr 3, 2015 10:00, "scargill" [email protected] wrote:

I thought I had it there - compiles ok, publishes ok, subscribes ok but
will not take any notice of subscribed topics - and if you keep firing
subscribed topics at it, the program eventually dies.

Here's my modified test - I even used millis() in case the 5 second delay
was causing problems - made no difference...

#include
#include

void* __dso_handle;

const char* ssid = "loft-east";
const char* password = "mypass";

char* topic = "blah";
char* server = "192.168.0.15";

unsigned long mymillis;

WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
Serial.print("Got message "); Serial.print(topic); Serial.print(" length
"); Serial.println(length);
}

String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}

void setup() {
Serial.begin(115200);
delay(10);

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());

// Generate client name based on MAC address and last 8 bits of
microsecond counter
String clientName;
clientName += "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
clientName += "-";
clientName += String(micros() & 0xff, 16);

Serial.print("Connecting to ");
Serial.print(server);
Serial.print(" as ");
Serial.println(clientName);

if (client.connect((char*) clientName.c_str()),"admin","mypass") {
Serial.println("Connected to MQTT broker");
Serial.print("Topic is: ");
Serial.println(topic);

if (client.publish(topic, "hello from ESP8266")) {
Serial.println("Publish ok");
if (client.subscribe("hmm")) Serial.println("Subscribed ok"); else ("Subscribed failed");
}
else {
Serial.println("Publish failed");
}

}
else {
Serial.println("MQTT connect failed");
Serial.println("Will reset and try again...");
abort();
}
mymillis=millis()+5000;
}

void loop() {
static int counter = 0;

if (mymillis<millis())
{
mymillis=millis()+5000;
String payload = "{"micros":";
payload += micros();
payload += ","counter":";
payload += counter;
payload += "}";

if (client.connected()){
Serial.print("Sending payload: ");
Serial.println(payload);

if (client.publish(topic, (char*) payload.c_str())) {
Serial.println("Publish ok");
}
else {
Serial.println("Publish failed");
}

}
++counter;
}
}

Any ideas?

Reply to this email directly or view it on GitHub
#38 (comment).

@scargill
Copy link
Author

scargill commented Apr 3, 2015

Right – that accounts for a lot – thanks for the speedy response – that appears to work a treat. Presumably my use of millis() is the right solution as a 5 second delay would similarly stop incoming responses. Shame the incoming subs can’t be done under interrupt…

From: Ivan Grokhotkov [mailto:[email protected]]
Sent: 03 April 2015 08:42
To: esp8266/Arduino
Cc: scargill
Subject: Re: [Arduino] PubSub missing (#38)

oh, you need to call client.loop(); from your loop function for the
callback to fire.
will update the gist.
On Apr 3, 2015 10:00, "scargill" <[email protected] mailto:[email protected] > wrote:

I thought I had it there - compiles ok, publishes ok, subscribes ok but
will not take any notice of subscribed topics - and if you keep firing
subscribed topics at it, the program eventually dies.

Here's my modified test - I even used millis() in case the 5 second delay
was causing problems - made no difference...

#include
#include

void* __dso_handle;

const char* ssid = "loft-east";
const char* password = "mypass";

char* topic = "blah";
char* server = "192.168.0.15";

unsigned long mymillis;

WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
Serial.print("Got message "); Serial.print(topic); Serial.print(" length
"); Serial.println(length);
}

String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}

void setup() {
Serial.begin(115200);
delay(10);

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());

// Generate client name based on MAC address and last 8 bits of
microsecond counter
String clientName;
clientName += "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
clientName += "-";
clientName += String(micros() & 0xff, 16);

Serial.print("Connecting to ");
Serial.print(server);
Serial.print(" as ");
Serial.println(clientName);

if (client.connect((char*) clientName.c_str()),"admin","mypass") {
Serial.println("Connected to MQTT broker");
Serial.print("Topic is: ");
Serial.println(topic);

if (client.publish(topic, "hello from ESP8266")) {
Serial.println("Publish ok");
if (client.subscribe("hmm")) Serial.println("Subscribed ok"); else ("Subscribed failed");
}
else {
Serial.println("Publish failed");
}

}
else {
Serial.println("MQTT connect failed");
Serial.println("Will reset and try again...");
abort();
}
mymillis=millis()+5000;
}

void loop() {
static int counter = 0;

if (mymillis<millis())
{
mymillis=millis()+5000;
String payload = "{"micros":";
payload += micros();
payload += ","counter":";
payload += counter;
payload += "}";

if (client.connected()){
Serial.print("Sending payload: ");
Serial.println(payload);

if (client.publish(topic, (char*) payload.c_str())) {
Serial.println("Publish ok");
}
else {
Serial.println("Publish failed");
}

}
++counter;
}
}

Any ideas?

Reply to this email directly or view it on GitHub
#38 (comment).


Reply to this email directly or view it on GitHub #38 (comment) . https://github.com/notifications/beacon/ABzUgzXuScjf0AbQ8j02hfC1Ca5MTYqBks5n7jvJgaJpZM4D5FT7.gif

@igrr
Copy link
Member

igrr commented Apr 3, 2015

There's a standard Arduino library, called Time, which can make stuff like "call this from a loop every N seconds" easier.

@scargill
Copy link
Author

scargill commented Apr 3, 2015

You are a life-saver – indeed that is of course correct and that now works..

But I’ve now found another issue…. While I was experimenting I realised my MQTT broker didn’t have security (something to do with a second mosquito.conf and an include that didn’t work)… and in the process of getting THAT working – which it does perfectly I’ve realised that….

if (client.connect((char*) clientName.c_str()),"my_username","my_password") {

Doesn’t work. It does not matter what username and password I put in there it SAYs it has connected – but now will not publish (and rightly so)…

Any ideas?

From: Ivan Grokhotkov [mailto:[email protected]]
Sent: 03 April 2015 08:42
To: esp8266/Arduino
Cc: scargill
Subject: Re: [Arduino] PubSub missing (#38)

oh, you need to call client.loop(); from your loop function for the
callback to fire.
will update the gist.
On Apr 3, 2015 10:00, "scargill" <[email protected] mailto:[email protected] > wrote:

I thought I had it there - compiles ok, publishes ok, subscribes ok but
will not take any notice of subscribed topics - and if you keep firing
subscribed topics at it, the program eventually dies.

Here's my modified test - I even used millis() in case the 5 second delay
was causing problems - made no difference...

#include
#include

void* __dso_handle;

const char* ssid = "loft-east";
const char* password = "mypass";

char* topic = "blah";
char* server = "192.168.0.15";

unsigned long mymillis;

WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
Serial.print("Got message "); Serial.print(topic); Serial.print(" length
"); Serial.println(length);
}

String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}

void setup() {
Serial.begin(115200);
delay(10);

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());

// Generate client name based on MAC address and last 8 bits of
microsecond counter
String clientName;
clientName += "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
clientName += "-";
clientName += String(micros() & 0xff, 16);

Serial.print("Connecting to ");
Serial.print(server);
Serial.print(" as ");
Serial.println(clientName);

if (client.connect((char*) clientName.c_str()),"admin","mypass") {
Serial.println("Connected to MQTT broker");
Serial.print("Topic is: ");
Serial.println(topic);

if (client.publish(topic, "hello from ESP8266")) {
Serial.println("Publish ok");
if (client.subscribe("hmm")) Serial.println("Subscribed ok"); else ("Subscribed failed");
}
else {
Serial.println("Publish failed");
}

}
else {
Serial.println("MQTT connect failed");
Serial.println("Will reset and try again...");
abort();
}
mymillis=millis()+5000;
}

void loop() {
static int counter = 0;

if (mymillis<millis())
{
mymillis=millis()+5000;
String payload = "{"micros":";
payload += micros();
payload += ","counter":";
payload += counter;
payload += "}";

if (client.connected()){
Serial.print("Sending payload: ");
Serial.println(payload);

if (client.publish(topic, (char*) payload.c_str())) {
Serial.println("Publish ok");
}
else {
Serial.println("Publish failed");
}

}
++counter;
}
}

Any ideas?

Reply to this email directly or view it on GitHub
#38 (comment).


Reply to this email directly or view it on GitHub #38 (comment) . https://github.com/notifications/beacon/ABzUgzXuScjf0AbQ8j02hfC1Ca5MTYqBks5n7jvJgaJpZM4D5FT7.gif

@igrr
Copy link
Member

igrr commented Apr 3, 2015

Frankly I've never tried using PubSubClient with auth... Need to
investigate what's happening inside the library.

On Fri, Apr 3, 2015 at 12:29 PM, scargill [email protected] wrote:

You are a life-saver - indeed that is of course correct and that now
works..

But I've now found another issue.... While I was experimenting I realised my
MQTT broker didn't have security (something to do with a second
mosquito.conf and an include that didn't work)... and in the process of
getting THAT working - which it does perfectly I've realised that....

if (client.connect((char*)
clientName.c_str()),"my_username","my_password") {

Doesn't work. It does not matter what username and password I put in there
it SAYs it has connected - but now will not publish (and rightly so)...

Any ideas?

From: Ivan Grokhotkov [mailto:[email protected]]
Sent: 03 April 2015 08:42
To: esp8266/Arduino
Cc: scargill
Subject: Re: [Arduino] PubSub missing (#38)

oh, you need to call client.loop(); from your loop function for the
callback to fire.
will update the gist.
On Apr 3, 2015 10:00, "scargill" <[email protected] <mailto:
[email protected]> > wrote:

I thought I had it there - compiles ok, publishes ok, subscribes ok but
will not take any notice of subscribed topics - and if you keep firing
subscribed topics at it, the program eventually dies.

Here's my modified test - I even used millis() in case the 5 second delay
was causing problems - made no difference...

#include
#include

void* __dso_handle;

const char* ssid = "loft-east";
const char* password = "mypass";

char* topic = "blah";
char* server = "192.168.0.15";

unsigned long mymillis;

WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
Serial.print("Got message "); Serial.print(topic); Serial.print(" length
"); Serial.println(length);
}

String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}

void setup() {
Serial.begin(115200);
delay(10);

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());

// Generate client name based on MAC address and last 8 bits of
microsecond counter
String clientName;
clientName += "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
clientName += "-";
clientName += String(micros() & 0xff, 16);

Serial.print("Connecting to ");
Serial.print(server);
Serial.print(" as ");
Serial.println(clientName);

if (client.connect((char*) clientName.c_str()),"admin","mypass") {
Serial.println("Connected to MQTT broker");
Serial.print("Topic is: ");
Serial.println(topic);

if (client.publish(topic, "hello from ESP8266")) {
Serial.println("Publish ok");
if (client.subscribe("hmm")) Serial.println("Subscribed ok"); else
("Subscribed failed");
}
else {
Serial.println("Publish failed");
}

}
else {
Serial.println("MQTT connect failed");
Serial.println("Will reset and try again...");
abort();
}
mymillis=millis()+5000;
}

void loop() {
static int counter = 0;

if (mymillis<millis())
{
mymillis=millis()+5000;
String payload = "{"micros":";
payload += micros();
payload += ","counter":";
payload += counter;
payload += "}";

if (client.connected()){
Serial.print("Sending payload: ");
Serial.println(payload);

if (client.publish(topic, (char*) payload.c_str())) {
Serial.println("Publish ok");
}
else {
Serial.println("Publish failed");
}

}
++counter;
}
}

Any ideas?

Reply to this email directly or view it on GitHub
#38 (comment).

Reply to this email directly or view it on GitHub <
https://github.com/esp8266/Arduino/issues/38#issuecomment-89206755> . <
https://github.com/notifications/beacon/ABzUgzXuScjf0AbQ8j02hfC1Ca5MTYqBks5n7jvJgaJpZM4D5FT7.gif>

Reply to this email directly or view it on GitHub
#38 (comment).

@scargill
Copy link
Author

scargill commented Apr 3, 2015

Doooohhhh. Parentheses…

Wrong..

if (client.connect((char*) clientName.c_str()),"my_username","my_password") {

Right..

if (client.connect((char*) clientName.c_str(),"my_username","my_password")) {

It works!

From: Ivan Grokhotkov [mailto:[email protected]]
Sent: 03 April 2015 10:33
To: esp8266/Arduino
Cc: scargill
Subject: Re: [Arduino] PubSub missing (#38)

Frankly I've never tried using PubSubClient with auth... Need to
investigate what's happening inside the library.

On Fri, Apr 3, 2015 at 12:29 PM, scargill <[email protected] mailto:[email protected] > wrote:

You are a life-saver - indeed that is of course correct and that now
works..

But I've now found another issue.... While I was experimenting I realised my
MQTT broker didn't have security (something to do with a second
mosquito.conf and an include that didn't work)... and in the process of
getting THAT working - which it does perfectly I've realised that....

if (client.connect((char*)
clientName.c_str()),"my_username","my_password") {

Doesn't work. It does not matter what username and password I put in there
it SAYs it has connected - but now will not publish (and rightly so)...

Any ideas?

From: Ivan Grokhotkov [mailto:[email protected]]
Sent: 03 April 2015 08:42
To: esp8266/Arduino
Cc: scargill
Subject: Re: [Arduino] PubSub missing (#38)

oh, you need to call client.loop(); from your loop function for the
callback to fire.
will update the gist.
On Apr 3, 2015 10:00, "scargill" <[email protected] mailto:[email protected]%20%3cmailto:%0b <mailto:
[email protected] mailto:[email protected] > > wrote:

I thought I had it there - compiles ok, publishes ok, subscribes ok but
will not take any notice of subscribed topics - and if you keep firing
subscribed topics at it, the program eventually dies.

Here's my modified test - I even used millis() in case the 5 second delay
was causing problems - made no difference...

#include
#include

void* __dso_handle;

const char* ssid = "loft-east";
const char* password = "mypass";

char* topic = "blah";
char* server = "192.168.0.15";

unsigned long mymillis;

WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
Serial.print("Got message "); Serial.print(topic); Serial.print(" length
"); Serial.println(length);
}

String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5)
result += ':';
}
return result;
}

void setup() {
Serial.begin(115200);
delay(10);

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());

// Generate client name based on MAC address and last 8 bits of
microsecond counter
String clientName;
clientName += "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
clientName += "-";
clientName += String(micros() & 0xff, 16);

Serial.print("Connecting to ");
Serial.print(server);
Serial.print(" as ");
Serial.println(clientName);

if (client.connect((char*) clientName.c_str()),"admin","mypass") {
Serial.println("Connected to MQTT broker");
Serial.print("Topic is: ");
Serial.println(topic);

if (client.publish(topic, "hello from ESP8266")) {
Serial.println("Publish ok");
if (client.subscribe("hmm")) Serial.println("Subscribed ok"); else
("Subscribed failed");
}
else {
Serial.println("Publish failed");
}

}
else {
Serial.println("MQTT connect failed");
Serial.println("Will reset and try again...");
abort();
}
mymillis=millis()+5000;
}

void loop() {
static int counter = 0;

if (mymillis<millis())
{
mymillis=millis()+5000;
String payload = "{"micros":";
payload += micros();
payload += ","counter":";
payload += counter;
payload += "}";

if (client.connected()){
Serial.print("Sending payload: ");
Serial.println(payload);

if (client.publish(topic, (char*) payload.c_str())) {
Serial.println("Publish ok");
}
else {
Serial.println("Publish failed");
}

}
++counter;
}
}

Any ideas?

Reply to this email directly or view it on GitHub
#38 (comment).

Reply to this email directly or view it on GitHub <
https://github.com/esp8266/Arduino/issues/38#issuecomment-89206755> . <
https://github.com/notifications/beacon/ABzUgzXuScjf0AbQ8j02hfC1Ca5MTYqBks5n7jvJgaJpZM4D5FT7.gif>

Reply to this email directly or view it on GitHub
#38 (comment).


Reply to this email directly or view it on GitHub #38 (comment) . https://github.com/notifications/beacon/ABzUg10oUx2TmGFAb9ftu8TdYOkq9dP6ks5n7lW-gaJpZM4D5FT7.gif

igrr added a commit that referenced this issue Oct 29, 2015
Be more clear about PubSubClient
Close #38
[ci skip]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants