Publishing data is pretty simple, subscriptions are a tad more challenging.
Lets look at another demo sketch this time mqtt_esp8266_2subs
This demo has two subscriptions, no publication (so it has to ping), and shows two ways to parse subscription data
Create New Slider Feed & Dash
To test out this demo, create a new feed called slider and add a slider to your dashboard
Output pins & new subscription
The first difference is we define two pins for LED output (controlled by the on-off button) and PWM output (controlled by the slider
// the on off button feed turns this LED on/off #define LED 2 // the slider feed sets the PWM output of this pin #define PWMOUT 12
We also only set up 2 subs, no pubs
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname> // Setup a feed called 'onoff' for subscribing to changes. Adafruit_MQTT_Subscribe ONOFF_FEED = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/ONOFF_FEED"); Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED); Adafruit_MQTT_Subscribe SLIDER_FEED = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/slider"); Adafruit_MQTT_Subscribe slider = Adafruit_MQTT_Subscribe(&mqtt, SLIDER_FEED);
Don't forget to subscribe to both feeds!
// Setup MQTT subscription for onoff & slider feed. mqtt.subscribe(&onoffbutton); mqtt.subscribe(&slider);
Add Ping()
Since there's no publish's in this code, you will have to ping every few minutes at least. Uncomment the ping code
// ping the server to keep the mqtt connection alive if(! mqtt.ping()) { mqtt.disconnect(); }
New Subscription Check & Parse
The real changes come in the part of the loop that checks the subscriptions
On Off Button
First up, we've updated the On-Off button check. Now it not only prints out the received data but also compares the data to determine whether the string received is ON or OFF
Since adafruit.io publishes the data as a string, using strcmp (string compare) is an easy way to determine whether you got a particular value. Don't forget it returns 0 on success!
Adafruit_MQTT_Subscribe *subscription; while ((subscription = mqtt.readSubscription(5000))) { // Check if its the onoff button feed if (subscription == &onoffbutton) { Serial.print(F("On-Off button: ")); Serial.println((char *)onoffbutton.lastread); if (strcmp((char *)onoffbutton.lastread, "ON") == 0) { digitalWrite(LED, LOW); } if (strcmp((char *)onoffbutton.lastread, "OFF") == 0) { digitalWrite(LED, HIGH); } }
(The digital pin 2 LED is common anode so pull the pin low to turn on)
// check if its the slider feed if (subscription == &slider) { Serial.print(F("Slider: ")); Serial.println((char *)slider.lastread); uint16_t sliderval = atoi((char *)slider.lastread); // convert to a number analogWrite(PWMOUT, sliderval); }
The data also shows up as a string, so we use atoi to convert it from ascii to integer. Then we can save it to sliderval and use that to analog/PWM write to our PWM pin
Page last edited March 08, 2024
Text editor powered by tinymce.