Initial commit
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
.pio
|
||||
.vscode/.browse.c_cpp.db*
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
{
|
||||
// See http://go.microsoft.com/fwlink/?LinkId=827846
|
||||
// for the documentation about the extensions.json format
|
||||
"recommendations": [
|
||||
"platformio.platformio-ide"
|
||||
],
|
||||
"unwantedRecommendations": [
|
||||
"ms-vscode.cpptools-extension-pack"
|
||||
]
|
||||
}
|
||||
@@ -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
|
||||
@@ -0,0 +1,15 @@
|
||||
#include <stdint.h>
|
||||
|
||||
namespace NetworkConfig
|
||||
{
|
||||
const char ssid[] = "LebennigHuus";
|
||||
const char password[] = "Punica-699";
|
||||
}
|
||||
|
||||
namespace MqttConfig
|
||||
{
|
||||
const char server[] = "192.168.1.7";
|
||||
const uint16_t port = 1883;
|
||||
const char user[] = "kleiax";
|
||||
const char password[] = "p?{$_~5%hBM7wrcFkr55KWr#";
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "component.h"
|
||||
|
||||
Component::Component(uint16_t loopDelay) {
|
||||
this->loopDelay = loopDelay;
|
||||
}
|
||||
|
||||
void Component::loop() {
|
||||
if (!this->active)
|
||||
return;
|
||||
|
||||
if (this->childComponents.size()){
|
||||
std::list<Component*>::iterator it;
|
||||
for (it = this->childComponents.begin(); it != this->childComponents.end(); it++)
|
||||
(*it)->loop();
|
||||
}
|
||||
this->runAsChild();
|
||||
|
||||
if (this->onlyChilds)
|
||||
return;
|
||||
|
||||
if (this->loopDelay && millis() - this->lastMillis < this->loopDelay)
|
||||
return;
|
||||
|
||||
this->lastMillis = millis();
|
||||
|
||||
this->beforeRun();
|
||||
this->run();
|
||||
this->afterRun();
|
||||
|
||||
if (this->timeUpdateAfter)
|
||||
this->lastMillis = millis();
|
||||
}
|
||||
|
||||
void Component::addChildComponent(Component* child) {
|
||||
this->childComponents.push_back(child);
|
||||
}
|
||||
|
||||
void Component::removeChildComponent(Component* child) {
|
||||
this->childComponents.remove(child);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* @file component.h
|
||||
* @author Alexander Klein (alex@kleiax.de)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-08-16
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <list>
|
||||
|
||||
class Component {
|
||||
public:
|
||||
Component() {}
|
||||
Component(uint16_t loopDelay);
|
||||
|
||||
void loop();
|
||||
|
||||
void deactivate() { this->active = false; }
|
||||
void activate() { this->active = false; }
|
||||
|
||||
protected:
|
||||
virtual void runAsChild() {}
|
||||
virtual void beforeRun() {}
|
||||
virtual void run() = 0;
|
||||
virtual void afterRun() {}
|
||||
|
||||
void addChildComponent(Component* child);
|
||||
void removeChildComponent(Component* child);
|
||||
|
||||
void activateOnlyChilds() { this->onlyChilds = true; }
|
||||
void deactivateOnlyChilds() { this->onlyChilds = false; }
|
||||
|
||||
void setTimerAfterTask() { this->timeUpdateAfter = true; }
|
||||
|
||||
uint16_t loopDelay = 0;
|
||||
|
||||
private:
|
||||
std::list<Component*> childComponents;
|
||||
|
||||
bool active = true;
|
||||
bool onlyChilds = false;
|
||||
bool timeUpdateAfter = false;
|
||||
uint32_t lastMillis = 0;
|
||||
};
|
||||
@@ -0,0 +1,156 @@
|
||||
/**
|
||||
* @file network.cpp
|
||||
* @author Alexander Klein (alex@kleiax.de)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-09-18
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#include "network.h"
|
||||
|
||||
Network::Network(const char *ssid, const char *passphrase)
|
||||
{
|
||||
if (!WiFi.mode(WIFI_STA))
|
||||
std::cout << "Network::connectWiFi failed WiFi.mode" << std::endl;
|
||||
|
||||
this->init(ssid, passphrase);
|
||||
}
|
||||
|
||||
Network::Network(const char *ssid, const char *passphrase, NetworkAddresses addresses)
|
||||
{
|
||||
this->addresses = addresses;
|
||||
|
||||
if (!WiFi.mode(WIFI_STA))
|
||||
std::cout << "Network::connectWiFi failed WiFi.mode" << std::endl;
|
||||
|
||||
if (!WiFi.config(this->addresses.localIP,
|
||||
this->addresses.gateway,
|
||||
this->addresses.subnet,
|
||||
this->addresses.dnsServer))
|
||||
{
|
||||
std::cout << "STA Failed to configure" << std::endl;
|
||||
}
|
||||
|
||||
this->init(ssid, passphrase);
|
||||
}
|
||||
|
||||
Network::~Network()
|
||||
{
|
||||
if (this->mqttClient)
|
||||
delete mqttClient;
|
||||
}
|
||||
|
||||
bool Network::activateMqtt(const char *user, const char *passphrase)
|
||||
{
|
||||
this->mqttUser = user;
|
||||
this->mqttPassphrase = passphrase;
|
||||
|
||||
this->mqttClient = new PubSubClient(this->wifiClient);
|
||||
this->mqttClient->setServer(this->addresses.mqttServer, this->addresses.mqttPort);
|
||||
this->mqttClient->setSocketTimeout(1);
|
||||
|
||||
if (this->wifiConnected)
|
||||
return this->connectMqtt();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Network::activateMqtt(const char *user, const char *passphrase, IPAddress server, uint16_t port)
|
||||
{
|
||||
this->addresses.mqttServer = server;
|
||||
this->addresses.mqttPort = port;
|
||||
return this->activateMqtt(user, passphrase);
|
||||
}
|
||||
|
||||
void Network::printIPs()
|
||||
{
|
||||
std::cout << std::endl;
|
||||
if (!this->wifiConnected)
|
||||
{
|
||||
std::cout << "WiFi is not connected." << std::endl;
|
||||
return;
|
||||
}
|
||||
std::cout << "WiFi is connected to" << std::endl;
|
||||
std::cout << "IP address: " << std::endl;
|
||||
std::cout << WiFi.localIP().toString().c_str() << std::endl;
|
||||
std::cout << "WiFi MAC Address: " << WiFi.macAddress().c_str() << std::endl
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
void Network::runAsChild()
|
||||
{
|
||||
if (!this->initSuccessful)
|
||||
return;
|
||||
|
||||
this->checkWifi();
|
||||
|
||||
if (this->wifiConnected && this->mqttClient)
|
||||
this->checkMqtt();
|
||||
}
|
||||
|
||||
void Network::init(const char *ssid, const char *passphrase)
|
||||
{
|
||||
// Connect to Wi-Fi network with SSID and password
|
||||
std::cout << "Connecting to " << ssid << std::endl;
|
||||
WiFi.begin(ssid, passphrase);
|
||||
uint8_t timeout = Network::wifiConnectTimeout;
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
delay(Network::wifiConnectLoopTime);
|
||||
std::cout << "." << std::flush;
|
||||
timeout--;
|
||||
if (timeout == 0)
|
||||
{
|
||||
std::cout << std::endl;
|
||||
std::cout << "WiFi NOT connected." << std::endl;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this->wifiConnected = true;
|
||||
this->printIPs();
|
||||
}
|
||||
|
||||
void Network::checkWifi()
|
||||
{
|
||||
if ((WiFi.status() != WL_CONNECTED) && (millis() - this->lastWifiReconnectAttempt >= Network::wifiReconnectDelay))
|
||||
{
|
||||
std::cout << "Reconnecting to WiFi..." << std::endl;
|
||||
WiFi.disconnect();
|
||||
this->wifiConnected = WiFi.reconnect();
|
||||
this->lastWifiReconnectAttempt = millis();
|
||||
}
|
||||
}
|
||||
|
||||
void Network::checkMqtt()
|
||||
{
|
||||
if (!this->mqttClient->connected() && millis() - this->lastMqttReconnectAttempt > Network::mqttReconnectDelay)
|
||||
{
|
||||
this->mqttConnected = this->connectMqtt();
|
||||
this->lastMqttReconnectAttempt = millis();
|
||||
}
|
||||
|
||||
if (this->mqttConnected)
|
||||
this->mqttClient->loop();
|
||||
}
|
||||
|
||||
bool Network::connectMqtt()
|
||||
{
|
||||
String clientId = "ESP32CrazyHead-";
|
||||
clientId += String(random(0xffff), HEX);
|
||||
|
||||
if (this->mqttUser)
|
||||
{
|
||||
if (this->mqttClient->connect(clientId.c_str(), this->mqttUser, this->mqttPassphrase))
|
||||
this->mqttClient->publish("CrazyHead/Info", "Connected to Mqtt-Broker");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (this->mqttClient->connect(clientId.c_str()))
|
||||
this->mqttClient->publish("CrazyHead/Info", "Connected to Mqtt-Broker");
|
||||
}
|
||||
|
||||
return this->mqttClient->connected();
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* @file network.h
|
||||
* @author Alexander Klein (alex@kleiax.de)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-09-18
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef NETWORK_H
|
||||
#define NETWORK_H
|
||||
|
||||
#include "component.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
|
||||
struct NetworkAddresses {
|
||||
IPAddress localIP;
|
||||
IPAddress gateway;
|
||||
IPAddress subnet;
|
||||
IPAddress dnsServer;
|
||||
IPAddress mqttServer;
|
||||
uint16_t mqttPort = 1883;
|
||||
};
|
||||
|
||||
class Network : public Component {
|
||||
public:
|
||||
Network(const char *ssid, const char *passphrase);
|
||||
Network(const char *ssid, const char *passphrase, NetworkAddresses addresses);
|
||||
|
||||
~Network();
|
||||
|
||||
bool activateMqtt(const char *user = nullptr, const char *passphrase = nullptr);
|
||||
bool activateMqtt(const char *user, const char *passphrase, IPAddress server, uint16_t port);
|
||||
|
||||
void printIPs();
|
||||
|
||||
bool isWifiConnected() const { return this->wifiConnected; }
|
||||
bool isMqttConnected() const { return this->mqttConnected; }
|
||||
PubSubClient* getMqttClient() const { return this->mqttClient; }
|
||||
|
||||
private:
|
||||
void run() override {};
|
||||
void runAsChild() override;
|
||||
void init(const char *ssid, const char *passphrase);
|
||||
void checkWifi();
|
||||
void checkMqtt();
|
||||
bool connectMqtt();
|
||||
|
||||
NetworkAddresses addresses;
|
||||
WiFiClient wifiClient;
|
||||
PubSubClient* mqttClient = nullptr;
|
||||
|
||||
bool initSuccessful = false;
|
||||
bool wifiConnected = false;
|
||||
bool mqttConnected = false;
|
||||
|
||||
const char *mqttUser;
|
||||
const char *mqttPassphrase;
|
||||
|
||||
uint32_t lastWifiReconnectAttempt = 0;
|
||||
uint32_t lastMqttReconnectAttempt = 0;
|
||||
|
||||
static constexpr uint8_t wifiConnectTimeout = 20;
|
||||
static constexpr uint16_t wifiConnectLoopTime = 500;
|
||||
static constexpr uint16_t wifiReconnectDelay = 5000;
|
||||
static constexpr uint16_t mqttReconnectDelay = 2500;
|
||||
|
||||
};
|
||||
|
||||
#endif //NETWORK_H
|
||||
+46
@@ -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
|
||||
@@ -0,0 +1,19 @@
|
||||
; 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:esp32doit-devkit-v1]
|
||||
platform = espressif32
|
||||
board = esp32doit-devkit-v1
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
knolleary/PubSubClient@^2.8
|
||||
dlloydev/ESP32 ESP32S2 AnalogWrite@^5.0.2
|
||||
monitor_speed = 115200
|
||||
upload_port = 192.168.101.147
|
||||
@@ -0,0 +1,90 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <WiFiUdp.h>
|
||||
#include <ESPmDNS.h>
|
||||
#include <ArduinoOTA.h>
|
||||
#include <Servo.h>
|
||||
|
||||
#include "network.h"
|
||||
#include "networkConfig.h"
|
||||
|
||||
class SaySomething : public Component
|
||||
{
|
||||
public:
|
||||
SaySomething() { this->loopDelay = 2000; }
|
||||
|
||||
private:
|
||||
void run() override { std::cout << "Hello from OTA 2" << std::endl; }
|
||||
};
|
||||
|
||||
Network *network;
|
||||
SaySomething say;
|
||||
Servo servo = Servo();
|
||||
|
||||
const int nickPin = 18;
|
||||
const int gierPin = 5;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
network = new Network(NetworkConfig::ssid, NetworkConfig::password);
|
||||
IPAddress server;
|
||||
server.fromString(MqttConfig::server);
|
||||
network->activateMqtt(MqttConfig::user, MqttConfig::password, server, MqttConfig::port);
|
||||
|
||||
if (MDNS.begin("ESP32CrazyHead"))
|
||||
{
|
||||
std::cout << "MDNS responder started" << std::endl;
|
||||
}
|
||||
|
||||
ArduinoOTA
|
||||
.onStart([]()
|
||||
{
|
||||
String type;
|
||||
if (ArduinoOTA.getCommand() == U_FLASH)
|
||||
type = "sketch";
|
||||
else // U_SPIFFS
|
||||
type = "filesystem";
|
||||
|
||||
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
|
||||
Serial.println("Start updating " + type); })
|
||||
.onEnd([]()
|
||||
{ Serial.println("\nEnd"); })
|
||||
.onProgress([](unsigned int progress, unsigned int total)
|
||||
{ Serial.printf("Progress: %u%%\r", (progress / (total / 100))); })
|
||||
.onError([](ota_error_t error)
|
||||
{
|
||||
Serial.printf("Error[%u]: ", error);
|
||||
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
|
||||
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
|
||||
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
|
||||
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
|
||||
else if (error == OTA_END_ERROR) Serial.println("End Failed"); });
|
||||
|
||||
ArduinoOTA.begin();
|
||||
|
||||
servo.write(nickPin, 90);
|
||||
servo.write(gierPin, 90);
|
||||
pinMode(15, INPUT_PULLUP);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
static bool runs = false;
|
||||
|
||||
|
||||
|
||||
network->loop();
|
||||
ArduinoOTA.handle();
|
||||
|
||||
if (digitalRead(15) && !runs) {
|
||||
servo.write(gierPin, 10, 10, 0.8);
|
||||
runs = true;
|
||||
} else if (runs) {
|
||||
servo.write(gierPin, 90, 10, 0.8);
|
||||
runs = false;
|
||||
}
|
||||
|
||||
say.loop();
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
|
||||
This directory is intended for PlatformIO Test Runner 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/en/latest/advanced/unit-testing/index.html
|
||||
Reference in New Issue
Block a user