我得到了这个学校项目,我制作了这个与谷歌表格链接的简单代码,问题是一个传感器给了我正确的读数,而另一个传感器只显示“0” 传感器本身工作正常,接线正常并且校准良好。我已经只使用了代码的 ph 部分来查看读数,并且确实有效,显示了 0 以上的不同值
#include <Arduino.h>
#include <WiFi.h>
#include "time.h"
#include <ESP_Google_Sheet_Client.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <GS_SDHelper.h>
#define WIFI_SSID "Sample"
#define WIFI_PASSWORD "Sample123"
#define PROJECT_ID "SampleIDLOL"
#define CLIENT_EMAIL "SampleMailLOL"
const char PRIVATE_KEY[] PROGMEM = "SampleKeyLOL";
const char spreadsheetId[] = "SampleIDLOL";
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;
void tokenStatusCallback(TokenInfo info);
int phpin = 4;
int tempPin = 5;
int redPin = 18;
int greenPin = 19;
int bluePin = 21;
int buzzer = 13;
float calibrationFactor = 3.5;
float offset = 0.0
OneWire oneWireObjeto(tempPin);
DallasTemperature sensorDS18B20(&oneWireObjeto);
const char* ntpServer = "pool.ntp.org";
unsigned long epochTime;
unsigned long getTime() {
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
//Serial.println("Failed to obtain time");
return(0);
}
time(&now);
return now;
}
void setup() {
pinMode(phpin,INPUT);
pinMode(tempPin, INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
Serial.println();
Serial.println();
configTime(-18000, 0, ntpServer);
GSheet.printf("ESP Google Sheet Client v%s\n\n", ESP_GOOGLE_SHEET_CLIENT_VERSION);
WiFi.setAutoReconnect(true);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
GSheet.setTokenCallback(tokenStatusCallback);
GSheet.setPrerefreshSeconds(10 * 60);
GSheet.begin(CLIENT_EMAIL, PROJECT_ID, PRIVATE_KEY);
sensorDS18B20.begin();
}
void loop() {
sensorDS18B20.requestTemperatures();
float temperature = (sensorDS18B20.getTempCByIndex(0));
int rawValue = analogRead(phpin);
float voltage = (rawValue * 5.0) / 10230.;
float phValue = (voltage * calibrationFactor) + offset;
bool ready = GSheet.ready();
if (ready && millis() - lastTime > timerDelay) {
lastTime = millis();
FirebaseJson response;
Serial.println("\nAppend spreadsheet values...");
Serial.println("----------------------------");
FirebaseJson valueRange;
epochTime = getTime();
valueRange.add("majorDimension", "COLUMNS");
valueRange.set("values/[0]/[0]", epochTime);
valueRange.set("values/[1]/[0]", phValue);
valueRange.set("values/[2]/[0]", temperature);
bool success = GSheet.values.append(&response /* returned response */, spreadsheetId /* spreadsheet Id to append */, "A3" /* range to append */, &valueRange /* data range to append */);
if (success) {
response.toString(Serial, true);
valueRange.clear();
}
else {
Serial.println(GSheet.errorReason());
}
Serial.println();
Serial.println(ESP.getFreeHeap());
}
if (phValue >= 7) {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);
digitalWrite(buzzer, HIGH);
}
if (phValue >= 3 && phValue < 7) {
digitalWrite(redPin, LOW);
digitalWrite(greenPin,LOW);
digitalWrite(bluePin, LOW);
}
if (phValue >= 0 && phValue < 3) {
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
}
if (phValue > 6.5) {
digitalWrite(buzzer, HIGH);
}
}
void tokenStatusCallback(TokenInfo info) {
if (info.status == token_status_error) {
GSheet.printf("Token info: type = %s, status = %s\n", GSheet.getTokenType(info).c_str(), GSheet.getTokenStatus(info).c_str());
GSheet.printf("Token error: %s\n", GSheet.getTokenError(info).c_str());
}
else {
GSheet.printf("Token info: type = %s, status = %s\n", GSheet.getTokenType(info).c_str(), GSheet.getTokenStatus(info).c_str());
}
}
谷歌表的代码运行良好,其他传感器读数与纪元时间一起显示良好。 ph 读数每次都显示“0”,不仅在谷歌表中,而且在串行监视器中(如果我包含它)
除了许多代码样式问题之外,在我看来,您已经除以大数(
float voltage = (rawValue * 5.0) / 10230.
:10230.0)。这会导致一个数字始终在0和1之间,即使在校准因子和偏移量之后也是如此添加,始终打开红色 LED。
在排除这些类型的错误时,您应该打印出电压和 pH 值,以查看它们的值以及您的系统如何反应。当您让编译器进行从浮点/双精度到整数的隐式类型转换时,这也将帮助您解决舍入错误。