Help getting started with MQTT gateway
-
I didn't bother trying. The ENC-library is gigantic and the MQTT implementation used does not support the ENC-module.
@hek Ahh okay, i was only wondering :). I'm setting up a ArduinoMega as we speak as a MQTTClient, im struggling to get my broker (mosquitto) on my raspberrypi controller to get the startup message from the gateway. Day long of debugging ahead of me ;)
-
(this post currently applies to the development branch)
You can run the MQTT gateway on an Arduino+W5100 ethernet module or the ESP8266. Connect radio and ethernet module exactly like for the normal gateway.
Download and install the MySensors development branch
The use one of the following sketches depending on the hardware you use:
https://github.com/mysensors/Arduino/blob/development/libraries/MySensors/examples/GatewayW5100MQTTClient/GatewayW5100MQTTClient.ino
or
https://github.com/mysensors/Arduino/blob/development/libraries/MySensors/examples/GatewayESP8266MQTTClient/GatewayESP8266MQTTClient.inoThe topic resembles the serial protocol. You must define you preferred subscribe and publish prefix in the sketch. The topic is build like this:
MY_MQTT_PUBLISH_TOPIC_PREFIX/FROM-NODE-ID/SENSOR-ID/CMD-TYPE/ACK-FLAG/SUB-TYPERead more about the serial protocol here. An example topic for data received from your gateway could look like this:
mygateway1-out/2/1/1/0/49
If you want to send data to your sensors use MY_MQTT_SUBSCRIBE_TOPIC_PREFIX defined in your sketch. Should be looking like this (using default sketch):
mygateway1-in/2/1/1/0/49
You can test your gateway using mosquitto as broker.
Make sure to set MY_IP_GATEWAY_ADDRESS in gateway pointing to you computers ip-number (on the same lan).
Start the broker (leave this running):
> mosquittoNow start your gateway and you should see something like this in the gateway log. Note that I have a GPS sensor running in my radio network which send data to the gateway all the time:
0;0;3;0;9;gateway started, id=0, parent=0, distance=0 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;MQTT connected 0;0;3;0;9;read: 2-2-0 s=1,c=1,t=49,pt=0,l=22,sg=0:55.722519;13.018120 0;0;3;0;9;Sending message on topic: mygateway1-out/2/1/1/0/49 0;0;3;0;9;read: 2-2-0 s=1,c=1,t=49,pt=0,l=22,sg=0:55.722519;13.018121 0;0;3;0;9;Sending message on topic: mygateway1-out/2/1/1/0/49Subscribe to messages (in another shell on your computer)
> mosquitto_sub -v -t 'mygateway1-out/#'You should see the messages like you saw in the gateway-log showing up like this:
mygateway1-out/2/1/1/0/49 55.722519;13.018121;13 mygateway1-out/2/1/1/0/49 55.722519;13.018114;12 mygateway1-out/2/1/1/0/49 55.722527;13.018120;11 mygateway1-out/2/1/1/0/49 55.722534;13.018122;10Sending (publishing) messages to your sensor network:
> mosquitto_pub -t 'mygateway1-in/2/1/1/0/49' -m '0,29'@hek I've changed the port number and MY_IP_GATEWAY_ADDRESS to the address of the RaspberryPi running Mosquitto. I've set my static IP, router gateway and subnet as follows in the sketch, uploaded it to the arduino and its not showing up in the subscribed topic of mosquitto_sub -v -t '#' when i restart the gateway.
// Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP) #define MY_IP_ADDRESS 192,168,0,85 // If using static ip you need to define Gateway and Subnet address as well #define MY_IP_GATEWAY_ADDRESS 192,168,0,1 #define MY_IP_SUBNET_ADDRESS 255,255,255,0 // MQTT broker ip address. #define MY_CONTROLLER_IP_ADDRESS 192, 168, 0, 21 // The MQTT broker port to to open #define MY_PORT 1883I am able to ping the address of the arduino, so i know the connection of the arduino is, somewhat, correct.
Is there anything silly that i'm doing wrong here? I've tried changing:
// W5100 Ethernet module SPI enable (optional if using a shield/module that manages SPI_EN signal) //#define MY_W5100_SPI_EN 4but that didn't work either. I'm struggling to see anything else that would be needed apart from the address for the broker itself.
Edit: Would it just be better for me to run the Gateway as a Broker itself rather than as a client connecting to the broker on the raspberryPi?
-
I didn't bother trying. The ENC-library is gigantic and the MQTT implementation used does not support the ENC-module.
I have my controller running openHAB and mosquitto perfect now, my MQTTGateway is working too. I've made a humidity sensor node with the sketch in the library folder, its passing (null) on the specified topic.
I'm guessing that the (null) means that there is no message in the communication. Am i correct in assuming this?
-
I have my controller running openHAB and mosquitto perfect now, my MQTTGateway is working too. I've made a humidity sensor node with the sketch in the library folder, its passing (null) on the specified topic.
I'm guessing that the (null) means that there is no message in the communication. Am i correct in assuming this?
-
@samuel235 said:
I'm guessing that the (null) means that there is no message in the communication. Am i correct in assuming this?
Some messages has no payload. But humidity/set should have one.
send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,sg=0,st=ok: H: 41.90 req id send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,sg=0,st=ok: T: 24.70 req id send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,sg=0,st=ok: H: 42.00 req id send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,sg=0,st=ok: H: 41.80 req id send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,sg=0,st=ok: T: 24.80 req idThis is just a snippet from my Humidity sensor, and there is nothing sitting at the end of the send line, so i'm guessing the sensor node is not releasing a value for humidity.
-
The node is requesting an id from the controller. "req id"...
So you either have to answer this request (through mqtt) or set a static id on the node.
#define MY_NODE_ID 42@hek I wondered why i was getting 255-255 everywhere. Does this have anything to do with that?
By the way, i assumed that all the configs were setup to handout random ID's, i'm sure i kept reading "automatic ID assign" when i was editing configs for the radios....
-
The node uses id 255 temporarily while asking for an id.
It's automatic if you're controller supports it. But as this mqtt topic layout is quite new, I doubt many controllers support it.
The previous (1.5) MQTT gateway did some handouting on its own which wasn't optimal (stored last used id in EEPROM resulting in no control/reuse of ids).
-
Ahhh right okay, well this is what i kept reading in 'MyConfig.h':
// Node id defaults to AUTO (tries to fetch id from controller) #ifndef MY_NODE_ID #define MY_NODE_ID AUTO #endifHowever, i shall manually set the ID on my nodes now.
I have put '#define MY_NODE_ID 42' into the humidity sketch, that isn't working, is there a specific place it needs to be? I'm just looking through the config files to see where about in the sendMessage code it calls for it and how it calls for it. I've tried to comment those lines out so it doesn't even attempt to get a ID from the controller, that isn't working at the moment.
-
I tried what you advised but it wasn't asigning a ID to the node still. So after a little looking around i found that you can specify the node id within gw.begin().
So i have got it as gw.begin(NULL, 4, false).
While i understand that the second piece of data is the node id, and the third is whether or not it goes into sleep mode. What is the first? Also, with me calling false for the sleep mode and then at the end of the sketch it tells the node to sleep for 30 seconds, am i over riding that sleep mode that i call at the end?
-
Ah.. you're running mixed library versions on node/gateway.. Yes, then you'd need to call gw.begin() on the node.
But will this overwrite any config/settings like sleep or anything? What does the NULL mean at the start of gw.begin?
-
I'm sorry, i got confused about which version i was running. I thought it was the development humidity sketch and therefor i didn't look for the API of it. Makes sense now. Thank you.
-
I now have my controller, broker and gateway setup working fine. I then created a temp and humidity sensor, works fine. I then made a motion node, works fine. Then a light switch node that all work perfectly fine together. Now, i come to configure my relay module (which im struggling with getting it to receive the messages) so i open my serial monitor for the gateway and see that even though its got a solid connection, it keeps trying to make another MQTT connection. Over and over again....
0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;MQTT connected 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;MQTT connected 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;MQTT connected 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;MQTT connected 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;MQTT connectedThere is nothing in the loop section of the sketch, so all i can assume is that it keeps dropping its connection to mosquitto broker thats wired over ethernet?
-
This post is deleted!
-
send: 4-4-0-0 s=255,c=0,t=18,pt=0,l=5,sg=0,st=fail:1.5.1 send: 4-4-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=fail:0 repeater started, id=4, parent=0, distance=1 send: 4-4-0-0 s=255,c=3,t=11,pt=0,l=5,sg=0,st=fail:Relay send: 4-4-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=fail:1.0 send: 4-4-0-0 s=1,c=0,t=3,pt=0,l=0,sg=0,st=fail: