Guide: Setting up and testing MQTT Client Gateway
-
(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 said:
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.
Have we managed to get a MQTT Gateway working on a Arduino with an ENC28J60 module rather than a w5100?
-
@hek said:
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.
Have we managed to get a MQTT Gateway working on a Arduino with an ENC28J60 module rather than a w5100?
-
@hek said:
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.
Have we managed to get a MQTT Gateway working on a Arduino with an ENC28J60 module rather than a w5100?
@samuel235 I don't really encourage you to use ENC. I spent months trying to make it stable and I failed. There are several manufactures and as far as I know that there are several software workarounds to fix problem in the silicon chips. I turned to use W5100 and it is much better till now.
N.B.: Some people managed to run ENC with good stability but as I have said, there may be different manufacturers so I got confused and tried the W5100. -
@ahmedadelhosni I have now turned to the W5100 shield myself. Works a charm, super smooth!
-
I tried to make MQTT client gateway but it is giving following error while compiling
C:\Users\Ahmed\AppData\Local\Temp\build8359488613260197686.tmp/arduino.ar(core_esp8266_postmortem.c.o): In function `abort': C:\Users\Ahmed\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\2.0.0-rc2\cores\esp8266/core_esp8266_postmortem.c:177: multiple definition of `abort' GatewayESP8266MQTTClient.cpp.o:C:\Users\Ahmed\Documents\Arduino\libraries\MySensors/core/MyMainESP8266.cpp:46: first defined here collect2.exe: error: ld returned 1 exit status Error compiling.Any suggestions @hek ?
-
If it helps:
in ESP8266 ver. 2.0.0 was added abort in
here
cores/esp8266/core_esp8266_postmortem.c:void abort() __attribute__((noreturn)); void abort(){ // cause exception s_abort_called = true; do { *((int*)0) = 0; } while(true); }i comment the lines in MySensors/core/MyMainESP8266.cpp and it compiled ok.
-
Hello.
I tried the development branch because I prefer having my gateway not as message broker.
I had some problems resulting in conflicting entries for the gateway in MyConfig.h and the sketch. MyConfig.h always wins :D for the release it might be more transparent for the user if there is only one location, where the gateway settings are defined: example or MyConfig.hSo much for the feedback, now to the problems:
I use the w5100 version of the sketch with a static IP.In the serial monitor i see:
0;0;3;0;9;Starting gateway (RNNGA-, 1.6.0-beta) 0;0;3;0;9;Radio init successful. IP: 0.40.68.10 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Init complete, id=0, parent=0, distance=0 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Attempting MQTT connection...in the log of the moquitto server i see:
1450112394: New connection from 192.168.100.10 on port 1883. 1450112394: New connection from 192.168.100.10 on port 1883. 1450112394: Socket error on client <unknown>, disconnecting. 1450112409: New connection from 192.168.100.10 on port 1883. 1450112409: New connection from 192.168.100.10 on port 1883.The gateway tries to open a lot of connections and all of them seem to die after a short while
Any idea? -
@dakky The gateway tries to open a lot of connections and all of them seem to die after a short while
I had a similar problem a while back, not with the mysensors dev branch, but while using the subpub MQTT library on a ESP8266. After upgrading my MQTT (mosquitto) server to version 1.3.5 it corrected to the problem. You may want to take a look at the version you are running.
Mike
-
Thx 4 replying ;)
mosquitto version 1.4.5 (build date Sun, 08 Nov 2015 21:43:12 +0000)unfortunetly this seems not to be the problem :(
EDIT: i updated the PubSubClient lib to version 2.4 instead of the delivered 1.9. Same behavior
Another EDIT: mosquitto itself seems to work fine: OpenHab successfully connects to the broker and delivers its messages -
Hi,
I'm trying to set up the MQTT gateway to work with Home-Assistant; since the serial gateway plugin lacks in binary switches (ex relays, and other things).
I have the MQTT sketch installed on a Nano with W5100 module, and serial output from the Arduino is good. But when I add a sensor to the Gateway, there is no communication.
Here is my output from Arduino, where would I go next?
0;0;3;0;9;Starting gateway (RNNGA-, 1.6.0-beta) 0;0;3;0;9;Radio init successful. IP: 192.168.86.112 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;MQTT connected 0;0;3;0;9;Init complete, id=0, parent=0, distance=0 ad: 0-0-0 s=0,c=0,t=0,pt=0,l=0,sg=0: 0;0;3;0;9;ver mismatch 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;MQTT connected``` -
@dakky said:
The gateway tries to open a lot of connections and all of them seem to die after a short while
Any idea?Narghs!
Seems to be a hardware problem. I changed the w5100 module with an w5100 arduino shield and now it works fine:
0;0;3;0;9;Starting gateway (RNNGA-, 1.6.0-beta) 0;0;3;0;9;Radio init successful. IP: 192.168.100.10 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;MQTT connected 0;0;3;0;9;Init complete, id=0, parent=0, distance=0I'm still interested in the reason. Hardware broken or wiring fail?
- pinging the arduino worked
- i can see the connections on the broker
=> basic networking seems to work, but "delivering content" failed. Any guesses?
Strange fact: with the "broken" hardware:
0;0;3;0;9;Radio init successful. IP: 0.40.68.10with the shield:
0;0;3;0;9;Radio init successful. IP: 192.168.100.10Same sketch, 192.168.100.10 is the correct Ip (obviously) ;)
Greeting
Dakky -
@dakky said:
The gateway tries to open a lot of connections and all of them seem to die after a short while
Any idea?Narghs!
Seems to be a hardware problem. I changed the w5100 module with an w5100 arduino shield and now it works fine:
0;0;3;0;9;Starting gateway (RNNGA-, 1.6.0-beta) 0;0;3;0;9;Radio init successful. IP: 192.168.100.10 0;0;3;0;9;Attempting MQTT connection... 0;0;3;0;9;MQTT connected 0;0;3;0;9;Init complete, id=0, parent=0, distance=0I'm still interested in the reason. Hardware broken or wiring fail?
- pinging the arduino worked
- i can see the connections on the broker
=> basic networking seems to work, but "delivering content" failed. Any guesses?
Strange fact: with the "broken" hardware:
0;0;3;0;9;Radio init successful. IP: 0.40.68.10with the shield:
0;0;3;0;9;Radio init successful. IP: 192.168.100.10Same sketch, 192.168.100.10 is the correct Ip (obviously) ;)
Greeting
Dakky@dakky I guess I had that problem once or twice before but I don't pay attention to it as there was other bugs which were more important.
I remember that I just reset the nano and it worked at once. Glad that it worked for you but as you have said, knowing the root cause it important.
Will try to reproduce it again when I have time.
-
Another question:
I don't have any experience with the classic setup (broker on gateway). But does
The 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-TYPEmean, that the consuming controllers configuration (openhab in my case) has to be changed?
Fromsensor-gw1-out/99/3/V_TRIPPEDTo
sensor-gw1-out/99/3/1/0/16in order to represent what is really pushed to the mqtt broker? Because thats what is posted to the broker?
-
A little bit cofused. Can this sketch be uploaded to ESP12 set as in the "ESP8266 WiFi Gateway"? Without the arduino?
After uploading it to esp all i getload 0x4010f000, len 1264, room 16 tail 0 chksum 0x42 csum 0x42 ~ld -
Can more detailed instructions be given on the development branch? As i understand this is not a simple copy the code from here https://github.com/mysensors/Arduino/blob/development/libraries/MySensors/examples/GatewayESP8266MQTTClient/GatewayESP8266MQTTClient.ino paste into arduino IDE compile and upload process?
-
Well i had to move all the libs from production branch and extract clean only the development branch in arduino IDE to get it to work.
Afterwards had to do the same thing back to production to compile the humidity sensor as it gave me the following error during compilationArduino: 1.6.5 (Windows 7), Board: "Arduino Pro or Pro Mini, ATmega328 (5V, 16 MHz)" In file included from Mysensors_DHT_Wireless.ino:31:0: \My Documents\Arduino\libraries\MySensors/MySensor.h:201:3: error: #error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless. #error No forward link or gateway feature activated. This means nowhere to send messages! Pretty pointless. ^ Mysensors_DHT_Wireless:39: error: 'MySensor' does not name a type Mysensors_DHT_Wireless.ino: In function 'void setup()': Mysensors_DHT_Wireless:50: error: 'gw' was not declared in this scope Mysensors_DHT_Wireless.ino: In function 'void loop()': Mysensors_DHT_Wireless:75: error: 'gw' was not declared in this scope Mysensors_DHT_Wireless:85: error: 'gw' was not declared in this scope Mysensors_DHT_Wireless:90: error: 'gw' was not declared in this scope 'MySensor' does not name a type