Dear Community,
Please let me share a
library for implementing JSON-RPC API on low end MCUs.
Examples, coming with the library, illustrate how to create JSON-RPC API on Arduino Mega and Particle Photon boards.
Here is a code snippet from Arduino example:
/* class, implementing RPC call digitalWrite */
class DigitalWrite : public Procedure {
public:
typedef DigitalWrite self;
/* micurpc calls this method to read procedure parameters */
bool read_params(lexer& in) noexcept {
return
Params<self, uint8_t, uint8_t>:: /* defines parameter types */
Names<name::pin, name::val>:: /* defines parameter names */
FieldPointers<&self::pin,&self::val>/* binds parameters to members*/
::json().read(*this, in); /* reads JSON */
}
/* micurpc calls this method to write result */
bool write_result(ostream& out) const noexcept {
/* result must be a JSON value (in its wide sense) */
return write(val, out);
}
/* micurpc calls this method to run the procedure */
void run(response::error_t& error) noexcept {
if(! digitalPinIsOK(pin, error)) return;
if( val != 0 && val != 1 ) {
invalidParameter(error);
return;
}
digitalWrite(pin, val);
val = digitalRead(pin);
}
/* defines a literal name for this procedure */
static cstring method() noexcept { return name::digitalWrite(); }
private:
uint8_t pin = -1; /* invalid initial values indicated missing parameter */
uint8_t val = -1;
};
And some screenshots:

