[英]ESP32 skips last value in JSON
I'm trying to send a JSON to a ESP32 and set the value to a variable, for the first 3 variables it works fine, but for some reason it skips the last one.我正在尝试将 JSON 发送到 ESP32 并将值设置为变量,对于前 3 个变量它工作正常,但由于某种原因它跳过了最后一个。 Right now it's sent in an array, but when it's not in an array and I make it a JSONObject instead of a JSONArray it even skips the last 2 values.
现在它以数组形式发送,但是当它不在数组中并且我将其设为 JSONObject 而不是 JSONArray 时,它甚至会跳过最后 2 个值。
When trying a hardcoded JSON it works fine though.尝试使用硬编码的 JSON 时,它运行良好。
Here is the code:这是代码:
#include <Arduino.h>
#include <EEPROM.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <NeoPixelBus.h>
#include <AsyncJson.h>
#include <ArduinoJson.h>
#define EEPROM_SIZE 512
const int powerStateAddress = 0;
const int groupNumberAddress = 2;
const uint16_t pixelCount = 82;
const uint pixelPin = 17;
const uint relayPin = 26;
const char* ssid = "";
const char* password = "";
AsyncWebServer server(80);
const size_t jsonCapacity = JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(4) + 70;
int powerState;
int groupNumber;
int colorSaturation = 255;
int greenValue;
int redValue;
int blueValue;
int whiteValue;
NeoPixelBus<NeoGrbwFeature, NeoEsp32I2s1800KbpsMethod> strip(pixelCount, pixelPin);
RgbwColor green(colorSaturation, 0, 0, 0);
RgbwColor red(0, colorSaturation, 0, 0);
RgbwColor blue(0, 0, colorSaturation, 0);
RgbwColor white(0, 0, 0, colorSaturation);
RgbwColor black(0);
void setGroupNumber(DynamicJsonDocument json) {
groupNumber = json["groupNumber"];
EEPROM.write(groupNumberAddress, groupNumber);
EEPROM.commit();
}
void setColor() {
for(uint16_t pixel = 0; pixel < pixelCount; pixel++) {
strip.SetPixelColor(pixel, RgbwColor(greenValue, redValue, blueValue, whiteValue));
}
}
void setColorValues(DynamicJsonDocument json) {
JsonArray colorValues = json["colorValues"];
if(greenValue != colorValues[0]) {
greenValue = colorValues[0];
}
if(redValue != colorValues[1]) {
redValue = colorValues[1];
}
if(blueValue != colorValues[2]) {
blueValue = colorValues[2];
}
if(whiteValue != colorValues[3]) {
whiteValue = colorValues[3];
}
setColor();
}
void setBrightness(DynamicJsonDocument json) {
colorSaturation = json["brightness"];
setColor();
}
DynamicJsonDocument parseData(AsyncWebServerRequest *request, uint8_t *data, String endpoint) {
DynamicJsonDocument doc(jsonCapacity);
DeserializationError err = deserializeJson(doc, data);
Serial.println(serializeJson(doc, Serial));
if(err) {
request->send(400, "text/plain", "err on" + endpoint);
} else {
request->send(200, "application/json", "{'msg': 'OK'}");
}
return doc;
}
void setup() {
Serial.begin(115200);
while(!Serial) {}
EEPROM.begin(EEPROM_SIZE);
if(EEPROM.read(powerStateAddress) == LOW || EEPROM.read(powerStateAddress) == HIGH) {
powerState = EEPROM.read(powerStateAddress);
if(powerState == HIGH) {
digitalWrite(relayPin, HIGH);
}
} else {
powerState = LOW;
EEPROM.write(powerStateAddress, powerState);
EEPROM.commit();
}
if(EEPROM.read(groupNumberAddress) != 255) {
groupNumber = EEPROM.read(groupNumberAddress);
}
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(250);
}
Serial.println(WiFi.localIP());
server.on("/controller/setGroup", HTTP_POST, [](AsyncWebServerRequest *request) {}, NULL,
[](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
DynamicJsonDocument doc = parseData(request, data, "/setGroup");
setGroupNumber(doc);
});
server.on("/controller/setColor", HTTP_POST, [](AsyncWebServerRequest *request) {}, NULL,
[](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
DynamicJsonDocument doc = parseData(request, data, "/setColor");
setColorValues(doc);
});
server.begin();
}
void loop() {
Serial.println(powerState);
Serial.println(EEPROM.read(powerStateAddress));
Serial.println(groupNumber);
Serial.println(EEPROM.read(groupNumberAddress));
Serial.print("Brightness: ");
Serial.println(colorSaturation);
Serial.print("green: ");
Serial.println(greenValue);
Serial.print("red: ");
Serial.println(redValue);
Serial.print("blue: ");
Serial.println(blueValue);
Serial.print("white: ");
Serial.println(whiteValue);
Serial.println("---------");
delay(2000);
}
Here is the JSON that I send:这是我发送的 JSON:
"colorValues": [
255,
255,
255,
255
]
}
This is what the ESP32 receives:这是 ESP32 收到的:
{"colorValues":[255,255,255,255]}33
And this is the result:这是结果:
green: 255
red: 255
blue: 255
white: 0
Okay here is an example of my JavasScript code when I ran into the issue好的,这是我遇到问题时的 JavaScript 代码示例
var colors = {"red":255, "green":255, "blue":255};
is just added the coma to the back like so只是像这样将昏迷添加到后面
var colors = {"red":255, "green":255, "blue":255,};
and now my arduino can find the blue I intend to use现在我的 arduino 可以找到我打算使用的蓝色
I ran into the same issue and weird enough if you add an element to the object like the 115:如果你向对象添加一个元素,比如 115,我遇到了同样的问题并且很奇怪:
111,
112,
113,
114,
115
]
}
Then it gets the last 114 and still doesn't get the 115 but atleast you can use you 4 elements, I am guessing it is somethint to do with the last elmenet doesn't have a comma然后它得到最后一个 114 并且仍然没有得到 115 但至少你可以使用你 4 个元素,我猜这与最后一个 elmenet 没有逗号有关
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.