184 lines
3.7 KiB
C++
184 lines
3.7 KiB
C++
#include <ESP8266WiFi.h>
|
|
#include <EEPROM.h>
|
|
#include <DHT.h>
|
|
|
|
const char* ssid = "FLRLAN";
|
|
const char* password = "XXXXXX";
|
|
|
|
#define DHT_PIN 4
|
|
|
|
DHT dht(DHT_PIN, DHT22);
|
|
|
|
WiFiServer server(80);
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
dht.begin();
|
|
EEPROM.begin(512);
|
|
|
|
delay(10);
|
|
|
|
// Connect to WiFi network
|
|
Serial.println();
|
|
Serial.println();
|
|
Serial.print("Connecting to ");
|
|
Serial.println(ssid);
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("");
|
|
Serial.println("WiFi connected");
|
|
|
|
// Start the server
|
|
server.begin();
|
|
Serial.println("Server started");
|
|
|
|
// Print the IP address
|
|
Serial.print("Use this URL to connect: ");
|
|
Serial.print("http://");
|
|
Serial.print(WiFi.localIP());
|
|
Serial.println("/");
|
|
}
|
|
|
|
#define REQ_SLP 0
|
|
#define REQ_SET 1
|
|
#define REQ_ERR 99
|
|
|
|
void loop() {
|
|
// Check if a client has connected
|
|
WiFiClient client = server.available();
|
|
if (!client) {
|
|
return;
|
|
}
|
|
|
|
// Wait until the client sends some data
|
|
Serial.println("new client");
|
|
while(!client.available()){
|
|
delay(1);
|
|
}
|
|
|
|
// Read the first line of the request
|
|
String requestStr = client.readStringUntil('\r');
|
|
Serial.println(requestStr);
|
|
client.flush();
|
|
|
|
int idxSpace = requestStr.indexOf(" ");
|
|
requestStr = requestStr.substring(idxSpace + 1);
|
|
|
|
int idxSndSpace = requestStr.indexOf(" ");
|
|
requestStr = requestStr.substring(0, idxSndSpace);
|
|
|
|
// Match the request
|
|
|
|
int value = REQ_ERR;
|
|
String parameter = "";
|
|
|
|
if (requestStr.indexOf("/SLEEP=") != -1) {
|
|
value = REQ_SLP;
|
|
int idx = requestStr.indexOf("/SLEEP=");
|
|
parameter = requestStr.substring(idx + 7);
|
|
} else if (requestStr.indexOf("/SET=") != -1) {
|
|
value = REQ_SET;
|
|
int idx = requestStr.indexOf("/SET=");
|
|
parameter = requestStr.substring(idx + 5);
|
|
} else {
|
|
value = REQ_ERR;
|
|
}
|
|
|
|
|
|
// Headers of the response
|
|
client.println("HTTP/1.1 200 OK");
|
|
client.println("Content-Type: text/html");
|
|
client.println("");
|
|
|
|
// HTML part
|
|
client.println("<!DOCTYPE HTML>");
|
|
client.println("<html>");
|
|
|
|
client.println("<h1>");
|
|
client.println(getNodeId());
|
|
client.println("</h1>");
|
|
|
|
float h = dht.readHumidity();
|
|
float t = dht.readTemperature();
|
|
|
|
while (isnan(h) || isnan(t)) {
|
|
h = dht.readHumidity();
|
|
t = dht.readTemperature();
|
|
}
|
|
|
|
client.print ("Temperature: ");
|
|
client.print (t);
|
|
client.print (" *C");
|
|
client.println("<br>");
|
|
|
|
client.print ("Humidity: ");
|
|
client.print (h);
|
|
client.print ("%");
|
|
client.println("<br>");
|
|
|
|
client.print("Request is ");
|
|
client.println(value);
|
|
|
|
client.print("Parameter is ");
|
|
client.println(parameter);
|
|
|
|
client.println("<br><br>");
|
|
client.println("</html>");
|
|
|
|
client.stop();
|
|
|
|
delay(1);
|
|
Serial.println("Client disonnected");
|
|
|
|
if (value == REQ_SLP) {
|
|
int sleepTime = parameter.toInt() * 1e6;
|
|
Serial.print("Going to sleep for ");
|
|
Serial.print(sleepTime);
|
|
Serial.print(" nanoseconds");
|
|
|
|
ESP.deepSleep(sleepTime);
|
|
} else if (value == REQ_SET) {
|
|
setNodeId(parameter);
|
|
}
|
|
}
|
|
|
|
#define LENGTH_ID 10
|
|
|
|
void setNodeId(String value) {
|
|
const char* c = value.c_str();
|
|
size_t addr = 0x0;
|
|
|
|
int givenLength = strlen(c);
|
|
if (givenLength >= LENGTH_ID) {
|
|
givenLength = LENGTH_ID;
|
|
}
|
|
|
|
size_t i = 0;
|
|
for (; i < givenLength; i++) {
|
|
EEPROM.write(addr + i, (uint8_t) c[i]);
|
|
}
|
|
for (; i < LENGTH_ID; i++) {
|
|
EEPROM.write(addr + i, 0);
|
|
}
|
|
EEPROM.commit();
|
|
}
|
|
|
|
String getNodeId() {
|
|
size_t addr = 0x0;
|
|
|
|
char* eepromId = new char[LENGTH_ID];
|
|
for (size_t i = 0; i < LENGTH_ID; i++) {
|
|
uint8_t temp = EEPROM.read(addr + i);
|
|
eepromId[i] = temp;
|
|
}
|
|
|
|
return String(eepromId);
|
|
|
|
}
|
|
|
|
|