Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create wifinina.ino #304

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions examples/wifinina/wifinina.ino
Original file line number Diff line number Diff line change
@@ -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 <SPI.h>
#include <WiFiNINA.h>
#include <aREST.h>

// 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;

}