2

We're using the ESP8266 to receive data from the Arduino Mega board, where our sensors are connected. Supposedly, our ESP is connected to the Blynk App. However, the values showing up in our dashboard are 0 but, the app says that the device is online.

The values in the sensors are showing up in our LCD, so we think that the problem is the connection between the ESP and the Arduino.

We've attached our ESP and Arduino Mega code below for anyone's reference.

ESP code:

    #define BLYNK_TEMPLATE_ID           "ID"
    #define BLYNK_TEMPLATE_NAME         "CROPBOX"
    #define BLYNK_AUTH_TOKEN            "TOKEN"

    /* Comment this out to disable prints and save space */
    #define BLYNK_PRINT Serial

    #include <ESP8266WiFi.h>
    #include <BlynkSimpleEsp8266.h>
    #include <SoftwareSerial.h>

    // Your WiFi credentials.
    // Set password to "" for open networks.
    char ssid[] = "WiFi_Name";
    char pass[] = "WiFi_PW";

    // This function is called every time the device is connected to the Blynk.Cloud

    SoftwareSerial swSerial(V5, V6);  // RX, TX

    void setup()
    {

    // Debug console
    Serial.begin(9600);
    Serial.println("ESP8266 standalone test");
    swSerial.begin(9600);
    Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
    Serial.print("connecting to WiFi");
    Serial.print(ssid);
    WiFi.begin(ssid, pass);
    Serial.println();
    Serial.print("Connecting");
    while(WiFi.status() != WL_CONNECTED ){
    delay(1000);
    Serial.println(".");
    }
    Serial.print("NodeMCU IP Address:");
    Serial.print(WiFi.localIP());
      
      pinMode(V5, INPUT);
      pinMode(V6, OUTPUT);
    }
    
    void loop()
    {
      
      Blynk.run();
      
      int i = 30;
      swSerial.print(i);
      swSerial.println("\n");
      delay(30);
      // You can inject your own code or combine it with other sketches.
      // Check other examples on how to communicate with Blynk. Remember
      // to avoid delay() function!
      
    }

Arduino code:

     /* Fill-in information from Blynk Device Info here */
    #define BLYNK_TEMPLATE_ID "ID"
    #define BLYNK_TEMPLATE_NAME "CROPBOX"
    #define BLYNK_AUTH_TOKEN "TOKEN"
    
    // Libraries
    #include <Wire.h>
    #include <RTClib.h>
    #include <LiquidCrystal_I2C.h>
    #include <DHT.h>
    #include <OneWire.h>
    #include <DallasTemperature.h>
    #include <HCSR04.h>
    #include <SoftwareSerial.h>
    #include <SD.h> // Include the SD library for microSD card functionality
    #include <BlynkSimpleStream.h>
    #include <Blynk.h>
    
    // Pin Connections
    #define DHTPIN 2
    #define DHTTYPE DHT11
    #define ONE_WIRE_BUS 4
    #define MQ135_AO A3
    #define MQ135_DO 7
    #define SD_CHIP_SELECT 53 // Pin 51 in Arduino Mega corresponds to pin 53 for SD_CHIP_SELECT
    #define CS_PIN 53        // Chip select pin for SD card
    #define SCK_PIN 52       // SCK pin for SD card
    #define MOSI_PIN 51      // MOSI pin for SD card
    #define MOSO_PIN 50      // MISO pin for SD card
    
    // Initialization
    DHT dht(DHTPIN, DHTTYPE);
    float calibration_value = 1 + 0.6;  // pH
    int phval = 0;
    unsigned long int avgval;
    int buffer_arr[10], temp;
    float ph_act;
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);
    namespace pin {
      const byte tds_sensor = A1;
    }
    namespace device {
      float aref = 4.3;
    }
    namespace sensor {
      float ec = 0;
      unsigned int tds = 0;
      float waterTemp = 0;
      float ecCalibration = 1;
    }
    
    
    UltraSonicDistanceSensor distanceSensor(12, 11);
    RTC_DS3231 rtc;
    LiquidCrystal_I2C lcd(0x27, 16, 2);
    SoftwareSerial s(5, 6);
    String str;
    
    File dataFile;
    
    
    void setup() {
      Serial.begin(9600);
      //ArduinoMega.begin(4800);
      Serial.println("Humidity and Temperature");
      dht.begin();
      sensors.begin();
      if (!rtc.begin()) {
        Serial.println("Couldn't find RTC");
        while (1);
      }
      lcd.begin(16, 2);
      lcd.backlight();
      s.begin(9600);
    
      // Initialize SD card
      if (!SD.begin(SD_CHIP_SELECT)) {
        Serial.println("Initialization failed!");
        return;
      }
      Serial.println("Initialization done.");
    
      // Create a file on the SD card if it doesn't exist
      if (!SD.exists("data.txt")) {
        dataFile = SD.open("data.txt", FILE_WRITE);
        if (dataFile) {
          dataFile.close();
          Serial.println("File created.");
        } else {
          Serial.println("Error creating file.");
        }
      }
    }
    
    void loop() {
    
      // Read all sensor values
      float h = readHumidity();
      float t = readAirTemperature();
      float ph = readPH();
      float waterTemp = readWaterTemperature();
      unsigned int tds = readTDS();
      float ec = readEC();
      float distance = readDistance();
      int lightIntensity = readLightIntensity();
      int co2Level = readCO2Level();
      DateTime now = rtc.now();
      
      Blynk.virtualWrite(V1, ec);
      Blynk.virtualWrite(V2, tds);
      Blynk.virtualWrite(V3, waterTemp);
      Blynk.virtualWrite(V4, ph);
      Blynk.virtualWrite(V5, h);
      Blynk.virtualWrite(V6, t);
      Blynk.virtualWrite(V7, distance);
      Blynk.virtualWrite(V8, lightIntensity);
      Blynk.virtualWrite(V9, co2Level);
       
      // Serial Output
      printToSerial(h, t, ph, waterTemp, tds, ec, distance, lightIntensity, co2Level, now);
    
      // Display Data on LCD
      LCDisplay(h, t, ph, waterTemp, tds, ec, distance, lightIntensity, co2Level, now);
    
      // Send to NodeMCU
      sendToNodeMCU(h, t, ph, waterTemp, tds, ec, distance, lightIntensity, co2Level);
    
      // Write data to SD card
      writeDataToSDCard(h, t, ph, waterTemp, tds, ec, distance, lightIntensity, co2Level, now);
      
    
      delay(3000);
    }
    
    
    float readHumidity() {
      return dht.readHumidity();
    }
    
    float readAirTemperature() {
      return dht.readTemperature();
    }
    
    float readPH() {
      for (int i = 0; i < 10; i++) {
        buffer_arr[i] = analogRead(A0);
        delay(30);
      }
      for (int i = 0; i < 9; i++) {
        for (int j = i + 1; j < 10; j++) {
          if (buffer_arr[i] > buffer_arr[j]) {
            temp = buffer_arr[i];
            buffer_arr[i] = buffer_arr[j];
            buffer_arr[j] = temp;
          }
        }
      }
      avgval = 0;
      for (int i = 2; i < 8; i++)
        avgval += buffer_arr[i];
      float volt = (float)avgval * 5.0 / 1024 / 6;
      return -5.70 * volt + calibration_value;
    }
    
    float readWaterTemperature() {
      sensors.requestTemperatures();
      return sensors.getTempCByIndex(0);
    }
    
    unsigned int readTDS() {
      sensors.requestTemperatures();
      sensor::waterTemp = sensors.getTempCByIndex(0);
      float rawEc = analogRead(pin::tds_sensor) * device::aref / 1024.0;
      float temperatureCoefficient = 1.0 + 0.02 * (sensor::waterTemp - 25.0);
      sensor::ec = (rawEc / temperatureCoefficient) * sensor::ecCalibration;
      sensor::tds = (133.42 * pow(sensor::ec, 3) - 255.86 * sensor::ec * sensor::ec + 857.39 * sensor::ec) * 0.5;
      return sensor::tds;
    }
    
    float readEC() {
      return sensor::ec;
    }
    
    float readDistance() {
      return distanceSensor.measureDistanceCm();
    }
    
    int readLightIntensity() {
      return analogRead(A2);
    }
    
    int readCO2Level() {
      return analogRead(MQ135_AO);
    }
    
    void LCDisplay(float h, float t, float ph, float waterTemp, unsigned int tds, float ec, float distance, int light, int co2, DateTime now) {
      lcd.clear();
      lcd.print("AirTemp: " + String(t) + "C");
      lcd.setCursor(0, 1);
      lcd.print("AirHumid: " + String(h) + "%");
      delay(2500);
      lcd.clear();
    
      lcd.print("pH: " + String(ph));
      lcd.setCursor(0, 1);
      lcd.print("WtrTemp: " + String(waterTemp) + "C");
      delay(2500);
      lcd.clear();
    
      lcd.print("TDS: " + String(tds) + "ppm");
      lcd.setCursor(0, 1);
      lcd.print("EC: " + String(ec) + " mS/cm");
      delay(2500);
      lcd.clear();
    
      lcd.print("WtrLvl: " + String(distance) + "cm");
      lcd.setCursor(0, 1);
      lcd.print("Light: " + String(light) + " units");
      delay(2500);
      lcd.clear();
    
      lcd.print("CO2: " + String(co2) + " ppm");
      delay(2500);
      lcd.clear();
      
      lcd.print(String(now.day()) + "/" + String(now.month()) + "/" + String(now.year()));
      lcd.setCursor(0, 1);
      lcd.print(String(now.hour()) + ":" + String(now.minute()) + ":" + String(now.second()));
      delay(2500);
    }
    
    void readTdsQuick() {
      sensors.requestTemperatures();
      sensor::waterTemp = sensors.getTempCByIndex(0);
      float rawEc = analogRead(pin::tds_sensor) * device::aref / 1024.0;
      float temperatureCoefficient = 1.0 + 0.02 * (sensor::waterTemp - 25.0);
      sensor::ec = (rawEc / temperatureCoefficient) * sensor::ecCalibration;
      sensor::tds = (133.42 * pow(sensor::ec, 3) - 255.86 * sensor::ec * sensor::ec + 857.39 * sensor::ec) * 0.5;
      Serial.print("TDS: ");
      Serial.println(sensor::tds);
      Serial.print("EC: ");
      Serial.println(sensor::ec, 2);
    }
    
    void printToSerial(float h, float t, float ph, float waterTemp, unsigned int tds, float ec, float distance, int lightIntensity, int co2Level, DateTime now) {
      Serial.print("Air Humidity: ");
      Serial.print(h);
      Serial.println("% ");
      Serial.print("Air Temperature: ");
      Serial.print(t);
      Serial.println("C ");
      
      Serial.print("pH Val: ");
      Serial.println(ph);
      
      Serial.print("Water Temperature: ");
      Serial.println(waterTemp);
    
      Serial.print("TDS: ");
      Serial.println(tds);
      
      Serial.print("EC: ");
      Serial.println(ec, 2);
    
      Serial.print("Ultrasonic Distance: ");
      Serial.println(distance);
      
      Serial.print("Light Intensity: ");
      Serial.println(lightIntensity);
      
      Serial.print("CO2 Level: ");
      Serial.println(co2Level);
      
      Serial.print(now.dayOfTheWeek());
      Serial.print(" ");
      Serial.print(now.day(), DEC);
      Serial.print('/');
      Serial.print(now.month(), DEC);
      Serial.print('/');
      Serial.print(now.year(), DEC);
      Serial.print(" -- ");
      Serial.print(now.hour(), DEC);
      Serial.print(':');
      Serial.print(now.minute(), DEC);
      Serial.print(':');
      Serial.println(now.second(), DEC);
    }
    
    void sendToNodeMCU(float h, float t, float ph, float waterTemp, unsigned int tds, float ec, float distance, int lightIntensity, int co2Level) {
      s.print(h, 2);    s.print("A");
      s.print(t, 2);    s.print("B");
      s.print(ph, 2); s.print("C");
      s.print(ec, 2); s.print("D");
      s.print(tds, 2); s.print("E");
      s.print(distance, 2); s.print("F");
      s.print(waterTemp, 2); s.print("G");
      s.print(co2Level); s.print("H");
      s.print("\n");
      delay(500);
    }
    
    void writeDataToSDCard(float h, float t, float ph, float waterTemp, unsigned int tds, float ec, float distance, int light, int co2, DateTime now) {
      // Open the data file in write mode
      dataFile = SD.open("data.txt", FILE_WRITE);
      if (dataFile) {
        // Write sensor data to the file
        dataFile.print(h);
        dataFile.print(",");
        dataFile.print(t);
        // Write other sensor data similarly
        dataFile.print(",");
        dataFile.print(ph);
        dataFile.print(",");
        dataFile.print(waterTemp);
        dataFile.print(",");
        dataFile.print(tds);
        dataFile.print(",");
        dataFile.print(ec);
        dataFile.print(",");
        dataFile.print(distance);
        dataFile.print(",");
        dataFile.print(light);
        dataFile.print(",");
        dataFile.print(co2);
    
        // Write timestamp
        dataFile.print(",");
        dataFile.print(now.year(), DEC);
        dataFile.print("/");
        dataFile.print(now.month(), DEC);
        dataFile.print("/");
        dataFile.print(now.day(), DEC);
        dataFile.print(" ");
        dataFile.print(now.hour(), DEC);
        dataFile.print(":");
        dataFile.print(now.minute(), DEC);
        dataFile.print(":");
        dataFile.println(now.second(), DEC);
      
        // Close the file
        dataFile.close();
      } else {
        Serial.println("Error opening data file.");
      }
    }
2
  • 1
    Why do you use Software Serial when both of those boards have extra real Serial ports to use? SoftwareSerial is for faking serial ports with software when you're out of real ones. But you've got real ports left. They'll work a lot better if you use them.
    – Delta_G
    Commented Jun 7 at 15:47
  • Test a basic communication sketch to verify that the SoftwareSerial communication between the ESP8266 and Arduino Mega is working correctly without the Blynk integration.
    – tepalia
    Commented Jun 14 at 7:16

1 Answer 1

0

This is not how BlynkSimpleStream was intended to be used, but it is not supported now anyway.

There are a few options how to use esp8266 to connect to Blynk.

One option is what you attempt. A esp8266 sketch with Blynk which exchanges data with the main Arduino. But you have to code the exchange. The Blynk library will not help you with that.

Better option is to use only a esp8266 development board like Wemos D1 mini or NodeMCU board and wire the sensors and actuators to it. Then you have one sketch with Blynk.

If you have to use the esp8266 only as a WiFi adapter and you need other Arduino to handle the sensors, it is simpler to put AT firmware into the esp8266 and use a WiFi library in the Arduino. I recommend my WiFiEspAT library (available in Library Manager). It has an example on how to use it with Blynk. (I see the example is a little outdated, but it will help.)

2
  • When I use the AT command and enter "AT" on the serial monitor of the ESP8266 module, nothing shows up. Commented Jun 7 at 13:18
  • and did you put AT firmware into the esp8266? now it has your sketch.
    – Juraj
    Commented Jun 7 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.