Как стартер:
Сначала отправьте правильный (синтаксис!) Тестовый запрос
curl \
request POST \
header "Content-Type:application/json" \
data '{"A":"B","C":"D"}' \
"http://192.168.4.1/myEndpoint"
есть множество отличных примеров, если вы выполните поиск:
arduino webserver ethe rnet library
в вашем любимом поисковике.
Один из них: https://startingelectronics.org/tutorials/arduino/ethernet-shield-web-server-tutorial/
или вы используете библиотеку веб-сервера из esp8266 и адаптируете ее (не совсем hard imho)
вы бы сделали на Arduino что-то вроде
webServer.on("/myroute/lighton", HTTP_POST, readJson);
char jsonField[64] = '\0'; //This is a predefined buffer for data handilng
функция будет выглядеть (частично рабочий код, частично псевдокод)
bool readJson(){
if (webserver.args() == 0) return false; // we could do in the caller an error handling on that
strcpy (jsonField, webserver.arg(1).c_str()); // here we copy the json to a buffer
/** Get rid of starting and finishing bracket and copy to */
strncpy(jsonField , jsonField + 1, strlen(jsonField) - 2);
jsonField[strlen(jsonField) - 2] = '\0';
uint16_t maxIndex = strlen(jsonField); // number of characters received - without the brackets
uint16_t index = 0;
int16_t nextIndex = 0;
uint8_t i = 0;
// In this routine we get the value pairs e.g. "A":"B"
while ((nextIndex != -1) && (nextIndex < maxIndex)) {
nextIndex = indexOf(jsonField, ',', index);
... the next step would be to process the value pairs by stripping the " and split on the ':' delimiter --
if you need just the values = content in your example B and D its easy,
you could do
if (strcmp (firstValofPair ,'A')==0) valueB = atoi(B); // given B is a number and we have to convert from char to int
.... some more logic and you have a simple reliable JSON Parser for all kind of web server usage
}
return true; // success parsing
}
I реализовали этот вид логики c в некоторых реальных сценариях ios, и все они работают надежно и стабильно уже несколько лет. Последний совет:
Никогда не используйте класс Arduino String в сценарии веб-сервера Arduino ios. Класс String ломает вашу кучу и разбивает Arduino. В моем примере я использую фиксированные символы, которые компилируются в стек и сохраняют вашу память счастливой.