# Node 

Since the node is what gathers all the data from the sensors, it is the most important part of the system. The node is responsible for reading the data from the sensors, processing it, and sending it to the server.

## Hardware

The node is composed of the following hardware components:

- [ESP32 S3 DevkitC](https://www.espressif.com/en/products/socs/esp32-s3)
- [SGP30](https://www.sensirion.com/en/environmental-sensors/gas-sensors/sgp30/)
- [DHT11](https://www.sparkfun.com/datasheets/Sensors/Temperature/DHT11.pdf)
- [OLED Display](https://www.tinytronics.nl/nl/displays/oled/1.3-inch-oled-display-128*64-pixels-wit-i2c)

## Software

The node is programmed using the Arduino IDE. The code is written in C++ and is responsible for reading the data from the sensors, processing it, and sending it to the server. The first couple versions of the code were written by Sietse, and because we all had to use Object Oriented Programming, Dano rewrote the code to be object-oriented. Which is now final version

### Libraries

The following libraries are used in the node code:

- Wire.h
- Adafruit_SH110X.h
- Adafruit_SGP30.h
- DHT.h
- WiFiMulti.h
- WiFi.h
- WebSocketsClient.
- nodeCodeHeader.h

### Code

The code is divided into the following classes:

- Node readings
- Websockets

The two classes that are used are split into the 2 becouse the node readings handels everything about reading information from the sensors and displaying them on the screen, all the local stuff is handeled here so to speak. And into Websockets this handels every thing from connecting to the wifi to sending the data that is recorded from the sensors into json format and sending that data to the websockets so that the data can be prossed over there.

### Communication

The node communicates with the server using WebSockets. The node sends the data to our website which uses a reverse proxy to route it to the websocket. The server then processes the data and stores it in the database / puts it on the website. We use the WebSocketsClient library to communicate with the server. Because [websocket connections](docs\rpi-documentation\websockets.md) are stateful, we need to keep the connection alive. So we do not use any delays in the code, but instead use the millis() function to keep track of time. 

Flow of the data:

```mermaid

flowchart LR
    A(Sensors)
    B(Node)
    D(Websocket)
    E(Website)
    F(DB-Client)
    G(Database)

    A-->|sensordata| B
    B -->|JSONData| D
    D -->|JSONData| E
    D -->|JSONData| F
    F -->|Data| G
```

### Wiring Diagram

The wiring diagram for the node is as follows:

![Wiring diagram of node](docs\assets\imagesSp3\wiringDiagramNode.png)


### Fritsing Diagram

![fritsing diagram of node](docs\assets\Node.fzz)

![fritsing diagram of node](docs\assets\Node.png)

## Future Improvements

The node is currently working as intended, but there are some improvements that could be made: 

- The node could be made more energy efficient by putting the ESP32 in deep sleep mode when it is not sending data.
- The node could be made more robust by adding error handling to the code.
- The node could be made more secure by adding encryption to the data that is sent to the server.
