1

我正在尝试将 JSON 发送到 ESP32 并将值设置为变量,对于前 3 个变量它工作正常,但由于某种原因它跳过了最后一个。现在它是在一个数组中发送的,但是当它不在一个数组中并且我将它设为 JSONObject 而不是 JSONArray 时,它甚至会跳过最后两个值。

在尝试硬编码 JSON 时,它可以正常工作。

这是代码:

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

这是我发送的 JSON:

    "colorValues": [
        255,
        255,
        255,
        255
        ]
}

这是 ESP32 收到的:

{"colorValues":[255,255,255,255]}33

这是结果:

green: 255
red: 255
blue: 255
white: 0
4

2 回答 2

0

好的,这是我遇到问题时的 JavaScript 代码示例,
var colors = {"red":255, "green":255, "blue":255};
只是将昏迷添加到后面,就像这样
var colors = {"red":255, "green":255, "blue":255,};

现在我的 arduino 可以找到我打算使用的蓝色

于 2020-09-28T12:53:52.683 回答
-1

如果您向对象添加一个像 115 这样的元素,我遇到了同样的问题并且很奇怪:

        111,
        112,
        113,
        114,
        115
        ]
}

然后它得到最后的 114 并且仍然没有得到 115 但至少你可以使用 4 个元素,我猜这与最后一个 elmenet 没有逗号有关

于 2020-09-28T12:45:04.463 回答