Amir Pournasserian
Published © MIT

Stand-Up Alarm Chair with ESP32 + Arduino IDE

Be reminded to get up by using loud sounds from your Stand-Up Alarm Chair. Stand up to sitting down and improve your health!

IntermediateFull instructions provided1 hour1,551

Things used in this project

Story

Read more

Schematics

ESP32 Setup

Code

Seat Client

Arduino
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino.h>
#include <EasyBuzzer.h>

const char* ESP_ssid = "ESP32-Access-Point";
const char* ESP_password = "123456789";

const char* url_switch = "http://192.168.4.1/On_Off";
int buzzer = 15;

String switcher;

int interval = 3600;
int period_rest = 1800;

void setup(){
  Serial.begin(115200);
  WiFi.begin(ESP_ssid, ESP_password);
  while (WiFi.status() != WL_CONNECTED) { //Check for the connection
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
 
  Serial.println("Connected to the WiFi network");
  Serial.println(WiFi.localIP());

  EasyBuzzer.setPin(buzzer);
}

void loop(){
  int temp_rest = 0;
  int counter = 0;
  for(int i = 0; i < 86400; i++){    
    EasyBuzzer.update();
    if(WiFi.status()== WL_CONNECTED){ 
      switcher = httpGETRequest(url_switch);
    }else{
      Serial.println("Error in WiFi connection");    
    }
    if((counter % interval) > period_rest){
      if(switcher == "1"){
        Serial.println("NOT RESTING");
        EasyBuzzer.singleBeep(2000, 1000);
      }else{
        Serial.println("RESTING");
        EasyBuzzer.stopBeep();
        counter = counter + 1;
      }
    }else if(switcher == "1"){
      Serial.println("SITTING");
      counter = counter + 1;
      temp_rest = 0;
    }else{
      Serial.println("NOT SITTING");
      temp_rest = temp_rest + 1;
      if(temp_rest > period_rest){
        counter = 0;
      }
    }
    delay(1000);
  }
}

String httpGETRequest(const char* serverName) {
  HTTPClient http;
    
  // Your IP address with path or Domain name with URL path 
  http.begin(serverName);
  
  // Send HTTP POST request
  int httpResponseCode = http.GET();
  
  String payload = "--"; 
  
  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    payload = http.getString();
  }
  else {
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  // Free resources
  http.end();

  return payload;
}

Seat Server

Arduino
#include <WiFi.h>
#include <HTTPClient.h>
#include <ESPAsyncWebServer.h>

int isObstaclePin = 15;  // This is our input pin
int isObstacle = HIGH;  // HIGH MEANS NO OBSTACLE

const char* ssid     = "Pokemon";              //Main Router      
const char* password = "1945000000";            //Main Router Password
const char* ESP_ssid = "ESP32-Access-Point";
const char* ESP_password = "123456789";
const char* url = "http://myworkspace.hub.ubeac.io/myPC";
String payload_pattern = "[{\"id\": \"MyESP\", \"sensors\": [{\"id\": \"Time Near Sensor\", \"value\": $timer$}, {\"id\": \"Close\", \"value\": $switcher$}]}]";
int dividen = 3600;
int counter = 0;
int switcher = 0;

AsyncWebServer server(80);

void setup() {
  
  //Setup IR Obstacle Sensor
  pinMode(isObstaclePin, INPUT);  
  Serial.begin(115200); // Starts the serial communication

  //Connect to Local WiFi
  delay(4000);   //Delay needed before calling the WiFi.begin
 
  WiFi.begin(ssid, password); 
 
  while (WiFi.status() != WL_CONNECTED) { //Check for the connection
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
 
  Serial.println("Connected to the WiFi network");

  //Setup ESP32 WiFi
  WiFi.softAP(ESP_ssid, ESP_password);

  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(IP);
  
  server.on("/On_Off", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", String(switcher).c_str());
  });

  bool status;

  server.begin();
}

void loop() {
  counter = 0;
  switcher = 0;
  for (int i = 0; i < 86400; i++){
    isObstacle = digitalRead(isObstaclePin);
    if (isObstacle == LOW)
    {
      Serial.println("OBSTACLE!!, OBSTACLE!!");
      counter = counter + 1;
      switcher = 1;
    }
    else
    {
      Serial.println("clear");
      switcher = 0;
    }
    
    float timer;
    timer = (float)counter / (float)dividen;
    String payload = payload_pattern;
    payload.replace("$timer$",String(timer));
    payload.replace("$switcher$",String(switcher));

    if(WiFi.status()== WL_CONNECTED){ 
 
      HTTPClient http;   
  
      http.begin(url);  
      int httpResponseCode = http.POST(payload); 
 
      if(httpResponseCode>0){
        String response = http.getString(); 
        Serial.println(httpResponseCode);
      }
      http.end();
 
    }else{
      Serial.println("Error in WiFi connection");    
    }
    delay(1000);
  }
}

Credits

Amir Pournasserian

Amir Pournasserian

10 projects • 15 followers
Data scientist, machine learning specialist, IoT nerd. CEO of Momentaj and Founder of uBeac, IoT thinktanks.

Comments