Send data from blynk to arduino (SOLVED)

Good day everyone!
I want to try something out with my project. Every hour my arduino sends a different value to my blynk app I’ve already set the values for each hour (1st hour = 0.4 , 2nd = 5.4, 3rd = 12). Now what I want is that I want to use blynk to change the hour on my arduino so let’s say its the first hour then if I send #3 then the values will change from 0.4 to 12? is it possible? thanks in advance

Yes, but without seeing how you’ve written your current code it’s impossible to advise about the best way of achieving this.

Pete.

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL6MvOvz2Pt"
#define BLYNK_TEMPLATE_NAME "MQ137 AMMONIA BEFORE"
#define BLYNK_AUTH_TOKEN "R-UxbpqFfYkc-L8N5pufRClhz9lBCNoj"
#include <Wire.h>
#include <BlynkSimpleWifi.h>

char ssid[] = "213";
char pass[] = "123456789";
BlynkTimer timer;


const int RELAY1_PIN = 4;
const int RELAY2_PIN = 5;
const int RELAY3_PIN = 6;

double rdm = 0.00;

void setup() {
  Serial.begin(9600);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  timer.setInterval(1000L, sendData);

  pinMode(RELAY1_PIN, OUTPUT);
  pinMode(RELAY2_PIN, OUTPUT);
  pinMode(RELAY3_PIN, OUTPUT);

  Serial.println("Initialization completed.");
}

void sendData() {
  static int currentHour = 0; // Initialize current hour

  // Check if any input is available from the serial monitor
  if (Serial.available() > 0) {
    int selectedHour = Serial.parseInt();
    if (selectedHour >= 0 && selectedHour <= 9) { // Corrected range
      currentHour = selectedHour;
      Serial.print("Changing to hour ");
      Serial.println(currentHour);
    } else {
      Serial.println("Invalid hour selection. Please enter a number between 0 and 9."); // Corrected range
    }
    // Consume any remaining characters in the serial buffer
    while (Serial.available() > 0) {
      Serial.read();
    }
  }

  generateAmmoniaPPM(currentHour); // Generate ammonia ppm based on current hour
  Blynk.virtualWrite(V0, rdm);
  Serial.print("Sending data for hour ");
  Serial.println(currentHour);
  Serial.print("rdm: ");
  Serial.println(rdm);
  controlRelays(); // Control relays based on ammonia ppm value
}

void loop() {
  Blynk.run();
  timer.run();
}

void generateAmmoniaPPM(int hour) {
  double lower_bound = 0.0;
  double upper_bound = 0.0;

  // Update lower and upper bounds based on the selected hour
  switch (hour) {
    case 0:
      lower_bound = 0.4;
      upper_bound = 0.6;
      break;
    case 1:
      lower_bound = 0.7;
      upper_bound = 0.9;
      break;
    case 2:
      lower_bound = 1.0;
      upper_bound = 1.4;
      break;
    case 3:
      lower_bound = 1.5;
      upper_bound = 1.9;
      break;
    case 4:
      lower_bound = 1.9;
      upper_bound = 2.4;
      break;
    case 5:
      lower_bound = 2.1;
      upper_bound = 2.4;
      break;
    case 6:
      lower_bound = 1.9;
      upper_bound = 2.2;
      break;
    case 7:
      lower_bound = 1.7;
      upper_bound = 1.9;
      break;
    case 8:
      lower_bound = 1.9;
      upper_bound = 2.2;
      break;
    case 9:
      lower_bound = 1.7;
      upper_bound = 1.9;
      break;
  }

  // Generate random ammonia ppm within the specified range
  rdm = random(lower_bound * 100, upper_bound * 100) / 100.0;
}

void controlRelays() {
  // Control relays based on ammonia ppm value
  if (rdm >= 2 && rdm <= 6) {
    digitalWrite(RELAY1_PIN, LOW);
  } else {
    digitalWrite(RELAY1_PIN, HIGH);
  }

  if (rdm> 6 && rdm <= 15) {
    digitalWrite(RELAY2_PIN, LOW);
  } else {
    digitalWrite(RELAY2_PIN, HIGH);
  }

  if (rdm > 15) {
    digitalWrite(RELAY3_PIN, LOW);
  } else {
    digitalWrite(RELAY3_PIN, HIGH);
  }
}

Here is my code sir

So you are currently changing thhe hour via the serial monitor input.
Do you want to retain that capability, or just allow tge hour to be changed via Blynk?

Your use of the term “hour” is somewhat confusing, as it doesn’t seem to be time based at all. It simply seems to be an option in the range 0-9.

Is that correct?

Pete.

What I want is to change the hour through Blynk. I am currently changing the code so that every hour the case will change automatically but I want the blynk app to let me send from 0-9 so that I can change the hour at will, even though its the first hour I’ll send 7 from the blynk app to the arduino to change it to 7th hour.

I still don’t u der stand why these are “hours” when they seem to be unrelated time.

What type of Blynk subscription do you have?

Pete.

I only have free subscription sir. Since you are confused let’s just focus on the fact that I want to change the case through blynk.

With the free subscription your widget options are very limited.
The ideal choice for what you are describing- choosing one of 10 options - would suit the Menu widget very well, but this isn’t available in the free plan.
I guess the Horizontal Slider widget is the best option from the limited choice available with your subscription.

If you took this approach you’d need to set-up a virtual datastream with datatype of Integer and min/max of 0/9. Assuming that this is datastream V1, you’d add a BLYNK_WRITE(V1) function to your code, and this will trigger when your slider value changes.
You can retrieve the slider value with param.asInt() and use this in your switch/case statement.
You’ll need to take care with variable scope, so using global variables may be best.

Pete.

1 Like

Thanks for the advice sir this is a big help for me.

This was the solution, sir. As always thank you for your help. ^^

1 Like