# Set and Get Variables & Time from Arduino Projects to Node-Red

**URL:** https://discourse.nodered.org/t/set-and-get-variables-time-from-arduino-projects-to-node-red/94083
**Category:** Share Your Projects
**Tags:** function-node, mqtt
**Created:** [21 December 2024 14:46 UTC](https://discourse.nodered.org/t/set-and-get-variables-time-from-arduino-projects-to-node-red/94083 "2024-12-21T14:46:10Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ajaybnl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ajaybnl/32/97442_2.png) [@ajaybnl](https://discourse.nodered.org/u/ajaybnl)
#### Post date: [21 December 2024 14:46 UTC](https://discourse.nodered.org/t/set-and-get-variables-time-from-arduino-projects-to-node-red/94083/1 "2024-12-21T14:46:10Z")

</div>

You can save variables in Node-red Flow/Storage Data using Arduino based boards like ESP8266 , ESP32, and so on.

**Concept:**

Send message to node/variable to store  
publish to TOPIC + /$get to get stored data

**Example:**

To Set data, send mqtt messages like : Topic: "_home/sensor1/variable1_" -\> Payload: "_1_"  
To Get data, subscribe to "_home/sensor1/variable1_"  
and publish message "_home/sensor1/variable1/$get_" -\> Payload : " **ANY**"  
The data will come in subscribed topic.

You can also get TIMESTAMP using same function just replace "variable1" with "time" using same method written above.

add a mqtt in (with your sensor root topic like "home/sensor1/#") and mqtt out (without topic) to output

**Note:**  
The Time is in IST. To convert to Your Host Time Remove This Part : " _+ IST\_offset;_"

**The Function Node Code :**

```auto

var topic = msg.topic;
var payload = msg.payload;
var isget = false;

//IS GET REQUEST    
if (topic.endsWith("/$get")) {
    topic = topic.replace("/$get", "");
    isget = true;
}

//Extract sensor_var
var topicParts = topic.split('/');
var nodename = topicParts[1];

var command = topicParts[topicParts.length - 1];
var variablename = nodename + "_" + command;

//GET REQUEST
if (isget) {

    if (topic.endsWith("/time")) {
        var date = new Date();
        var IST_offset = 5.5 * 60 * 60 * 1000;
        var IST_timestamp_ms = date.getTime() + IST_offset;
        var IST_timestamp_sec = Math.floor(IST_timestamp_ms / 1000); // Convert to seconds (32-bit Unix timestamp)
        msg.topic = topic;
        msg.payload = IST_timestamp_sec;
        return msg;
    } else if (topic.endsWith("status") || topic.startsWith("reboot")) {
        //STATUS OR REBOOT
        //IGNORE
    } else {
        //TIME

        // ALL TOPICS
        let da = flow.get(variablename);
        if (da == undefined) da = 0;
        msg.topic = topic;
        msg.payload = da;
        return msg;

    }

} else {

    //SAVE
    if (topic.endsWith("status") || topic.endsWith("/time") || topic.startsWith("reboot")) {
    //STATUS TIME OR REBOOT
    //IGNORE
    } else {
    //ALL OTHER
        flow.set(variablename, payload);
    }
}

```

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [21 December 2024 14:52 UTC](https://discourse.nodered.org/t/set-and-get-variables-time-from-arduino-projects-to-node-red/94083/2 "2024-12-21T14:52:39Z")

</div>

@ajaybnl

Please don't use tags, that are not associated to your content.

`node-red-dashboard` removed

Thank you

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [21 December 2024 16:11 UTC](https://discourse.nodered.org/t/set-and-get-variables-time-from-arduino-projects-to-node-red/94083/3 "2024-12-21T16:11:13Z")

</div>

Thanks for sharing that @ajaybnl

I am confused which end of the Arduino - Node-red link is sending which message and when.

Can you share a screen capture which includes your mqtt-in and mqtt-out nodes to make it clearer?

---

<div class="post-metadata">

### Author: ![ajaybnl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ajaybnl/32/97442_2.png) [@ajaybnl](https://discourse.nodered.org/u/ajaybnl)
#### Post date: [23 December 2024 04:38 UTC](https://discourse.nodered.org/t/set-and-get-variables-time-from-arduino-projects-to-node-red/94083/4 "2024-12-23T04:38:43Z")

</div>

**From Node-red**

 ![Clipboard_12-23-2024_01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/f/afa1a567f353c995563965fa9baa80002b35c689.jpeg)

**From ESP8266 Arduino**  
Using Library EspMqttClient

**Publish Get Variable from Node-Red**

_Timer for incremental responsemode after 2 seconds (to get a window for receiving a mqtt message)_

```auto

  if ((((millis() - timer3))) >= 2000)
    {
      timer3 = millis();

      if (client.isConnected())
      {

        if (responsemode == 0)
        {

        // Get variable1 from Node-Red
          client.publish("home/sensor/variable1/$get", "0");

        //to increment the queue        
         responsemode++;

        }
        else if (responsemode == 1)
        {

        // Get Time from Node-Red
         client.publish("home/sensor/time/$get", "0");

          responsemode++;
        }
      }
}

```

**Subscribing Mqtt code**

```auto

  client.subscribe("home/sensor/variable1", [](const String &payload){
    
    if (payload.length() > 0) {

      int val = payload.toInt();

     //Global variable
      variable1 = val;

    
    } });

  //----------
  // Time

  client.subscribe("home/sensor/time", [](const String &payload){

      if (payload.length() > 0) {
   
        unsigned long timestamp = payload.toInt();

        //TimeLib (Set Timestamp)

        setTime(timestamp);
       
      } });
}

```
