marcoschwartz / aREST

A RESTful environment for Arduino
http://aREST.io/
Other
1.2k stars 279 forks source link

Request example sketch for local use of API key #292

Closed cbitterfield closed 3 years ago

cbitterfield commented 3 years ago

Please setup an example for MKR 1010 (using WiFiNINA) v (WiFi) that has an API key for local authentication.

The arest.io website is not working.

the function rest.setKey(key,client) does not seem to work. [no matching function for call to 'aREST::aREST(PubSubClient&)']

I am not sure what this construct does:

// Clients
WiFiClient espClient;
PubSubClient client(espClient);

I have the API working with id and name:

// Setup RESET API
// Give name and ID to device (ID should be 6 characters long)
 rest.set_id("008");
 rest.set_name("unlaiden_swallow");

Platform: MKR 1010 OSX IDE (1.8.2) arest 2.9.0

source_code (minus config files)

/*
 * Rest API for sensors
 * Boat Automation Project v1.0
 * Created December 28,2020
 * Updated xxxxxx
 * by Colin Bitterfield
 * for the unladen swallow
 */

#include <SPI.h>
#include <WiFiNINA.h>
#include <aREST.h>

//Clients

// Include local files
#include "secrets.h"
#include "config.h"

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;

WiFiServer server(80);

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

// aREST API key (that you can get at dashboard.arest.io)
char * key = "your_arest_key";

WiFiClient client = server.available();
rest.handle(client);

// RESTapi variables
int temperature;
int humidity;

// Pin Variables
int buttonState = 0;

void setup() {
   rest.setKey(key);
   //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // 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);
  }
  server.begin();
  // you're connected now, so print out the status:
  printWifiStatus();

  // Setup RESET API
  // Give name and ID to device (ID should be 6 characters long)
  rest.set_id("008");
  rest.set_name("unlaiden_swallow");

  //
  temperature = 24;
  humidity = 40;
  rest.variable("temperature",&temperature);
  rest.variable("humidity",&humidity);
  rest.variable("uv",&uv);
  rest.variable("button",&buttonState);

  Serial.print("The button pin is:");

}

void loop() {

  buttonState = digitalRead(buttonPin);
  if  (buttonState == HIGH) {
    Serial.println("The button is pressed");
  }

}

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

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

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

Error Message: boat-rest-api-v1.0:37:1: error: 'rest' does not name a type rest.handle(client); ^~~~ /Users/colin/Documents/Arduino/boat-rest-api-v1.0/boat-rest-api-v1.0.ino: In function 'void setup()': boat-rest-api-v1.0:48:9: error: 'class aREST' has no member named 'setKey'; did you mean 'set_id'? rest.setKey(key); ^~~~~~ set_id exit status 1 'rest' does not name a type

marcoschwartz commented 3 years ago

Hello, we had some issues with the aREST.io cloud server but now all is fixed :)

For a local auth with the API key this is indeed an interesting project but for now we are focusing on the cloud aspect of aREST, however we'll mark this as a future feature to be implemented.