如何修复NodeMCU V3上不必要的函数调用和数据接收

时间:2019-05-19 18:01:41

标签: arduino-ide arduino-esp8266 arduino-c++

每次读取值时运行代码时,都会对cit_valori()函数进行不必要的调用,并且在读取值后,不会将它们正确发送到firebase数据库,当我检查代码时,它们是错误的数据库。

#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>

// Set these to run example.
#define FIREBASE_HOST <>
#define FIREBASE_AUTH <>
#define WIFI_SSID <>
#define WIFI_PASSWORD <>
#define MINUTES 6e7
#define nb_of_sensors 4
float air_hum, air_temp, soil_temp, soil_hum;

float SR_read(){
  while (!Serial.available()) {
  ; // wait for serial port to connect. Needed for native USB port only
}
  fflush(stdin);
  float value = Serial.read();
  fflush(stdin);
  return value;
}
void cit_valori()
{//se citesc de la arduinno datela tuturor senzorilor
    String valori;
    while (!Serial.available()) {
      ; // wait for serial port to connect. Needed for native USB port only
    }
    valori  = Serial.readString();
    fflush(stdin);
    /*while (Serial.available()) {
        Serial.write(Serial.read());
    }*/
    Serial.write("00");
      if(valori == "Trimit 1\n"){
        air_hum = SR_read();
        Serial.write("Confirm 1");
        delay(100);
      }else if(valori== "Trimit 2\n"){
        air_temp = SR_read();
        Serial.write("Confirm 2");
        delay(100);
      }else if(valori == "Trimit 3\n"){
        soil_hum = SR_read();
        Serial.write("Confirm 3");
        delay(100);
      }else if(valori == "Trimit 4\n"){
        soil_temp = SR_read();
        Serial.write("Confirm 4");
        delay(100);
      }
}
void setup() {
//Conectare ESP la Arduino pentru a citi datele de la acesta

Serial.begin(115200);


//Conectare ESP la WI-FI si transmitere de date intr-o baza de date google

//connect to wifi.
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("connecting");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.print("connected: ");
  Serial.println(WiFi.localIP());

  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}



void loop() {
int n=0;
if (!Serial.available()) {   
    while(++n <= nb_of_sensors*2)
      cit_valori();
}

    // append a new set of values
  String a = Firebase.pushFloat("Umiditate_aer", air_hum);
  // handle error
  if (Firebase.failed()) {
      Serial.print("pushing /logs failed:");
      Serial.println(Firebase.error());  
      return;
  }
  String b = Firebase.pushFloat("Umiditate_sol", soil_hum);
  // handle error
  if (Firebase.failed()) {
      Serial.print("pushing /logs failed:");
      Serial.println(Firebase.error());  
      return;
  }
  String c = Firebase.pushFloat("Temperatura_aer", air_temp);
  // handle error
  if (Firebase.failed()) {
      Serial.print("pushing /logs failed:");
      Serial.println(Firebase.error());  
      return;
  }
  String d = Firebase.pushFloat("Temperatura_sol", soil_temp);
  // handle error
  if (Firebase.failed()) {
      Serial.print("pushing /logs failed:");
      Serial.println(Firebase.error());  
      return;
  }
  delay(1000);

  //
  //
  //Parte pentru deep sleep NodeMCU
  //  Pinul D0 trebuie conectat la pinul de reset pentru a putea functiona
  //
  ESP.deepSleep(0.1*MINUTES,WAKE_RF_DEFAULT);
}

当我调用函数时,我应该看到如下输出: 00确认100确认200确认300确认4

但是我得到: 00确认10000确认20000确认30000确认400

0 个答案:

没有答案