Hello,
I'm using a NodeMCU Lolin v3 to control a LED connected to D1 remotely over the Wifi connection.
I found the aRest v2.7.0 and the aRest_UI v1.1.1 libraries which seem ideal for this purpose.
I first tried just using only the aRest library with this sketch
// Import required libraries
#include <ESP8266WiFi.h>
#include <aREST.h>
// Create aREST instance
aREST rest = aREST();
// WiFi parameters
const char* ssid = "****";
const char* password = "*****";
IPAddress staticIP(10, 99, 99, 127);
IPAddress gateway(10, 99, 99, 250);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns1(10, 99, 99, 205);
IPAddress dns2(10, 99, 99, 204);
#define LISTEN_PORT 80 //The port to listen for incoming TCP connections
WiFiServer server(LISTEN_PORT); //Create an instance of the server
// Variables to be exposed to the API
int temperature; int humidity;
// Declare functions to be exposed to the API
int ledControl(String command);
int ledPin = D1;
void setup(void)
{
//Initialize serial and wait for port to open:
Serial.begin(115200); while (!Serial);
Serial.println("\naRest Example Ledcontrol");
// Init variables and expose them to REST API
temperature = 24; humidity = 40;
rest.variable("temperature",&temperature);
rest.variable("humidity",&humidity);
rest.function("led",ledControl); //Function to be exposed
// Give name & ID to the device (ID should be 6 characters long)
rest.set_id("1");
rest.set_name("esp8266");
// Connect to WiFi
Serial.print("Connecting to "); Serial.println(ssid);
WiFi.begin(ssid, password);
WiFi.config(staticIP, gateway, subnet, dns1, dns2);
while (WiFi.status() != WL_CONNECTED)
{
delay(500); Serial.print(".");
}
Serial.println();
Serial.println("Wifi Parameters");
Serial.printf("Connection status: %d\n", WiFi.status());
if (WiFi.status() == WL_CONNECTED)
{
Serial.printf("RSSI: %d dBm\n", WiFi.RSSI());
Serial.printf("Hostname: %s\n", WiFi.hostname().c_str());
Serial.printf("Mac Address: %s\n", WiFi.macAddress().c_str());
Serial.println(WiFi.localIP());
Serial.println(WiFi.subnetMask());
Serial.println(WiFi.gatewayIP());
Serial.println(WiFi.dnsIP(0));
Serial.println(WiFi.dnsIP(1));
}
// Start the server
server.begin();
Serial.println("Server started");
pinMode(ledPin, OUTPUT);
Serial.println("\nEnd Setup\n");
}
void loop()
{
WiFiClient client = server.available(); //Handle REST calls
if (!client) return;
while(!client.available()) {delay(1);}
rest.handle(client);
Serial.print(".");
}
// Custom function accessible by the API
int ledControl(String command)
{
Serial.print("incoming: "); Serial.println(command);
if (command=="on") {digitalWrite(ledPin, 1); Serial.println("command on");}
if (command=="off") {digitalWrite(ledPin, 0); Serial.println("command off");}
return 1;
}
This compiles and uploads fine without any problems.
It even works when I use Chrome to send the commands:
http://10.99.99.127/led?params=on -- turns effectively the LED on
http://10.99.99.127/led?params=off -- turns effectively the LED off
Now I wanted to take this a step further and include the aRest_UI library to improve usability with this sketch
#include <ESP8266WiFi.h>
#include <aREST.h>
#include <aREST_UI.h>
aREST_UI rest = aREST_UI(); // Create aREST instance
// WiFi parameters
const char* ssid = "*****";
const char* password = "*****";
IPAddress staticIP(10, 99, 99, 127);
IPAddress gateway(10, 99, 99, 250);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns1(10, 99, 99, 205);
IPAddress dns2(10, 99, 99, 204);
#define LISTEN_PORT 80 //The port to listen for incoming TCP connections
WiFiServer server(LISTEN_PORT); //Create an instance of the server
// Variables to be exposed to the API
int temperature; float humidity;
// Declare functions to be exposed to the API
int ledControl(String command);
int ledPin = D1;
void setup(void)
{
//Initialize serial and wait for port to open:
Serial.begin(115200); while (!Serial);
Serial.println("\nESP8266 aRestUI Ledcontrol");
rest.title("aREST UI Demo"); //Set the title
rest.button(ledPin); //Create button to control pin 5
// Init variables and expose them to REST API
temperature = 22; humidity = 39.1;
rest.variable("temperature", &temperature);
rest.variable("humidity", &humidity);
// Labels
rest.label("temperature");
rest.label("humidity");
// Function to be exposed
rest.function("led", ledControl);
// Give name and ID to device
rest.set_id("1");
rest.set_name("esp8266");
// Connect to WiFi
Serial.print("Connecting to "); Serial.println(ssid);
WiFi.begin(ssid, password);
WiFi.config(staticIP, gateway, subnet, dns1, dns2);
while (WiFi.status() != WL_CONNECTED)
{
delay(500); Serial.print(".");
}
Serial.println();
Serial.println("Wifi Parameters");
Serial.printf("Connection status: %d\n", WiFi.status());
if (WiFi.status() == WL_CONNECTED)
{
Serial.printf("RSSI: %d dBm\n", WiFi.RSSI());
Serial.printf("Hostname: %s\n", WiFi.hostname().c_str());
Serial.printf("Mac Address: %s\n", WiFi.macAddress().c_str());
Serial.println(WiFi.localIP());
Serial.println(WiFi.subnetMask());
Serial.println(WiFi.gatewayIP());
Serial.println(WiFi.dnsIP(0));
Serial.println(WiFi.dnsIP(1));
}
// Start the server
server.begin();
Serial.println("Server started");
Serial.println("\nEnd Setup\n");
}
void loop()
{
// Handle REST calls
WiFiClient client = server.available();
if (!client) return;
while (!client.available()) {delay(1);}
rest.handle(client);
Serial.print(".");
}
// Custom function accessible by the API
int ledControl(String command)
{
Serial.print("incoming: "); Serial.println(command);
if (command=="on") {analogWrite(ledPin, 1023); Serial.println("command on");}
if (command=="off") {analogWrite(ledPin, 0); Serial.println("command off");}
return 1;
}
Now this won't even compile with a bunch of error messages:
Arduino: 1.8.5 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, 4M (3M SPIFFS), v2 Prebuilt (MSS=536), Disabled, None, 115200"
In file included from H:\Arduino\Sketches\NodeMCU\aRestUI01\aRestUI01.ino:10:0:
H:\Arduino\libraries\aREST_UI-1.1.1/aREST_UI.h: In member function 'virtual void aREST_UI::root_answer()':
H:\Arduino\libraries\aREST_UI-1.1.1/aREST_UI.h:93:69: error: no matching function for call to 'aREST_UI::addToBuffer(const char [45])'
addToBuffer("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
^
H:\Arduino\libraries\aREST_UI-1.1.1/aREST_UI.h:93:69: note: candidates are:
In file included from H:\Arduino\Sketches\NodeMCU\aRestUI01\aRestUI01.ino:9:0:
H:\Arduino\libraries\aREST-2.7.0/aREST.h:1600:6: note: template void aREST::addToBuffer(T, bool)
void addToBuffer(T toAdd, bool quotable) {
^
------ the full error log is in attachment because it was to big to post --------
H:\Arduino\libraries\aREST-2.7.0/aREST.h:1600:6: note: template argument deduction/substitution failed:
In file included from H:\Arduino\Sketches\NodeMCU\aRestUI01\aRestUI01.ino:10:0:
H:\Arduino\libraries\aREST_UI-1.1.1/aREST_UI.h:219:37: note: candidate expects 2 arguments, 1 provided
addToBuffer("\r\n");
^
In file included from H:\Arduino\Sketches\NodeMCU\aRestUI01\aRestUI01.ino:9:0:
H:\Arduino\libraries\aREST-2.7.0/aREST.h:1606:6: note: template void aREST::addToBuffer(T (*)(), bool)
void addToBuffer(T(*toAdd)(), bool quotable) {
^
H:\Arduino\libraries\aREST-2.7.0/aREST.h:1606:6: note: template argument deduction/substitution failed:
In file included from H:\Arduino\Sketches\NodeMCU\aRestUI01\aRestUI01.ino:10:0:
H:\Arduino\libraries\aREST_UI-1.1.1/aREST_UI.h:219:37: note: mismatched types 'T()' and 'const char'
addToBuffer("\r\n");
^
exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module).
This looks to me like there are a number of overlapping functions between the aRest and the aRest_Ui library.
Can anyone help me out on this?
Thanks, Chris.
errors.txt (121 KB)