/*
General Purpose Webserver adjusted for Olimex ESP32-PoE board.
It can control Olimex modules: MOD-IO; MOD-LCD 4.3"; MOD-LTR501; MOD-BME280, MOD-IRDA, MOD-RFID1356-MIFARE.
This sketch requires Arduino for ESP32 - how to install it read here: https://github.com/espressif/arduino-esp32
This project is based on the one created by David Bird. The original can be found and downloaded here: https://github.com/G6EJD/ESP32-General-Purpose-Webserver
The MIT License (MIT) Copyright (c) 2017 by David Bird.
### The formulation and calculation method of an IAQ - Internal Air Quality index ###
### The provision of a general purpose webserver ###
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, but not to use it commercially for profit making or to sub-license and/or to sell copies of the Software or to
permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
See more at http://dsbird.org.uk
*/
//################# LIBRARIES ################
#include
#include
#include
#include //https://github.com/Pedroalbuquerque/ESP32WebServer download and place in your Libraries folder
#include "Adafruit_BME280.h" // search in library packages of Arduino for: "Adafruit BME280 Library" and install it
#include // https://github.com/z3t0/Arduino-IRremote
#include
enum UART_Modules_Type
{
NONE = 0, MOD_IRDA, MOD_RFID_MIFARE, MOD_LCD
};
//################ VARIABLES ################
const char* ssid = "OlimexTenda"; // WiFi SSID
const char* password = "0pen5ourceHardware"; // WiFi Password
String siteheading = "ESP32-PoE"; // Site's Main Title
String sitetitle = "ESP32-PoE Webserver"; // Appears on the tabe of a Web Browser
String yourfootnote = "Olimex demo for ESP32-PoE"; // A foot note e.g. "My Web Site"
String siteversion = "v1.3"; // Version of your Website
#define sitewidth 1024 // Adjust site page width in pixels as required
#define SEALEVELPRESSURE_HPA (1013.25) // required for MOD-BME280
int MOD_IRDA_RECV_PIN = 36;
UART_Modules_Type Active_UART_Module = NONE;
Adafruit_BME280 bme; // I2C
IRrecv irrecv(MOD_IRDA_RECV_PIN);
decode_results MOD_IRDA_results;
const char MOD_IRDA_Decode_Type [][16] = {"UNUSED", "RC5", "RC6", "NEC", "SONY", "PANASONIC", "JVC", "SAMSUNG", "WHYNTER",
"AIWA_RC_T501", "LG", "SANYO", "MITSUBISHI", "DISH", "SHARP", "DENON", "PRONTO", "LEGO_PF"};
String webpage = ""; // General purpose variable to hold HTML code
ESP32WebServer server(80); // Start server on port 80 (default for a web-browser, change to your requirements, e.g. 8080 perhaps, if your Router uses port 80
// To access server from outside of a WiFi (LAN) network e.g. on port 8080 add a rule on your Router that forwards a connection request
// to http://your_network_WAN_address:8080 to http://your_network_LAN_address:8080 and then you can view your ESP server from anywhere.
// Example http://yourhome.ip:8080 and your ESP Server is at 192.168.0.40, then the request will be directed to http://192.168.0.40:8080
#define BUTTON_PRESSED() (!digitalRead (34))
void MOD_IO_SetRelay (int Value)
{
Wire.beginTransmission(0x58);
Wire.write(0x10);
Wire.write(Value);
Wire.endTransmission();
}
void MOD_LTR501_Write(unsigned char reg, unsigned char data)
{
Wire.beginTransmission(0x23);
Wire.write(reg);
Wire.write(data);
Wire.endTransmission();
}
unsigned char MOD_LTR501_Read(unsigned char reg)
{
unsigned char data;
Wire.beginTransmission(0x23);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom(0x23, 1);
while(Wire.available())
data = Wire.read();
return data;
}
int MOD_LTR501_Available (void)
{
if(MOD_LTR501_Read(0x86) == 0x80)
return 1;
else
return 0;
}
char MOD_IRDA_Type[16]=" ", MOD_IRDA_Code[10]=" ";
void MOD_IRDA_loop_Routine()
{
if (Active_UART_Module == MOD_IRDA)
{
if (irrecv.decode(&MOD_IRDA_results))
{
if ((MOD_IRDA_results.decode_type != UNKNOWN) && (MOD_IRDA_results.value != 0xFFFFFFFF))
{
sprintf (MOD_IRDA_Type, MOD_IRDA_Decode_Type[MOD_IRDA_results.decode_type]);
sprintf (MOD_IRDA_Code, "%X", MOD_IRDA_results.value);
}
irrecv.resume(); // Receive the next value
}
}
}
void AJAX_Data_Function()
{
static char MOD_MIFARE_Text_Output[32+1]="";
static char MOD_LCD_Text_Output[256+1]="";
/* server responds 200 with a json payload */
/* although preferably concatenate your real sensor data here */
String message="{";
// MOD-BME
if (bme.begin(0x76))
{
message += "\"BME_s\": ";
message += "\"Connected\"";
message += ", \"BME_t\": ";
message += bme.readTemperature();
message += ", \"BME_p\": ";
message += bme.readPressure()/100.0F;
message += ", \"BME_a\": ";
message += bme.readAltitude(SEALEVELPRESSURE_HPA);
message += ", \"BME_h\": ";
message += bme.readHumidity();
}
else
{
message += "\"BME_s\": ";
message += "\"Disconnected\"";
message += ", \"BME_t\": ";
message += 0;
message += ", \"BME_p\": ";
message += 0;
message += ", \"BME_a\": ";
message += 0;
message += ", \"BME_h\": ";
message += 0;
}
// MOD-LTR
static unsigned int ADC_0=0, ADC_1=0, LTR_PS=0;
static float distance=0;
if (MOD_LTR501_Available())
{
int stat;
MOD_LTR501_Write(0x80, 0x03); //Active mode, 64k lux range
MOD_LTR501_Write(0x81, 0x03); //PS active mode, x1 GAIN
MOD_LTR501_Write(0x82, 0x6B); //LED 60Hz, 50% duty, 50mA
MOD_LTR501_Write(0x83, 0x7F); //127 pulses
MOD_LTR501_Write(0x84, 0x02); //PS 100ms measure rate
MOD_LTR501_Write(0x85, 0x03); //ALS Integration 100ms, repeat rate 500ms
stat = MOD_LTR501_Read(0x8C);
if (stat & 0x04)
{
unsigned char data[4];
for(int i = 0; i < 4; i++)
{
data[i] = MOD_LTR501_Read(0x88 + i);
}
ADC_0 = (data[3] << 8) | data[2];
ADC_1 = (data[1] << 8) | data[0];
}
if(stat & 0x01)
{
//PS new data
unsigned char data[2];
for(int i = 0; i < 2; i++)
{
data[i] = MOD_LTR501_Read(0x8D + i);
}
LTR_PS = (data[1] << 8) | data[0];
distance = 10 -(10.0/2047)*LTR_PS;
}
message += ", \"LTR_s\": ";
message += "\"Connected\"";
message += ", \"LTR_a0\": ";
message += ADC_0;
message += ", \"LTR_a1\": ";
message += ADC_1;
message += ", \"LTR_d\": ";
message += distance;
}
else
{
message += ", \"LTR_s\": ";
message += "\"Disconnected\"";
message += ", \"LTR_a0\": ";
message += 0;
message += ", \"LTR_a1\": ";
message += 0;
message += ", \"LTR_d\": ";
message += 0;
}
// MOD-IrDA
if (Active_UART_Module == MOD_IRDA)
{
message += ", \"IRDA_s\": ";
message += "\"Connected\"";
message += ", \"IRDA_t\": ";
message += "\"";
message += MOD_IRDA_Type;
message += "\"";
message += ", \"IRDA_c\": ";
message += "\"";
message += MOD_IRDA_Code;
message += "\"";
}
else
{
message += ", \"IRDA_s\": ";
message += "\"Disconnected\"";
message += ", \"IRDA_t\": ";
message += "\"\"";
message += ", \"IRDA_c\": ";
message += "\"\"";
}
// MOD-MIFARE
if (Active_UART_Module == MOD_RFID_MIFARE)
{
int i=0, Flag=0;
if (Serial2.available () > 0)
{
while (Serial2.available () > 0)
{
char TempChar;
TempChar = (char)Serial2.read();
if ((TempChar == '\n') || (TempChar == '\r'))
{
MOD_MIFARE_Text_Output[i] = 0;
Flag = 0;
}
if (Flag)
{
if (i<32)
MOD_MIFARE_Text_Output[i++] = TempChar;
}
if (TempChar == '-')
{
i = 0;
Flag = 1;
}
}
MOD_MIFARE_Text_Output[i] = 0;
}
message += ", \"MIFARE_s\": ";
message += "\"Connected\"";
message += ", \"MIFARE_d\": ";
message += "\"";
message += MOD_MIFARE_Text_Output;
message += "\"";
}
else
{
MOD_MIFARE_Text_Output[0] = 0;
message += ", \"MIFARE_s\": ";
message += "\"Disconnected\"";
message += ", \"MIFARE_d\": ";
message += "\"\"";
}
// MOD-LCD
if (Active_UART_Module == MOD_LCD)
{
int i=0;
if (Serial2.available () > 0)
{
while (Serial2.available () > 0)
{
char TempChar;
TempChar = (char)Serial2.read();
if (i>250) // 255 is the maximum of buffer
continue;
switch (TempChar)
{
case '\r':
break;
case '\n':
MOD_LCD_Text_Output[i++] = '\\';
MOD_LCD_Text_Output[i++] = 'n';
break;
case '"':
MOD_LCD_Text_Output[i++] = '\\';
MOD_LCD_Text_Output[i++] = '"';
break;
default:
MOD_LCD_Text_Output[i++] = TempChar;
break;
}
}
MOD_LCD_Text_Output[i] = 0;
}
message += ", \"LCD_s\": ";
message += "\"Connected\"";
message += ", \"LCD_o\": ";
message += "\"";
message += MOD_LCD_Text_Output;
message += "\"";
}
else
{
MOD_LCD_Text_Output[0] = 0;
message += ", \"LCD_s\": ";
message += "\"Disconnected\"";
message += ", \"LCD_o\": ";
message += "\"\"";
}
message += "}";
server.send(200, "application/json",message);
}
void setup()
{
Serial.begin(115200); // initialize serial communications
Serial2.begin (115200, SERIAL_8N1, 36, 4, false); // UEXT UART
Wire.begin (13, 16); // SPP
Serial.println("Connect your I2C sensors to the default SDA, SCL pins for your board shown here:");
Serial.println("I2C SDA pin = " + String(SDA));
Serial.println("I2C SCL pin = " + String(SCL)); // Connect I2C sensors to the default SDA and SCL pins! Check Serial port for details
StartWiFi(ssid, password);
//----------------------------------------------------------------------
Serial.println("Use this URL to connect: http://" + WiFi.localIP().toString() + "/"); // Print the IP address
server.on("/", I2C_Modules); // If the user types at their browser http://192.168.0.100/ control is passed here and then to user_input, you get values for your program...
server.on("/I2C_Modules", I2C_Modules);
server.on("/UART_Modules", UART_Modules);
server.on("/AJAX_Data", AJAX_Data_Function); // AJAX data
server.onNotFound(handleNotFound); // If the user types something that is not supported, say so
server.begin(); Serial.println(F("Webserver started...")); // Start the webserver
irrecv.enableIRIn(); // Start the receiver
pinMode (34, INPUT);
}
void loop ()
{
server.handleClient();
if (Active_UART_Module == MOD_IRDA)
MOD_IRDA_loop_Routine ();
}
void handleNotFound()
{
String message = "The request entered could not be found, please try again with a different option\n";
server.send(404, "text/plain", message);
}
void I2C_Modules ()
{
String MOD_IO_CheckBoxChoice = "";
webpage = ""; // don't delete this command, it ensures the server works reliably!
append_HTML_header();
String IPaddress = WiFi.localIP().toString();
webpage += " I2C modules
";
// MOD-IO
webpage += "
";
// MOD-BME280
webpage += "
";
// MOD-LTR501
webpage += "
";
webpage += "
"; // container
// And so-on
webpage += "
";
webpage += "";
webpage += "
";
webpage += "
" + sitetitle + "
";
webpage += "" + siteheading + " " + siteversion + "
";
// For each new page you add or remove, make sure there is a menu item to call it or remove it when not used
webpage += "";
}
void append_HTML_footer()
{
webpage += "
";
webpage += "
";
append_HTML_footer();
server.send(200, "text/html", webpage); // Send a response to the client to enter their inputs, if needed, Enter=defaults
if (server.args() > 0 )
{ // Arguments were received
for ( uint8_t i = 0; i < server.args(); i++ )
{
static int MOD_IO_Temp=0, MOD_IO_RelayValue = 0;
String Argument_Name = server.argName(i);
String client_response = server.arg(i);
if (Argument_Name == "MOD_IO_CheckBox")
{
MOD_IO_CheckBoxChoice = client_response; // Checking for more than one check-box being selected too, 'a' if more than one
MOD_IO_Temp = MOD_IO_CheckBoxChoice.toInt () - 1; // SPP
MOD_IO_RelayValue = MOD_IO_RelayValue | (1 << MOD_IO_Temp);
Serial.println (MOD_IO_RelayValue);
MOD_IO_SetRelay (MOD_IO_RelayValue);
}
if (Argument_Name == "MOD_IO_Relay")
{
Serial.print ("Value: ");
Serial.println (MOD_IO_RelayValue);
MOD_IO_SetRelay (MOD_IO_RelayValue);
MOD_IO_RelayValue = 0;
}
}
}
}
void UART_Modules ()
{
String MOD_LCD_Text;
webpage = ""; // don't delete this command, it ensures the server works reliably!
append_HTML_header();
String IPaddress = WiFi.localIP().toString();
// UART Modules
webpage += "
"; // div container
webpage += " ";
// And so-on
webpage += "";
append_HTML_footer();
server.send(200, "text/html", webpage); // Send a response to the client to enter their inputs, if needed, Enter=defaults
if (server.args() > 0 )
{ // Arguments were received
for ( uint8_t i = 0; i < server.args(); i++ )
{
String Argument_Name = server.argName(i);
String client_response = server.arg(i);
if (Argument_Name == "MOD_LCD_Text")
MOD_LCD_Text = client_response;
if (Argument_Name == "MOD_LCD_Button")
{
if (Active_UART_Module == MOD_LCD)
{
Serial2.print (MOD_LCD_Text);
Serial2.print ("\n\r");
}
}
if (Argument_Name == "Init_MOD_LCD")
{
Serial2.begin (115200, SERIAL_8N1, 36, 4, false); // UEXT UART
Serial2.print ("\n\r");
Active_UART_Module = MOD_LCD;
}
if (Argument_Name == "Init_MOD_IRDA")
{
Active_UART_Module = MOD_IRDA;
}
if (Argument_Name == "Init_MOD_RFID_MIFARE")
{
Serial2.begin (38400, SERIAL_8N1, 36, 4, false); // UEXT UART
Active_UART_Module = MOD_RFID_MIFARE;
}
}
}
}
void StartWiFi(const char* ssid, const char* password)
{
int connAttempts = 0;
Serial.print(F("\r\nConnecting to: ")); Serial.println(String(ssid));
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED )
{
delay(500); Serial.print(".");
if (connAttempts > 20) {
Serial.println("Failed to connect to WiFi");
}
connAttempts++;
}
Serial.print(F("WiFi connected at: "));
Serial.println(WiFi.localIP());
}
void append_HTML_header()
{
webpage = "";
webpage += "