Code Monkey home page Code Monkey logo

toiletpeek-api's Introduction

ToiletPeek API

Broadcast real-time updates of the toilet occupancy from the ESP32 devices to the NestJS application with socket.io server and broadcast the updates to the employees in the organization.

The idea of this project is to provide a real-time API for the status of the toilets occupancy in the company building. This project is just a very general concept and prototype, requiring a lot more work and refinement.

NodeJS NestJS Redis TypeScript Postgres Docker

Screenshot from DevTools and Console

I wanted to see how to create a web application using NestJS, so I spent almost 2 weeks on this project with 0 knowledge about NestJS. For now, I don't want to develop further this project, I will leave it as it is. The code is probably absolute garbage, but whatever.

I managed to implement these basic functionalities:

  • Real-time data streaming from multiple ESP32 to the server using WebSockets.
  • Broadcasting new toilets statuses to the employees in real-time.
  • User can register multiple buildings (organizations), which have many restrooms on different floors and each restroom can contain multiple toilets.
  • Each toilet has a unique ID and a token that is used to authenticate the ESP32 device.
  • Only authenticated employees can access and see the real-time updates of the toilets in the organization.

Usage

  1. Register a new user by sending a following request to the server:
POST http://localhost:8535/auth/sign-up

{
    "email": "[email protected]",
    "password": "password",
    "firstName": "John",
    "lastName": "Doe"
}
  1. Log in to the application:
POST http://localhost:8535/auth/sign-in

{
    "email": "[email protected]",
    "password": "password",
}
  1. Create a new organization (building):
POST http://localhost:8535/organizations

{
    "name": "Office Rzeszow",
    "address": "Warszawska 1, 35-000 Rzeszów",
    "password": "password"
}
  1. Create a new restroom in the organization:
POST http://localhost:8535/restrooms?organizationId=<organizationId>

{
    "name": "General Restroom",
    "floor": 1,
    "type": "general"
}
  1. Create a new toilet in the restroom:
POST http://localhost:8535/toilets?organizationId=<organizationId>&restroomId=<restroomId>

{
    "name": "Toilet#123",
    "type": "men",
    "token": "password"
}
  1. Download the main.ino sketch, fill in the:
  • wifiSSID and wifiPassword with your WiFi credentials.
  • serverAddress with the address or domain of the server where the API is running (without the http:// part).
  • serverPort with the port of the server where the API is running.
  • authorizationHeader must be in the format Authorization: Basic xxx where xxx is the base64 encoded string of toiletId:token of the toilet you created.
  1. Upload the example sketch to the ESP32 device and check the recieved events in the web application. You have to wire up the reed switch, light sensor or any other sensor that will detect the toilet occupancy and implement the logic in the sketch on your own.

  2. Now in order to recieve real time updates of the toilets in the organization as an employee, you need to authenticate in the organization:

POST http://localhost:8535/employee-access/sign-in

{
    "organizationId": "<organizationId>",
    "password": "password"
}
  1. With obtained JWT token, you can download all current toilets statuses from http://localhost:8535/employee-access/ endpoint.

  2. Now with the socket.io client, you can listen to the real-time updates of the toilets in the organization, but you have to provide JWT token in the Authorization header, which you received after logging in as an employee. You will join to the socket room with people from the same organization.

const socket = io("http://localhost:8535/employee-access", {
  extraHeaders: {
    Authorization:
      "Bearer xxx",
  },
});

socket.on("toilet_occupancy", (message) => {
  console.log("Toilet occupany update:", message);
});

toiletpeek-api's People

Contributors

maciejb2k avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.