/* This a simple example of the aREST Library for the ESP8266 WiFi chip. This example illustrate the cloud part of aREST that makes the board accessible from anywhere See the README file for more details. Written in 2015 by Marco Schwartz under a GPL license, forked by Skickar and MinePro120 */ // Import required libraries #include #include #include // Clients WiFiClient espClient; PubSubClient client(espClient); // Create aREST instance aREST rest = aREST(client); // Unique ID to identify the device for cloud.arest.io char* device_id = "unique_device_id"; // WiFi parameters const char* ssid = ""; const char* password = ""; const char* mssid = ""; //see line 47 for more AP options, and edit line 54 const char* mpassword = ""; // Variables to be exposed to the API int temperature; int humidity; String local_ip = ""; // The port to listen for incoming TCP connections. You are advised to change this. #define LISTEN_PORT 80 // Create an instance of the server WiFiServer server(LISTEN_PORT); // Functions void callback(char* topic, byte* payload, unsigned int length); void setup(void) { /* Create a Soft AP mssid - character string containing network SSID (max. 31 characters) mpassword - optional character string with a password. For WPA2-PSK network it should be at least 8 character long. If not specified, the access point will be open for anybody to connect, (max. 63 characters). channel - optional parameter to set Wi-Fi channel, from 1 to 13. Default channel = 1. hidden - optional parameter, if set to true will hide SSID. max_connection - optional parameter to set max simultaneous connected stations, from 0 to 8. Defaults to 4. Once the max number has been reached, any other station that wants to connect will be forced to wait until an already connected station disconnects. */ Serial.print("Setting soft-AP... "); Serial.println(WiFi.softAP(mssid, mpassword, channel, hidden, max_connection) ? "Ready" : "Failed!"); Serial.print("Soft-AP IP address = "); Serial.println(WiFi.softAPIP()); // Start Serial Serial.begin(115200); // Set callback client.setCallback(callback); // Init variables and expose them to REST API temperature = 24; humidity = 40; rest.variable("temperature", &temperature); rest.variable("humidity", &humidity); rest.variable("local_ip", &local_ip); // Give name & ID to the device (ID should be 6 characters long) rest.set_id(device_id); rest.set_name("esp8266"); // Connect to WiFi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Start the server server.begin(); Serial.println("Local server started on IP:"); // Print the IP address Serial.println(WiFi.localIP()); local_ip = ipToString(WiFi.localIP()); } void loop() { // Connect to the cloud rest.handle(client); // Handle Local aREST calls WiFiClient clientLocal = server.available(); if (!clientLocal) { return; } while(!clientLocal.available()){ delay(1); } rest.handle(clientLocal); } // Handles message arrived on subscribed topic(s) void callback(char* topic, byte* payload, unsigned int length) { rest.handle_callback(client, topic, payload, length); } // Convert IP address to String String ipToString(IPAddress address) { return String(address[0]) + "." + String(address[1]) + "." + String(address[2]) + "." + String(address[3]); }