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_TEMP
So 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.