从 mqtt 打印后保持值 [英] Holding value after printing from mqtt

查看:97
本文介绍了从 mqtt 打印后保持值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题要问你们所有人,打印后是否可以在内存中保存一个值.我给你举个例子:

I have a question for all of you out there is it possible to hold a value in memory after printing it. I will give you an example:

Nodered 创建的界面有一个滑块,上面有一个数字,比如从 2​​0 到 30 的滑块.你滑动它并选择一个像 25 这样的值,然后该值被发送回 arduino 或 esp 串行监视器,有一个命令可以创建一个那个数字 25 的变量.好吧,这一切都很好,但我知道当你发送这个数字时,它会触发变量 1 次.而且我需要一个可以在使用时记住该值的函数

Nodered created interface there is a slider with numbers like slider from 20 to 30. you slide it and choose a value like 25 then that value is send back to arduino or esp serial monitor, there is a command that you can create a variable for that number 25. okay that all good but I understand that when you send the number it triggers the variable 1 time. And I need a function that can remember the value when its time to use it

我正在建造一个温室(多么原始),我想无线使用它,比如在温度为 22 摄氏度时设置植物浇水,光照百分比为 10%,时间为 19:00 到 22:00给它浇水

I am building a greenhouse (how original) and I want to use it wireless, like set the watering of the plants when the temperature is at 22 C, light percentage at 10% and time from 19:00 to 22:00 to water it

问题是我如何使用 nodered 中的滑块设置的变量在设置的确切温度下给我的植物浇水

And the question is how can I use the variable that was set by the slider in nodered to water my plants at that exact temperature which was set

这是我将在 mqttFloodInterval 末尾使用的代码我可以设置我将从 nodered 获取的变量

this is the code I will be using at the end of mqttFloodInterval I can set what variable I'm gonna get from nodered

void mqttCallback(char* topic, byte* message, unsigned int length) {
  Serial.print("MQTT message received on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;
  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();
  Serial.println(messageTemp);
  if (String(topic) == mqttFloodInterval) {
    Serial.print("*** (Flood Interval Received)");
  }
  
}

对 ocrdu 进行如下

edit for ocrdu something like this:

if (String(topic) == mqttFloodInterval) {
    Serial.print("*** (Flood Interval Received)");
   mqttFloodInterval = wtrtemp;
  }
   if(wtrtemp == 23){
 digitalWrite(RELAY_PIN, HIGH); // turn on pump 5 seconds
     delay(5000);
  } 
     else {
     
       digitalWrite(RELAY_PIN, LOW);  // turn off pump 5 seconds
      delay(5000);
  }

然后做所有的 if 就像 if(wtrtemp == 24), if(wtrtemp == 25).... 等等

And do all the if's like if(wtrtemp == 24), if(wtrtemp == 25).... and so on

推荐答案

我还是没有完全理解你在这里的目标是什么,所以我在下面的代码中给了你两个选项.如果您从 //*option 1 中选择代码片段,您将能够一次将一个温度存储到 esp32 的全局内存中,这是最简单的,所以如果可行,请选择这个选项.

Yet again i did not fully understand what exactly your goal was here so i gave you two options in the code below. if you choose the code snippets from //*option 1 you will simply be able to store one temperature at a time to the global memory of your esp32, this is the simplest, so if this works pick this option.

//*option 2 是一个温度值列表,用于存储在全局内存中,因此如果您希望使用大量温度以供将来参考,这将是您的选择.>

//*option 2 is instead a list of temperature values for storing in the global memory, so if you wish to use alot of temperature for future reference, this will be your choice.

//----Two options choose one of them you prefer----//
//----whichever option you choose use the same option for all the code below----//

//*option 1
//store a single temperature value to a global variable
int global_memory_single_temperature = -1; //-1 not defined yet

//*option 2
//if you want to store multiple values, lets say 500 values use this:
int global_memory_multiple_temperature[500];
int current_temperatureValue = 0;


void setup()
{
  Serial.begin(115200);
}
void loop()
{

  //this happens some time in the future after you have recieved a value
  if (String(topic) == mqttFloodInterval)
  {
    Serial.print("*** (Flood Interval Received)");

    //----Two options choose one of them you prefer----//

    //*option 1
    //store a single wtrtemp to a global variable
    global_memory_single_temperature = wtrtemp;

    //*option 2
    //if you would like to store alot of temperatures for later use:
    global_memory_multiple_temperature[current_temperatureValue++];

    mqttFloodInterval = wtrtemp;
  }

  //Below can happen once your timer? i think hits the goal timer..

  //*if you choose option 1:
  //Later to check what the wtrtemp was use this:
  if (global_memory_single_temperature == 23)
  {
    digitalWrite(RELAY_PIN, HIGH); // turn on pump 5 seconds
    delay(5000);
  }
  else
  {

    digitalWrite(RELAY_PIN, LOW); // turn off pump 5 seconds
    delay(5000);
  }

  //*if you choose option 2:
  //or if you want to use multiple temperatures that are stored use this:
  for (int i = 0; i < current_temperatureValue; i++)
  {
    //this will loop through all previous stored temperatures and do the stuff below
    int currentWtrTempValue = global_memory_multiple_temperature[i];
    if (currentWtrTempValue == 23)
    {
      digitalWrite(RELAY_PIN, HIGH); // turn on pump 5 seconds
      delay(5000);
    }
    else
    {

      digitalWrite(RELAY_PIN, LOW); // turn off pump 5 seconds
      delay(5000);
    }
  }
}

-顺便说一句,我希望你已经在草图的 setup() 部分有了这个部分.

-Btw, i hope you already have this section in your setup() part of the sketch.

void setup() {
  pinMode(RELAY_PIN, OUTPUT);    // sets the relay pin available for digitalWrites...
}

这篇关于从 mqtt 打印后保持值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆