Sooo... I am trying to make what is basically a smart chair to stop spending around 7 to 8 hours on my computer. If you sit on it, a capacitive sensor goes HIGH and the esp8266 should log it and store the time.
later, the user checks the server and sees the time that they sat on it and how long.
my problem is, i am trash at coding and i am just cobbling together spare parts of code and somehow it compiles, but this is basic ah.
i would like an implementation of a table of values, it doesnt need to be fancy, or be synchronous, it just bneeds to be readable. like, a person was detwected sitting for x hours, at x time. and needs to retain data between reboots. if you can do it, kudos!
heres the code i have-
#include <ESP8266WiFi.h>
//#include "DHT.h"
unsigned long buttonBecamePressedAt;
unsigned long buttonHasBeenPressedForTotal = 0;
unsigned long buttonHasBeenPressedForThisTime = 0;
// Uncomment one of the lines below for whatever DHT sensor type you're using!
//#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
// Replace with your network details
const char* ssid = "EveryoneWantsFreeWifi";
const char* password = "I want free wifi";
bool buttonState; // current state of the button
bool lastButtonState;
const int buttonPin = 16;
// Web Server on port 80
WiFiServer server(80);
// DHT Sensor
//const int DHTPin = 16;
// Initialize DHT sensor.
//DHT dht(DHTPin, DHTTYPE);
// Temporary variables
//static char celsiusTemp[7];
//static char fahrenheitTemp[7];
//static char humidityTemp[7];
// only runs once on boot
void setup() {
// Initializing serial port for debugging purposes
Serial.begin(115200);
delay(10);
pinMode(buttonPin, INPUT);
buttonState = digitalRead(buttonPin);
lastButtonState = buttonState;
//dht.begin();
// Connecting to WiFi network
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");
// Starting the web server
server.begin();
Serial.println("Web server running. Waiting for the ESP IP...");
delay(10000);
// Printing the ESP IP address
Serial.println(WiFi.localIP());
}
// runs over and over again
void loop() {
// Listenning for new clients
WiFiClient client = server.available();
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) // means it changed... but which way?
{
if (buttonState == LOW) // changed to pressed
{
// if the current state is LOW then the button was pressed
Serial.print("Newly pressed at ");
Serial.print(millis());
Serial.print(" ms");
buttonBecamePressedAt = millis();
} else // changed to released
{
// if the current state is HIGH then the button was released
Serial.print(", newly released at ");
Serial.print(millis());
Serial.println(" ms");
buttonHasBeenPressedForThisTime = millis() - buttonBecamePressedAt;
buttonHasBeenPressedForTotal = buttonHasBeenPressedForTotal + buttonHasBeenPressedForThisTime;
Serial.print(" This press: ");
Serial.print(buttonHasBeenPressedForThisTime);
Serial.print(" ms");
}
}
if (client) {
Serial.println("New client");
// bolean to locate when the http request ends
boolean blank_line = true;
while (client.connected()) {
if (client.available()) {
//char c = client.read();
/*if (c == '\n' && blank_line) {
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
strcpy(celsiusTemp,"Failed");
strcpy(fahrenheitTemp, "Failed");
strcpy(humidityTemp, "Failed");
}
else{
// Computes temperature values in Celsius + Fahrenheit and Humidity
float hic = dht.computeHeatIndex(t, h, false);
dtostrf(hic, 6, 2, celsiusTemp);
float hif = dht.computeHeatIndex(f, h);
dtostrf(hif, 6, 2, fahrenheitTemp);
dtostrf(h, 6, 2, humidityTemp);
// You can delete the following Serial.print's, it's just for debugging purposes
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.print(" *F");
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
}
*/
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// your actual web page that displays temperature and humidity
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head></head><body><h1>ESP8266 - Temperature and Humidity</h1><h3>Temperature in Celsius: ");
//client.println(celsiusTemp);
client.println("*C</h3><h3>Temperature in Fahrenheit: ");
//client.println(fahrenheitTemp);
client.println("*F</h3><h3>Humidity: ");
//client.println(humidityTemp);
client.println("%</h3><h3>");
client.println("</body></html>");
break;
//}
//if (c == '\n') {
// when starts reading a new line
//blank_line = true;
//}
//else if (c != '\r') {
// when finds a character on the current line
//blank_line = false;
//}
}
//}
// closing the client connection
delay(1);
client.stop();
Serial.println("Client disconnected.");
}
}
}
As you can see, it was made from a temperature sensor server and a timing thing mushed together.