SIM900 Flow sensor Blynk

Hi,
im making a project that allow me to monitoring sensor data from the sim900 module to the blynk app with a display value widget, and allow me too to control a valve that can interrupt the water flow on a tube.

My problem is that i cant see the values of the sensor through the app, i dont know why. i leave the code below.

thanks for helping me.

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  Attention! Please check out TinyGSM guide:
    https://tiny.cc/tinygsm-readme

  Change GPRS apm, user, pass, and Blynk auth token to run :)
  Feel free to apply it to any other example. It's simple!

 *************************************************************/

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

// Select your modem:

#define TINY_GSM_MODEM_SIM900


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

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

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

// Your GPRS credentials
// Leave empty, if missing user or pass
char apn[]  = "m2mco.tigo.com";
char user[] = "";
char pass[] = "";

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

BLYNK_CONNECTED() {
}

volatile int NumPulsos; 
const int sensorPin= 2;    
float factor_conversion=7.11; 
float volumen=0;
long dt=0; 
long t0=0; 
byte sensorInterrupt = 0; 

void ContarPulsos ()  
{ 
  NumPulsos++; 
} 

int ObtenerFrecuecia() 
{
  int frecuencia;
  NumPulsos = 0;  
  interrupts();  
  delay(1000);  
  noInterrupts(); 
  frecuencia=NumPulsos; 
  return frecuencia;
}


void sendSensor()
{
  float frecuencia=ObtenerFrecuecia(); 
  float caudal_L_m=frecuencia/factor_conversion; 
  dt=millis()-t0; 
  t0=millis();
  volumen=volumen+(caudal_L_m/60)*(dt/1000); 

  Serial.print ("Caudal: "); 
  Serial.print (caudal_L_m,3); 
  Serial.print ("L/min\tVolumen: "); 
  Serial.print (volumen,3); 
  Serial.println (" L");
  
   Blynk.virtualWrite(V4, caudal_L_m);
   Blynk.virtualWrite(V5, volumen);
}


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

TinyGsm modem(SerialAT);

void setup()
{

  digitalWrite(9, HIGH);   // Descomentar para activar la alimentación de la tarjeta por Software
  delay(1000);             
  digitalWrite(9, LOW);
  delay (5000);
  
  // Debug console
  Serial.begin(9600);

  delay(10);

  // Set GSM module baud rate
  SerialAT.begin(19200);
  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");

  pinMode(sensorPin, INPUT); 

  attachInterrupt(digitalPinToInterrupt(2), ContarPulsos,RISING);
  t0=millis();

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

void loop()
{
  Blynk.run();

}

You need to call function void sendSensor() periodically using BlynkTimer. Timer interval must be much larger than 1000ms because of delay(1000) was called in ObtenerFrecuecia() if you still use that function.

Add to your code

// 10s
#define READ_SENSOR_INTERVAL_MS      10000 
BlynkTimer  timer;

...

void setup()
{
  ....
  Blynk.begin(auth, modem, apn, user, pass);
  timer.setInterval(READ_SENSOR_INTERVAL_MS, sendSensor);
}

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

I still don’t like the way you’re using :

  1. delay(1000) in ObtenerFrecuecia().
  2. noInterrupts() / delay() / interrupts as this is not efficient way to deal with ISR

Try the following code, based on yours, with translation to English for other forum members :

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

// Select your modem:

#define TINY_GSM_MODEM_SIM900


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

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

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

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

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

volatile int pulseCount;

#define SENSOR_PIN     2

float factor_conversion = 7.11;
float volume = 0;
long deltaTime = 0;
long startTime = 0;

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

TinyGsm modem(SerialAT);

// 10s
#define READ_SENSOR_INTERVAL_MS      10000 
BlynkTimer  timer;

BLYNK_CONNECTED()
{
}

void pulseCounter ()
{
  pulseCount++;
}

void sendSensor()
{
  static float frequency;
  static float caudal_L_m;

  noInterrupts();
  frequency = pulseCount;
  pulseCount = 0;
  interrupts();
  
  caudal_L_m  = frequency / factor_conversion;

  deltaTime = millis() - startTime;
  startTime = millis();

  volume = volume + (caudal_L_m / 60) * (deltaTime / 1000);

  Serial.print ("Caudal: ");
  Serial.print (caudal_L_m, 3);
  Serial.print ("L/min\tvolume: ");
  Serial.print (volume, 3);
  Serial.println (" L");

  Blynk.virtualWrite(V4, caudal_L_m);
  Blynk.virtualWrite(V5, volume);
}

void setup()
{
  digitalWrite(9, HIGH);   // Uncomment to activate card power by Software
  delay(1000);
  digitalWrite(9, LOW);
  delay (5000);

  // Debug console
  Serial.begin(115200);

  delay(10);

  // Set GSM module baud rate
  SerialAT.begin(19200);
  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");

  pinMode(SENSOR_PIN, INPUT);

  attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), pulseCounter, RISING);
  startTime = millis();

  Blynk.begin(auth, modem, apn, user, pass);
  timer.setInterval(READ_SENSOR_INTERVAL_MS, sendSensor);
}

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

it didnt worked, the modem tried to connect but it doesnt work. but with my code the modem can make de connection. i dont know what might be the problem with your code.

i modified the code this way, changing the baud rate of the software serieal to 9600, and comment the serial1 for the arduino mega, because i am using the arduino uno.

#define BLYNK_PRINT Serial

// Select your modem:

#define TINY_GSM_MODEM_SIM900


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

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

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

// Your GPRS credentials
// Leave empty, if missing user or pass
char apn[]  = "m2mco.tigo.com";
char user[] = "";
char pass[] = "";

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

volatile int pulseCount;

#define SENSOR_PIN     2

float factor_conversion = 7.11;
float volume = 0;
long deltaTime = 0;
long startTime = 0;

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

TinyGsm modem(SerialAT);

// 10s
#define READ_SENSOR_INTERVAL_MS      10000 
BlynkTimer  timer;

BLYNK_CONNECTED()
{
}

void pulseCounter ()
{
  pulseCount++;
}

void sendSensor()
{
  static float frequency;
  static float caudal_L_m;

  noInterrupts();
  frequency = pulseCount;
  pulseCount = 0;
  interrupts();
  
  caudal_L_m  = frequency / factor_conversion;

  deltaTime = millis() - startTime;
  startTime = millis();

  volume = volume + (caudal_L_m / 60) * (deltaTime / 1000);

  Serial.print ("Caudal: ");
  Serial.print (caudal_L_m, 3);
  Serial.print ("L/min\tvolume: ");
  Serial.print (volume, 3);
  Serial.println (" L");

  Blynk.virtualWrite(V4, caudal_L_m);
  Blynk.virtualWrite(V5, volume);
}

void setup()
{
  digitalWrite(9, HIGH);   // Uncomment to activate card power by Software
  delay(1000);
  digitalWrite(9, LOW); 
  delay (5000);

  // Debug console
  Serial.begin(9600);

  delay(10);

  // Set GSM module baud rate
  SerialAT.begin(19200);
  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");

  pinMode(SENSOR_PIN, INPUT);

  attachInterrupt(digitalPinToInterrupt(SENSOR_PIN), pulseCounter, RISING);
  startTime = millis();

  Blynk.begin(auth, modem, apn, user, pass);
  timer.setInterval(READ_SENSOR_INTERVAL_MS, sendSensor);
}

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

That’s good you know where to modify.
I had to assume you’re using Mega as you didn’t tell anything about the board.
You also have to reformat your posted code with ``` like you did in the first post. Otherwise you’ll get blamed and posted code deleted by @PeteKnight.

Ok, i’ll do that. But the code isn’t work yet. I don’t know why.

Nobody in the forum knows how to help with so little info such as the code isn’t work yet. It might be because of the software, hardware, wiring, power, board, etc.

Even we just recently know the type of board you’re running is UNO.

You need to add more debug display in strategic locations in the code to know exactly where / when, then post the terminal debug output, hopefully we here have more info.