MQTT Remote Control

TipControl devices registered under your username can be controlled remotely using MQTT.  

By publishing a message to the device, you can either:

– Execute raw code directly on the device  

– Run a macro that has already been installed

Directions

To send executable code, publish to the following topic:
<username>.<projectname>.<chipname>/exec

For example:

demo.demo.esp32/exec


Building the Request Payload

Every request body must start with `:{` and end with `}`.  

Inside, you can write either raw code or macro calls.

Raw code


Here’s an example that loops 25 times, generates random RGB values, and sets a NeoPixel LED:

 String payload = ":{ "                              // remember to start the code with :
               "for (i = 0, i < 25, i += 1) { "   // loop 25 times
               "red = lib.math.random(0,255); "    // generate random red value
               "green = lib.math.random(0,255); "  // generate random green value
               "blue = lib.math.random(0,255); "   // generate random blue value
               "drv.neopixel.setLeds(red, green, blue, 1, 0); " // set the neopixel LED colors
               "delay(1000);}; } "; // wait 1 second before next iteration

the receiving device will execute this code immediately.

Run a Macro

Run the macro called Blink installed on the driver called LED

String body = ":{ " // remember to start the code with :
              "drv.LED.Blink();}"// end the line of code with ;
              //need to end with }

the receiving device will find this macro and run the code within immediately

Receive sensor data

You can also instruct the device to publish sensor values.  

For example, if you have an “environment” sensor with a “temperature variable:

Example: if you had an environment sensor called “environment” that had the variable “temperature” then to read that value you will publish the following code to the device. making sure you are subscribed to the device’s publish topic

String body = ":{ " // remember to start the code with :
              "payload = \"{ temperature: \" + drv.environment.var.temperature\" + \"}\";"
              //publish(topic,payload,qos)
              "drv.mqtt.publish(\"demo.sensors/environment\",payload,qos);}"// end the line of code with ;
              //need to end with }


The device will publish the result to the topic, e.g.:
{ temperature: 30 }

Key Points

-Always start with `:{` and end with `}`.  

– End each line of code with a semicolon `;`.  

– Make sure you are subscribed to the device’s publish topic if you expect data back. 

– The device can only send data to a topic defined as <username>.<something>/<something>