首页 > 解决方案 > 我想使用 Nodemcu 和 blynk 连接两个 yf-s201 水流传感器,一个作为流入,另一个作为流出

问题描述

这主要是我在谷歌上找到的代码的复制粘贴,

我想使用 2 个水流传感器制作一个项目,其中 inflow() 显示我吸入了多少升,outflow() 显示流出了多少升。

这是我达到的程度,需要代码方面的帮助,我不是高级编码人员,因此非常感谢描述性代码和支持。另请参阅 maincode(),在该部分中我试图实现一个循环,我的意思是如果传感器 1 为高,则应显示传感器 1(流入())输出,如果传感器 2 为高,则应显示传感器 2(流出() ) 输出。

面临的问题:当我同时调用流入()和流出()时,输出不起作用,一个函数起作用,(我认为它与板的中断引脚有关?)。


#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();
}```

标签: c++arduinoesp8266nodemcublynk

解决方案


推荐阅读