vincent wong
Published © GPL3+

Control your MKR1000 with aREST Framework

You can control your MKR1000 remotely with a RESTFUL interface.

BeginnerShowcase (no instructions)8,092
Control your MKR1000 with aREST Framework

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
×1
LED (generic)
LED (generic)
×2
Resistor 220 ohm
Resistor 220 ohm
×2
Jumper wires (generic)
Jumper wires (generic)
×5

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Breadboard

Schematic

Code

MKR100 WiFi aREST skecth

Arduino
// Import required libraries
#include <SPI.h>
#include <WiFi101.h>

#define DEBUG_MODE 0

#include <aREST.h>

// Create aREST instance
aREST rest = aREST();

char ssid[] = "your_network_ssid";      // your network SSID (name)
char pass[] = "your_network_password";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

WiFiServer restServer(80);

void setup(void)
{
  // Function to be exposed
  rest.function("led",ledControl);

  // Give name and ID to device
  rest.set_id("008");
  rest.set_name("mighty_cat");

  // Start Serial
  Serial.begin(115200);

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  Serial.println();

  // you're connected now, so print out the status:
  printWifiStatus();

  // Start server
  restServer.begin();
  Serial.println(F("Listening for connections..."));

  // Enable watchdog
  //wdt_enable(WDTO_4S);
}

void loop() {

  // Handle REST calls
  WiFiClient client = restServer.available();
  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(5,state);
  return 1;
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  IPAddress subnet = WiFi.subnetMask();
  Serial.print("Netmask: ");
  Serial.println(subnet);

  IPAddress gateway = WiFi.gatewayIP();
  Serial.print("Gateway: ");
  Serial.println(gateway);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Credits

vincent wong

vincent wong

80 projects • 203 followers

Comments