edit arduino code
Took 5 seconds
This commit is contained in:
parent
8abb7737c6
commit
20def40001
@ -1,39 +1,71 @@
|
|||||||
/**
|
/**
|
||||||
* Official proxy code for iTender GPIO Communication
|
* Official proxy code for iTender GPIO Communication
|
||||||
**/
|
**/
|
||||||
|
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
const int BUFFER_SIZE = 256;
|
// Define the size of the JSON buffer
|
||||||
char buffer[BUFFER_SIZE];
|
#define JSON_BUFFER_SIZE 256
|
||||||
int bufferIndex = 0;
|
|
||||||
|
// Create a JSON object for incoming messages
|
||||||
|
StaticJsonDocument<JSON_BUFFER_SIZE> incomingJson;
|
||||||
|
|
||||||
|
// Create a JSON object for outgoing messages
|
||||||
|
DynamicJsonDocument<JSON_BUFFER_SIZE> outgoingJson;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
// Initialize serial communication
|
||||||
|
Serial.begin(9600);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void(* resetFunc) (void) = 0; //declare reset function @ address 0
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if (Serial.available() > 0) {
|
// Wait for a new line on the serial console
|
||||||
char incomingByte = Serial.read();
|
if (Serial.available()) {
|
||||||
buffer[bufferIndex] = incomingByte;
|
// Read the incoming JSON message
|
||||||
bufferIndex++;
|
DeserializationError error = deserializeJson(incomingJson, Serial);
|
||||||
|
|
||||||
if (incomingByte == '\n') {
|
|
||||||
buffer[bufferIndex] = '\0';
|
|
||||||
bufferIndex = 0;
|
|
||||||
|
|
||||||
StaticJsonDocument<200> doc;
|
|
||||||
DeserializationError error = deserializeJson(doc, buffer);
|
|
||||||
if (error) {
|
if (error) {
|
||||||
Serial.print(F("deserializeJson() failed: "));
|
// Handle error
|
||||||
Serial.println(error.c_str());
|
} else {
|
||||||
return;
|
// Extract the "type" and "data" fields from the JSON object
|
||||||
|
String id = incomingJson["id"];
|
||||||
|
String type = incomingJson["type"];
|
||||||
|
JsonVariant data = incomingJson["data"];
|
||||||
|
|
||||||
|
// Create a nested object in the root object
|
||||||
|
JsonObject outgoingData = outgoingJson.to<JsonObject>().createNestedObject("data");
|
||||||
|
|
||||||
|
outgoingData["success"] = true;
|
||||||
|
|
||||||
|
// Handle the message based on the "type" field
|
||||||
|
switch (type) {
|
||||||
|
case "ACK":
|
||||||
|
// Handle ACK message
|
||||||
|
break;
|
||||||
|
case "SET_PIN":
|
||||||
|
// Handle SET_PIN message
|
||||||
|
break;
|
||||||
|
case "GET_VAL":
|
||||||
|
// Handle GET_VAL message
|
||||||
|
break;
|
||||||
|
case "GET_SENSOR":
|
||||||
|
// Handle GET_SENSOR message
|
||||||
|
break;
|
||||||
|
case "RESTART":
|
||||||
|
resetFunc(); //call reset
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// Handle unknown message type
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do something with the deserialized JSON object
|
// Prepare the outgoing JSON message
|
||||||
int value = doc["value"];
|
outgoingJson["id"] = id;
|
||||||
Serial.println(value);
|
outgoingJson["type"] = type;
|
||||||
}
|
outgoingJson["data"] = "";
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Send the outgoing JSON message
|
||||||
|
serializeJson(outgoingJson, Serial);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user