#include "WiFiProv.h"
#include "WiFi.h"
#include <ESPmDNS.h>
extern "C" {
uint8_t temprature_sens_read() ;
}
uint8_t temprature_sens_read() ;
// state machine switch, case, break statments
// Set web server port number to 80
WiFiServer server(80);
// #define USE_SOFT_AP // Uncomment if you want to enforce using Soft AP method instead of BLE
const char * pop = "abcd1234"; // Proof of possession - otherwise called a PIN - string provided by the device, entered by user in the phone app
const char * service_name = "PROV_123"; // Name of your device (the Espressif apps expects by default device name starting with "Prov_")
const char * service_key = NULL; // Password used for SofAP method (NULL = no password needed)
bool reset_provisioned = true; // When true the library will automatically delete previously provisioned data.
int state;
bool statechanged = false;
void SysProvEvent(arduino_event_t *sys_event)
{
switch (sys_event->event_id) {
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
Serial.print("\nConnected IP address : ");
Serial.println(IPAddress(sys_event->event_info.got_ip.ip_info.ip.addr));
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
Serial.println("\nDisconnected. Connecting to the AP again... ");
break;
case ARDUINO_EVENT_PROV_START:
Serial.println("\nProvisioning started\nGive Credentials of your access point using smartphone app");
state = 1;
statechanged = true;
break;
case ARDUINO_EVENT_PROV_CRED_RECV: {
Serial.println("\nReceived Wi-Fi credentials");
Serial.print("\tSSID : ");
Serial.println((const char *) sys_event->event_info.prov_cred_recv.ssid);
Serial.print("\tPassword : ");
Serial.println((char const *) sys_event->event_info.prov_cred_recv.password);
state = 2;
statechanged = true;
break;
}
case ARDUINO_EVENT_PROV_CRED_FAIL: {
Serial.println("\nProvisioning failed!\nPlease reset to factory and retry provisioning\n");
state = 3;
statechanged = true;
if(sys_event->event_info.prov_fail_reason == WIFI_PROV_STA_AUTH_ERROR)
Serial.println("\nWi-Fi AP password incorrect");
else
Serial.println("\nWi-Fi AP not found....Add API \" nvs_flash_erase() \" before beginProvision()");
break;
}
case ARDUINO_EVENT_PROV_CRED_SUCCESS:
Serial.println("\nProvisioning Successful");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println(WiFi.status());
state = 4;
statechanged = true;
break;
case ARDUINO_EVENT_PROV_END:
Serial.println("\nProvisioning Ends");
state = 5;
statechanged = true;
break;
default:
break;
}
}
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the current output state
String output26State = "off";
String output27State = "off";
String output25State = "off";
String output32State = "off";
// Assign output variables to GPIO pins
const int output26 = 26;
const int output27 = 27;
const int output25 = 25;
const int output32 = 32;
const int boardled = 2;
bool all = false;
int ledPin;
int interval;
int hallvalue;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
unsigned long previousTime1 = 0;
unsigned long previousTime2 = 0;
unsigned long previousTime3 = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
void setup() {
Serial.begin(115200);
// if (WiFi.status() != WL_CONNECTED){
// digitalWrite(boardled, LOW);
// }
WiFi.onEvent(SysProvEvent);
pinMode(output26, OUTPUT);
pinMode(output27, OUTPUT);
pinMode(output25, OUTPUT);
pinMode(output32, OUTPUT);
pinMode(boardled, OUTPUT);
pinMode(ledPin, OUTPUT);
// Set outputs to LOW
digitalWrite(output26, LOW);
digitalWrite(output27, LOW);
digitalWrite(output25, LOW);
digitalWrite(output32, LOW);
digitalWrite(ledPin, LOW);
#if CONFIG_IDF_TARGET_ESP32 && CONFIG_BLUEDROID_ENABLED && not USE_SOFT_AP
Serial.println("Begin Provisioning using BLE");
// Sample uuid that user can pass during provisioning using BLE
uint8_t uuid[16] = {0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf,
0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02 };
WiFiProv.beginProvision(WIFI_PROV_SCHEME_BLE, WIFI_PROV_SCHEME_HANDLER_FREE_BTDM, WIFI_PROV_SECURITY_1, pop, service_name, service_key, uuid, reset_provisioned);
#else
Serial.println("Begin Provisioning using Soft AP");
WiFiProv.beginProvision(WIFI_PROV_SCHEME_SOFTAP, WIFI_PROV_SCHEME_HANDLER_NONE, WIFI_PROV_SECURITY_1, pop, service_name, service_key);
#endif
#if CONFIG_BLUEDROID_ENABLED && not USE_SOFT_AP
log_d("ble qr");
WiFiProv.printQR(service_name, pop, "ble");
#else
log_d("wifi qr");
WiFiProv.printQR(service_name, pop, "softap");
#endif
// Initialize the output variables as outputs
if (MDNS.begin("esp32")) {
Serial.println("MDNS responder started");
}
server.begin();
}
void loop(){
checkstate();
if (statechanged == true){
digitalWrite(output32, LOW);
digitalWrite(output25, LOW);
digitalWrite(output27, LOW);
digitalWrite(output26, LOW);
statechanged = false;
}
if (all == false){
if ((millis()- previousTime1) >= interval){
previousTime1 = millis();
Serial.print(state);
digitalWrite(ledPin, !digitalRead(ledPin));
}
}
else{
if ((millis()- previousTime1) >= interval){
previousTime1 = millis();
Serial.print(state);
digitalWrite(output27, !digitalRead(output27));
digitalWrite(output26, !digitalRead(output26));
digitalWrite(output32, !digitalRead(output32));
digitalWrite(output25, !digitalRead(output25));
}
}
hallvalue = hallRead();
float temperature = (temprature_sens_read()-32 ) /1.8;
int touchvalue1 = touchRead(T0);
int touchvalue2 = touchRead(T3);
if ((millis()- previousTime2) >= 3000){
previousTime2 = millis();
Serial.print("hall value = ");
Serial.println(hallvalue);
Serial.print("Temperature = ");
Serial.print(temperature);
Serial.println( " C");
Serial.print("Touch value one");
Serial.println(touchvalue1); //gpio 14
Serial.print("Touch value two");
Serial.println(touchvalue2);//gpio 12
}
if (touchvalue1 <= 30){
blinkleds();
}
if (touchvalue2 <= 30){
returnState();
}
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
currentTime = millis();
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
if (header.indexOf("GET /sensors") >= 0) {
Serial.println("Sensor readings page requested");
client.println("<!DOCTYPE html><html>");
client.println("<head>\<meta http-equiv='refresh' content='3'/>\<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button-green { background-color: #4CAF50; }</style></head>");
client.println("<body><h1>Sensor Readings</h1>");
client.println("<p>Hall Sensor Value: " + String(hallvalue) + "</p>");
client.println("<p>Temperature: " + String(temperature) + " C</p>");
client.println("<p>Touch Value 1: " + String(touchvalue1) + "</p>");
client.println("<p>Touch Value 2: " + String(touchvalue2) + "</p>");
client.println("</body></html>");
}
else {
// turns the GPIOs on and off
if (header.indexOf("GET /32/on") >= 0) {
Serial.println("GPIO 32 on");
output32State = "on";
digitalWrite(output32, HIGH);
} else if (header.indexOf("GET /32/off") >= 0) {
Serial.println("GPIO 32 off");
output32State = "off";
digitalWrite(output32, LOW);
} else if (header.indexOf("GET /26/on") >= 0) {
Serial.println("GPIO 26 on");
output26State = "on";
digitalWrite(output26, HIGH);
} else if (header.indexOf("GET /26/off") >= 0) {
Serial.println("GPIO 26 off");
output26State = "off";
digitalWrite(output26, LOW);
}
else if (header.indexOf("GET /25/on") >= 0) {
Serial.println("GPIO 25 on");
output25State = "on";
digitalWrite(output25, HIGH);
} else if (header.indexOf("GET /25/off") >= 0) {
Serial.println("GPIO 25 off");
output25State = "off";
digitalWrite(output25, LOW);
}
else if (header.indexOf("GET /27/on") >= 0) {
Serial.println("GPIO 27 on");
output27State = "on";
digitalWrite(output27, HIGH);
} else if (header.indexOf("GET /27/off") >= 0) {
Serial.println("GPIO 27 off");
output27State = "off";
digitalWrite(output27, LOW);
}
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button-green { background-color: #4CAF50; }");
client.println(".button-yellow { background-color: #FFEB3B; }");
client.println(".button-blue { background-color: #2196F3; }");
client.println(".button-red { background-color: #f44336; }");
client.println(".button2 {background-color: #555555;}</style></head>");
// Web Page Heading
client.println("<body><h1>Output controller</h1>");
// Display current state, and ON/OFF buttons for GPIO 32
client.println("<p>Green Led " + output32State + "</p>");
// If the output32State is off, it displays the ON button
if (output32State=="off") {
client.println("<p><a href=\"/32/on\"><button class=\"button button-green\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/32/off\"><button class=\"button button2\">OFF</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 27
client.println("<p>Yellow Led " + output26State + "</p>");
// If the output27State is off, it displays the ON button
if (output26State=="off") {
client.println("<p><a href=\"/26/on\"><button class=\"button button-yellow\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/26/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("<p>Blue Led " + output25State + "</p>");
// If the output32State is off, it displays the ON button
if (output25State=="off") {
client.println("<p><a href=\"/25/on\"><button class=\"button button-blue\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/25/off\"><button class=\"button button2\">OFF</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 27
client.println("<p>Red Led " + output27State + "</p>");
// If the output27State is off, it displays the ON button
if (output27State=="off") {
client.println("<p><a href=\"/27/on\"><button class=\"button button-red\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/27/off\"><button class=\"button button2\">OFF</button></a></p>");
}
}
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
void checkstate(){
switch (state){
case 1 :
ledPin = 27;
interval = 700;
break;
case 2 :
ledPin = 32;
interval = 300;
break;
case 3 :
ledPin = 26;
interval = 1000;
break;
case 4 :
all = true;
interval = 300;
break;
case 5 :
all = false;
ledPin = 2;
interval = 300;
break;
default :
ledPin = 27;
interval = 1000;
break;
}
}
void blinkleds(){
if ((millis()- previousTime3) >= 150){
previousTime3 = millis();
if(output26State == "on"){
digitalWrite(output26, !digitalRead (output26));
}
if(output27State == "on"){
digitalWrite(output27, !digitalRead (output27));
}
if(output25State == "on"){
digitalWrite(output25, !digitalRead (output25));
}
if(output32State == "on"){
digitalWrite(output32, !digitalRead (output32));
}
}
}
void returnState() {
if(output26State == "on"){
digitalWrite(output26, HIGH);
}
else{
digitalWrite(output26, LOW);
}
if(output25State == "on"){
digitalWrite(output25, HIGH);
}
else{
digitalWrite(output25, LOW);
}
if(output27State == "on"){
digitalWrite(output27, HIGH);
}
else{
digitalWrite(output27, LOW);
}
if(output32State == "on"){
digitalWrite(output32, HIGH);
}
else{
digitalWrite(output32, LOW);
}
}