From a580f1ee734c06868b812db2b0bd904b0082074f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Philippe=20C=C3=B4t=C3=A9?= Date: Tue, 29 Mar 2022 17:36:53 -0400 Subject: [PATCH] Create wifinina.ino Basic example for Arduino Uno WiFi rev2 or other boards using the WiFiNINA lib --- examples/wifinina/wifinina.ino | 98 ++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 examples/wifinina/wifinina.ino diff --git a/examples/wifinina/wifinina.ino b/examples/wifinina/wifinina.ino new file mode 100644 index 0000000..b3c8cbc --- /dev/null +++ b/examples/wifinina/wifinina.ino @@ -0,0 +1,98 @@ +/* + This a simple example of the aREST Library for the Arduino Uno WiFi rev2. + See the README file for more details. +*/ + +// Import required libraries +#include +#include +#include + +// Status +int status = WL_IDLE_STATUS; + +// Create aREST instance +aREST rest = aREST(); + +// WiFi parameters +char ssid[] = "network name"; +char password[] = "network password"; + +// The port to listen for incoming TCP connections +#define LISTEN_PORT 80 + +// Create an instance of the server +WiFiServer server(LISTEN_PORT); + +// Variables to be exposed to the API +int temperature; +int humidity; + +// Declare functions to be exposed to the API +int ledControl(String command); + +void setup(void) +{ + // Start Serial + Serial.begin(57600); + + // Init variables and expose them to REST API + temperature = 24; + humidity = 40; + rest.variable("temperature",&temperature); + rest.variable("humidity",&humidity); + + // Function to be exposed + rest.function("led", ledControl); + + // Give name and ID to device (ID should be 6 characters long) + rest.set_id("1"); + rest.set_name("mkr1000"); + + // Connect to WiFi + while (status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(ssid); + status = WiFi.begin(ssid, password); + + // Wait 10 seconds for connection: + delay(10000); + } + Serial.println("WiFi connected"); + + // Start the server + server.begin(); + Serial.println("Server started"); + + // Print the IP address + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); +} + +void loop() { + + // Handle REST calls + WiFiClient client = server.available(); + if (!client) { + return; + } + while(!client.available()){ + delay(1); + } + rest.handle(client); + +} + +// Custom function accessible by the API +int ledControl(String command) { + + Serial.println(command); + + // Get state from command + int state = command.toInt(); + + digitalWrite(6, state); + return 1; + +}