JSON to lekki i dość uniwersalny sposób wymiany danych komputerowych. Nie znalazłem tutorialu, który pokazałby jak przesyłać tego typu dane przez sieć opartą o moduł radiowy nRF24L01. O nRF24l01 pisałem wielokrotnie. Wykorzystując ciekawą bibliotekę bblanchon ArduinoJson i korzystając z pomocy niezawodnych kolegów z forum elektroda.pl udało mi się napisać program na arduino (TX) i nodeMCU (RX) które pokazują jak prosto można korzystać z dobrodziejstw formatu JSON. W przykładzie przesyłana jest prosta struktura danych:
temp: 35.33
hum: 77.77
light: 198
batt: 3.33
TX JSON – arduino
/* TX Json, arduino Nano, nRF24l01 */ #include <ArduinoJson.h> #include <SPI.h> #include <Wire.h> #include <RF24.h> #include <RF24Network.h> #include <Timers.h> Timers <1> akcja; RF24 radio(7, 8); RF24Network network(radio); // Include the radio in the network const uint16_t this_node = 01; // Address of this node in Octal format ( 04,031, etc) const uint16_t node00 = 00; // (Address where the data is going) void setup() { Serial.begin(115200); SPI.begin(); radio.begin(); radio.setPALevel(RF24_PA_HIGH); network.begin(2, this_node); //(channel, node address) akcja.attach(0, 5000, wyslij_co5sek); // inicjalizacja procesu } void loop() { network.update(); akcja.process(); //pooling timers } // ======================== FUNKCJE =============================== void wyslij_co5sek() { //wlasny podprogram do wykonania co 5s biblioteka Timers.h DynamicJsonDocument doc(200); doc["temp"] = 33.33 + random(10); doc["hum"] = 77.77; doc["light"] = 198; doc["batt"] = 3.33; String output = ""; serializeJson(doc, output); // tworzenie dokumentu Json w zmiennej output Serial.println(output); RF24NetworkHeader header(node00); // (Address where the data is going) bool ok = network.write(header, output.c_str(), output.length()); // Send the data if (ok) { Serial.println("Poszło do node00"); } else { Serial.println("Brak kontaktu z node00!"); } }
RX JSON – nodeMCU ESP8266
/* RX JSON, nodeMCU ESP8266 */ #include <SPI.h> #include <Wire.h> #include <RF24.h> #include <RF24Network.h> #include <ArduinoJson.h> RF24 radio(2, 0); // D4, D3 nRF24L01 (CE,CSN), D1, D2 zwolnione dla I2C RF24Network network(radio); // Include the radio in the network const uint16_t this_node = 00; // Address of this node in Octal format ( 04,031, etc) const uint16_t node01 = 01; // (Address where the data is going) char incomingData[64]; void setup() { Serial.begin(115200); SPI.begin(); radio.begin(); radio.setPALevel(RF24_PA_HIGH); network.begin(2, this_node); //(channel, node address) } void loop() { network.update(); while ( network.available() ) { // Is there any incoming data? RF24NetworkHeader header; network.read(header, &incomingData, sizeof(incomingData)); // Read the incoming data if (header.from_node == 1) { // If data comes from Node 01 Serial.println("Przyszło z node01"); //Serial.println(incomingData); // ================== sekcja Json ================= //StaticJsonDocument<200> doc; DynamicJsonDocument doc(200); DeserializationError error = deserializeJson(doc, incomingData); if (error) { Serial.print(F("ESP deserializeJson() failed: ")); Serial.println(error.c_str()); return; } serializeJsonPretty(doc, Serial); Serial.println(); float hum = doc["hum"]; Serial.print("Wilgotność = "); Serial.println(hum); Serial.println(); } } }
W formacie JSON wygląda ona tak:
{
„temp”: 35.33,
„hum”: 77.77,
„light”: 198,
„batt”: 3.33
}
Odwołanie do dowolnego elementu struktury po wywołaniu metody deserializeJson(doc, incomingData) wygląda tak:
float temperatura = doc["temp"];
Serial.print("Temperatura = ");
Serial.println(temp);
Serial.println();