0

publish text to a stream block
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

publish text to a stream block

by chickenshack on Tue Nov 13, 2018 11:57 pm

Where can I find some documentation with examples on how to publish to a String to stream block. I am using a lolin d32 and the Adafruit MQTT example to connect to my adafruit.io. I have a stream block on my dashboard, but I cannot figure out how to pass it a text string from my D32. Any help would be great.

Code: Select all | TOGGLE FULL SIZE
Adafruit_MQTT_Publish cooptemp = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/cooptemp");


void checkTemp() {

  int curHumid = dht.readHumidity();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  int curTemp = dht.readTemperature(true);


  Serial.print("Humidity: ");
  Serial.println(curHumid);

  Serial.print("Temperature: ");
  Serial.println(curTemp);

  String tempString = String(curTemp);
  String humidString = String(curHumid);
 
  String tempText = "Temperature: "+ tempString;
  String humidText = "Humidity: "+ humidString;

  Serial.println(tempText);
  Serial.println(humidText);

  //The 2 lines below break the code
  cooptemp.publish(tempText);
  cooptemp.publish(humidText);
 
}


chickenshack
 
Posts: 1
Joined: Thu Oct 11, 2018 12:32 pm

Re: publish text to a stream block

by brubell on Wed Nov 14, 2018 11:28 am

Our Adafruit IO Arduino Library (https://github.com/adafruit/Adafruit_IO_Arduino) has helpers for this, but the MQTT library does not.

Personally, I don't like sending strings (or large amounts of data) to Adafruit IO when there are other (easier) ways of displaying that data on a Dashboard. I like having the dashboard "do the heavy lifting", not the hardware.

For example:
Instead of typecasting the temperature value to a string:
Code: Select all | TOGGLE FULL SIZE
  String tempString = String(curTemp);
  String humidString = String(curHumid);


You could make use of dashboard elements to set up a gauge block which displays the temperature nicely. Or a text block to hold the temperature value (as an int or float) and append a label (and unit) to identify it.
Attachments
IO_-_Environmental_Monitor.png
IO_-_Environmental_Monitor.png (26.82 KiB) Viewed 48 times

brubell
 
Posts: 189
Joined: Fri Jul 17, 2015 10:33 pm

Please be positive and constructive with your questions and comments.