BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

How to get real time sensor data from Blynk and analyse in Python

Hello, I am making the transmission of ECG data with Arduino and GSM (SIM900) over Blynk, and that data should be loaded into Python and analyzed in real time. Is that possible with Blynk? I tried some simple code in Python, but nothing happened when running. I am still not that familiar with Python and possibly didn’t put something important which whould solve the thing.
The other thing I tried is to use webhook and send the data to Thingspeak which I eventually succeeded, but the data Thingspeak is displaying is incorrect! I used DHT11 sensor for the trial since I am still waiting to get that ECG.
Does anyone have an idea how to solve any of those two issues? My Arduino and Python codes are below.
Arduino code:

#include "DHT.h"
#define BLYNK_PRINT Serial

// Select your modem:
//#define TINY_GSM_MODEM_SIM800
#define TINY_GSM_MODEM_SIM900
//#define TINY_GSM_MODEM_M590
//#define TINY_GSM_MODEM_A6
//#define TINY_GSM_MODEM_A7
//#define TINY_GSM_MODEM_BG96
//#define TINY_GSM_MODEM_XBEE

// Default heartbeat interval for GSM is 60
// If you want override this value, uncomment and set this option:
//#define BLYNK_HEARTBEAT 30

#define BLYNK_TIMEOUT_MS     15000UL

#define DHTPIN 2
#define DHTTYPE DHT11

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);

// Create global varibales to store temperature and humidity
float t; // temperature in celcius
float f; // temperature in fahrenheit
float h; // humidity

#include <TinyGsmClient.h>
#include <BlynkSimpleTinyGSM.h>
#include <BlynkSimpleSIM800.h>

BlynkTimer timer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "auth";
char thingspeak = "thingspeak_write_api"; 

// Your GPRS credentials
// Leave empty, if missing user or pass
char apn[]  = "apn";
char user[] = "usr";
char pass[] = "pass";

// Hardware Serial on Mega, Leonardo, Micro
//#define SerialAT Serial1

// or Software Serial on Uno, Nano
//#include <SoftwareSerial.h>
//SoftwareSerial SerialAT(2, 3); // RX, TX

#include <SoftwareSerial.h>
SoftwareSerial SerialAT(7,8);
TinyGsm modem(SerialAT);

void setup()
{
  // Debug console
  Serial.begin(9600);
  timer.setInterval(17000L, SendtoThingspeak); 
  pinMode(8, OUTPUT);
  digitalWrite(8, HIGH);
  delay(1000);
  digitalWrite(8, LOW);

  delay(10);
  dht.begin();
  // Set GSM module baud rate
  SerialAT.begin(9600);
  delay(3000);

  // Restart takes quite some time
  // To skip it, call init() instead of restart()
  Serial.println("Initializing modem...");
  modem.restart();

  // Unlock your SIM card with a PIN
  //modem.simUnlock("1234");

  Blynk.begin(auth, modem, apn, user, pass);
}

void loop()
{
  Blynk.run();
  timer.run();
  t=readTemp();
  h=readHum();

  Serial.print(t); 
  Serial.println(" *C");
  Serial.print(h); 
  Serial.println(" %");

  Blynk.virtualWrite(V1, t);
  delay(5000);

  Blynk.virtualWrite(V2, h);
  delay(5000);
}

float readTemp() {
  // Read temperature as Celsius
  t = dht.readTemperature();
  //Read humidity
  h = dht.readHumidity();

  // Compute temperature values in Celcius
  t = dht.computeHeatIndex(t,h,false);
  
  // Check if any reads failed and exit early (to try again).
  if (isnan(t)) {
    Serial.println("Failed to read temp from DHT sensor!");
    return 1;
  }

  return t;
}

float readHum() {
  //Read humidity
  h = dht.readHumidity();
  // Read temperature as Celsius
  t = dht.readTemperature();

  // Compute temperature values in Celcius
  t = dht.computeHeatIndex(t,h,false);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h)) {
    Serial.println("Failed to read hum from DHT sensor!");
    return 1;
  }

  return h;
}

void SendtoThingspeak()
{
  String t = String(dht.readTemperature());
  Blynk.virtualWrite(V0, t);
}

Python code:

# -*- coding: utf-8 -*-
"""
Created on Sat Aug 10 23:45:00 2019

@author: Milos
"""

import blynklib
import random

BLYNK_AUTH = '2Wl_LQwI1W_-6RTpecDeK9OPZ0WrMTAT'

# initialize blynk
blynk = blynklib.Blynk(BLYNK_AUTH)

READ_PRINT_MSG = "[READ_VIRTUAL_PIN_EVENT] Pin: V{1}"


# register handler for virtual pin V11 reading
@blynk.handle_event('read V1')
def read_virtual_pin_handler(pin):
    print(READ_PRINT_MSG.format(pin))
    a=blynk.virtual_read(pin, random.randint(0, 255))

    while True:
        blynk.run()
        print(a)
###########################################################
# infinite loop that waits for event
###########################################################
        
#while True:
#    blynk.run()

First of all, you should read this:

Secondly, it’s not clear if you’re using the same Blynk Auth token in both the Arduino and Python code. If you are, then you shouldn’t be.

Thirdly, you should read this about the granularity of data stored on the Blynk server (last part of the Superchart section):

Pete.

Thank you for the answer. Yes, I am using the same auth token for both Arduino and Python. I didn’t know that there may be more auth tokens. How can I get a new auth token for Python?

You add a new device in the app, then use Bridge to connect the two devices in your code. I have no idea if Bridge works in Python, as I’ve never used it.

Pete.