如何将传感器的读数发送到API



错误是。expected ')' before rainrate。我需要POSTrainrate的值到我的API,然后在POST后重置雨率计数。我该怎么做呢?谢谢!

int httpResponseCode = http.POST("{"amount":"rainrate"}");
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
const char* ssid = "*************";
const char* password = "*************";
//Your Domain name with URL path or IP address with path
const char* serverName = "************************";
const byte interruptPin = 4;
const int interval = 500;
volatile unsigned long tiptime = micros();
static float rainrate;
//float totalrainrate = 0.3;
void ICACHE_RAM_ATTR count();
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 15000;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());

Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
// Set up our digital pin as an interrupt
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), count, RISING);
}
void loop() {
wifi();
rain();
}
void rain() {
unsigned long curtime = micros();

// Make sure we don't record bounces
if ((curtime - tiptime) < interval) {
return;
}
// How long since the last tip?
unsigned long tipcount = curtime - tiptime;
tiptime = curtime;

// Calculate mm/hr from period between cup tips
Serial.print("Rain rate: ");
Serial.print((float) rainrate * 0.1);
Serial.println("mm/hr");
delay (1000);
}
void count() {
//rainrate = totalrainrate - 1;
//rainrate - 1;
rainrate++;


}
void wifi() {
//Send an HTTP POST request every 10 minutes
if ((millis() - lastTime) > timerDelay) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;

// Your Domain name with URL path or IP address with path
http.begin(serverName);
// Specify content-type header
http.addHeader("Content-Type", "application/json");
// Data to send with HTTP POST
int httpResponseCode = http.POST("{"amount":"rainrate"}");

Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);

// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}

您正在尝试发送带有双引号的字符串,但没有转义传递给POST()的字符串中的所有引号。

可以这样做:

int httpResponseCode = http.POST("{"amount":"rainrate"}");

原始字符串终止于文本"rainrate"的开头。因此,解析器希望此时有一个关闭的)

最新更新