hoffsta
(Sean)
January 10, 2017, 10:56pm
1
I’ve been trying to get a sketch working to pair a NodeMCU/PIR sensor into Home Assistant via MQTT. My MQTT broker is working fine but I do have it set to require a username and password. All the example sketches I can find are running with no authentication enabled and I don’t know exactly how to integrate the user/pass into the sketch.
Can anyone share a simple sketch that I can use to connect these?
sebk-666
(Sebastian)
January 10, 2017, 11:39pm
2
Look at this example - it works for me:
/*
Basic ESP8266 MQTT example
This sketch demonstrates the capabilities of the pubsub library in combination
with the ESP8266 board/library.
It connects to an MQTT server then:
- publishes "hello world" to the topic "outTopic" every two seconds
- subscribes to the topic "inTopic", printing out any messages
it receives. NB - it assumes the received payloads are strings not binary
- If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
else switch it off
It will reconnect to the server if the connection is lost using a blocking
reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
achieve the same result without blocking the main loop.
To install the ESP8266 board, (using Arduino 1.6.4+):
- Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
http://arduino.esp8266.com/stable/package_esp8266com_index.json
This file has been truncated. show original
Sebastian
1 Like
hangy
(Angelo La Mazza)
January 10, 2017, 11:49pm
3
using the pubsubclient library che connection to the broker is like this:
// client parameters
PubSubClient client(mqtt_server, 1883, callback, espClient);
if (client.connect("433toMQTTto433", "username", "password"))
find this part in your sketch and add user and pass… i think
hoffsta
(Sean)
January 11, 2017, 12:04am
4
Thanks, I hadn’t considered the adafruit, I thought it was just for connecting to their cloud service. I’ll give that a shot!
hoffsta
(Sean)
January 11, 2017, 12:05am
5
I missed that part of the pubsubclient. Thanks for the heads up!
sebk-666
(Sebastian)
January 11, 2017, 8:10am
6
I’m using that exact same sketch - I just threw out the callback() function, because it’s not needed here and I added a line to turn off the LED on the nodeMCU board, because that thing ist bright as hell
Sebastian
1 Like
hoffsta
(Sean)
January 11, 2017, 6:50pm
7
Thanks, I got this working using your example!