Hi, I feel really stupid need to post this question.
I'm trying to get value from a Json UDP string. The string, serialized using ArduinoJson looks in the UDP receive buffer like:
{"value":42,"lat":48.74801,"lon":2.293491}
However, I cannot extract the values (value, lat and lon).
My code below:
#define ARDUINOJSON_ENABLE_ARDUINO_STREAM 1
#include <ArduinoJson.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <WiFiUdp.h>
int status = WL_IDLE_STATUS;
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key index number (needed only for WEP)
IPAddress myIP(192, 168, 68, 77);
IPAddress remoteIP(192, 168, 68, 78); // Top unit
unsigned short remotePort = 2390; // Top unit port
unsigned int localPort = 2290; // local port to listen on
const int capacity = JSON_OBJECT_SIZE(3);
StaticJsonDocument doc;
char packetBuffer[256]; //buffer to hold incoming packet
char ReplyBuffer[] = "acknowledged"; // a string to send back
WiFiUDP Udp;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
// attempt to connect to WiFi network:
WiFi.config(myIP); // configur static IP address
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println("Conn=ected to WiFi");
printWifiStatus();
Serial.print("capacity is: ");
Serial.println(capacity);
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
Udp.begin(localPort);
}
void loop() {
// Use Assistant | ArduinoJson 6 to compute the capacity.
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remoteIp = Udp.remoteIP();
Serial.print(remoteIP);
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
int len = Udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
Serial.print("packetlengt is: ");
Serial.println(len);
Serial.println("Contents:");
Serial.println(packetBuffer);
DeserializationError error = deserializeMsgPack(doc, packetBuffer);
// char[] = {"value":42,"lat":48.74801,"lon":2.293491}
Serial.print("Json error is: ");
Serial.println(error.c_str());
if (error) {
Serial.print(F("deserializeJson() failed with code: "));
Serial.println(error.c_str());
}
// Log
// Serial.print(F("Sending to "));
// Serial.print(remoteIP);
// Serial.print(F(" on port "));
// Serial.println(remotePort);
// serializeJson(doc, Serial);
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(remoteIP, remotePort);
Udp.write(ReplyBuffer);
//serializeJson(doc, Udp);
//Udp.println();
Udp.endPacket();
const char* retrievedValue = doc["value"];
long retrievedLat = doc["lat"];
long retrievedLon = doc["lon"];
Serial.print("Value is: ");
Serial.println(*retrievedValue);
Serial.print("Lat is: ");
Serial.println(retrievedLat);
Serial.print("Lon is: ");
Serial.println(retrievedLon);
}
//delay(5000);
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void waitForSerial(){
while (!Serial.available()) {
}
Serial.println(Serial.read());
}