2026-04-05 23:00:46 +05:30
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
|
#include <spi_flash.h>
|
2026-07-30 17:36:24 +05:30
|
|
|
#include <SoftwareSerial.h>
|
2026-04-05 23:00:46 +05:30
|
|
|
|
2026-07-30 15:13:00 +05:30
|
|
|
#define WIFI_SSID "KAIPPUZHAKKUNNEL"
|
|
|
|
|
#define WIFI_PASS "7042754150"
|
2026-04-05 23:00:46 +05:30
|
|
|
|
2026-07-30 17:36:24 +05:30
|
|
|
WiFiServer server(80);
|
2026-04-05 23:00:46 +05:30
|
|
|
|
2026-07-30 17:36:24 +05:30
|
|
|
SoftwareSerial sensorSerial;
|
|
|
|
|
unsigned long int lastPrintTime = 0;
|
|
|
|
|
unsigned long int lastWiFiCheck = 0;
|
2026-04-05 23:00:46 +05:30
|
|
|
|
2026-07-30 17:36:24 +05:30
|
|
|
int distance_d = 0;
|
2026-04-05 23:00:46 +05:30
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
|
delay(5000);
|
2026-07-30 17:36:24 +05:30
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
|
WiFi.setAutoReconnect(true);
|
2026-04-05 23:00:46 +05:30
|
|
|
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
|
|
|
|
while(WiFi.status() != WL_CONNECTED) {
|
|
|
|
|
delay(500);
|
|
|
|
|
}
|
2026-07-30 17:36:24 +05:30
|
|
|
String status = WiFi.localIP().toString();
|
2026-04-05 23:00:46 +05:30
|
|
|
Serial.begin(9600);
|
|
|
|
|
Serial.println("Got IP: " + status);
|
2026-07-30 17:36:24 +05:30
|
|
|
sensorSerial.begin(9600, SWSERIAL_8N1, D5, D6, false, 64);
|
|
|
|
|
server.begin();
|
2026-04-05 23:00:46 +05:30
|
|
|
}
|
|
|
|
|
|
2026-07-30 17:36:24 +05:30
|
|
|
|
2026-04-05 23:00:46 +05:30
|
|
|
void loop() {
|
|
|
|
|
|
2026-07-30 17:36:24 +05:30
|
|
|
uint8_t data_header = 0xFF;
|
|
|
|
|
uint8_t data[3]; //comprises of data_high, data_low & checksum.
|
|
|
|
|
if(sensorSerial.available() >= 4) {
|
|
|
|
|
if(sensorSerial.read() == 0xFF) {
|
|
|
|
|
data[0] = sensorSerial.read();
|
|
|
|
|
data[1] = sensorSerial.read();
|
|
|
|
|
data[2] = sensorSerial.read();
|
|
|
|
|
uint8_t checksum = (data_header + data[0] + data[1]) & 0xFF;
|
|
|
|
|
bool correct_checksum = false;
|
|
|
|
|
if(data[2] == checksum) {
|
|
|
|
|
correct_checksum = true;
|
|
|
|
|
distance_d = (data[0] << 8) | data[1];
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
while(sensorSerial.available()) {
|
|
|
|
|
sensorSerial.read();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(millis() - lastPrintTime >= 3000) {
|
|
|
|
|
Serial.println("Got Header Byte.");
|
|
|
|
|
if(correct_checksum)
|
|
|
|
|
Serial.println("Checksum is correct.");
|
|
|
|
|
else
|
|
|
|
|
Serial.println("Checksum is incorrect.");
|
|
|
|
|
Serial.printf("Current Distance: %d mm\n", distance_d);
|
|
|
|
|
lastPrintTime = millis();
|
|
|
|
|
}
|
2026-04-05 23:00:46 +05:30
|
|
|
}
|
|
|
|
|
}
|
2026-07-30 17:36:24 +05:30
|
|
|
if(WiFi.status() != WL_CONNECTED) {
|
|
|
|
|
if(millis() - lastWiFiCheck >= 10000) {
|
|
|
|
|
WiFi.reconnect();
|
|
|
|
|
lastWiFiCheck = millis();
|
|
|
|
|
}
|
|
|
|
|
return;
|
2026-04-05 23:00:46 +05:30
|
|
|
}
|
2026-07-30 17:36:24 +05:30
|
|
|
WiFiClient client = server.available();
|
|
|
|
|
if (client) {
|
|
|
|
|
unsigned long clientTimeout = millis();
|
|
|
|
|
Serial.println("New Client.");
|
|
|
|
|
while(client.connected() && !client.available()) {
|
|
|
|
|
if (millis() - clientTimeout > 1000) {
|
|
|
|
|
Serial.println("Client timeout.");
|
|
|
|
|
client.stop();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
delay(1);
|
|
|
|
|
}
|
2026-04-05 23:00:46 +05:30
|
|
|
|
2026-07-30 17:36:24 +05:30
|
|
|
if (client.available()) {
|
|
|
|
|
while (client.available()) {
|
|
|
|
|
client.read();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client.println("HTTP/1.1 200 OK");
|
|
|
|
|
client.println("Content-Type: application/json");
|
|
|
|
|
client.println("Connection: close");
|
|
|
|
|
client.println();
|
|
|
|
|
client.printf("{\"distance\": %d, \"unit\": \"mm\"}\n", distance_d);
|
|
|
|
|
client.stop();
|
|
|
|
|
Serial.println("Client disconnected.");
|
|
|
|
|
}
|
2026-04-05 23:00:46 +05:30
|
|
|
}
|