Add ESP8266 code
This commit is contained in:
5
ESP8266/.gitignore
vendored
Normal file
5
ESP8266/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
.pio
|
||||
.vscode/.browse.c_cpp.db*
|
||||
.vscode/c_cpp_properties.json
|
||||
.vscode/launch.json
|
||||
.vscode/ipch
|
||||
10
ESP8266/.vscode/extensions.json
vendored
Normal file
10
ESP8266/.vscode/extensions.json
vendored
Normal file
@@ -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"
|
||||
]
|
||||
}
|
||||
4
ESP8266/.vscode/settings.json
vendored
Normal file
4
ESP8266/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"C_Cpp.clang_format_fallbackStyle": "Visual Studio",
|
||||
"C_Cpp.clang_format_style": "{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4}"
|
||||
}
|
||||
1
ESP8266/include/.gitignore
vendored
Normal file
1
ESP8266/include/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
credentials.hpp
|
||||
39
ESP8266/include/README
Normal file
39
ESP8266/include/README
Normal 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
|
||||
46
ESP8266/lib/README
Normal file
46
ESP8266/lib/README
Normal 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
|
||||
17
ESP8266/platformio.ini
Normal file
17
ESP8266/platformio.ini
Normal file
@@ -0,0 +1,17 @@
|
||||
; 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:esp12e]
|
||||
platform = espressif8266
|
||||
board = esp12e
|
||||
framework = arduino
|
||||
lib_deps = knolleary/PubSubClient@^2.8
|
||||
monitor_speed = 921600
|
||||
upload_speed = 921600
|
||||
181
ESP8266/src/main.cpp
Normal file
181
ESP8266/src/main.cpp
Normal file
@@ -0,0 +1,181 @@
|
||||
#include <ENC28J60lwIP.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
#include <credentials.hpp>
|
||||
|
||||
/* C:\Users\Gabriel\.platformio\packages\framework-arduinoespressif8266\libraries\lwIP_enc28j60\src\utility\enc28j60.cpp
|
||||
* Set DEBUG 0 or 1 for enc28j60 debug printf
|
||||
*/
|
||||
|
||||
ENC28J60lwIP ethernet(16);
|
||||
WiFiClientSecure espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
void mqttCallback(char *topic, byte *payload, unsigned int length) {
|
||||
uint8_t message[256];
|
||||
memcpy(message, payload, length);
|
||||
Serial.printf("Message arrived: topic=%s, length=%d , message=%.*s\n",
|
||||
topic, length, length, message);
|
||||
if (!strcmp(topic, ROOT_TOPIC "/ledBuiltin/set")) {
|
||||
if (length > 1) {
|
||||
Serial.printf("Payload length = %d > 1", length);
|
||||
return;
|
||||
}
|
||||
if (message[0] == '0') {
|
||||
// builtin led is active low
|
||||
digitalWrite(LED_BUILTIN, 1);
|
||||
} else if (message[0] == '1') {
|
||||
digitalWrite(LED_BUILTIN, 0);
|
||||
} else {
|
||||
Serial.printf("Undefined payload value\n");
|
||||
}
|
||||
} else {
|
||||
// If topic is not recognized, exit without publishing to /get
|
||||
Serial.printf("Unrecognized topic %s\n", topic);
|
||||
return;
|
||||
}
|
||||
// Publish payload to /get if topic ends with /set
|
||||
if (strlen(topic) > 4) {
|
||||
if (!strcmp(topic + (strlen(topic) - 4), "/set")) {
|
||||
char gettopic[256];
|
||||
strcpy(gettopic, topic);
|
||||
gettopic[strlen(topic) - 3] = 'g';
|
||||
/* This function changes original callback payload parameter,
|
||||
* because PubSubClient uses same buffer for send and receive */
|
||||
client.publish(gettopic, message, length, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setClock() {
|
||||
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
|
||||
Serial.print("Waiting for NTP time sync: ");
|
||||
time_t now = time(nullptr);
|
||||
while (now < 8 * 3600 * 2) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
now = time(nullptr);
|
||||
}
|
||||
Serial.println("");
|
||||
struct tm timeinfo;
|
||||
gmtime_r(&now, &timeinfo);
|
||||
Serial.print("Current time: ");
|
||||
Serial.print(asctime(&timeinfo));
|
||||
}
|
||||
|
||||
void setClockOffline() {
|
||||
time_t epoch_t = 1665619602;
|
||||
timeval tv = {epoch_t, 0};
|
||||
settimeofday(&tv, nullptr);
|
||||
time_t now = time(nullptr);
|
||||
struct tm timeinfo;
|
||||
gmtime_r(&now, &timeinfo);
|
||||
Serial.print("Current time: ");
|
||||
Serial.print(asctime(&timeinfo));
|
||||
}
|
||||
|
||||
void connectWiFi() {
|
||||
WiFi.begin(ssid, password);
|
||||
Serial.print("Connecting to WiFi..");
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.println(".");
|
||||
delay(500);
|
||||
}
|
||||
Serial.println();
|
||||
Serial.println("Connected to WiFi");
|
||||
Serial.print("WiFi SSID: ");
|
||||
Serial.print(WiFi.SSID());
|
||||
Serial.print(" RSSI: ");
|
||||
Serial.println(WiFi.RSSI());
|
||||
Serial.print("WiFi IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
Serial.print("WiFi subnet mask: ");
|
||||
Serial.println(WiFi.subnetMask());
|
||||
Serial.print("WiFi default gateway: ");
|
||||
Serial.println(WiFi.gatewayIP());
|
||||
Serial.print("WiFi DNS server: ");
|
||||
Serial.println(WiFi.dnsIP());
|
||||
}
|
||||
|
||||
void connectEthernet() {
|
||||
SPI.begin();
|
||||
SPI.setBitOrder(MSBFIRST);
|
||||
SPI.setDataMode(SPI_MODE0);
|
||||
SPI.setFrequency(4000000);
|
||||
uint8_t macaddr[6];
|
||||
int present = ethernet.begin(WiFi.macAddress(macaddr));
|
||||
if (!present) {
|
||||
Serial.println("FATAL: No ethernet hardware present");
|
||||
while (1) {
|
||||
}
|
||||
}
|
||||
Serial.print("Connecting to Ethernet");
|
||||
while (!ethernet.connected()) {
|
||||
Serial.print(".");
|
||||
delay(500);
|
||||
}
|
||||
Serial.println();
|
||||
Serial.println("Connected to Ethernet");
|
||||
Serial.print("Ethernet IP address: ");
|
||||
Serial.println(ethernet.localIP());
|
||||
Serial.print("Ethernet subnet mask: ");
|
||||
Serial.println(ethernet.subnetMask());
|
||||
Serial.print("Ethernet default gateway: ");
|
||||
Serial.println(ethernet.gatewayIP());
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
Serial.begin(921600);
|
||||
Serial.println();
|
||||
BearSSL::X509List *serverTrustedCA =
|
||||
new BearSSL::X509List(ca_cert);
|
||||
BearSSL::X509List *serverCertList =
|
||||
new BearSSL::X509List(client_cert);
|
||||
BearSSL::PrivateKey *serverPrivKey =
|
||||
new BearSSL::PrivateKey(client_private_key);
|
||||
espClient.setTrustAnchors(serverTrustedCA);
|
||||
espClient.setClientRSACert(serverCertList, serverPrivKey);
|
||||
ethernet.setDefault();
|
||||
connectEthernet();
|
||||
// setClockOffline();
|
||||
// connectWiFi();
|
||||
setClock();
|
||||
/* Por um bug na biblioteca ENC28J60lwIP ou na WifiClientSecure, o DNS não
|
||||
* funciona quando ambas são usadas em conjunto. Assim, é necessário colocar
|
||||
* o DNS em cache usando o WiFiClient antes de finalmente conectar ao
|
||||
* servidor devidamente usando o WiFiClientSecure.
|
||||
*/
|
||||
WiFiClient wifi_test;
|
||||
wifi_test.connect(mqttBroker, mqttPort);
|
||||
wifi_test.stop();
|
||||
espClient.setInsecure();
|
||||
client.setServer(mqttBroker, mqttPort);
|
||||
client.setCallback(mqttCallback);
|
||||
char err_buf[256];
|
||||
while (!client.connected()) {
|
||||
String client_id = "esp8266-client-";
|
||||
client_id += String(WiFi.macAddress());
|
||||
Serial.printf("%s Connecting to MQTT broker %s\n", client_id.c_str(), mqttBroker);
|
||||
if (client.connect(client_id.c_str())) {
|
||||
Serial.printf("Connected to MQTT broker %s\n", mqttBroker);
|
||||
} else {
|
||||
Serial.print("Connection failed with state ");
|
||||
Serial.println(client.state());
|
||||
espClient.getLastSSLError(err_buf, sizeof(err_buf));
|
||||
Serial.print("SSL error: ");
|
||||
Serial.println(err_buf);
|
||||
delay(2000);
|
||||
}
|
||||
}
|
||||
// TODO: Support set topic with more than one level
|
||||
char settopic[64];
|
||||
strcpy(settopic, ROOT_TOPIC);
|
||||
strcat(settopic, "/+/set");
|
||||
client.subscribe(settopic);
|
||||
Serial.print("Subscribed to topic: ");
|
||||
Serial.println(settopic);
|
||||
}
|
||||
|
||||
void loop() { client.loop(); }
|
||||
11
ESP8266/test/README
Normal file
11
ESP8266/test/README
Normal file
@@ -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