[SOLVED] Having trouble with blynk_write command

I am using Blynk to read and write data from a Blynk local server to an ESP8266. I have connected several DS18B20 sensors to the ESP8266 and am reading temperatures from them. I am sending this temperature data as well as alarm active data to the app. I am pulling the alarm threshold information from the app via a slider and blynk_write. I can not get any of the writes to write the variables on the ESP8266. I can see that the data is being pushed to the database when I look at the admin page and I am having no trouble sending the temperature data or the alarm data to the database or app. I’m sure there is something I’m doing wrong, but I can’t find any differences between my code and the example on the blynk_write portion. Thanks in advance.

  
    #include <BlynkSimpleEsp8266.h> 

    #include <OneWire.h>
    #include <DallasTemperature.h> 

    #define ONE_WIRE_BUS 4         

    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);

    DeviceAddress probe01 = { 0x28, 0xFF, 0x62, 0xEC, 0x71, 0x16, 0x03, 0x61 };   //probe01 address
    DeviceAddress probe02 = { 0x28, 0xFF, 0x93, 0x72, 0x70, 0x16, 0x05, 0x88 };   //probe02 address
    DeviceAddress probe03 = { 0x28, 0xFF, 0x89, 0xEA, 0x81, 0x16, 0x03, 0x4F };   //probe03 address
    DeviceAddress probe04 = { 0x28, 0xFF, 0x71, 0xE7, 0x90, 0x16, 0x04, 0x28 };   //probe04 address
    DeviceAddress probe05 = { 0x28, 0xFF, 0x4D, 0x96, 0x82, 0x16, 0x03, 0x2C };   //probe05 address
    DeviceAddress probe06 = { 0x28, 0xFF, 0x51, 0xB0, 0x90, 0x16, 0x04, 0x8E };   //probe06 address

    char auth[] = "token";
    char ssid[] = "AP";
    char pass[] = "Password";

    SimpleTimer timer;



    int motorSpeed;          // motor frequency
    int motorAmps;           // motor amperage draw
    int motorAlarm;          // motor amperage alarm
    int motorSlowSpeed = .8*motorSpeed;    //motor slow down speed in the event of alarm
    int motorAmpsAlarm;      // motor amperage draw alarm

    int sensor01;            // Sensor 1 temperature in F
    int sensor02;            // Sensor 2 temperature in F
    int sensor03;            // Sensor 3 temperature in F
    int sensor04;            // Sensor 4 temperature in F
    int sensor05;            // Sensor 5 temperature in F
    int sensor06;            // Sensor 6 temperature in F

    int alarmTemp01;          // sensor 1 alarm temperature in F
    int alarmTemp02;          // sensor 2 alarm temperature in F
    int alarmTemp03;          // sensor 3 alarm temperature in F
    int alarmTemp04;          // sensor 4 alarm temperature in F
    int alarmTemp05;          // sensor 5 alarm temperature in F
    int alarmTemp06;          // sensor 6 alarm temperature in F

    int alarm01;              // sensor 1 alarm active
    int alarm02;              // sensor 2 alarm active
    int alarm03;              // sensor 3 alarm active
    int alarm04;              // sensor 4 alarm active
    int alarm05;              // sensor 5 alarm active
    int alarm06;              // sensor 6 alarm active
    bool alarm;               // boolean for any alarm active

    int ledState;             // LED on or off
    int alarmOut = 12;        // LED pin location
    int alarmStatus;          // Integer that corresponds with a drop down on the Blynk app that determines alarm function


    BLYNK_WRITE(V7)
     {int alarmTemp01 = param.asInt(); // assigning incoming value from pin V7 to a variable
     }
    BLYNK_WRITE(V8)
     {int alarmTemp02 = param.asInt(); // assigning incoming value from pin V8 to a variable
    } 
    BLYNK_WRITE(V9)
     {int alarmTemp03 = param.asInt(); // assigning incoming value from pin V9 to a variable
     }
     BLYNK_WRITE(V10)
     {int alarmTemp04 = param.asInt(); // assigning incoming value from pin V10 to a variable
     }
     BLYNK_WRITE(V11)
     {int alarmTemp05 = param.asInt(); // assigning incoming value from pin V11 to a variable
     }
     BLYNK_WRITE(V12)
     {int alarmTemp06 = param.asInt(); // assigning incoming value from pin V12 to a variable
     }
     BLYNK_WRITE(V0)
     {int alarmStatus = param.asInt(); // assigning incoming value from pin V0 to a variable
     }
     BLYNK_WRITE(V22)
     {int motorAmpsAlarm = param.asInt(); // assigning incoming value from pin V19 to a variable
     }

    void setup()
    {
      pinMode(alarmOut, OUTPUT);
 
      Blynk.begin(auth, ssid, pass, IPAddress(192, 168, 42, 1), 8442);

      while (Blynk.connect() == false) {
        // Wait until connected
      }

      sensors.begin();                        // Starts the DS18B20 sensor(s).
  
      sensors.setResolution(probe01, 10);
      sensors.setResolution(probe02, 10);
      sensors.setResolution(probe03, 10);
      sensors.setResolution(probe04, 10);
      sensors.setResolution(probe05, 10);   
      sensors.setResolution(probe06, 10);     
  
      timer.setInterval(2000L, sendTemps);    // Temperature sensor read interval. 2000 (ms) = 2 seconds.
      timer.setInterval(2000L, alarmCheck);    // Temperature sensor read interval. 2000 (ms) = 2 seconds.

    }

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

    void sendTemps()    //function to get temperatures from DS18B20 sensors and send to Blynk virtual pins
    {
      sensors.requestTemperatures();                  // Polls the sensors.
  
      sensor01 = sensors.getTempF(probe01);           // Stores temperature from probe01 into sensor01. 
      sensor02 = sensors.getTempF(probe02);           // Stores temperature from probe02 into sensor02.
      sensor03 = sensors.getTempF(probe03);           // Stores temperature from probe03 into sensor03.
      sensor04 = sensors.getTempF(probe04);           // Stores temperature from probe04 into sensor04.
      sensor05 = sensors.getTempF(probe05);           // Stores temperature from probe05 into sensor05.
      sensor06 = sensors.getTempF(probe06);           // Stores temperature from probe06 into sensor06.

      Blynk.virtualWrite(1, sensor01);         // Send temperature to Blynk app virtual pin 1.
      Blynk.virtualWrite(2, sensor02);         // Send temperature to Blynk app virtual pin 2.
      Blynk.virtualWrite(3, sensor03);         // Send temperature to Blynk app virtual pin 3.
      Blynk.virtualWrite(4, sensor04);         // Send temperature to Blynk app virtual pin 4.
      Blynk.virtualWrite(5, sensor05);         // Send temperature to Blynk app virtual pin 5.
      Blynk.virtualWrite(6, sensor06);         // Send temperature to Blynk app virtual pin 6.

      Blynk.virtualWrite(13, alarm01);         // Send alarm to Blynk app virtual pin 13.
      Blynk.virtualWrite(14, alarm02);         // Send alarm to Blynk app virtual pin 14.
      Blynk.virtualWrite(15, alarm03);         // Send alarm to Blynk app virtual pin 15.
      Blynk.virtualWrite(16, alarm04);         // Send alarm to Blynk app virtual pin 16.
      Blynk.virtualWrite(17, alarm05);         // Send alarm to Blynk app virtual pin 17.
      Blynk.virtualWrite(18, alarm06);         // Send alarm to Blynk app virtual pin 18.

      Blynk.virtualWrite(19, motorSpeed);         // Send motorSpeed to Blynk app virtual pin 19.
      Blynk.virtualWrite(20, motorAmps);         // Send motorAmps to Blynk app virtual pin 20.

      Blynk.virtualWrite(21, motorAlarm);         // Send motorAlarm to Blynk app virtual pin 20.
  
    }

    void alarmCheck()   //function to set alarm bit if the temperature of a sensor is higher than the alarm set point
    {
     if (sensor01 > alarmTemp01){
        alarm01 = 1023;                //and set alarm value to 1 to pass to blynk
     }
     else{
      alarm01 = 0;
     }
 
     if (sensor02 > alarmTemp02){
        alarm02 = 1023;                //and set alarm value to 1 to pass to blynk
     }
      else{
      alarm01 = 0;
     }
 
     if (sensor03 > alarmTemp03){
        alarm03 = 1023;                //and set alarm value to 1 to pass to blynk
     }
     else{
      alarm03 = 0;
     }
 
     if (sensor04 > alarmTemp04){
        alarm04 = 1023;                //and set alarm value to 1 to pass to blynk
     }
     else{
      alarm04 = 0;
     }
 
     if (sensor05 > alarmTemp05){
        alarm05 = 1023;                //and set alarm value to 1 to pass to blynk
     }
     else{
      alarm05 = 0;
     }
 
     if (sensor06 > alarmTemp06){
        alarm06 = 1023;                //and set alarm value to 1 to pass to blynk
     }
     else{
      alarm06 = 0;
     }

    if (sensor01 > alarmTemp01){
        alarm = 1;                //set alarm to 1 if any of the probes are above alarm setpoint
     }
     else if (sensor02 > alarmTemp02){
      alarm = 1;
    }
     else if (sensor03 > alarmTemp03){
      alarm = 1;
     }
     else if (sensor04 > alarmTemp04){
      alarm = 1;
     }
     else if (sensor05 > alarmTemp05){
      alarm = 1;
     }
     else if (sensor06 > alarmTemp06){
      alarm = 1;
     }
     else {
      alarm = 0;
     }

 
     if (alarm == 1){
      if (ledState == LOW){
            ledState = HIGH;
            //Serial.write("High\n");
      }
      else{
            ledState = LOW;
            //Serial.write("Low\n");
      } 
          // set the LED with the ledState of the variable:
        digitalWrite(alarmOut, ledState);     
      }
    }

I have added#include <esp8266WiFi.h> to the headers and also tried Blynk.syncAll() and Blynk.syncVirtual(V0, V7, V8, etc) with no luck. I am moving the sliders around on the app to make them send different numbers as well.

Presumably the app shows your MCU going offline, right?

Looks like you are flooding the server with 15 consecutive virtualWrite() calls.

I don’t think it does, but I will comment out a bunch of the calls. If that works, I can pull them to different functions running in the simple timer loops.

I turned off all the writes except the temps and alarms for 05 and 06 and everything is working. Is there a preferred number of writes to have in a row? I can at least break it up into 3 different functions, one for temps, one for alarms, and one for the torque etc. From what I read, putting a delay() into the function is a bad idea. That would put 5 writes in each function. I will report back if that works or not.

Thanks for the advice Costas.

I never do more than about 3 or 4.

I believe officially it’s no more than 10 data points per second but that doesn’t work for me.

Thank you Costas. That solved my problem. Everything is running smoothly now.

1 Like