71 lines
1.9 KiB
C++
71 lines
1.9 KiB
C++
/**
|
|
* Official proxy code for iTender GPIO Communication
|
|
**/
|
|
#include <ArduinoJson.h>
|
|
|
|
// Define the size of the JSON buffer
|
|
#define JSON_BUFFER_SIZE 256
|
|
|
|
// 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() {
|
|
// Initialize serial communication
|
|
Serial.begin(9600);
|
|
}
|
|
|
|
void(* resetFunc) (void) = 0; //declare reset function @ address 0
|
|
|
|
void loop() {
|
|
// Wait for a new line on the serial console
|
|
if (Serial.available()) {
|
|
// Read the incoming JSON message
|
|
DeserializationError error = deserializeJson(incomingJson, Serial);
|
|
if (error) {
|
|
// Handle error
|
|
} else {
|
|
// 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;
|
|
}
|
|
|
|
// Prepare the outgoing JSON message
|
|
outgoingJson["id"] = id;
|
|
outgoingJson["type"] = type;
|
|
outgoingJson["data"] = "";
|
|
|
|
// Send the outgoing JSON message
|
|
serializeJson(outgoingJson, Serial);
|
|
}
|
|
}
|
|
} |