MQTT sample setup on ESP8266
- 
 Being new to this stuff it is sometimes hard to find all the necessary info, so I would like to provide you with a sample and hope it eases your way to experimenting with this cool project. We will connect a sample sketch from the PubSubClient library for ESP8266 to MyController through MQTT. Tested on SNAPSHOT, find it here - Activate the MQTT broker on your MyController
 Settings -> MQTT broker -> Status Enabled
 I prefer to allow anonymous in the beginning to eliminate one source of errors.
  - Add MQTT gateway :
 Resources -> Gateway -> Add gateway.
 You can use mostly default values, important is Network Type, Type and the correct direction of your topics: mygateway-out is where your sensors should post data, mygateway-in is what your client should subscribe to get changes from MyController (e.g. a relay).
  - 
Install PubSubClient library in Arduino IDE 
 Sketch Menu -> Include Libraries -> Manage Libraries
- 
Open ESP8266 Example 
 File -> Examples -> PubSubClient -> mqtt_esp8266
- 
Save it in your own project path 
- 
Edit SSID, Password in your sketch and set mqtt_server constant to the IP of your MyController MQTT broker 
- 
Now the fun part: MyController can accept MQTT messages in MySensors format. The API is described at Serial API 2.0 
 For your sketch this means you need to publish into topics following this format, e.g.:#define temperature_topic "mygateway-out/7/0/1/0/0" 
 Here MyConroller subscribes to mygateway-out and will interpret: 
 /5: Node ID 5
 /0: Child sensor ID 0
 /1: Message type 'set'
 /0: ack-parameter: Normal message
 /0: Sub-type: V_TEMPSo add the #define in your sketch e.g. after the #includes and go to line 100 where client.publish is called: client.publish("outTopic","hello world"); change this to use your topic from above and post a temperature of whatever degrees: client.publish(temperature_topic,"10.50"); Compile and run the sketch - Monitor incoming messages
 Status -> Resources logs
 Now you should see incoming temperature messages with 10.50 degrees.
  The sensor resource should be created automatically. Now if you want to control a relay for example, you create that sensor (actually actuator) and in your sketch do a client.subscribe("mygateway-in/<NodeID>/<ChildID>/1/0/<Sub-type>"); 
 I sometimes got the mygateway-in/out switched accidentally, you will see nothing on the logs, so double-check!Thanks to Jeeva for this great piece of software and untiring support! EDIT: 
 Two quotes from jkandasa that might be helpful:Mycontroller format is described here: https://github.com/mycontroller-org/mycontroller/blob/development/modules/core/src/main/java/org/mycontroller/standalone/provider/mc/McpRawMessage.java#L81 When you set this line <logger level="DEBUG" name="org.mycontroller.standalone.gateway.mqtt" /> on conf/logback.xml you can see MQTT debug logs. 
- Activate the MQTT broker on your MyController
- 
 @ragflyer Thank you for the great tutorial! I have created MyController direct support ESP8266 available here. Using this even you can update your ESP8266 Firmware from MyController. For now sample code is available here. 
- 
 @jkandasa Awesome. I've been trying your sample, but where do you define ssid and password? 
 For now I've set it up through the web interface of the node, but then I cannot find where the serial output is coming from, especially where the topic "in_3/ESP_0F..." is defined? It seems like none of the files contains this...
 Finally I need to set up a gateway type MyController correct? Probably I need to use a different port if the MQTT gateway should be running alongside?
- 
 Yes, you have to configure Node via configuration GUI from node(Connect to SSID of your node and enter http://192.168.4.1 on your browser). And you have to specify Feed idfield which is used on MQTT topic and subscription.in_3/ESP_0F..looks like you have configured3asfeed id, In MyController you have to create MQTT gateway entry with the following settings,
- 
 @ragflyer Current SNAPSHOT build needs ESP driver update from here 
- 
 @jkandasa OK, how do you change the node config once it is saved? Uploading the sketch does not set it back to AP mode  
- 
 @ragflyer said: OK, how do you change the node config once it is saved? Uploading the sketch does not set it back to AP mode Node config will be stored on EEPROM. When you update firmware. No need to change node config. However when you want to change node config, just trigger Erase configurationfrom GUI (Resources >> Nodes >> Select your node >> Erase configuration). Now you will get AP on the name/ssid of ESP device and configuration url http://192.168.4.1
- 
 Nice it works, thanks for pointing this out! Few more things that might be helpful for newcomers: - 
If you misconfigured so far that no connection to MyController is possible, the above "Erase configuration" obviously won't work. Load the example sketch for eeprom_clear and upload it, then upload your MyControllerDevice sketch again, connect to the ESP SSID and rerun configuration at http://192.168.4.1 
- 
Feed ID will not take more than 6 characters 
- 
You cannot set up a second gateway at say :1884 if you configured the broker to run at :1883. But it seems to be ok to define several Gateways with different topics on the same port (so you don't have to migrate existing nodes to another topic now). 
 
- 
- 
 @ragflyer said: Few more things that might be helpful for newcomers: Thanks for the nice points!! If you misconfigured so far that no connection to MyController is possible, the above "Erase configuration" obviously won't work. Load the example sketch for eeprom_clear and upload it, then upload your MyControllerDevice sketch again, connect to the ESP SSID and rerun configuration at http://192.168.4.1 Yes, We have to configure a PIN on ESP8266 to do force reset from device side. We have an option for this, actual code is here. But I never tested. Feed ID will not take more than 6 characters Yes, This is limitation, to limit topic length. You cannot set up a second gateway at say :1884 if you configured the broker to run at :1883. But it seems to be ok to define several Gateways with different topics on the same port (so you don't have to migrate existing nodes to another topic now). But for ESP8266 device you no need to configure more than one gateway. You can use same feed id across on all devices. MyController specific protocol born to support all the ESP devices from single gateway. 
- 
 I have been using this set up with 5 esp8266 sensors from https://www.wemos.cc/. Tested even the update firmware OTA.  working great. working great.Just remember to Change MQTT_MAX_PACKET_SIZE to 512 bytes on PubSubClient.h to allow for OTA 
- 
 @paul Hi Paul, can you share you code, I am having problems with the Wemos D1 Mini boards, and also would like some inspiration to my code. Thanks! 
- 
 Hello, 
 I also use ESP8266 (ESP07 & ESP12F & WEMO)Now i tried to send MQTT with MQTTBOX to see what happened .... arhh nothing     Have you any fully list of MQTT order to easy way write my arduino sample ? Thanks Best regards 
- 
 Another thing , Should i have a MQTT Password ? 
 Because i use MQTT with another gateway (Domoticz) , i would like to keep it in this way.
 Thanks.
- 
 @Stephan35 MQTT password is optional. 
 Right now MyController gives direct support for ESP devices. you can have a look on https://github.com/mycontroller-org/MyControllerDevice
- 
 Hello, i send an issue on GitHub, 
 ESP does not take good AP , take old one. ... thanks 
- 
 @Stephan35 Hope issue addressed on GitHub 
- 
 Right, Now two other things ... 
 First i try autodetect MQTT server, but failed .Now ESP disapear from wifi, so i can't access to ESP . So, is there any special parameter to set MQTT as autodiscover ? How can i change ESP parameter, i tried to flash it again, but keep parameter in memory. thanks - esp reset after one hour of waiting ... lost memory ?
 C[I]: Configuration(NodeEUI:[ESP_8C0125], Mqtt{mDNS-status:[1], Server:[0.0.0.0], Port:[0], FeedId:[esp], User:[]}) 
 MC[I]: MQTT settings(Broker:[0.0.0.0], Port:[0])
 MC[I]: MQTT authenticating as anonymous
 MC[I]: MQTT connection failed, rc=-2
 MC[I]: MQTT failed! Retryting to connect...
 MC[I]: Sending mDNS query[_mc_mqtt._tcp]...
 Skipping compressed pointerSoft WDT reset ctx: sys 
 sp: 3ffffa60 end: 3fffffb0 offset: 01b0stack>>> 
 3ffffc10: 402079e1 3ffffe20 3fff01c0 402079d6
 3ffffc20: 61636f6c a48a006c 58540d4b 43076fd5
 3ffffc30: 68965955 4e5f233f 58a15990 8cecc38a
 3ffffc40: 36deb08a bd6b9bdb 358e7912 cbe029c8
 3ffffc50: 1d343e93 7d7369fe 0896a8c6 4a9ff10c
 3ffffc60: 863b39b9 6ff1395e 7f7e23b7 32d5f369
 3ffffc70: e209547c 6d3c8869 38895b9a 912fb242
 3ffffc80: 00000005 00000000 00000020 40101d0e
 3ffffc90: 3ffe9e25 40104feb 3ffed8b0 ffffffff
 3ffffca0: 40102a21 3ffed8b0 00000008 05b7eda3
 3ffffcb0: 00000017 3f7c5564 3ffee29c 40102bb0
 3ffffcc0: 3ffea6d0 00000000 00000000 3ffedf70
 3ffffcd0: 00000000 3f7c5564 40102fee 00000100
 3ffffce0: 7fffffff 3ffea6d0 3ffea6d0 00000001
 3ffffcf0: 00000001 401041df 3ffedfa0 00000000
 3ffffd00: 40104642 3f7c5564 00000000 4000050c
 3ffffd10: 3fffc278 40102d88 3fffc200 c0000022
 3ffffd20: 7674780c 3d737265 70720c31 7070693d
 3ffffd30: 6972702f 6e05746e 3d65746f 6f747108
 3ffffd40: 3d6c6174 72700b31 69726f69 313d7974
 3ffffd50: 79741635 6e61433d 4d206e6f 30353347
 3ffffd60: 65732030 73656972 6f72701d 74637564
 3ffffd70: 6143283d 206e6f6e 3533474d 73203030
 3ffffd80: 65697265 70312973 613d6c64 696c7070
 3ffffd90: 69746163 6f2f6e6f 74657463 7274732d
 3ffffda0: 2c6d6165 67616d69 72752f65 6d692c66
 3ffffdb0: 2f656761 6765706a 6d646137 72756e69
 3ffffdc0: 74683d6c 2f3a7074 3732342f 30324543
 3ffffdd0: 30303030 6f6c2e30 2e6c6163 646e692f
 3ffffde0: 682e7865 3f6c6d74 45474150 5041415f
 3ffffdf0: 6273750d 47464d5f 6e61433d 75156e6f
 3ffffe00: 4d5f6273 4d3d4c44 30353347 65732030
 3ffffe10: 73656972 6273750b 444d435f 4652553d
 3ffffe20: 49555529 30303d44 30303030 302d3030
 3ffffe30: 2d303030 30303031 3030382d 32342d30
 3ffffe40: 32454337 30303030 55003030 563d4652
 3ffffe50: 2c332e31 2c315043 2d345150 53522c35
 3ffffe60: 2c303036 42475253 a8c03432 424f0c0a
 3ffffe70: 464f2c32 00000001 00000277 0000fffe
 3ffffe80: 0000000c 4408543d 656c7075 06543d78
 3ffffe90: 6e616353 4605543d 463d7861 70615011
 3ffffea0: 614d7265 656c3d78 2d6c6167 6b1c3441
 3ffffeb0: 3d646e69 75636f64 746e656d 766e652c
 3ffffec0: 706f6c65 68702c65 156f746f 3d63616d
 3ffffed0: 313a3036 42383a32 3a32343a 453a4337
 3ffffee0: 00001432 00000000 3ffead68 3fff1c54
 3ffffef0: 000000e0 3fffdc80 3fff2194 401074a0
 3fffff00: 40220000 00000000 3ffeba21 3fff1c50
 3fffff10: 3ffead68 3fff1c54 3fff1ce4 4022e70c
 3fffff20: 00000002 3fff2114 3fff2114 3ffee9d0
 3fffff30: 00000000 3fff1ce4 0000001c 3fff2114
 3fffff40: 3ffead5a 00000000 3fff1ce4 4022db15
 3fffff50: 0c0aa8c0 000002a8 00000000 0000003b
 3fffff60: 00000002 00000018 4020e23b 3ffed888
 3fffff70: 3ffead34 3fffdcc0 3ffe9ee0 3ffe9ee0
 3fffff80: 4020e1ae 3ffed888 00000000 3fff219c
 3fffff90: 3fffdc80 00000000 3fff1ce4 402224a7
 3fffffa0: 40000f49 3fffdab0 3fffdab0 40000f49
 <<<stack<<<ets Jan 8 2013,rst cause:2, boot mode:(1,6) ets Jan 8 2013,rst cause:4, boot mode:(1,6) wdt reset 
- 
 Tried this : #define FACTORY_RESET_PIN 4 
 #define FACTORY_RESET_PIN_STATE 4
 #define FACTORY_RESET_TIME 1000*1 //hold the button 9 secondsNo success , still no AP . 
- 
 Again me , 
 Is it possible to test MQTT to mycontroller without ESP ?
 Can you give me, please, some MQTT order example to understand what is wrong.
 MQTT works fine , made some test with domoticz implemented on the same server.
 Thanks.
- 
 @Stephan35 said: First i try autodetect MQTT server, but failed . On MyController server your have to enable mcc.mdns.service.enable=trueon mycontroller.properties and stop and start.#define FACTORY_RESET_PIN 4 
 #define FACTORY_RESET_PIN_STATE 4
 #define FACTORY_RESET_TIME 1000*1 //hold the button 9 secondsNo success , still no AP . FACTORY_RESET_PIN_STATEshould be either0or1. When you press the button, If your pin(FACTORY_RESET_PIN) connects onGND, Then you have to enter0, otherwise1.MC[I]: Sending mDNS query[_mc_mqtt._tcp]... 
 Skipping compressed pointerYes, Sometimes I see this error when we use mDNS enabled on ESP device. This error happens on mDNS library. 
