Keyserv

FREE
By keyserv | Updated לפני חודשיים | Tools
Health Check

N/A

Back to All Tutorials (3)

Arduino

1) Checking if serial is current

In these two examples we will be using an Arduino Ethernet Shield 2 to make GET requests to the server. Now because our given microcontroller doesn’t have the requisite horse-power to handle an HTTPS request we will be doing it over simple port 80, HTTP. Observe that we are passing query parameter “default=false” in order to pass through HTTP requests. Sample serial: 63533594-b30c-430f-a6fd-7472d1829d0d used. We are also using the gold standard in JSON deserialization ArduinoJson (v6) on the microcontroller. You can use the handy calculator they have available online: https://arduinojson.org/v6/assistant to compute the DynamicJsonDocument size.

// #include <SPI.h>
#include <Ethernet2.h>
#include <ArduinoJson.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don’t want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = “keyserv.solutions”; // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 10, 48);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

// The response string we get back from the server
String response = “”;

//Fetch “current” object from json
const size_t capacity = JSON_OBJECT_SIZE(1) + 10;

int l = 0;

void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);

// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println(“connecting…”);

// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println(“connected”);
// Make a HTTP request:
client.println(“GET /v1/KeysApi/Current/63533594-b30c-430f-a6fd-7472d1829d0d?default=false HTTP/1.1”);
client.println(“Host: keyserv.solutions”);
client.println(“Connection: close”);
client.println();
}
else {
// if you didn’t get a connection to the server:
Serial.println(“connection failed”);
}
}

void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
if (l > 0) {
response += c;
} else if (c == ‘{’) {
response += c;
l = response.length();
}
}

// if the server’s disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println(“disconnecting.”);
client.stop();

DynamicJsonDocument doc(capacity);
DeserializationError error = deserializeJson(doc, response);

// if no errors deserializing json:
if (!error) {

  JsonObject obj = doc.as<JsonObject>();

  // if the response contains \["current": true\]
  // therefore the key is currently valid:
  if (obj\["current"\] == true) {

    // switch on light
    digitalWrite(LED\_BUILTIN, HIGH);
    Serial.println("\\r\\n-- LIGHT ON --");
  } else {
    digitalWrite(LED\_BUILTIN, LOW);
    Serial.println("\\r\\n-- LIGHT OFF --");
  }

} else Serial.println(error.c\_str());

// do nothing forevermore:
while (true);

}
}

And this is what the Arduino Serial console outputs:

connecting…
connected

disconnecting.
{“current”:true}

– LIGHT ON –


2) Checking the expiry of a serial

// #include <SPI.h>
#include <Ethernet2.h>
#include <ArduinoJson.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don’t want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = “keyserv.solutions”; // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 10, 48);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

// The response string we get back from the server
String response = “”;

//Fetch “expires” and “time” objects from json
const size_t capacity = JSON_OBJECT_SIZE(2) + 62;

int l = 0;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}

// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println(“connecting…”);

// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println(“connected”);
// Make a HTTP request:
client.println(“GET /v1/KeysApi/Expiry/63533594-b30c-430f-a6fd-7472d1829d0d?default=false HTTP/1.1”);
client.println(“Host: keyserv.solutions”);
client.println(“Connection: close”);
client.println();
}
else {
// if you didn’t get a connection to the server:
Serial.println(“connection failed”);
}
}

void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
if (l > 0) {
response += c;
} else if (c == ‘{’) {
response += c;
l = response.length();
}
}

// if the server’s disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println(“disconnecting.”);
client.stop();

DynamicJsonDocument doc(capacity);
DeserializationError error = deserializeJson(doc, response);

// if no errors deserializing json:
if (!error) {
  JsonObject obj = doc.as<JsonObject>();
  Serial.println("Key expiration date: " + obj\["expires"\].as<String>());
  Serial.println("Time from now: " + obj\["time"\].as<String>());
} else Serial.println(error.c\_str());

// do nothing forevermore:
while (true);

}
}

And the Serial console displaying the deserialized values:

connecting…
connected

disconnecting.
Key expires at: 2021-06-01T20:39:00+02:00
Time from now: 731.00:00:00