Initial commit

This commit is contained in:
s3lph 2022-07-10 04:27:32 +02:00
commit caec46621e
13 changed files with 3558 additions and 0 deletions

33
README.md Normal file
View file

@ -0,0 +1,33 @@
## ESP8266 Firmware
### Building
Edit esp/src/main.cpp and set the configuration (WiFi credentials, MQTT broker & topic)!
For Sanyo Z4:
```
cd esp
pio run -e sanyoz4 -t upload --upload-port /dev/ttyUSB0
```
For Benq X3000i
```
cd esp
pio run -e benqx3000i -t upload --upload-port /dev/ttyUSB0
```
## PCB
* ESP-01
* [This specific MAX3232-Board from AliExpress][max3232]
* 1x 10k 0805 SMD Resistor
* 1x 2x2 2.54mm pin header
* 1x 2x4 2.54mm socket header
* 1x 1x4 2.54mm socket header
* USB Micro-B socket
* SOT-223 3v3 voltage regulator
[max3232]: https://de.aliexpress.com/item/1005001548466845.html?spm=a2g0o.productlist.0.0.29142932HtTivS&algo_pvid=37640f61-ffd9-40fb-b468-f261709c162b&algo_exp_id=37640f61-ffd9-40fb-b468-f261709c162b-1&pdp_ext_f=%7B%22sku_id%22%3A%2212000016543520800%22%7D&pdp_npi=2%40dis%21CHF%21%210.41%21%21%21%21%21%402101d8b516564877891056410e1251%2112000016543520800%21sea

1
esp/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.pio

39
esp/include/README Normal file
View file

@ -0,0 +1,39 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

153
esp/include/benqx3000i.cpp Normal file
View file

@ -0,0 +1,153 @@
#ifndef _BENQ_X3000I_H_
#define _BENQ_X3000I_H_
#include <Arduino.h>
enum Input {
HDMI1 = 1,
HDMI2 = 2,
HDMI3 = 3,
UNKNOWN = 127,
};
void setup() {
Serial.begin(9600);
Serial.setTimeout(1000);
client.enableLastWillMessage("tele/" TOPIC "/LWT", "Offline");
}
int8_t queryPower() {
Serial.print("\r*pow=?#\r");
Serial.flush();
Serial.readStringUntil('\r');
String line = Serial.readStringUntil('\r');
if (line.length() < 6) {
Serial.print("\r*pow=?#\r");
Serial.flush();
Serial.readStringUntil('\r');
line = Serial.readStringUntil('\r');
}
if (line == "*pow=on#") {
return 1;
} else if (line == "*pow==off#") {
return 0;
}
return -1;
}
Input parseInput(const String& input) {
if (input == "HDMI1") return HDMI1;
if (input == "HDMI2") return HDMI2;
if (input == "HDMI3") return HDMI3;
return UNKNOWN;
}
String inputToString(Input input) {
if (input == HDMI1) return "HDMI1";
if (input == HDMI2) return "HDMI2";
if (input == HDMI3) return "HDMI3";
return "UNKNOWN";
}
void sendInputCommand(Input input) {
switch(input) {
case HDMI1:
Serial.print("\r*sour=hdmi#\r");
break;
case HDMI2:
Serial.print("\r*sour=hdmi2#\r");
break;
case HDMI3:
Serial.print("\r*sour=smartsystem#\r");
break;
default:
break;
}
Serial.flush();
Serial.readStringUntil('\r');
}
Input parseInputState(const String& line) {
if (line == "*sour=hdmi#") return HDMI1;
if (line == "*sour=hdmi2#") return HDMI2;
if (line == "*sour=smartsystem#") return HDMI3;
return UNKNOWN;
}
Input queryInput() {
Serial.print("\r*sour=?\r");
Serial.flush();
Serial.readStringUntil('\r');
String line = Serial.readStringUntil('\r');
if (line.length() < 8) {
Serial.print("\r*sour=?\r");
Serial.flush();
Serial.readStringUntil('\r');
line = Serial.readStringUntil('\r');
}
if (line.length() <8) {
return UNKNOWN;
}
return parseInputState(line);
}
void onInput(const String& message) {
Input state = queryInput();
Input desiredState = parseInput(message);
if (state != desiredState && desiredState != UNKNOWN) {
sendInputCommand(desiredState);
state = queryInput();
}
client.publish("resp/" TOPIC "/input", inputToString(state), true);
}
void onPower(const String& message) {
bool state = queryPower();
if (message == "ON" || message == "OFF") {
bool desiredState = message == "ON";
if (state != desiredState) {
if (desiredState) {
Serial.print("\r*pow=on#\r");
Serial.flush();
Serial.readStringUntil('\r');
Serial.readStringUntil('\r');
for (uint8_t i = 0; i < 5; ++i) {
state = queryPower();
if (state) {
break;
}
}
} else {
for (uint8_t i = 0; i < 3; ++i) {
Serial.print("\r*pow=on#\r");
Serial.flush();
Serial.readStringUntil('\r');
Serial.readStringUntil('\r');
delay(1000);
}
for (uint8_t i = 0; i < 5; ++i) {
state = queryPower();
if (!state) {
break;
}
}
}
}
}
client.publish("resp/" TOPIC "/power", state ? "ON" : "OFF", true);
}
void onConnectionEstablished() {
client.subscribe("cmnd/" TOPIC "/power", onPower);
client.subscribe("cmnd/" TOPIC "/input", onInput);
client.publish("tele/" TOPIC "/LWT", "Online", true);
}
void loop() {
client.loop();
}
#endif

164
esp/include/sanyoz4.cpp Normal file
View file

@ -0,0 +1,164 @@
#ifndef _SANYO_Z4_H_
#define _SANYO_Z4_H_
#include <Arduino.h>
enum Input {
VIDEO = 0,
S_VIDEO = 1,
COMP1 = 2,
COMP2 = 3,
HDMI = 4,
VGA = 5,
UNKNOWN = 127,
};
void setup() {
Serial.begin(19200);
Serial.setTimeout(1000);
client.enableLastWillMessage("tele/" TOPIC "/LWT", "Offline");
}
bool queryPower() {
Serial.print("CR0\r");
Serial.flush();
String line = Serial.readStringUntil('\r');
if (line.length() != 2) {
Serial.print("CR0\r");
Serial.flush();
line = Serial.readStringUntil('\r');
}
return line == "00" || line == "40";
}
Input parseInput(const String& input) {
if (input == "VIDEO") return VIDEO;
if (input == "S-VIDEO") return S_VIDEO;
if (input == "COMP1") return COMP1;
if (input == "COMP2") return COMP2;
if (input == "VGA") return VGA;
if (input == "HDMI") return HDMI;
return UNKNOWN;
}
String inputToString(Input input) {
if (input == VIDEO) return "VIDEO";
if (input == S_VIDEO) return "S-VIDEO";
if (input == COMP1) return "COMP1";
if (input == COMP2) return "COMP2";
if (input == VGA) return "VGA";
if (input == HDMI) return "HDMI";
return "UNKNOWN";
}
void sendInputCommand(Input input) {
switch(input) {
case VIDEO:
Serial.print("C23\r");
break;
case S_VIDEO:
Serial.print("C24\r");
break;
case COMP1:
Serial.print("C25\r");
break;
case COMP2:
Serial.print("C26\r");
break;
case HDMI:
Serial.print("C53\r");
break;
case VGA:
Serial.print("C50\r");
break;
default:
break;
}
Serial.flush();
Serial.readStringUntil('\r');
}
Input parseInputState(const String& line) {
if (line == "0") return VIDEO;
if (line == "1") return S_VIDEO;
if (line == "2") return COMP1;
if (line == "3") return COMP2;
if (line == "4") return HDMI;
if (line == "5") return VGA;
return UNKNOWN;
}
Input queryInput() {
Serial.print("CR1\r");
Serial.flush();
String line = Serial.readStringUntil('\r');
if (line.length() != 1) {
Serial.print("CR1\r");
Serial.flush();
line = Serial.readStringUntil('\r');
}
if (line.length() != 1) {
return UNKNOWN;
}
return parseInputState(line);
}
void onInput(const String& message) {
Input state = queryInput();
Input desiredState = parseInput(message);
if (state != desiredState && desiredState != UNKNOWN) {
sendInputCommand(desiredState);
state = queryInput();
}
client.publish("resp/" TOPIC "/input", inputToString(state), true);
}
void onPower(const String& message) {
bool state = queryPower();
if (message == "ON" || message == "OFF") {
bool desiredState = message == "ON";
if (state != desiredState) {
if (desiredState) {
client.publish("cmnd/sonoff/irrc/IRSend", "{\"Protocol\":\"NEC\",\"Bits\":32,\"Data\":\"0xCC0000FF\"}");
Serial.print("C00\r");
Serial.flush();
Serial.readStringUntil('\r');
for (uint8_t i = 0; i < 5; ++i) {
state = queryPower();
if (state) {
break;
}
}
} else {
for (uint8_t i = 0; i < 3; ++i) {
Serial.print("C02\r");
Serial.flush();
Serial.readStringUntil('\r');
delay(1000);
}
for (uint8_t i = 0; i < 5; ++i) {
state = queryPower();
if (!state) {
break;
}
}
}
}
}
client.publish("resp/" TOPIC "/power", state ? "ON" : "OFF", true);
}
void onConnectionEstablished() {
client.subscribe("cmnd/" TOPIC "/power", onPower);
client.subscribe("cmnd/" TOPIC "/input", onInput);
client.publish("tele/" TOPIC "/LWT", "Online", true);
}
void loop() {
client.loop();
}
#endif

46
esp/lib/README Normal file
View file

@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

25
esp/platformio.ini Normal file
View file

@ -0,0 +1,25 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:sanyoz4]
build_flags = -D SANYO_Z4
platform = espressif8266
board = nodemcuv2
framework = arduino
upload_protocol = esptool
lib_deps = plapointe6/EspMQTTClient@^1.13.3
[env:benqx3000i]
build_flags = -D BENQ_X3000I
platform = espressif8266
board = nodemcuv2
framework = arduino
upload_protocol = esptool
lib_deps = plapointe6/EspMQTTClient@^1.13.3

19
esp/src/main.cpp Normal file
View file

@ -0,0 +1,19 @@
#include <Arduino.h>
// https://registry.platformio.org/libraries/plapointe6/EspMQTTClient
#include "EspMQTTClient.h"
#define TOPIC "choose a topic"
#define BROKER "mqtt broker ip"
#define SSID "your wifi ssid here"
#define PSK "your wifi psk here"
EspMQTTClient client(SSID, PSK, BROKER, TOPIC);
#ifdef SANYO_Z4
#include "sanyoz4.cpp"
#endif
#ifdef BENQ_X3000I
#include "benqx3000i.cpp"
#endif

11
esp/test/README Normal file
View file

@ -0,0 +1,11 @@
This directory is intended for PlatformIO Unit Testing and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/page/plus/unit-testing.html

31
pcb/.gitignore vendored Normal file
View file

@ -0,0 +1,31 @@
# https://github.com/github/gitignore/blob/main/KiCad.gitignore
# For PCBs designed using KiCad: https://www.kicad.org/
# Format documentation: https://kicad.org/help/file-formats/
# Temporary files
*.000
*.bak
*.bck
*.kicad_pcb-bak
*.kicad_sch-bak
*-backups
*.kicad_prl
*.sch-bak
*~
_autosave-*
*.tmp
*-save.pro
*-save.kicad_pcb
fp-info-cache
# Netlist files (exported from Eeschema)
*.net
# Autorouter files (exported from Pcbnew)
*.dsn
*.ses
# Exported BOM files
*.xml
*.csv

1359
pcb/pcb.kicad_pcb Normal file

File diff suppressed because it is too large Load diff

436
pcb/pcb.kicad_pro Normal file
View file

@ -0,0 +1,436 @@
{
"board": {
"design_settings": {
"defaults": {
"board_outline_line_width": 0.09999999999999999,
"copper_line_width": 0.19999999999999998,
"copper_text_italic": false,
"copper_text_size_h": 1.5,
"copper_text_size_v": 1.5,
"copper_text_thickness": 0.3,
"copper_text_upright": false,
"courtyard_line_width": 0.049999999999999996,
"dimension_precision": 4,
"dimension_units": 3,
"dimensions": {
"arrow_length": 1270000,
"extension_offset": 500000,
"keep_text_aligned": true,
"suppress_zeroes": false,
"text_position": 0,
"units_format": 1
},
"fab_line_width": 0.09999999999999999,
"fab_text_italic": false,
"fab_text_size_h": 1.0,
"fab_text_size_v": 1.0,
"fab_text_thickness": 0.15,
"fab_text_upright": false,
"other_line_width": 0.15,
"other_text_italic": false,
"other_text_size_h": 1.0,
"other_text_size_v": 1.0,
"other_text_thickness": 0.15,
"other_text_upright": false,
"pads": {
"drill": 0.762,
"height": 1.524,
"width": 1.524
},
"silk_line_width": 0.15,
"silk_text_italic": false,
"silk_text_size_h": 1.0,
"silk_text_size_v": 1.0,
"silk_text_thickness": 0.15,
"silk_text_upright": false,
"zones": {
"45_degree_only": false,
"min_clearance": 0.0
}
},
"diff_pair_dimensions": [
{
"gap": 0.0,
"via_gap": 0.0,
"width": 0.0
}
],
"drc_exclusions": [],
"meta": {
"version": 2
},
"rule_severities": {
"annular_width": "error",
"clearance": "error",
"copper_edge_clearance": "error",
"courtyards_overlap": "error",
"diff_pair_gap_out_of_range": "error",
"diff_pair_uncoupled_length_too_long": "error",
"drill_out_of_range": "error",
"duplicate_footprints": "warning",
"extra_footprint": "warning",
"footprint_type_mismatch": "error",
"hole_clearance": "error",
"hole_near_hole": "error",
"invalid_outline": "error",
"item_on_disabled_layer": "error",
"items_not_allowed": "error",
"length_out_of_range": "error",
"malformed_courtyard": "error",
"microvia_drill_out_of_range": "error",
"missing_courtyard": "ignore",
"missing_footprint": "warning",
"net_conflict": "warning",
"npth_inside_courtyard": "ignore",
"padstack": "error",
"pth_inside_courtyard": "ignore",
"shorting_items": "error",
"silk_over_copper": "warning",
"silk_overlap": "warning",
"skew_out_of_range": "error",
"through_hole_pad_without_hole": "error",
"too_many_vias": "error",
"track_dangling": "warning",
"track_width": "error",
"tracks_crossing": "error",
"unconnected_items": "error",
"unresolved_variable": "error",
"via_dangling": "warning",
"zone_has_empty_net": "error",
"zones_intersect": "error"
},
"rules": {
"allow_blind_buried_vias": false,
"allow_microvias": false,
"max_error": 0.005,
"min_clearance": 0.0,
"min_copper_edge_clearance": 0.0,
"min_hole_clearance": 0.25,
"min_hole_to_hole": 0.25,
"min_microvia_diameter": 0.19999999999999998,
"min_microvia_drill": 0.09999999999999999,
"min_silk_clearance": 0.0,
"min_through_hole_diameter": 0.3,
"min_track_width": 0.19999999999999998,
"min_via_annular_width": 0.049999999999999996,
"min_via_diameter": 0.39999999999999997,
"solder_mask_clearance": 0.0,
"solder_mask_min_width": 0.0,
"use_height_for_length_calcs": true
},
"track_widths": [
0.0,
0.25,
0.4,
0.5
],
"via_dimensions": [
{
"diameter": 0.0,
"drill": 0.0
}
],
"zones_allow_external_fillets": false,
"zones_use_no_outline": true
},
"layer_presets": []
},
"boards": [],
"cvpcb": {
"equivalence_files": []
},
"erc": {
"erc_exclusions": [],
"meta": {
"version": 0
},
"pin_map": [
[
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
2
],
[
0,
2,
0,
1,
0,
0,
1,
0,
2,
2,
2,
2
],
[
0,
0,
0,
0,
0,
0,
1,
0,
1,
0,
1,
2
],
[
0,
1,
0,
0,
0,
0,
1,
1,
2,
1,
1,
2
],
[
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
2
],
[
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
2
],
[
1,
1,
1,
1,
1,
0,
1,
1,
1,
1,
1,
2
],
[
0,
0,
0,
1,
0,
0,
1,
0,
0,
0,
0,
2
],
[
0,
2,
1,
2,
0,
0,
1,
0,
2,
2,
2,
2
],
[
0,
2,
0,
1,
0,
0,
1,
0,
2,
0,
0,
2
],
[
0,
2,
1,
1,
0,
0,
1,
0,
2,
0,
0,
2
],
[
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2,
2
]
],
"rule_severities": {
"bus_definition_conflict": "error",
"bus_entry_needed": "error",
"bus_label_syntax": "error",
"bus_to_bus_conflict": "error",
"bus_to_net_conflict": "error",
"different_unit_footprint": "error",
"different_unit_net": "error",
"duplicate_reference": "error",
"duplicate_sheet_names": "error",
"extra_units": "error",
"global_label_dangling": "warning",
"hier_label_mismatch": "error",
"label_dangling": "error",
"lib_symbol_issues": "warning",
"multiple_net_names": "warning",
"net_not_bus_member": "warning",
"no_connect_connected": "warning",
"no_connect_dangling": "warning",
"pin_not_connected": "error",
"pin_not_driven": "error",
"pin_to_pin": "warning",
"power_pin_not_driven": "error",
"similar_labels": "warning",
"unannotated": "error",
"unit_value_mismatch": "error",
"unresolved_variable": "error",
"wire_dangling": "error"
}
},
"libraries": {
"pinned_footprint_libs": [],
"pinned_symbol_libs": []
},
"meta": {
"filename": "pcb.kicad_pro",
"version": 1
},
"net_settings": {
"classes": [
{
"bus_width": 12.0,
"clearance": 0.2,
"diff_pair_gap": 0.25,
"diff_pair_via_gap": 0.25,
"diff_pair_width": 0.2,
"line_style": 0,
"microvia_diameter": 0.3,
"microvia_drill": 0.1,
"name": "Default",
"pcb_color": "rgba(0, 0, 0, 0.000)",
"schematic_color": "rgba(0, 0, 0, 0.000)",
"track_width": 0.25,
"via_diameter": 0.8,
"via_drill": 0.4,
"wire_width": 6.0
}
],
"meta": {
"version": 2
},
"net_colors": null
},
"pcbnew": {
"last_paths": {
"gencad": "",
"idf": "",
"netlist": "",
"specctra_dsn": "",
"step": "",
"vrml": ""
},
"page_layout_descr_file": ""
},
"schematic": {
"annotate_start_num": 0,
"drawing": {
"default_line_thickness": 6.0,
"default_text_size": 50.0,
"field_names": [],
"intersheets_ref_own_page": false,
"intersheets_ref_prefix": "",
"intersheets_ref_short": false,
"intersheets_ref_show": false,
"intersheets_ref_suffix": "",
"junction_size_choice": 3,
"label_size_ratio": 0.375,
"pin_symbol_size": 25.0,
"text_offset_ratio": 0.15
},
"legacy_lib_dir": "",
"legacy_lib_list": [],
"meta": {
"version": 1
},
"net_format_name": "",
"ngspice": {
"fix_include_paths": true,
"fix_passive_vals": false,
"meta": {
"version": 0
},
"model_mode": 0,
"workbook_filename": ""
},
"page_layout_descr_file": "",
"plot_directory": "",
"spice_adjust_passive_values": false,
"spice_external_command": "spice \"%I\"",
"subpart_first_id": 65,
"subpart_id_separator": 0
},
"sheets": [
[
"78a16d3a-21cc-459c-881d-94d53fce7eac",
""
]
],
"text_variables": {}
}

1241
pcb/pcb.kicad_sch Normal file

File diff suppressed because it is too large Load diff