Webhook with Sparkfun Blynk Board

I added a webhook to a running project on the Blynk Board (original code loaded) that measures humidity and temperature. The webhook sends data to thingspeak https://thingspeak.com/channels/12633 The temperature and humidity values are updated on the Blynk app every 59 seconds but the frequency on the Thinspeak varies a lot
My questions:

  1. Is this because the webhook needs to be triggered timewise in the code on the Blynk Board as I make currently to many calls to Thinsgspeak
  2. will the webhook run even when the Blynk app is not active

Thx, gernot

Yes you need to trigger the Webhook, a simple virtualWrite in the right place should be fine.

Do you use frequency reading widget setting?

Hi Dmitriy yes i have set the humidity reading (v7) to 59 seconds frequency and the webhook is referring to v7

@Geab reading widgets work only when app is opened (this will be fixed soon). For now you need timer on hardware side with

Blynk.virtualWrite(V7, val);

1 Like

okay - will try to digg this in the original Blynk Board code by Sparkfun; evetually good to share with them to make the Blynk Board even more user friendly

did add delay in Blynk Board code (bold in below code) and now its working :grinning: : https://thingspeak.com/channels/12633
not completely stable yet /time variation occur - the device goes offline nearly between every reading and as mentioned by @Dmitriyit stops when the app is not running or the phone goes in standby

Blynk Board Code Github

`/* 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
4 Experiment 4: Values 4
4 Widget(s): 4
4 - Value: TempF, V5, 0-1023, 1 s 4
4 - Value: TempC, V6, 0-1023, 1 s 4
4 - Value: Humidity, V7, 0-1023, 1 s 4
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 */
// Value ranges are ignored once values are written
// Board runs hot, subtract an offset to try to compensate:
float tempCOffset = 0; //-8.33;
unsigned int millisecond = 20000; // delay for webhook (ms)
BLYNK_READ(TEMPERATURE_F_VIRTUAL)
{
float tempC = thSense.readTemperature(); // Read from the temperature sensor
tempC += tempCOffset; // Add any offset
float tempF = tempC * 9.0 / 5.0 + 32.0; // Convert to farenheit
// Create a formatted string with 1 decimal point:
Blynk.virtualWrite(TEMPERATURE_F_VIRTUAL, tempF); // Update Blynk virtual value
BB_DEBUG("Blynk Read TempF: " + String(tempF));
delay(millisecond); // delay for webhook on Thingspeak
}

BLYNK_READ(TEMPERATURE_C_VIRTUAL)
{
float tempC = thSense.readTemperature(); // Read from the temperature sensor
tempC += tempCOffset; // Add any offset
Blynk.virtualWrite(TEMPERATURE_C_VIRTUAL, tempC); // Update Blynk virtual value
BB_DEBUG("Blynk Read TempC: " + String(tempC));
delay(millisecond); // delay for webhook on Thingspeak
}

BLYNK_READ(HUMIDITY_VIRTUAL)
{
float humidity = thSense.readHumidity(); // Read from humidity sensor
Blynk.virtualWrite(HUMIDITY_VIRTUAL, humidity); // Update Blynk virtual value
BB_DEBUG("Blynk Read Humidity: " + String(humidity));
delay(millisecond); // delay for webhook on Thingspeak
}

BLYNK_WRITE(TEMP_OFFSET_VIRTUAL) // Very optional virtual to set the tempC offset
{
tempCOffset = param.asInt();
BB_DEBUG("Blynk TempC Offset: " + String(tempCOffset));
}
`

@Geab you don’t want anything like 20000. If you need any delay start with around 200 and increase by 100 for each test. when you know how to use Blynk you will be able to use the Slider widget to vary the delay but just hard code for now.

2 Likes

@Geab be sure to read the site guidelines too as we are not going to bother looking through unformatted code to find your bugs. It hurts our eyes.

Also search for SimpleTimer on this and the Arduino site as it is MUCH better than the dreadful delay() function.

@Costas you need a delay of 15 s = 15000 ms for Thingspeak - its Sparkfuns code just modified and as described its working with the current limits of webhook
and would be nice with AI to autodetect code :slight_smile:

thanks for pointing at simple timer - for prototyping the delay works in first instance

Then you MUST use SimpleTimer.

Blynk has a heartbeat and you are stopping it with 20S WAIT, WAIT, WAIT. Blynk then kicks you off the server. See the docs.

1 Like

Heartbeat is 5s to 15s depending on what Blynk are using at the time.
So every Webhook call you make results in a disconnection from their server.

took me some time to understand the original Blynk Board code; I would like to keep it and modify it to work with webhook. The code/board is straight forward for newbies to setup IoT and adding the functionality to upload to Thingspeak would make it even more usefull and usable in eg STEM education; thats why I do not want to change more than necessary.

In the BlynkBoard_Core_Firmware.ino I did add as recommended by @Costas and @Dmitriy a simpletimer and use timer to call blynkLoop.

timer.setInterval(20000, blynkLoop);

timer.run(); //GEAB

blynkLoop runs in BlynkBoard_BlynkMode.ino and somehow also executes the code below which sends temperature and humidity data to Blynk app and now also thingsspeak https://thingspeak.com/channels/12633

It looks like the “functions” (what are those blocks exactly?) below like BLYNK_READ(TEMPERATURE_C_VIRTUAL)
is only executed when the pin is defined in the Blynk app by eg a label field; how is that controlled/identified?

/* 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
 4 Experiment 4: Values                4
 4 Widget(s):                          4
 4  - Value: TempF, V5, 0-1023, 1 s    4
 4  - Value: TempC, V6, 0-1023, 1 s    4
 4  - Value: Humidity, V7, 0-1023, 1 s 4
 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 */
// Value ranges are ignored once values are written
// Board runs hot, subtract an offset to try to compensate:
float tempCOffset = 0; //-8.33;
unsigned int millisecond = 20000;  // delay for webhook (ms)

BLYNK_READ(TEMPERATURE_F_VIRTUAL)
{
  float tempC = thSense.readTemperature(); // Read from the temperature sensor
  tempC += tempCOffset; // Add any offset
  float tempF = tempC * 9.0 / 5.0 + 32.0; // Convert to farenheit
  // Create a formatted string with 1 decimal point:
  Blynk.virtualWrite(TEMPERATURE_F_VIRTUAL, tempF); // Update Blynk virtual value
  BB_PRINT("Blynk Read TempF: " + String(tempF));
  BB_DEBUG("                                    Blynk Read TempF: " + String(tempF));
  //delay(millisecond); // delay for webhook on Thingspeak
}

//void read123() //GEAB
BLYNK_READ(TEMPERATURE_C_VIRTUAL)
{
  float tempC = thSense.readTemperature(); // Read from the temperature sensor
  tempC += tempCOffset; // Add any offset
  Blynk.virtualWrite(TEMPERATURE_C_VIRTUAL, tempC); // Update Blynk virtual value
  BB_DEBUG("Blynk Read TempC: " + String(tempC));
  BB_PRINT("                                Blynk Read TempC: " + String(tempC));
  //delay(millisecond); // delay for webhook on Thingspeak
}

BLYNK_READ(HUMIDITY_VIRTUAL)
{
  float humidity = thSense.readHumidity(); // Read from humidity sensor
  Blynk.virtualWrite(HUMIDITY_VIRTUAL, humidity); // Update Blynk virtual value
  BB_DEBUG("Blynk Read Humidity: " + String(humidity));
  BB_PRINT("                                Blynk Read Humidity: " + String(humidity));
  BB_PRINT("Humidity loop");//GEAB
  //Serial.println("Humidity loop");//GEAB
  //delay(millisecond); // delay for webhook on Thingspeak
}

BLYNK_WRITE(TEMP_OFFSET_VIRTUAL) // Very optional virtual to set the tempC offset
{
  tempCOffset = param.asInt();
  BB_DEBUG("Blynk TempC Offset: " + String(tempCOffset));
}

@Geab I don’t use the Sparfun Blynk board so I’m working in the dark but I can see the firmware file on GitHub.

OK I have also found the “BlynkMode” at https://github.com/sparkfun/Blynk_Board_ESP8266/blob/master/Firmware/BlynkBoard_Core_Firmware/BlynkBoard_BlynkMode.ino

Lines 55 to 57 and 78 define 4 virtual pins that you have in your sketch extract. These are V5, V6, V7 and V28. So these are the 4 virtual pins you need to use in your project. These can be changed but best left alone for now. The first 3 look like they are Value Display widgets and I’m guessing V28 is a Slider widget.

HTH

okay I changed the timer to

timer.setInterval(20000, Thingspeak);

and added

void Thingspeak()
{
float tempC = thSense.readTemperature(); // Read from the temperature sensor
//tempC += tempCOffset; // Add any offset
Blynk.virtualWrite(TEMPERATURE_C_VIRTUAL, tempC); // Update Blynk virtual value
BB_DEBUG(“Blynk Read TempC: " + String(tempC));
BB_PRINT(” Blynk Read TempC: " + String(tempC));
}

I am not fully there but thanks for keeping me going :slight_smile:

by the way pin V28 is for adding a temperature offset as the Blynk Board is running hot and the temperature sensor needs to be corrected.

actually its up an running now https://thingspeak.com/channels/12633/

@Dmitriy “reading widgets work only when app is opened (this will be fixed soon” when do you think it will be fixed ?

1 Like