2018-11-26 16:02:08 +00:00
|
|
|
#include <DNSServer.h>
|
2017-10-20 09:49:54 +00:00
|
|
|
#include <ESPUI.h>
|
2017-05-19 23:32:31 +00:00
|
|
|
|
2018-11-26 16:02:08 +00:00
|
|
|
const byte DNS_PORT = 53;
|
2019-03-03 20:17:49 +00:00
|
|
|
IPAddress apIP( 192, 168, 1, 1 );
|
2018-11-26 16:02:08 +00:00
|
|
|
DNSServer dnsServer;
|
|
|
|
|
2017-12-01 16:11:16 +00:00
|
|
|
#if defined(ESP32)
|
2018-11-26 16:02:08 +00:00
|
|
|
#include <WiFi.h>
|
2017-12-01 16:11:16 +00:00
|
|
|
#else
|
2018-11-26 16:02:08 +00:00
|
|
|
#include <ESP8266WiFi.h>
|
2017-12-01 16:11:16 +00:00
|
|
|
#endif
|
|
|
|
|
2019-03-03 20:17:49 +00:00
|
|
|
// true for verbose, false for quiet
|
|
|
|
ESPUIClass ESPUI( Verbosity::VerboseJSON );
|
|
|
|
|
|
|
|
const char* ssid = "ESPUI";
|
|
|
|
const char* password = "espui";
|
|
|
|
const char* hostname = "EspuiTest";
|
2017-05-19 23:32:31 +00:00
|
|
|
|
2017-10-19 11:46:47 +00:00
|
|
|
long oldTime = 0;
|
2017-11-13 15:11:42 +00:00
|
|
|
bool switchi = false;
|
2017-10-19 11:46:47 +00:00
|
|
|
|
2019-03-04 19:49:18 +00:00
|
|
|
|
|
|
|
void numberCall( Control* sender, int type ) {
|
|
|
|
Serial.println( sender->value );
|
2019-03-03 20:17:49 +00:00
|
|
|
}
|
2018-11-26 17:25:10 +00:00
|
|
|
|
2019-03-04 19:49:18 +00:00
|
|
|
void textCall( Control* sender, int type ) {
|
|
|
|
Serial.print( "Text: ID: " );
|
|
|
|
Serial.print( sender->id );
|
|
|
|
Serial.print( ", Value: " );
|
|
|
|
Serial.println( sender->value );
|
2019-03-03 20:17:49 +00:00
|
|
|
}
|
2018-11-26 17:25:10 +00:00
|
|
|
|
2019-03-04 19:49:18 +00:00
|
|
|
void slider( Control* sender, int type ) {
|
|
|
|
Serial.print( "Slider: ID: " );
|
|
|
|
Serial.print( sender->id );
|
|
|
|
Serial.print( ", Value: " );
|
|
|
|
Serial.println( sender->value );
|
2019-03-03 20:17:49 +00:00
|
|
|
}
|
2017-11-29 10:35:54 +00:00
|
|
|
|
2019-03-04 19:49:18 +00:00
|
|
|
void buttonCallback( Control* sender, int type ) {
|
2019-03-03 20:17:49 +00:00
|
|
|
switch ( type ) {
|
2018-11-26 16:02:08 +00:00
|
|
|
case B_DOWN:
|
2019-03-03 20:17:49 +00:00
|
|
|
Serial.println( "Button DOWN" );
|
2018-11-26 16:02:08 +00:00
|
|
|
break;
|
2019-03-03 20:17:49 +00:00
|
|
|
|
2018-11-26 16:02:08 +00:00
|
|
|
case B_UP:
|
2019-03-03 20:17:49 +00:00
|
|
|
Serial.println( "Button UP" );
|
2018-11-26 16:02:08 +00:00
|
|
|
break;
|
2017-10-19 11:46:47 +00:00
|
|
|
}
|
2017-10-17 09:55:25 +00:00
|
|
|
}
|
2017-10-19 11:46:47 +00:00
|
|
|
|
2019-03-04 19:49:18 +00:00
|
|
|
void buttonExample( Control* sender, int type ) {
|
2019-03-03 20:17:49 +00:00
|
|
|
switch ( type ) {
|
2018-11-26 16:02:08 +00:00
|
|
|
case B_DOWN:
|
2019-03-03 20:17:49 +00:00
|
|
|
Serial.println( "Status: Start" );
|
2019-03-04 19:49:18 +00:00
|
|
|
ESPUI.updateControl( "Status:", "Start" );
|
2018-11-26 16:02:08 +00:00
|
|
|
break;
|
2019-03-03 20:17:49 +00:00
|
|
|
|
2018-11-26 16:02:08 +00:00
|
|
|
case B_UP:
|
2019-03-03 20:17:49 +00:00
|
|
|
Serial.println( "Status: Stop" );
|
2019-03-04 19:49:18 +00:00
|
|
|
ESPUI.updateControl( "Status:", "Stop" );
|
2018-11-26 16:02:08 +00:00
|
|
|
break;
|
2017-10-19 11:46:47 +00:00
|
|
|
}
|
2017-10-17 09:55:25 +00:00
|
|
|
}
|
2019-03-04 19:49:18 +00:00
|
|
|
void padExample( Control* sender, int value ) {
|
2019-03-03 20:17:49 +00:00
|
|
|
switch ( value ) {
|
2018-11-26 16:02:08 +00:00
|
|
|
case P_LEFT_DOWN:
|
2019-03-03 20:17:49 +00:00
|
|
|
Serial.print( "left down" );
|
2018-11-26 16:02:08 +00:00
|
|
|
break;
|
2019-03-03 20:17:49 +00:00
|
|
|
|
2018-11-26 16:02:08 +00:00
|
|
|
case P_LEFT_UP:
|
2019-03-03 20:17:49 +00:00
|
|
|
Serial.print( "left up" );
|
2018-11-26 16:02:08 +00:00
|
|
|
break;
|
2019-03-03 20:17:49 +00:00
|
|
|
|
2018-11-26 16:02:08 +00:00
|
|
|
case P_RIGHT_DOWN:
|
2019-03-03 20:17:49 +00:00
|
|
|
Serial.print( "right down" );
|
2018-11-26 16:02:08 +00:00
|
|
|
break;
|
2019-03-03 20:17:49 +00:00
|
|
|
|
2018-11-26 16:02:08 +00:00
|
|
|
case P_RIGHT_UP:
|
2019-03-03 20:17:49 +00:00
|
|
|
Serial.print( "right up" );
|
2018-11-26 16:02:08 +00:00
|
|
|
break;
|
2019-03-03 20:17:49 +00:00
|
|
|
|
2018-11-26 16:02:08 +00:00
|
|
|
case P_FOR_DOWN:
|
2019-03-03 20:17:49 +00:00
|
|
|
Serial.print( "for down" );
|
2018-11-26 16:02:08 +00:00
|
|
|
break;
|
2019-03-03 20:17:49 +00:00
|
|
|
|
2018-11-26 16:02:08 +00:00
|
|
|
case P_FOR_UP:
|
2019-03-03 20:17:49 +00:00
|
|
|
Serial.print( "for up" );
|
2018-11-26 16:02:08 +00:00
|
|
|
break;
|
2019-03-03 20:17:49 +00:00
|
|
|
|
2018-11-26 16:02:08 +00:00
|
|
|
case P_BACK_DOWN:
|
2019-03-03 20:17:49 +00:00
|
|
|
Serial.print( "back down" );
|
2018-11-26 16:02:08 +00:00
|
|
|
break;
|
2019-03-03 20:17:49 +00:00
|
|
|
|
2018-11-26 16:02:08 +00:00
|
|
|
case P_BACK_UP:
|
2019-03-03 20:17:49 +00:00
|
|
|
Serial.print( "back up" );
|
2018-11-26 16:02:08 +00:00
|
|
|
break;
|
2019-03-03 20:17:49 +00:00
|
|
|
|
2018-11-26 16:02:08 +00:00
|
|
|
case P_CENTER_DOWN:
|
2019-03-03 20:17:49 +00:00
|
|
|
Serial.print( "center down" );
|
2018-11-26 16:02:08 +00:00
|
|
|
break;
|
2019-03-03 20:17:49 +00:00
|
|
|
|
2018-11-26 16:02:08 +00:00
|
|
|
case P_CENTER_UP:
|
2019-03-03 20:17:49 +00:00
|
|
|
Serial.print( "center up" );
|
2018-11-26 16:02:08 +00:00
|
|
|
break;
|
2017-10-19 15:39:31 +00:00
|
|
|
}
|
2019-03-03 20:17:49 +00:00
|
|
|
|
|
|
|
Serial.print( " " );
|
2019-03-04 19:49:18 +00:00
|
|
|
Serial.println( sender->id );
|
2017-10-19 15:39:31 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 19:49:18 +00:00
|
|
|
void switchExample( Control* sender, int value ) {
|
2019-03-03 20:17:49 +00:00
|
|
|
switch ( value ) {
|
2018-11-26 16:02:08 +00:00
|
|
|
case S_ACTIVE:
|
2019-03-03 20:17:49 +00:00
|
|
|
Serial.print( "Active:" );
|
2018-11-26 16:02:08 +00:00
|
|
|
break;
|
2019-03-03 20:17:49 +00:00
|
|
|
|
2018-11-26 16:02:08 +00:00
|
|
|
case S_INACTIVE:
|
2019-03-03 20:17:49 +00:00
|
|
|
Serial.print( "Inactive" );
|
2018-11-26 16:02:08 +00:00
|
|
|
break;
|
2017-10-19 15:39:31 +00:00
|
|
|
}
|
2019-03-03 20:17:49 +00:00
|
|
|
|
|
|
|
Serial.print( " " );
|
2019-03-04 19:49:18 +00:00
|
|
|
Serial.println( sender->id );
|
2017-10-19 15:39:31 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 19:49:18 +00:00
|
|
|
void otherSwitchExample( Control* sender, int value ) {
|
2019-03-03 20:17:49 +00:00
|
|
|
switch ( value ) {
|
2018-11-26 16:02:08 +00:00
|
|
|
case S_ACTIVE:
|
2019-03-03 20:17:49 +00:00
|
|
|
Serial.print( "Active:" );
|
2018-11-26 16:02:08 +00:00
|
|
|
break;
|
2019-03-03 20:17:49 +00:00
|
|
|
|
2018-11-26 16:02:08 +00:00
|
|
|
case S_INACTIVE:
|
2019-03-03 20:17:49 +00:00
|
|
|
Serial.print( "Inactive" );
|
2018-11-26 16:02:08 +00:00
|
|
|
break;
|
2017-10-19 11:46:47 +00:00
|
|
|
}
|
2019-03-03 20:17:49 +00:00
|
|
|
|
|
|
|
Serial.print( " " );
|
2019-03-04 19:49:18 +00:00
|
|
|
Serial.println( sender->id );
|
2017-05-19 23:32:31 +00:00
|
|
|
}
|
2018-01-08 11:25:36 +00:00
|
|
|
|
2019-03-03 20:17:49 +00:00
|
|
|
void setup( void ) {
|
|
|
|
Serial.begin( 115200 );
|
|
|
|
|
|
|
|
#if defined(ESP32)
|
|
|
|
WiFi.setHostname( hostname );
|
|
|
|
#else
|
|
|
|
WiFi.hostname( hostname );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// try to connect to existing network
|
|
|
|
WiFi.begin( ssid, password );
|
|
|
|
Serial.print( "\n\nTry to connect to existing network" );
|
|
|
|
|
|
|
|
{
|
|
|
|
uint8_t timeout = 5;
|
2018-01-08 11:25:36 +00:00
|
|
|
|
2019-03-03 20:17:49 +00:00
|
|
|
// Wait for connection, 2.5s timeout
|
|
|
|
do {
|
|
|
|
delay( 500 );
|
|
|
|
Serial.print( "." );
|
|
|
|
timeout--;
|
|
|
|
} while ( timeout && WiFi.status() != WL_CONNECTED );
|
|
|
|
|
|
|
|
// not connected -> create hotspot
|
|
|
|
if ( WiFi.status() != WL_CONNECTED ) {
|
|
|
|
Serial.print( "\n\nCreating hotspot" );
|
|
|
|
|
|
|
|
WiFi.mode( WIFI_AP );
|
|
|
|
WiFi.softAPConfig( apIP, apIP, IPAddress( 255, 255, 255, 0 ) );
|
|
|
|
WiFi.softAP( ssid );
|
|
|
|
|
|
|
|
timeout = 5;
|
|
|
|
|
|
|
|
do {
|
|
|
|
delay( 500 );
|
|
|
|
Serial.print( "." );
|
|
|
|
timeout--;
|
|
|
|
} while ( timeout );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dnsServer.start( DNS_PORT, "*", apIP );
|
|
|
|
|
|
|
|
Serial.println( "\n\nWiFi parameters:" );
|
|
|
|
Serial.print( "Mode: " );
|
|
|
|
Serial.println( WiFi.getMode() == WIFI_AP ? "Station" : "Client" );
|
|
|
|
Serial.print( "IP address: " );
|
|
|
|
Serial.println( WiFi.getMode() == WIFI_AP ? WiFi.softAPIP() : WiFi.localIP() );
|
|
|
|
|
|
|
|
ESPUI.label( "Status:", COLOR_TURQUOISE, "Stop" );
|
|
|
|
ESPUI.label( "Millis:", COLOR_EMERALD, "0" );
|
2019-03-04 19:49:18 +00:00
|
|
|
ESPUI.button( "Push Button", &buttonCallback, COLOR_PETERRIVER, "Press" );
|
2019-03-03 20:17:49 +00:00
|
|
|
ESPUI.button( "Other Button", &buttonExample, COLOR_WETASPHALT, "Press" );
|
|
|
|
ESPUI.pad( "Pad with center", true, &padExample, COLOR_SUNFLOWER );
|
|
|
|
ESPUI.pad( "Pad without center", false, &padExample, COLOR_CARROT );
|
|
|
|
ESPUI.switcher( "Switch one", false, &switchExample, COLOR_ALIZARIN );
|
|
|
|
ESPUI.switcher( "Switch two", true, &otherSwitchExample, COLOR_NONE );
|
|
|
|
ESPUI.slider( "Slider one", &slider, COLOR_ALIZARIN, "30" );
|
|
|
|
ESPUI.slider( "Slider two", &slider, COLOR_NONE, "100" );
|
|
|
|
ESPUI.text( "Text Test:", &textCall, COLOR_ALIZARIN, "a Text Field" );
|
|
|
|
ESPUI.number( "Numbertest", &numberCall, COLOR_ALIZARIN, 5, 0, 10 );
|
2018-01-08 11:25:36 +00:00
|
|
|
|
2018-05-27 09:47:53 +00:00
|
|
|
/*
|
2019-03-03 20:17:49 +00:00
|
|
|
* .begin loads and serves all files from PROGMEM directly.
|
|
|
|
* If you want to serve the files from SPIFFS use ESPUI.beginSPIFFS
|
|
|
|
* (.prepareFileSystem has to be run in an empty sketch before)
|
2018-05-27 09:47:53 +00:00
|
|
|
*/
|
2018-11-26 17:25:10 +00:00
|
|
|
|
2018-12-26 12:38:38 +00:00
|
|
|
/*
|
|
|
|
* Optionally you can use HTTP BasicAuth. Keep in mind that this is NOT a
|
2019-03-03 20:17:49 +00:00
|
|
|
* SECURE way of limiting access.
|
2018-12-26 12:38:38 +00:00
|
|
|
* Anyone who is able to sniff traffic will be able to intercept your password
|
2019-03-03 20:17:49 +00:00
|
|
|
* since it is transmitted in cleartext. Just add a string as username and password,
|
|
|
|
* for example begin("ESPUI Control", "username", "password")
|
|
|
|
*/
|
|
|
|
ESPUI.begin( "ESPUI Control" );
|
2018-01-08 11:25:36 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 20:17:49 +00:00
|
|
|
void loop( void ) {
|
2018-11-26 16:02:08 +00:00
|
|
|
dnsServer.processNextRequest();
|
|
|
|
|
2019-03-03 20:17:49 +00:00
|
|
|
if ( millis() - oldTime > 5000 ) {
|
|
|
|
ESPUI.print( "Millis:", String( millis() ) );
|
2018-01-08 11:25:36 +00:00
|
|
|
switchi = !switchi;
|
2019-03-03 20:17:49 +00:00
|
|
|
ESPUI.updateSwitcher( "Switch one", switchi );
|
2018-01-08 11:25:36 +00:00
|
|
|
oldTime = millis();
|
|
|
|
}
|
2019-03-03 20:17:49 +00:00
|
|
|
}
|