ESP8266 Code

#include "Arduino.h"
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ArduinoJson.h>
#define USE_ARDUINO_INTERRUPTS true  // For the BPM
#include <PulseSensorPlayground.h>
 
// WLAN Data
const char* ssid = "hfg-gast";
const char* password = "3FarbenGrau";
 
// Our input
const int pulseWire = 0;
int Threshold = 500;
 
ESP8266WebServer server(80);
 
PulseSensorPlayground pulseSensor;
 
// Hello World Test
void getHelloWord() {
  DynamicJsonDocument doc(512);
  doc["name"] = "Hello world";
 
  Serial.print(F("Stream..."));
  String buf;
  serializeJson(doc, buf);
  server.send(200, "application/json", buf);
  Serial.print(F("done."));
}
 
// GET REQUEST
void getSensor() {
  DynamicJsonDocument doc(512);
 
  doc["ip"] = WiFi.localIP().toString();
  if (analogRead(pulseWire) >= 550) {
    doc["pulse"] = analogRead(pulseWire) - Threshold;
  } else {
    doc["pulse"] = 0;
  }
  doc["time"] = millis();
 
  if (server.arg("signalStrength") == "true") {
    doc["signalStrengh"] = WiFi.RSSI();
  }
 
  // Query Params
  if (server.arg("motorVal") == "true") {
    doc["vibrationVal"] = digitalRead(4);
  }
  if (server.arg("chipInfo") == "true") {
    doc["chipId"] = ESP.getChipId();
    doc["flashChipId"] = ESP.getFlashChipId();
    doc["flashChipSize"] = ESP.getFlashChipSize();
    doc["flashChipRealSize"] = ESP.getFlashChipRealSize();
  }
  if (server.arg("freeHeap") == "true") {
    doc["freeHeap"] = ESP.getFreeHeap();
  }
 
  Serial.print(F("Stream..."));
  String buf;
  serializeJson(doc, buf);
  server.send(200, F("application/json"), buf);
  Serial.println(F("done."));
}
 
void restServerRouting() {
  server.on("/", HTTP_GET, []() {
    server.send(200, F("text/html"),
                F("Welcome to the REST Web Server"));
  });
  server.on(F("/helloWorld"), HTTP_GET, getHelloWord);
  server.on(F("/getsensor"), HTTP_GET, getSensor);
  server.on(F("/setsensor"), HTTP_POST, setSensor);
}
 
// POST REQUEST
void setSensor() {
  String postBody = server.arg("plain");
  Serial.println(postBody);
 
  DynamicJsonDocument doc(512);
  DeserializationError error = deserializeJson(doc, postBody);
  if (error) {
    // if the file didn't open, print an error:
    Serial.print(F("Error parsing JSON "));
    Serial.println(error.c_str());
 
    String msg = error.c_str();
 
    server.send(400, F("text/html"),
                "Error in parsin json body! <br>" + msg);
 
  } else {
    JsonObject postObj = doc.as<JsonObject>();
 
    Serial.print(F("HTTP Method: "));
    Serial.println(server.method());
 
    if (server.method() == HTTP_POST) {
      if (postObj.containsKey("pin") && postObj.containsKey("value")) {
 
        Serial.println(F("done."));
 
        int pinNr = postObj["pin"];
        int valueChar = postObj["value"];
        digitalWrite(pinNr, valueChar);
 
        DynamicJsonDocument doc(512);
        doc["status"] = "OK";
 
        Serial.print(F("Stream..."));
        String buf;
        serializeJson(doc, buf);
 
        server.send(201, F("application/json"), buf);
        Serial.print(F("done."));
 
      } else {
        DynamicJsonDocument doc(512);
        doc["status"] = "KO";
        doc["message"] = F("No data found, or incorrect!");
 
        Serial.print(F("Stream..."));
        String buf;
        serializeJson(doc, buf);
 
        server.send(400, F("application/json"), buf);
        Serial.print(F("done."));
      }
    }
  }
}
 
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) {
  Serial.begin(115200);
 
  pulseSensor.analogInput(pulseWire);
  pulseSensor.setThreshold(Threshold);
 
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 
  if (MDNS.begin("esp8266")) {
    Serial.println("MDNS responder started");
  }
 
  pinMode(4, OUTPUT);
  digitalWrite(4, 0);
 
  restServerRouting();
  server.onNotFound(handleNotFound);
  server.begin();
  Serial.println("HTTP server started");
 
  pulseSensor.analogInput(pulseWire);
  pulseSensor.setThreshold(Threshold);
}
 
void loop(void) {
  server.handleClient();
}
Last updated on February 2, 2023