{"id":2453,"date":"2016-12-01T15:27:53","date_gmt":"2016-12-01T23:27:53","guid":{"rendered":"http:\/\/visualgdb.com\/w\/?p=2453"},"modified":"2017-05-26T10:30:15","modified_gmt":"2017-05-26T17:30:15","slug":"using-mqtt-to-remotely-control-an-esp8266-board","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/esp8266\/mqtt\/","title":{"rendered":"Using MQTT to remotely control an ESP8266 board"},"content":{"rendered":"<p>This tutorial shows how to use the MQTT protocol to remotely control the LED on the ESP8266 board and to monitor the state of the on-board button.<\/p>\n<p>MQTT is a protocol that allows exchanging short messages between different small devices. It is optimized to use as little traffic as possible and to run on small devices like ESP8266.<\/p>\n<p>MQTT communication involves a central MQTT broker (e.g. <a href=\"https:\/\/mosquitto.org\/\">Mosquitto <\/a>running on Raspberry Pi) and one or more MQTT clients. A client can subscribe to one or more topics and then once any other client posts a message to that topic, the subscribed clients will receive the message. The routing of messages is done by the MQTT broker, so the clients don&#8217;t need to handle that. In our example the ESP8266 board will subscribe to a topic called <strong>led<\/strong> and will turn the on-board LED on when someone sends &#8216;1&#8217; to that topic and off when &#8216;0&#8217; is sent instead. On the PC side we will run a basic program that will send ones and zeroes to the <strong>led<\/strong> topic to control the LED:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/scheme.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2472\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/scheme.png\" alt=\"scheme\" width=\"1258\" height=\"235\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/scheme.png 1258w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/scheme-300x56.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/scheme-1024x191.png 1024w\" sizes=\"(max-width: 1258px) 100vw, 1258px\" \/><\/a>The advantage of the MQTT protocol over just sending ones and zeroes via TCP or UDP is automatic configuration. The PC does not need to know how many ESP8266 boards are listening to the <strong>led<\/strong> topic. All it needs to do is send a message to the MQTT broker and every node that subscribed for this topic will receive. Likewise, the ESP8266 does not need to know which PC is currently controlling the LED. As long as it is subscribed to the <strong>led<\/strong> topic, the MQTT broker will route the messages automatically.<\/p>\n<p>Before you begin, install VisualGDB 5.2 or later and update your ESP8266 toolchain to the latest version. Then install the <a href=\"https:\/\/mosquitto.org\/\">mosquitto<\/a> broker on your Raspberry Pi (or any other Linux machine) and ensure it is running:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/mosquitto.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2473\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/mosquitto.png\" alt=\"mosquitto\" width=\"806\" height=\"293\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/mosquitto.png 806w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/mosquitto-300x109.png 300w\" sizes=\"(max-width: 806px) 100vw, 806px\" \/><\/a>Now we can begin with the main part of the tutorial.<\/p>\n<ol>\n<li>Start Visual Studio and open the VisualGDB Embedded Project Wizard:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/01-prjname.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2454\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/01-prjname.png\" alt=\"01-prjname\" width=\"786\" height=\"509\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/01-prjname.png 786w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/01-prjname-300x194.png 300w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/a><\/li>\n<li>Select &#8220;Create a new project -&gt; MSBuild&#8221;:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/02-msb.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2455\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/02-msb.png\" alt=\"02-msb\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/02-msb.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/02-msb-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>Select the ESP8266 toolchain and proceed with the regular ESP8266 device:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/03-device.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2456\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/03-device.png\" alt=\"03-device\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/03-device.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/03-device-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>The MQTT is currently supported by the IoT SDK (a.k.a. NONOS SDK) only, so select the HTTP Server (IoT SDK) sample:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/04-httpserver.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2457\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/04-httpserver.png\" alt=\"04-httpserver\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/04-httpserver.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/04-httpserver-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>On the Debug Method page select OpenOCD, connect your JTAG programmer to ESP8266, pick your programming interface and click &#8220;Test settings&#8221; to automatically check the connection:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/05-debug.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2458\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/05-debug.png\" alt=\"05-debug\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/05-debug.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/05-debug-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>Once the JTAG connection is tested, press &#8220;Finish&#8221; to generate the project: <a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/06-test.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2459\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/06-test.png\" alt=\"06-test\" width=\"738\" height=\"565\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/06-test.png 738w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/06-test-300x230.png 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/a><\/li>\n<li>Open VisualGDB Project Properties for the newly generated project and add a reference to the MQTT client. This will automatically add a reference to an <a href=\"https:\/\/github.com\/tuanpmt\/esp_mqtt\">open-source esp_mqtt library<\/a>:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/07-mqttlib.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2460\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/07-mqttlib.png\" alt=\"07-mqttlib\" width=\"969\" height=\"625\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/07-mqttlib.png 969w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/07-mqttlib-300x193.png 300w\" sizes=\"(max-width: 969px) 100vw, 969px\" \/><\/a><\/li>\n<li>Replace the contents of your main source file with the following:\n<pre class=\"\">extern \"C\"\r\n{\r\n\u00a0\u00a0 \u00a0#include \"ets_sys.h\"\r\n\u00a0\u00a0 \u00a0#include \"osapi.h\"\r\n\u00a0\u00a0 \u00a0#include \"mqtt.h\"\r\n\u00a0\u00a0 \u00a0#include \"wifi.h\"\r\n\u00a0\u00a0 \u00a0#include \"debug.h\"\r\n\u00a0\u00a0 \u00a0#include \"gpio.h\"\r\n\u00a0\u00a0 \u00a0#include \"user_interface.h\"\r\n\u00a0\u00a0 \u00a0#include \"mem.h\"\r\n\u00a0\u00a0 \u00a0#include \"espmissingincludes.h\"\r\n\u00a0\u00a0 \u00a0#include \"gpio.h\"\r\n}\r\n\r\nstatic MQTT_Client s_MQTTClient;\r\n\r\nstatic os_timer_t s_Timer;\r\n\r\nextern \"C\" void ICACHE_FLASH_ATTR user_init(void)\r\n{\r\n\u00a0\u00a0 \u00a0#error TODO: enter Raspberry Pi address below\r\n\u00a0\u00a0\u00a0 MQTT_InitConnection(&amp;s_MQTTClient, (uint8_t*)\"&lt;YOUR RASPBERRY PI IP ADDRESS&gt;\", 1883, 0);\r\n\u00a0\u00a0\u00a0 if (!MQTT_InitClient(&amp;s_MQTTClient, (uint8_t*)\"client\", 0, 0, 100, 0))\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 os_printf(\"Failed to initialize properly. Check MQTT version.\\r\\n\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return;\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 MQTT_InitLWT(&amp;s_MQTTClient, (uint8_t*)\"\/lwt\", (uint8_t*)\"offline\", 0, 0);\r\n\u00a0\u00a0\u00a0 MQTT_OnConnected(&amp;s_MQTTClient, mqttConnectedCb);\r\n\u00a0\u00a0\u00a0 MQTT_OnDisconnected(&amp;s_MQTTClient, mqttDisconnectedCb);\r\n\u00a0\u00a0\u00a0 MQTT_OnPublished(&amp;s_MQTTClient, mqttPublishedCb);\r\n\u00a0\u00a0\u00a0 MQTT_OnData(&amp;s_MQTTClient, mqttDataCb);\r\n\r\n\u00a0\u00a0 \u00a0#error TODO: enter WiFi network settings below\r\n\u00a0\u00a0\u00a0 WIFI_Connect((uint8_t*)\"&lt;YOUR WIFI NETWORK NAME&gt;\", (uint8_t*)\"&lt;YOUR WIFI NETWORK PASSWORD&gt;\", wifiConnectCb);\r\n\u00a0\u00a0\u00a0 os_timer_setfn(&amp;s_Timer, TimerFunction, NULL);\r\n\u00a0\u00a0\u00a0 os_timer_arm(&amp;s_Timer, 100 , 1);\r\n}<\/pre>\n<p>Do not forget to substitute the IP address of your Raspberry Pi and your Wi-Fi network settings to the lines marked with #error.<\/li>\n<li>Add a function that will start the MQTT connection once the Wi-Fi connection is established:\n<pre class=\"\">static void wifiConnectCb(uint8_t status)\r\n{\r\n\u00a0\u00a0\u00a0 if (status == STATION_GOT_IP) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 MQTT_Connect(&amp;s_MQTTClient);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 else {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 MQTT_Disconnect(&amp;s_MQTTClient);\r\n\u00a0\u00a0\u00a0 }\r\n}<\/pre>\n<\/li>\n<li>Add <strong>mqttConnectedCb()<\/strong> function that will subscribe to the &#8220;led&#8221; topic and a two other callbacks for other MQTT events:\n<pre class=\"\">static void mqttConnectedCb(uint32_t *args)\r\n{\r\n\u00a0\u00a0\u00a0 MQTT_Client* client = (MQTT_Client*)args;\r\n\u00a0\u00a0\u00a0 os_printf(\"MQTT: Connected\\r\\n\");\r\n\u00a0\u00a0\u00a0 MQTT_Subscribe(client, \"led\", 0);\r\n}\r\n\r\nstatic void ICACHE_FLASH_ATTR mqttDisconnectedCb(uint32_t *args)\r\n{\r\n\u00a0\u00a0\u00a0 MQTT_Client* client = (MQTT_Client*)args;\r\n\u00a0\u00a0\u00a0 os_printf(\"MQTT: Disconnected\\r\\n\");\r\n}\r\n\r\nstatic void ICACHE_FLASH_ATTR mqttPublishedCb(uint32_t *args)\r\n{\r\n\u00a0\u00a0\u00a0 MQTT_Client* client = (MQTT_Client*)args;\r\n\u00a0\u00a0\u00a0 os_printf(\"MQTT: Published\\r\\n\");\r\n}<\/pre>\n<\/li>\n<li>Now add a function that will turn the LED on and off when someone posts to the &#8220;led&#8221; topic:\n<pre class=\"\">static void mqttDataCb(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t data_len)\r\n{\r\n\u00a0\u00a0\u00a0 static bool initialized;\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 if (!initialized)\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 initialized = true;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 gpio_init();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_GPIO1);\r\n\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0\u00a0 if (topic_len == 3 &amp;&amp; !memcmp(topic, \"led\", topic_len))\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (data_len &gt;= 1 &amp;&amp; data[0])\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 gpio_output_set(0, BIT1, BIT1, 0);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 else\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 gpio_output_set(BIT1, 0, BIT1, 0);\r\n\u00a0\u00a0\u00a0 }\r\n}<\/pre>\n<\/li>\n<li>We will also add a small function that will periodically poll the GPIO inputs of the ESP8266 and send them to the &#8220;inputs&#8221; topic so that the PC can check whether the on-board button is pressed:\n<pre class=\"\">void TimerFunction(void *arg)\r\n{\r\n\u00a0\u00a0\u00a0 unsigned gpioState = gpio_input_get();\r\n\u00a0\u00a0\u00a0 MQTT_Publish(&amp;s_MQTTClient, \"inputs\", (char *)&amp;gpioState, 4, 2, 0);\r\n}<\/pre>\n<\/li>\n<li>Try building the code. You should get an error that the INFO() function is not found. This happens because the ESP8266 MQTT library expects your code to define it as a macro in user_config.h:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/08-info.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2461\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/08-info.png\" alt=\"08-info\" width=\"977\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/08-info.png 977w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/08-info-300x198.png 300w\" sizes=\"(max-width: 977px) 100vw, 977px\" \/><\/a><\/li>\n<li>Add the macro so that it calls os_printf(). Now you will get the &#8220;region iram1_0_seg overflowed&#8221; message:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/09-overflow.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2462\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/09-overflow.png\" alt=\"09-overflow\" width=\"977\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/09-overflow.png 977w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/09-overflow-300x198.png 300w\" sizes=\"(max-width: 977px) 100vw, 977px\" \/><\/a><\/li>\n<li>This happens because too much code got placed into RAM. Open VisualGDB Project Properties and configure the ICACHE_FLASH_ATTR macro to actually place code into FLASH:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/10-flash.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2463\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/10-flash.png\" alt=\"10-flash\" width=\"1033\" height=\"722\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/10-flash.png 1033w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/10-flash-300x210.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/10-flash-1024x716.png 1024w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/10-flash-130x90.png 130w\" sizes=\"(max-width: 1033px) 100vw, 1033px\" \/><\/a><\/li>\n<li>Now the build should succeed:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/11-build.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2464\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/11-build.png\" alt=\"11-build\" width=\"977\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/11-build.png 977w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/11-build-300x198.png 300w\" sizes=\"(max-width: 977px) 100vw, 977px\" \/><\/a><\/li>\n<li>If you are using the Custom edition of VisualGDB or higher and your ESP8266 UART port is connected to a COM-to-USB adapter, you can configure the Raw Terminal in VisualGDB Project Properties to connect to it and display the ESP8266 debug output inside Visual Studio:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/terminal.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2474\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/terminal.png\" alt=\"terminal\" width=\"1159\" height=\"736\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/terminal.png 1159w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/terminal-300x191.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/terminal-1024x650.png 1024w\" sizes=\"(max-width: 1159px) 100vw, 1159px\" \/><\/a><\/li>\n<li>Press F5 to start debugging. VisualGDB will automatically program the SPI FLASH and start debugging:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/12-start.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2465\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/12-start.png\" alt=\"12-start\" width=\"977\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/12-start.png 977w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/12-start-300x198.png 300w\" sizes=\"(max-width: 977px) 100vw, 977px\" \/><\/a><\/li>\n<li>Verify that the ESP8266 joins your Wi-Fi network and connects to the MQTT broker. The UART output should contain something similar to this:\n<pre class=\"\">rf cal sector: 120\r\nrf[112] : 03\r\nrf[113] : 00\r\nrf[114] : 01\r\n\r\nSDK ver: 2.0.0(656edbf) compiled @ Jul 19 2016 17:58:40\r\nphy ver: 1055, pp ver: 10.2\r\n\r\nWIFI_INIT\r\nmode : sta(18:fe:34:a2:06:eb)\r\nadd if0\r\nf r0, STATION_IDLE\r\nSTATION_IDLE\r\nSTATION_IDLE\r\nscandone\r\nstate: 0 -&gt; 2 (b0)\r\nSTATION_IDLE\r\nstate: 2 -&gt; 3 (0)\r\nstate: 3 -&gt; 5 (10)\r\nadd 0\r\naid 4\r\ncnt \r\n\r\nconnected with &lt;SSID&gt;, channel 2\r\ndhcp client start...\r\nSTATION_IDLE\r\nSTATION_IDLE\r\nSTATION_IDLE\r\nSTATION_IDLE\r\nSTATION_IDLE\r\nSTATION_IDLE\r\nSTATION_IDLE\r\nip:192.168.0.223,mask:255.255.255.0,gw:192.168.0.1\r\nMQTT: Connected\r\nMQTT: Published\r\nMQTT: Published<\/pre>\n<\/li>\n<li>Now we will create a very basic Windows MQTT client in C#. Open another instance of Visual Studio and create a Windows Forms application:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/13-client.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2466\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/13-client.png\" alt=\"13-client\" width=\"803\" height=\"558\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/13-client.png 803w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/13-client-300x208.png 300w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/13-client-392x272.png 392w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/13-client-130x90.png 130w\" sizes=\"(max-width: 803px) 100vw, 803px\" \/><\/a><\/li>\n<li>Open View-&gt;Other Windows-&gt;Package Manager and type the following command there:\n<pre class=\"\">Install-Package M2Mqtt<\/pre>\n<p>This will automatically download the C# MQTT client package and reference it from your application:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/14-nuget.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2467\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/14-nuget.png\" alt=\"14-nuget\" width=\"977\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/14-nuget.png 977w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/14-nuget-300x198.png 300w\" sizes=\"(max-width: 977px) 100vw, 977px\" \/><\/a><\/li>\n<li>Place a checkbox and a panel on your form. You can change the <strong>Appearance<\/strong> parameter of the checkbox to make it look like a button:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/15-controls.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2468\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/15-controls.png\" alt=\"15-controls\" width=\"977\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/15-controls.png 977w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/15-controls-300x198.png 300w\" sizes=\"(max-width: 977px) 100vw, 977px\" \/><\/a><\/li>\n<li>Replace contents of Form1.cs with the following code:\n<pre class=\"\">using System;\r\nusing System.Collections.Generic;\r\nusing System.ComponentModel;\r\nusing System.Data;\r\nusing System.Drawing;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Threading;\r\nusing System.Threading.Tasks;\r\nusing System.Windows.Forms;\r\nusing uPLibrary.Networking.M2Mqtt;\r\nusing uPLibrary.Networking.M2Mqtt.Messages;\r\n\r\nnamespace MQTTClient\r\n{\r\n\u00a0\u00a0\u00a0 public partial class Form1 : Form\r\n\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private MqttClient _Client;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 public Form1()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 InitializeComponent();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 _Client = new MqttClient(\"raspberrypi\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 _Client.Connect(\"C#Client\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 _Client.Subscribe(new[] { \"inputs\" }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 _Client.MqttMsgPublishReceived += _Client_MqttMsgPublishReceived;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private void _Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 BeginInvoke(new Action(() =&gt;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 uint inputs = BitConverter.ToUInt32(e.Message, 0);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if ((inputs &amp; 1) == 0)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 panel1.BackColor = Color.Green;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 else\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 panel1.BackColor = Color.Transparent;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }));\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 private void checkBox1_CheckedChanged(object sender, EventArgs e)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 _Client.Publish(\"led\", new [] { checkBox1.Checked ? (byte)1 : (byte)0 });\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 }\r\n}<\/pre>\n<p>Do not forget to assign <strong>checkBox1_CheckedChanged()<\/strong> as a CheckedChanged handler for the checkbox so that the client can actually publish the messages to <strong>led<\/strong> when you click it.<\/li>\n<li>Start your C# program and try pressing the button on the ESP8266 board. The panel should change its color to green when the button is pressed:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/16-window.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2469\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/16-window.png\" alt=\"16-window\" width=\"286\" height=\"293\" \/><\/a><\/li>\n<li>You can see what is going on by placing a breakpoint inside the _Client_MqttMsgPublishReceived() method. The <strong>inputs<\/strong> variable will hold the GPIO inputs sampled by the ESP8266 board and reported via the <strong>inputs<\/strong> channel:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/17-inputs.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2470\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/17-inputs.png\" alt=\"17-inputs\" width=\"977\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/17-inputs.png 977w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/17-inputs-300x198.png 300w\" sizes=\"(max-width: 977px) 100vw, 977px\" \/><\/a><\/li>\n<li>Try clicking at the checkbox and observe the ESP8266 LED. See how it turns on and off. You can set a breakpoint in the <strong>mqttDataCb()<\/strong> function to see how the message gets delivered:<a href=\"http:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/18-led.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2471\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/18-led.png\" alt=\"18-led\" width=\"977\" height=\"646\" srcset=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/18-led.png 977w, https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2016\/12\/18-led-300x198.png 300w\" sizes=\"(max-width: 977px) 100vw, 977px\" \/><\/a>Warning: the LED and the UART TX signal share the same pin. Turning the LED on or off will disrupt the debug output via UART.<\/li>\n<\/ol>\n<p>The esp_mqtt library shown in this tutorial only works with the ESP8266 IoT SDK and is not compatible with the FreeRTOS SDK. If you want to use MQTT in a FreeRTOS-based application, you can try porting the <a href=\"https:\/\/eclipse.org\/paho\/clients\/c\/embedded\/\">official MQTT client library for embedded devices<\/a> that supports FreeRTOS, but does not yet have an ESP8266-specific port.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use the MQTT protocol to remotely control the LED on the ESP8266 board and to<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[142],"tags":[100,56,139],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2453"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=2453"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2453\/revisions"}],"predecessor-version":[{"id":2475,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/2453\/revisions\/2475"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=2453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=2453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=2453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}