How can a mqtt client subscibe to a topic and save the value, that another client publish to

Hello everyone,

i have a little problem that i can not solve on my own. Any help would be much appriciated.
I have made a weather station (with esp8266) that publishes (mqtt) the luminosity (in lux) in a topic:

char* pub_TopicLight = "Home/Grd/LocalWeatherStation/Light";

as string:

dtostrf(TSL2561SensorValue, 4, 2, msg);
client.publish(pub_TopicLight, msg); 

Now i need a second device (again esp8266, mqtt) to save that value and use it in some way. So if i subscribe to that topic, how can i convert that value to integer and store it? Somethig like:

String LuminosityStr = client.subscribe(TopicLight);
int LuminosityInt = LuminosityStr.toInt();

or schould i send a massege with the value to the second device and after that save the value?

Thank you in advance,

Is the answer
Subscribe to the topic on your second ESP and store the value

By the way,

String LuminosityStr = client.subscribe(TopicLight);
int LuminosityInt = LuminosityStr.toInt();

Is the wrong syntax for subscribing to topics in arduino

 Basic MQTT example
 This sketch demonstrates the basic capabilities of the library.
 It connects to an MQTT server then:
  - publishes "hello world" to the topic "outTopic"
  - subscribes to the topic "inTopic", printing out any messages
    it receives. NB - it assumes the received payloads are strings not binary
 It will reconnect to the server if the connection is lost using a blocking
 reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
 achieve the same result without blocking the main loop.

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// Update these with values suitable for your network.
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(172, 16, 0, 100);
IPAddress server(172, 16, 0, 2);

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print("] ");
  for (int i=0;i<length;i++) {

EthernetClient ethClient;
PubSubClient client(ethClient);

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("arduinoClient")) {
      // Once connected, publish an announcement...
      client.publish("outTopic","hello world");
      // ... and resubscribe
    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying

void setup()

  client.setServer(server, 1883);

  Ethernet.begin(mac, ip);
  // Allow the hardware to sort itself out

void loop()
  if (!client.connected()) {
1 Like

Vincent thank for your reply,

i am struggling on how to send mqtt message from raspberry to esp8266.
As i said, the weather station publishes the luminosity as string:

dtostrf(TSL2561SensorValue, 4, 2, msg);
client.publish(pub_TopicLight, msg);

in an item:

Number LocalWeatherStationLight "Luminosity [%.0f Lux]" <sun> (Grd_LocalForecastStation) {mqtt="<[broker:Home/Grd/LocalWeatherStation/Light:state:default]"}

So inside a rule i have to convert it to integer and then send it to the second device.
I have created a new item for the second device:

Number MiddleStorageRoomLuminosity "Luminosity [%.0f Lux]" <none> (Grd_MiddleStorageRoom) {mqtt=">[broker:Home/Grd/MiddleStorageRoom/Luminosity:state:default]"}

and then a rule that it is NOT working:

rule "Grd Middle Storage Room luminosity"
        Item LocalWeatherStationLight received update
        var Number Luminosity = LocalWeatherStation80Light as Number

Any suggestions or ideas on how to accomplish this would be very helpful.

Number MiddleStorageRoomLuminosity "Luminosity [%.0f Lux]" <none> (Grd_MiddleStorageRoom) {mqtt=">[broker:Home/Grd/MiddleStorageRoom/Luminosity:command:*:default]"}
1 Like

Ok now i have the luminosity value published, so now i have to send it to the esp device. I will try some things and probably will come for more instructions. Thanks!

You dont have to send it TO the second one, the second one has to subscribe to the topic on the same broker as the first one. all clients subscribed to the same topic get the same topic/value. that is the meaning of the mqtt broker

Thank you FyingEaglE for your reply. That was my first thought too (as i describe it in the first half of the first post). But unfortunately i can’t find a way to make it work…

I the arduino code I have given you above you can specify a topic to subcribe to. If you subscribe to that topic, you will get the value.
You can even publish from an ESP to another ESP without having to go through openHAB.
I’ll guide you if you need help