-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ino
156 lines (131 loc) · 4.56 KB
/
main.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* ESP32 AJAX Demo
* Updates and Gets data from webpage without page refresh
* https://circuits4you.com
*/
#include <WebServer.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include "BluetoothSerial.h"
#include "index.h" //Web page header file
//=======================================================
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
std::string bt_data;
String wifi_data = "test";
//==============================================
WebServer server(80);
//Enter your SSID and PASSWORD
const char* ssid = "MK";
const char* password = "123456789";
//===============================================================
// This routine is executed when you open its IP in browser
//===============================================================
void handleRoot() {
String s = MAIN_page; //Read HTML contents
server.send(200, "text/html", s); //Send web page
}
void handleTime() {
//int a = analogRead(A0);
int a = millis();
String adcValue = String(a);
server.send(200, "text/plane", adcValue); //Send ADC value only to client ajax request
}
void handleBT() {
//int a = analogRead(A0);
server.send(200, "text/plane", bt_data.c_str()); //Send ADC value only to client ajax request
}
void handleWifi() {
//int a = analogRead(A0);
server.send(200, "text/plane", wifi_data); //Send ADC value only to client ajax request
}
//===============================================================
// Setup
//==============================================================
IPAddress local_IP(192, 168, 2, 105);
IPAddress gateway(192, 168, 2, 9);
IPAddress subnet(255, 255, 255, 0);
void setup(void) {
Serial.begin(115200);
Serial.println();
Serial.println("Booting Sketch...");
SerialBT.begin("ESP32test"); //Bluetooth device name
/*
//ESP32 As access point
WiFi.mode(WIFI_AP); //Access Point mode
WiFi.softAP(ssid, password);
*/
//ESP32 connects to your wifi -----------------------------------
// WiFi.mode(WIFI_STA); //Connectto your wifi
WiFi.mode(WIFI_AP_STA);
WiFi.softAP("TEST-DSP", "12345678");
WiFi.softAPConfig(local_IP, gateway, subnet);
WiFi.begin(ssid, password);
Serial.println("Connecting to ");
Serial.print(ssid);
//Wait for WiFi to connect
uint8_t reconnect_counter = 0;
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.print(".");
reconnect_counter++;
if (reconnect_counter > 3) ESP.restart();
}
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
//----------------------------------------------------------------
server.on("/", handleRoot); //This is display page
server.on("/readTime", handleTime); //To get update of ADC Value only
server.on("/readBT", handleBT);
server.on("/readWifi", handleWifi);
server.begin(); //Start server
Serial.println("HTTP server started");
}
//===============================================================
// This routine is executed when you open its IP in browser
//===============================================================
int k = 0;
void loop(void) {
server.handleClient();
if (SerialBT.available()) {
bt_data = "";
while (SerialBT.available()) {
bt_data += SerialBT.read();
//Serial.write(SerialBT.read());
}
Serial.print(bt_data.c_str());
}
if (k > 10000) {
wifi_data = "";
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
wifi_data = "no networks found";
} else {
wifi_data += n;
wifi_data += " networks found";
wifi_data += "<br>";
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
wifi_data += (i + 1);
wifi_data += ": ";
wifi_data += WiFi.SSID(i);
wifi_data += " (";
wifi_data += WiFi.RSSI(i);
wifi_data += ")";
wifi_data += ((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*");
wifi_data += "<br>";
//delay(10);
}
}
//Serial.println("");
k = 0;
}
delay(1);
k++;
}