BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

How can i change data value without reload code in IDE

esp8266
nonblynkrelated
hardware
#1

hi guys
How can I change the value I declared in define without reloading the code in the IDE. in this case how can i change value of hot_temp and dry_soil without reloading IDE. i tried assign value in virtual pin but it’s not working

#define BLYNK_PRINT Serial    
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
WidgetLED PUMP(V0);  
WidgetLED LAMP(V1); 


#include "DHT.h"

/* TIMER */
#include <SimpleTimer.h>

#define DHTPIN D3     

#define SOIL_MOIST_1_PIN A0 

#define PUMP_ON_BUTTON D0   
#define LAMP_ON_BUTTON D1  
#define SENSORS_READ_BUTTON D4

#define PUMP_PIN D6   //Bom
#define LAMP_PIN D7   //Den

#define DHTTYPE DHT11   // DHT 11

/* Thông số cho chế độ tự động */
#define DRY_SOIL      66
#define WET_SOIL      85
#define COLD_TEMP     12
#define HOT_TEMP      22
#define TIME_PUMP_ON  15
#define TIME_LAMP_ON  15

/* TIMER */
#define READ_BUTTONS_TM   1L  // Tương ứng với giây
#define READ_SOIL_HUM_TM  10L //Đọc cảm biến ẩm đất
#define READ_AIR_DATA_TM  2L  //Đọc DHT
#define DISPLAY_DATA_TM   10L //Gửi dữ liệu lên terminal
#define SEND_UP_DATA_TM   10L //Gửi dữ liệu lên blynk
#define AUTO_CTRL_TM      60L //Chế độ tư động
//Token Blynk và wifi
char auth[] = "936ad7907db74090932822f3*****"; // Blynk token
char ssid[] = "Nokia5.1"; //Tên wifi
char pass[] = "88888888"; //Mật khẩu

// Biến lưu các giá trị cảm biến
float humDHT = 0;
float tempDHT = 0;
//int lumen;
int soilMoist = 0;
// Biến lưu trạng thái bơm
boolean pumpStatus = 0;
boolean lampStatus = 0;

int timePumpOn = 10;
 
long sampleTimingSeconds = 50;
long startTiming = 0;
long elapsedTime = 0;

SimpleTimer timer;

// Khởi tạo cảm biến
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  pinMode(PUMP_PIN, OUTPUT);
  pinMode(LAMP_PIN, OUTPUT);
  pinMode(PUMP_ON_BUTTON, INPUT_PULLUP);
  pinMode(LAMP_ON_BUTTON, INPUT_PULLUP);
  pinMode(SENSORS_READ_BUTTON, INPUT_PULLUP);
  aplyCmd();
  
  Serial.begin(115200);

  dht.begin();   
  Blynk.begin(auth, ssid, pass);
  PUMP.off();
  LAMP.off();
  startTimers();
}

void loop() {
  timer.run(); // 
  Blynk.run();
}
/****************************************************************
* Hàm điều khiển nhận tín hiệu từ blynk
****************************************************************/
BLYNK_WRITE(3) //
{
  int i = param.asInt();
  if (i == 1)
  {
    pumpStatus = !pumpStatus;
    aplyCmd();
  }
}

BLYNK_WRITE(4) //
{
  int i = param.asInt();
  if (i == 1)
  {
    lampStatus = !lampStatus;
    aplyCmd();
  }
}

void getSoilMoist(void)
{
  int i = 0;
  soilMoist = 0;
  for (i = 0; i < 10; i++)  //
  {
    soilMoist += analogRead(SOIL_MOIST_1_PIN); 
    delay(50);   
  }
  soilMoist = soilMoist / (i);
  soilMoist = map(soilMoist, 1023, 0, 100, 0); 

}

void getDhtData(void)
{

  tempDHT = dht.readTemperature();
  humDHT = dht.readHumidity();
  if (isnan(humDHT) || isnan(tempDHT))   
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
}
void printData(void)
{
  // IN thông tin ra màn hình
  Serial.print("Do am: ");
  Serial.print(humDHT);
  Serial.print(" %\t");
  Serial.print("Nhiet do: ");
  Serial.print(tempDHT);
  Serial.print(" *C\t");

  Serial.print(" %\t");
  Serial.print("Do am dat: ");
  Serial.print(soilMoist);
  Serial.println(" %");
}

/****************************************************************

****************************************************************/
void readLocalCmd()
{
  boolean digiValue = debounce(PUMP_ON_BUTTON);
  if (!digiValue)
  {
    pumpStatus = !pumpStatus;
    aplyCmd();
  }

  digiValue = debounce(LAMP_ON_BUTTON);
  if (!digiValue)
  {
    lampStatus = !lampStatus;
    aplyCmd();
  }

  digiValue = debounce(SENSORS_READ_BUTTON);
  if (!digiValue)
  {
    getDhtData();
    getSoilMoist();
    printData();
  }
}
/***************************************************

****************************************************/
void aplyCmd()
{
  if (pumpStatus == 1)
  {
    Blynk.notify("NDTRBOT: Canh bao ==>> BOM ON");
    digitalWrite(PUMP_PIN, LOW);
    PUMP.on();
  }

  else {
    digitalWrite(PUMP_PIN, HIGH);
    PUMP.off();
  }

  if (lampStatus == 1)
  {
    Blynk.notify("NDTRBOT: Canh bao ==>> DEN ON");
    digitalWrite(LAMP_PIN, LOW);
    LAMP.on();
  }
  else
  {
    digitalWrite(LAMP_PIN, HIGH);
    LAMP.off();
  }
}
/***************************************************
  Hàm kiểm tra trạng thái phím bấm
****************************************************/
boolean debounce(int pin)
{
  boolean state;
  boolean previousState;
  const int debounceDelay = 60;

  previousState = digitalRead(pin);
  for (int counter = 0; counter < debounceDelay; counter++)
  {
    delay(1);
    state = digitalRead(pin);
    if (state != previousState)
    {
      counter = 0;
      previousState = state;
    }
  }
  return state;
}
/***************************************************
* Chế độ tự động dựa trên thông số cảm biến
****************************************************/
void autoControlPlantation(void)
{
  if (soilMoist < DRY_SOIL)
  {
    turnPumpOn();
  }

  if (tempDHT < COLD_TEMP)
  {
    turnLampOn();
  }
}
/***************************************************
* Bật bơm trong thời gian định sẵn
****************************************************/
void turnPumpOn()
{
  pumpStatus = 1;
  aplyCmd();
  delay (TIME_PUMP_ON * 1000);
  pumpStatus = 0;
  aplyCmd();
}

/***************************************************
* Bật đèn trong thời gian định sẵn
****************************************************/
void turnLampOn()
{
  lampStatus = 1;
  aplyCmd();
  delay (TIME_LAMP_ON * 1000);
  lampStatus = 0;
  aplyCmd();
}

/***************************************************
  Khởi động Timers
****************************************************/
void startTimers(void)
{
  timer.setInterval(READ_BUTTONS_TM * 1000, readLocalCmd);
  timer.setInterval(READ_AIR_DATA_TM * 1000, getDhtData);
  timer.setInterval(READ_SOIL_HUM_TM * 1000, getSoilMoist);
  timer.setInterval(SEND_UP_DATA_TM * 1000, sendUptime);
  timer.setInterval(AUTO_CTRL_TM * 1000, autoControlPlantation);
  timer.setInterval(DISPLAY_DATA_TM * 1000, printData);
}
/***************************************************
 * Gửi dữ liệu lên Blynk
 **************************************************/
void sendUptime()
{
  Blynk.virtualWrite(10, tempDHT); 
  Blynk.virtualWrite(11, humDHT); 
  Blynk.virtualWrite(12, soilMoist); 
}
0 Likes

#2

Please format code properly so we can read it. Please use 3 back ticks if you can’t find them on your keyboard copy these ``` or google it. Put them before and after your code.

2 Likes

#3

As @daveblynk said.

And in reply to your question, it’s not possible to adjust them as define statements are compiled into the code rather than acting like a variable that is in memory.

0 Likes

#4

If you edit your first post (using the pencil icon at the bottom of the post) and insert the triple backticks as mentioned earlier, then I’ll give you some advice about how you can change the values through Blynk.

Pete.

1 Like

#5

hi Pete

Sorry, this is my first post and I have no experience. I changed so please give me some advice about my problem

0 Likes

#6

Okay.
There are two approaches you could take, and it depends on what you want to do.

Option 1
Use Wi-Fi manager or something similar. This is intended to be used as a way of selecting a local Wi-Fi network and inputting a new password, but you can also input other data like a Blynk Auth code or values for variables. These are stored in non-volatile memory on your device - NVRAM or SPIFFS, depending on your device type - and loaded when you restart your device.
This option is suitable for settings that you don’t want to change very often, such as location specific data and values.

Option 2
Use Blynk to save the values you want. In this option the values are stored on the Blynk server and you would load them from the server once the device has booted-up and connected to Blynk.
You would save the values on virtual pins, and retrieve them using the Blynk.syncVirtual command.
You could create a ‘settings’ tab in your project in the app, and add widgets that allow you to update these values within acceptable ranges.
This option is probably better for variables that you want to adjust more frequently, or that you want to change “on the fly” without rebooting your device and entering the Wi-Fi Manager setup screen.
Of course, if you don’t have a connection to Blynk then your device won’t work, although you could save your last settings to NVRAM/SPIFFS if you wanted to be able to retrieve and use them 8n offline mode.

Pete.

0 Likes

#7

Thanks for your advice. I tried but it has some problems. When I save the value of the virtual pin and change the value on blynk, that value doesn’t change. It returns the value I installed in the first program and I used the Blynk.syncVirtual command, can you give me some examples?
thanks you so much

0 Likes

#8

Demonstrate what you’ve tried, with simple easy to follow details including code, app screenshots, serial output etc and we’ll give you some feedback on what you’re doing wrong.

Pete.

1 Like

#9

I had declared the wrong type, I did it
thanks you a lot Pete

0 Likes