RFID + FLOW SENSOR + GETREQUEST/POSTREQUEST

So i’m on a project that makes a beer machine automatic. My problem is that the sensor only works one time per cicle, right? And i have a get request an post request…

My code is supposed to work like this:

1 - Passes the RFID card and get the info of value on teh Database (Working)
2 - Wait to start pour the beer and count the amount of beer poured.
3 - Send the data to the DB, the amount poured…

the code is this:

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#define RST_PIN  D3
#define SS_PIN   D4

#include <Wire.h>  //INCLUSÃO DE BIBLIOTECA
#include <LiquidCrystal_I2C.h> //INCLUSÃO DE BIBLIOTECA
LiquidCrystal_I2C lcd(0x27, 20, 4); //FUNÇÃO DO TIPO "LiquidCrystal_I2C"

#include <SPI.h>
#include <MFRC522.h>

MFRC522 mfrc522(SS_PIN, RST_PIN);


volatile byte pulseCount;
byte sensorInterrupt = 10;
byte sensorPin = 10; // GPIO10
float calibrationFactor = 55;
float flowRate;
unsigned int flowMilliLitres;
unsigned long totalMilliLitres;
unsigned long oldTime;
unsigned long totalml; // VALOR TOTAL SERVIDO
unsigned long totalmlcomp;


unsigned long valorml = 0.02; // VALOR PAGO POR ML SERVIDO DE CHOPP


unsigned int valorbebido = 0; // totalml X valorml
const char* valordinheiro = 0; // QTD DE DINHEIRO QUE O CARTÃO POSSUI
const char* nome; // NOME DA PESSOA DONA DO CARTÃO

int rele = 16; //pin D0

const char* ssid = "Terres";
const char* ssidPassword = "terres00";
StaticJsonBuffer<600> jsonBuffer;
HTTPClient http;

void setup() {
 pinMode(rele, OUTPUT);
 digitalWrite(rele, HIGH);

 pinMode(sensorPin, INPUT);
 digitalWrite(sensorPin, HIGH);
 pulseCount        = 0;
 flowRate          = 0.0;
 flowMilliLitres   = 0;
 totalMilliLitres  = 0;
 oldTime           = 0;
 attachInterrupt(sensorInterrupt, pulseCounter, FALLING);

 SPI.begin();      // Initiate  SPI bus
 mfrc522.PCD_Init();

 WiFi.mode(WIFI_STA);
 WiFi.begin(ssid, ssidPassword);
 while (WiFi.status() != WL_CONNECTED) {
   delay(500);

 }

 char json[300];

 lcd.init();   // INICIALIZA O DISPLAY LCD
 lcd.backlight(); // HABILITA O BACKLIGHT (LUZ DE FUNDO)
 lcd.setCursor(0, 0); //SETA A POSIÇÃO EM QUE O CURSOR INCIALIZA(LINHA 1)
 lcd.print("* BREJARIA TERRES **"); //ESCREVE O TEXTO NA PRIMEIRA LINHA DO DISPLAY LCD
 lcd.setCursor(0, 1); //SETA A POSIÇÃO EM QUE O CURSOR RECEBE O TEXTO A SER MOSTRADO(LINHA 2)
 lcd.print("**** PURA PROSA ****"); //ESCREVE O TEXTO NA SEGUNDA LINHA DO DISPLAY LCD
}

void loop() {

 lcd.setCursor(0, 0); //SETA A POSIÇÃO EM QUE O CURSOR INCIALIZA(LINHA 1)
 lcd.print("* BREJARIA TERRES **"); //ESCREVE O TEXTO NA PRIMEIRA LINHA DO DISPLAY LCD
 lcd.setCursor(0, 1); //SETA A POSIÇÃO EM QUE O CURSOR RECEBE O TEXTO A SER MOSTRADO(LINHA 2)
 lcd.print("**** PURA PROSA ****"); //ESCREVE O TEXTO NA SEGUNDA LINHA DO DISPLAY LCD

 // SET TO ZERO ALL THE INFO OF FLOW SENSOR TO START EVERY NEW CARD
 pulseCount        = 0;
 flowRate          = 0.00;
 flowMilliLitres   = 0;
 totalMilliLitres  = 0;
 oldTime           = 0;
 totalml           = 0;
 valordinheiro     = 0;
 valorbebido       = 0;

 digitalWrite(rele, HIGH);

 jsonBuffer.clear();

 if ( ! mfrc522.PICC_IsNewCardPresent())
 {
   return;
 }
 // Select one of the cards
 if ( ! mfrc522.PICC_ReadCardSerial())
 {
   return;
 }
 String content = "";
 byte letter;
 for (byte i = 0; i < mfrc522.uid.size; i++)
 {
   content.concat(String(mfrc522.uid.uidByte[i], HEX));
 }
 String payload = "";
 http.begin("http://192.168.0.101:8095/pour-on/public/api/rfid/" + content); //Get Request to fetch all data in json format
 int httpCode = http.GET();
 if (httpCode > 0) {
   if (httpCode == HTTP_CODE_OK) {
     payload = http.getString();
   }

   if (payload.length() > 0) {
     JsonObject& root = jsonBuffer.parseObject(payload);
     if (!root.success()) {
       return;
     } else {
       //Serial.println("Data Fetched");
       nome = root[String("name")].as<const char*>();
       valordinheiro = (root["value"].as<const char*>());
       http.end();

       lcd.setCursor(0, 2);
       lcd.print("NOME: ");
       lcd.setCursor(6, 2);
       lcd.print(nome);
       lcd.setCursor(0, 3);
       lcd.print("R$");
       lcd.setCursor(2, 3);
       lcd.print(valordinheiro);


       // HERE START THE CODE WITH SENSOR AND RELAY
       if (valordinheiro > 0) { // IF THE VALUE OF MONEY ON THE CARD, FETCHED FROM DB IS >0. THIS GOES UNTIL THE PART BEFORE POST REQUEST
         digitalWrite(rele, LOW); // TURN RELAY ON TO ALLOW THE PERSON TO POUR ITS BEER
         for (int i = 0; i < 1000; i++) { // HERE IT STAYS READING THE FLOW SENSOR FOR A TIME TO SEE IF THE PERSON POURS THE BEER, BECAUSE IN CASE THE PERSON
           // PASSES THE CARD AND DOESN'T WANT ANYMORE THIS BEER THEN IT GOES STRAIGHT TO THE POST REQUEST

           if ((millis() - oldTime) > 100) {
             detachInterrupt(sensorInterrupt);
             flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
             oldTime = millis();
             flowMilliLitres = (flowRate / 60) * 1000;
             totalMilliLitres += flowMilliLitres;
             unsigned int frac;
             totalml = totalMilliLitres / 10;
             pulseCount = 0;
             attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
             delay(150);
           }

           if ( totalml > 1) {
             i = 1000;
           }
         }
         if (totalml > 1) { // HERE TOTALML HAS THE INFO Of THE FLOW SENSOR, TOTALML HAS THE TOTAL AMOUNT POURED OF THE BEER, IF THE PERSON HAS POURED SOME BEER IT COMES TO THIS.
           while (flowRate > 1) { //WHILE POURING THE BEER IT KEEPS SHOWING THE VALUE POURED UNTIL THE PERSON STOPS POURING THE BEER
             if ((millis() - oldTime) > 100) {
               detachInterrupt(sensorInterrupt);
               flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
               oldTime = millis();
               flowMilliLitres = (flowRate / 60) * 1000;
               totalMilliLitres += flowMilliLitres;
               unsigned int frac;
               totalml = totalMilliLitres / 10;
               pulseCount = 0;
               attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
               delay(150);
               totalmlcomp = totalml;
             }


             lcd.setCursor(8, 3);
             lcd.print("             ");
             lcd.print(valorbebido);

             if (valorbebido > *valordinheiro) {
               break;
             }
           }

           digitalWrite(rele, HIGH);
         }
       }

       //        Post Request
       http.begin("http://192.168.0.101:8095/pour-on/public/api/rfid/update");
       http.addHeader("Content-Type", "application/json");
       valorbebido = (totalml * valorml);
       String req = "{\"rfid\": \"" + content + "\" ,\"value\":" + valorbebido + "}";
       http.POST( req);
       http.end();
       payload = "{}";
       content = "";
     }
   } else {

   }

 }
 lcd.setCursor(0, 2); //SETA A POSIÇÃO EM QUE O CURSOR RECEBE O TEXTO A SER MOSTRADO(LINHA 3)
 lcd.print("                    "); // LIMPA LCD
 lcd.setCursor(0, 3); //SETA A POSIÇÃO EM QUE O CURSOR RECEBE O TEXTO A SER MOSTRADO(LINHA 4)
 lcd.print("                    "); // LIMPA LCD
}

void pulseCounter()
{
 // Increment the pulse counter
 pulseCount++;
}

(Code tags added by moderator)

unsigned long valorml = 0.02; // VALOR PAGO POR ML SERVIDO DE CHOPP

Very funny. What is this supposed to do?

       nome = root[String("name")].as<const char*>();

Why are you pissing away resources wrapping the string literal in a String that has to then be unwrapped?

             if (valorbebido > *valordinheiro) {

valordinheiro points to a string. It does not make sense to test that an int is greater than a string.

My code is supposed to work like this:

Instead, it does the hokey pokey?