Я хочу подключить два датчика расхода воды yf-s201, один как приток, а другой как отток, используя Nodemcu & blynk - PullRequest
0 голосов
/ 13 февраля 2020

Это в основном копия-вставка кода, который я нашел в Google,

Я хочу создать проект, использующий 2 датчика потока воды, в котором приток () показывает, сколько литров я взял in и outflow (), который показывает, сколько литров вылетело.

Это то, как далеко я достиг, Пожалуйста, помогите с кодом, пожалуйста, я не продвинутый кодер, поэтому описательный код и поддержка высоко ценятся , см. также основной код (), в этом разделе я пытаюсь достичь al oop, я имею в виду, если датчик 1 высокий, он должен отображать выходной сигнал датчика 1 (приток ()), а если датчик 2 высокий, он должен отображать датчик 2 (отток) ()) вывод.

Проблемы, с которыми сталкиваются: вывод не работает, когда я вызываю одновременно функции inflow () и outflow (), одна функция работает (я думаю, что она как-то связана с контактами прерывания платы?) .


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "SECRET";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wifi";
char pass[] = "password";

//byte statusLed    = 13;

byte inFlowSensor = D2;  
byte outFlowSensor= D3;

float calibrationFactor = 4.5;
BlynkTimer timer;

volatile byte pulseCount;  

float inFlowRate; // V2 - inflowrate 
float outFlowRate; // V4 - outFowRate
boolean sensorInput = 0;
unsigned int inFlowMilliLitres; 
unsigned int outFlowMilliLitres; 
unsigned long inTotalMilliLitres; // V1 - inTotalLitres
//unsigned long totalLitres;
unsigned long outTotalMilliLitres; // V3 - outTotalLitres
unsigned long oldTime;

BLYNK_CONNECTED() { // runs once at device startup, once connected to server.

  Blynk.syncVirtual(V1); //gets last known value of V1 virtual pin
  Blynk.syncVirtual(V3); //gets last known value of V4 
}

BLYNK_WRITE(V1)
{
  inTotalMilliLitres = param.asFloat();

}

BLYNK_WRITE(V2)
{
  inFlowRate = param.asFloat();

}

BLYNK_WRITE(V3)
{
  outTotalMilliLitres = param.asFloat();

}

BLYNK_WRITE(V4)
{
  outFlowRate = param.asFloat();

}


BLYNK_WRITE(V5) {  // reset all data with button in PUSH mode on virtual pin V4
  int resetdata = param.asInt();
  if (resetdata == 0) {
    Serial.println("Clearing Data");
    Blynk.virtualWrite(V1, 0);
    Blynk.virtualWrite(V2, 0);
    inFlowRate = 0;
    outFlowRate = 0;
    inFlowMilliLitres = 0;
    outFlowMilliLitres = 0;
    inTotalMilliLitres = 0;
    outTotalMilliLitres = 0;
    //totalLitres = 0;
    //totalLitresold = 0;
  }
}

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

void inflow()
{

   if((millis() - oldTime) > 1000)    // Only process counters once per second
  { 
    detachInterrupt(inFlowSensor);

    inFlowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;

    oldTime = millis();

    inFlowMilliLitres = (inFlowRate / 60) * 1000;

    // Add the millilitres passed in this second to the cumulative total
    inTotalMilliLitres += inFlowMilliLitres;

    unsigned int frac;

    // Print the flow rate for this second in litres / minute
    Serial.print("Flow rate: ");
    Serial.print(int(inFlowRate));  // Print the integer part of the variable
    Serial.print(".");             // Print the decimal point
    // Determine the fractional part. The 10 multiplier gives us 1 decimal place.
    frac = (inFlowRate - int(inFlowRate)) * 10;
    Serial.print(frac, DEC) ;      // Print the fractional part of the variable
    Serial.print("L/min");
    // Print the number of litres flowed in this second
    Serial.print("  Current Fuel Flowing: ");             // Output separator
    Serial.print(inFlowMilliLitres);
    Serial.print("mL/Sec");

    // Print the cumulative total of litres flowed since starting
    Serial.print("  Input Fuel Quantity: ");             // Input separator
    Serial.print(inTotalMilliLitres);
    Serial.println("mL"); 

    // Reset the pulse counter so we can start incrementing again
    pulseCount = 0;

    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(inFlowSensor, pulseCounter, FALLING);
  }
}

void outflow()
{

   if((millis() - oldTime) > 1000)    // Only process counters once per second
  { 
    detachInterrupt(outFlowSensor);

    outFlowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;

    oldTime = millis();

    outFlowMilliLitres = (outFlowRate / 60) * 1000;

    // Add the millilitres passed in this second to the cumulative total
    outTotalMilliLitres += outFlowMilliLitres;

    unsigned int frac;

    // Print the flow rate for this second in litres / minute
    Serial.print("Flow rate: ");
    Serial.print(int(outFlowRate));  // Print the integer part of the variable
    Serial.print(".");             // Print the decimal point
    // Determine the fractional part. The 10 multiplier gives us 1 decimal place.
    frac = (outFlowRate - int(outFlowRate)) * 10;
    Serial.print(frac, DEC) ;      // Print the fractional part of the variable
    Serial.print("L/min");
    // Print the number of litres flowed in this second
    Serial.print("  Current Fuel Flowing: ");             // Output separator
    Serial.print(outFlowMilliLitres);
    Serial.print("mL/Sec");

    // Print the cumulative total of litres flowed since starting
    Serial.print("  Out Fuel Quantity: ");             // Input separator
    Serial.print(outTotalMilliLitres);
    Serial.println("mL"); 

    // Reset the pulse counter so we can start incrementing again
    pulseCount = 0;

    // Enable the interrupt again now that we've finished sending output
    attachInterrupt(outFlowSensor, pulseCounter, FALLING);
  }
}


void sendtoBlynk()  // In this function we are sending values to blynk server
{
  Blynk.virtualWrite(V2, inFlowRate);
  Blynk.virtualWrite(V1, inTotalMilliLitres);
  Blynk.virtualWrite(V4, outFlowRate);      
  Blynk.virtualWrite(V3, outTotalMilliLitres);
}

void setup()
{

  Serial.begin(9600); //38400
  Blynk.begin(auth,ssid,pass);
  Serial.println("Setup Started");

  pulseCount        = 0;
  inFlowRate          = 0.0;
  outFlowRate          = 0.0;
  inFlowMilliLitres   = 0;
  outFlowMilliLitres   = 0;
  inTotalMilliLitres  = 0;
  outTotalMilliLitres  = 0;
  oldTime           = 0;

  attachInterrupt(inFlowSensor, pulseCounter, FALLING);
  //attachInterrupt(outFlowSensor, pulseCounter, FALLING);
  timer.setInterval(10000L, sendtoBlynk);
}

void maincode(){
  inflow();
  //outflow();
}

/**
 *  program loop
 */
void loop(){
  Blynk.run();
  timer.run();
  Serial.println("Timer and Blynk Started");
  Serial.println(inFlowSensor);
  Serial.println(outFlowSensor);
  maincode();
}```
...