You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

120 lines
2.9 KiB

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <DHT12.h>
#include <html.h>
const char *ssid = "NodeMCU";
const char *password = "kvartira138";
ESP8266WebServer server(80);
DHT12 dht12(5, true);
const int led = 13;
long int lastUpdate = 0;
int t12 = 0;
int h12 = 0;
int acc = 0;
const int graph_size = 200;
int graph1[graph_size] = { 200 };
int graph1counter = 0;
void handleRoot()
{
const int arr_size = 10000;
char *html_code = new char[arr_size];
memset(html_code, '\0', sizeof(char)*arr_size);
int16_t bias = 0;
int16_t bias2 = 0;
bias += getHtml(HTML_BEGIN, *html_code, arr_size, 0);
bias += getHtml(STYLE, *html_code, arr_size, bias);
bias += getHtml(SCRIPT_BEGIN, *html_code, arr_size, bias);
for (int i = 0; i != graph_size - 1; i++) {
bias += snprintf((html_code+bias), arr_size - bias, "context.lineTo(%i*2, %i);", i, graph1[i]);
}
bias += getHtml(SCRIPT_END, *html_code, arr_size, bias);
bias += snprintf(
(html_code+bias), arr_size - bias,
"<br> Current temperature: %i \
<br> Current humudity: %i \
<br />Current accelerometr: %i",
t12, h12, acc);
bias += getHtml(HTML_END, *html_code, arr_size, bias);
server.send(200, "text/html", html_code);
delete[] html_code;
}
void updateDHT()
{
if ((millis() - lastUpdate) > 20)
{
// t12 = (int)dht12.readTemperature();
// int t12_remapped = map(t12, -20, 100, 200, 0);
// h12 = (int)dht12.readHumidity();
acc = map(analogRead(A0), 512, 768, 200, 0);
if (graph1counter < graph_size - 1) {
graph1[graph1counter] = acc;
graph1counter++;
} else {
for(int i = 0; i != graph1counter; i++) {
graph1[i] = graph1[i + 1];
}
graph1[graph1counter] = acc;
}
lastUpdate = millis();
}
}
void handleNotFound()
{
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++)
{
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void setup(void)
{
pinMode(A0, INPUT);
Serial.begin(115200);
WiFi.softAP(ssid);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRoot);
server.on("/inline", []() {
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void)
{
updateDHT();
server.handleClient();
}