Publish and subscribe to the same topic(temp) from esp32 (device registered on iot-core)

#1

If you are asking a question, please follow this template:

  1. My goal is: [publish mqtt Temp from esp32 to google iot core ,and then test if I can read back that value by subscribing to the same topic…later want to publish temp from esp32-no1 and then read it by subscribing to the same mqtt topic on esp32-no2]
  2. My actions are: [publishing temp over mqtt using the following js code // This function reads data from the DHT sensor every 2 second
    Timer.set(3000 /* milliseconds */, Timer.REPEAT, function() {

let msg = JSON.stringify({Temperature: dht.getTemp(), Humidity: dht.getHumidity()});
MQTT.pub(esp32_topic, msg, 1);
print(‘Temperature:’, dht.getTemp(), ‘*C’);
print(‘Humidity:’, dht.getHumidity(), ‘%’);
}, null);
3. The result I see is: [it is getting published and I can read the humidity and temperature value on google iot core]]
4. My expectation & question is: [Now I want to subscribe to this topic esp32_topic and pull out the temp and print the temp value on the serial port of the mos tool …I tried the following code MQTT.sub(esp32_topic, function(conn, esp32_topic, msg) {
print(‘Topic:’, esp32_topic, ‘message:’, msg);
let obj = JSON.parse(msg) || {Humidity: 0};
print(‘Humidity data from mqtt:’, obj.Humidity, ‘%’);
}, null);…it does not work]

#2

Google’s MQTT “broker” is not a standard broker, it has a proprietary function and it does not allow you to do that. The ‘events’ topic is one way device --> cloud and all content is published to Pub/Sub, you can’t subscribe to an event or state topic, only to config and commands topics.
You can do that with AWS, but not with Google. AWS broker is closer to standard, GCP’s is a protocol bridge

PS: Unless they’ve changed it from when I studied it, in mid 2020.