Help with Blynk & Database Connection?


I am trying to get my project to work with Blynk. Currently we send the temperature out to a database as you can see in the code below. But I am wondering what the best approach is to this. We want the temperature to be read every 30 seconds and sent to the database while we want the Blynk app to keep an constant update on the temperature. Currently, because of the 30 second delay the Blynk app is only online for a few seconds then goes offline. If we take the delay away it works but the database is spammed with temperature readings. Would you have any recommendations on how to achieve this?



Board: Arduino MKR1000
Blynk App on iOS 13.3.1

#define BLYNK_PRINT SerialUSB
#include <SPI.h>
#include <WiFi101.h>
#include <BlynkSimpleWiFiShield101.h>
#include "arduino_secrets.h" 

char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
char server[] = "";
char auth[] = "";

int status = WL_IDLE_STATUS;
IPAddress gateway;
WiFiClient client;

int tempPin = A0; 
int tempValue = 0;
double temp;

int redPin= 10;
int bluePin = 11;
int greenPin = 12;

BlynkTimer timer;

void printWiFiData() {
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");

void getTemp() {
  tempValue = analogRead(tempPin); 
  temp = (double)tempValue / 1024;
  temp = temp * 5;
  temp = temp - 0.5;                  
  temp = temp * 100;
  temp = temp - 32;
  temp = temp * 0.5556;
  Serial.print("Current Temperature in oC: ");
  if (temp > 21) {
    RGB_color(255, 0, 0);
  else if (temp <18 ) {
    RGB_color(0, 0, 255);
  else {
    RGB_color(0, 255, 0);

void myTimerEvent()
  tempValue = analogRead(A0);
  Blynk.virtualWrite(V6, tempValue);

void dbSend() {
  if (client.connect(server, 8888)) {
    Serial.println("Connected to database\n");
    Serial.print("Sent data to database\n");
    client.print("GET /database.php?temperature=");
    client.print(" ");
    client.println("Connection: close");
  } else {
    Serial.println("Connection Failed");

void RGB_color(int redLedValue, int greenLedValue, int blueLedValue)
  analogWrite(redPin, redLedValue);
  analogWrite(greenPin, greenLedValue);
  analogWrite(bluePin, blueLedValue);

void setup() {
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, myTimerEvent);
  while (!Serial) { 

  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true);

  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    status = WiFi.begin(ssid, pass);


  Serial.print("You're connected to the network\n");


void loop() {

Start by reading this:

Then read the last part of this Supercart documentation, about granularity:

You’ll see that if you send data more frequently then it will be averaged over the course of a minute and that average stored in the database. If you send once every 30 seconds then those two readings will be averaged and stored in the database, because the maximum granularity is one record per minute.



Thanks for the reply.

I looked through your response. I am still trying to understand. So I have seperated the temperatures being sent to the database and the Blynk readings. I have it set up to read and send the readings separately. Is it possible to use the BlynkTimer within the loop to send the data so it doesnt cause conflict with the Blynk app?


Re-read the “keep your void loop clean” document.
It shows you how to move code out of your void loop into a function, which you call with a timer.

In your case you would move the code that takes and processes the temperature readings (minibus the delay part, which would be deleted entirely) into a function and call it every 30 seconds using BlynkTimer. This would achieve the same result as at present, but without the Blynk disconnections.

You can then review the granularity documentation and decide if one trading every 30 seconds is the optimal frequency for you.


What temperature sensor are you using. The only reference in your code is temp pin… no temp libraries ect being used?