# A gateway for 433 MHz devices
It uses [rc_switch](https://github.com/sui77/rc-switch) library for controlling wall sockets.

It supports receiving:
- serial commands for controlling wall switches - all protocols
- RC commands from wall switches remotes - protocols 1 and 2
- sensors states over `Protocol 2`

It acts as a serial gateway, communicating in JSON format, and it is connected to a Raspberry Pi running Home Assistant.

It uses an Arduino Pro Mini 5v 16 Mhz.
The original bootloader has been replaced with Optiboot using this tutorial: https://andreasrohner.at/posts/Electronics/How-to-make-the-Watchdog-Timer-work-on-an-Arduino-Pro-Mini-by-replacing-the-bootloader/

## Receive RC
### Sensors
Sensors transmit state using `Protocol 2` and the gateway translates them into JSON over serial.
##### Protocol structure
```
   STATE
     |
1010|1|101010101|1010101010101|10101
----|-----------|-------------|-----
TYPE|    VALUE  |     VCC     |  ID
```
##### Sensors types
```C++
enum SensorType : unsigned short {
	GENERIC = 4,
	HUMIDITY = 5,
	TEMPERATURE = 6,
	CONTACT = 7
};
```
##### Sensors IDs
```C++
enum SensorId : unsigned short {
	WINDOW1 = 1,
	WINDOW2 = 2,
	WATER_SENSOR = 3,
	TEMP_SENSOR = 4,
	LIGHT_SENSOR = 5,
	MOVEMENT_SENSOR = 6,
	OIL_SENSOR = 7
};
```
#### Stairs temperature
##### Value and voltage
```json
{
  "sensor": {
    "id": 4,
    "diagnostic": {
      "voltage": 2.239
    },
    "temperature": 16.2
  }
}
```
##### Value only
```json
{
  "sensor": {
    "id": 4,
    "temperature": 16.2
  }
}
```
#### Oil sensor
##### Value and voltage
```json
{
  "sensor": {
    "id": 7,
    "diagnostic": {
      "voltage": 4.282
    },
    "value": 13
  }
}
```
### Switches
Gateway receives remote commands over RC Switch and translates them into JSON over serial
#### Protocol 1
##### on
```json
{
  "rcSwitch": {
    "protocol": 1,
    "state": true,
    "group": "1",
    "channel": 1,
    "raw_value": 5571921
  }
}
```
##### off
```json
{
  "rcSwitch": {
    "protocol": 1,
    "state": false,
    "group": "1",
    "channel": 1,
    "raw_value": 5571921
  }
}
```

#### Protocol 2
```json
{
  "rcSwitch": {
    "protocol": 2,
    "value": 2650807673
  }
}
```

#### Other protocols
```json
{
  "rcSwitch": {
    "protocol": 4,
    "value": 1234567890
  }
}
```
### DHT
Reads local DHT11 sensor and outputs JSON over serial
```
{
  "dht11": {
    "temperature": 31.9,
    "humidity": 45
  }
}
```
---
## Receive commands
It receives commands over serial, in JSON format, and executes them.
#### Protocol 1
##### on
```json
{
  "rcSwitch": {
    "protocol": 1,
    "group": "00001",
    "channel": 1,
    "state": true
  }
}
```
##### off
```json
{
  "rcSwitch": {
    "protocol": 1,
    "group": "00001",
    "channel": 1,
    "state": false
  }
}
```

#### Protocol 2
```json
{
  "rcSwitch": {
    "protocol": 2,
    "value": "10010110000000000001110101111001"
  }
}
```

#### Other protocols
```json
{
  "rcSwitch": {
    "protocol": 4,
    "value": "010001101001100101110100"
  }
}
```
