Initial commit
This commit is contained in:
295
src/main.cpp
Normal file
295
src/main.cpp
Normal file
@@ -0,0 +1,295 @@
|
||||
#include <Arduino.h>
|
||||
#include <Preferences.h>
|
||||
#include <WiFi.h>
|
||||
#include <AsyncTCP.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <Wire.h>
|
||||
#include <SHT31.h> // temp. sensor
|
||||
#include <NMEA2000.h>
|
||||
#include <N2kMsg.h>
|
||||
#include <N2kMessages.h>
|
||||
#include "main.h"
|
||||
#include "Nmea2kTwai.h"
|
||||
|
||||
Preferences preferences; // persistent storage for configuration
|
||||
|
||||
const char* wifi_ssid = "OBPKP61";
|
||||
const char* wifi_pass = "keypad61";
|
||||
AsyncWebServer server(80);
|
||||
const char index_html[] PROGMEM = R"rawliteral(
|
||||
<!DOCTYPE HTML><html>
|
||||
<head>
|
||||
<title>ESP Web Server</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="icon" href="data:,">
|
||||
</head>
|
||||
<body>
|
||||
<h2>ESP Web Server</h2>
|
||||
<p>Work in progress</p>
|
||||
</body>
|
||||
</html>
|
||||
)rawliteral";
|
||||
|
||||
unsigned long lastPrint = 0;
|
||||
unsigned long counter = 0;
|
||||
|
||||
bool rgb_r = false;
|
||||
bool rgb_g = false;
|
||||
bool rgb_b = false;
|
||||
|
||||
char destination = 'A'; // A | B | C
|
||||
|
||||
SHT31 sht(SHT31_ADDRESS);
|
||||
|
||||
int nodeid; // NMEA2000 id on bus
|
||||
Nmea2kTwai &NMEA2000=*(new Nmea2kTwai(CAN_TX, CAN_RX, CAN_RECOVERY_PERIOD));
|
||||
|
||||
|
||||
String processor(const String& var) {
|
||||
// dummy for now
|
||||
return "";
|
||||
}
|
||||
|
||||
/* Low level wifi setup (alternative)
|
||||
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data)
|
||||
{
|
||||
// printf("Event nr: %ld!\n", event_id);
|
||||
}
|
||||
|
||||
void wifi_init_softap()
|
||||
{
|
||||
esp_netif_init();
|
||||
esp_event_loop_create_default();
|
||||
esp_netif_create_default_wifi_ap();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL);
|
||||
esp_wifi_init(&cfg);
|
||||
wifi_config_t wifi_config = {
|
||||
.ap = {
|
||||
.ssid = wifi_ssid,
|
||||
.ssid_len = strlen(wifi_ssid),
|
||||
.channel = WIFI_CHANNEL,
|
||||
.password = wifi_pass,
|
||||
.max_connection = WIFI_MAX_STA,
|
||||
.authmode = WIFI_AUTH_WPA2_PSK,
|
||||
.pmf_cfg = {
|
||||
.required = true,
|
||||
},
|
||||
},
|
||||
};
|
||||
esp_wifi_set_mode(WIFI_MODE_AP);
|
||||
esp_wifi_set_config(WIFI_IF_AP, &wifi_config);
|
||||
esp_wifi_start();
|
||||
|
||||
ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
|
||||
ESP_WIFI_SSID, ESP_WIFI_PASS, ESP_WIFI_CHANNEL);
|
||||
}
|
||||
*/
|
||||
|
||||
void setup() {
|
||||
|
||||
nodeid = N2K_DEFAULT_NODEID;
|
||||
Serial.print("N2K default node id=");
|
||||
Serial.println(nodeid);
|
||||
|
||||
preferences.begin("nvs", false);
|
||||
nodeid = preferences.getInt("LastNodeId", N2K_DEFAULT_NODEID);
|
||||
preferences.end();
|
||||
|
||||
/*
|
||||
// Connect as client to existing network
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(1000);
|
||||
Serial.println("Connecting to WiFi..");
|
||||
}
|
||||
Serial.println(WiFi.localIP()); */
|
||||
|
||||
WiFi.persistent(false);
|
||||
WiFi.mode(WIFI_MODE_AP);
|
||||
|
||||
IPAddress ap_addr(192, 168, 15, 1);
|
||||
IPAddress ap_subnet(255, 255, 255, 0);
|
||||
IPAddress ap_gateway(ap_addr);
|
||||
|
||||
int channel = WIFI_CHANNEL;
|
||||
bool hidden = false;
|
||||
WiFi.softAP(wifi_ssid, wifi_pass, channel, hidden, WIFI_MAX_STA);
|
||||
|
||||
// Route for root / web page
|
||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
|
||||
request->send(200, "text/html", index_html, processor);
|
||||
});
|
||||
server.begin();
|
||||
|
||||
NMEA2000.SetProductInformation("00000001", // Manufacturer's Model serial code
|
||||
74, // Manufacturer's product code
|
||||
"OBPkeypad6/1", // Manufacturer's Model ID
|
||||
"1.0.0 (2025-11-28)", // Manufacturer's Software version code
|
||||
"0.1" // Manufacturer's Model version
|
||||
);
|
||||
|
||||
// TODO Device unique id stored in preferences
|
||||
NMEA2000.SetDeviceInformation(1, // Unique number. Use e.g. Serial number.
|
||||
130, // Device function=Atmospheric
|
||||
85, // Device class=External Environment
|
||||
2046
|
||||
);
|
||||
|
||||
// Buttons active-low, internal resistor
|
||||
pinMode(KEY_1, INPUT_PULLUP);
|
||||
pinMode(KEY_2, INPUT_PULLUP);
|
||||
pinMode(KEY_3, INPUT_PULLUP);
|
||||
pinMode(KEY_4, INPUT_PULLUP);
|
||||
pinMode(KEY_5, INPUT_PULLUP);
|
||||
pinMode(KEY_6, INPUT_PULLUP);
|
||||
pinMode(KEY_DST, INPUT_PULLUP);
|
||||
|
||||
// internal user led (red)
|
||||
pinMode(LED_USER, OUTPUT);
|
||||
digitalWrite(LED_USER, HIGH);
|
||||
delay(1000);
|
||||
digitalWrite(LED_USER, LOW);
|
||||
|
||||
// destination leds
|
||||
pinMode(LED_A, OUTPUT);
|
||||
digitalWrite(LED_A, HIGH);
|
||||
pinMode(LED_B, OUTPUT);
|
||||
digitalWrite(LED_B, LOW);
|
||||
pinMode(LED_C, OUTPUT);
|
||||
digitalWrite(LED_C, LOW);
|
||||
|
||||
// Init onbard RGB LED
|
||||
// TODO
|
||||
|
||||
// enclosure rgb led (common anode)
|
||||
pinMode(LED_RGBA, OUTPUT);
|
||||
digitalWrite(LED_RGBA, HIGH);
|
||||
pinMode(LED_RGBB, OUTPUT);
|
||||
digitalWrite(LED_RGBB, HIGH);
|
||||
pinMode(LED_RGBC, OUTPUT);
|
||||
digitalWrite(LED_RGBC, HIGH);
|
||||
|
||||
Serial.begin(115200);
|
||||
delay(500);
|
||||
Serial.println("Starting...");
|
||||
|
||||
// I²C
|
||||
Serial.print("SHT31_LIB_VERSION: ");
|
||||
Serial.println(SHT31_LIB_VERSION);
|
||||
Wire.begin(I2C_SDA, I2C_SCL);
|
||||
Wire.setClock(100000);
|
||||
uint16_t stat = sht.readStatus();
|
||||
Serial.print(stat, HEX);
|
||||
// stat = ffff anscheinend Fehler
|
||||
// = 8010 läuft anscheinend
|
||||
Serial.println();
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
// Button pressed? (active low)
|
||||
uint8_t button = 0;
|
||||
if (digitalRead(KEY_1) == LOW) {
|
||||
Serial.println("Button detected: 1");
|
||||
button = 1;
|
||||
if (rgb_r) {
|
||||
rgb_r = false;
|
||||
digitalWrite(LED_RGBA, HIGH);
|
||||
} else {
|
||||
rgb_r = true;
|
||||
digitalWrite(LED_RGBA, LOW);
|
||||
}
|
||||
}
|
||||
if (digitalRead(KEY_2) == LOW) {
|
||||
Serial.println("Button detected: 2");
|
||||
button += 2;
|
||||
if (rgb_g) {
|
||||
rgb_g = false;
|
||||
digitalWrite(LED_RGBB, HIGH);
|
||||
} else {
|
||||
rgb_g = true;
|
||||
digitalWrite(LED_RGBB, LOW);
|
||||
}
|
||||
}
|
||||
if (digitalRead(KEY_3) == LOW) {
|
||||
Serial.println("Button detected: 3");
|
||||
button += 4;
|
||||
if (rgb_b) {
|
||||
rgb_b = false;
|
||||
digitalWrite(LED_RGBC, HIGH);
|
||||
} else {
|
||||
rgb_b = true;
|
||||
digitalWrite(LED_RGBC, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
if (digitalRead(KEY_4) == LOW) {
|
||||
Serial.println("Button detected: 4");
|
||||
button += 8;
|
||||
digitalWrite(LED_USER, HIGH); // Turn LED on
|
||||
delay(500); // Keep it on 0.5s
|
||||
digitalWrite(LED_USER, LOW); // Turn LED off
|
||||
}
|
||||
if (digitalRead(KEY_5) == LOW) {
|
||||
Serial.println("Button detected: 5");
|
||||
button += 16;
|
||||
digitalWrite(LED_USER, HIGH); // Turn LED on
|
||||
delay(500); // Keep it on 0.5s
|
||||
digitalWrite(LED_USER, LOW); // Turn LED off
|
||||
}
|
||||
if (digitalRead(KEY_6) == LOW) {
|
||||
Serial.println("Button detected: 6");
|
||||
button += 32;
|
||||
digitalWrite(LED_USER, HIGH); // Turn LED on
|
||||
delay(500); // Keep it on 0.5s
|
||||
digitalWrite(LED_USER, LOW); // Turn LED off
|
||||
}
|
||||
|
||||
if (digitalRead(KEY_DST) == LOW) {
|
||||
Serial.println("Button detected: DST");
|
||||
button += 64;
|
||||
|
||||
if (destination == 'A') {
|
||||
destination = 'B';
|
||||
digitalWrite(LED_A, LOW);
|
||||
digitalWrite(LED_B, HIGH);
|
||||
} else if (destination == 'B') {
|
||||
destination = 'C';
|
||||
digitalWrite(LED_B, LOW);
|
||||
digitalWrite(LED_C, HIGH);
|
||||
} else {
|
||||
destination = 'A';
|
||||
digitalWrite(LED_C, LOW);
|
||||
digitalWrite(LED_A, HIGH);
|
||||
}
|
||||
|
||||
/*
|
||||
digitalWrite(LED_USER, HIGH); // Turn LED on
|
||||
delay(500); // Keep it on 0.5s
|
||||
digitalWrite(LED_USER, LOW); // Turn LED off
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
if (button > 0) {
|
||||
sht.read();
|
||||
Serial.print(sht.getTemperature(), 1);
|
||||
Serial.print("\t");
|
||||
Serial.println(sht.getHumidity(), 1);
|
||||
// Debounce delay to avoid multiple triggers
|
||||
delay(200);
|
||||
}
|
||||
|
||||
|
||||
// ---- PRINT NUMBER EVERY SECOND ----
|
||||
if (millis() - lastPrint >= 1000) {
|
||||
lastPrint = millis();
|
||||
counter++;
|
||||
Serial.printf("Loop counter: %lu\n", counter);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user