add existing files

This commit is contained in:
Philipp Kramer 2025-01-10 16:03:21 +01:00
commit dec05df251
8 changed files with 444 additions and 0 deletions

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

10
.vscode/extensions.json vendored Normal file
View 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"
]
}

39
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

12
include/credentials.h Normal file
View file

@ -0,0 +1,12 @@
/************************* WiFi Access Point *********************************/
#define WLAN_SSID ""
#define WLAN_PASS ""
/************************* MQTT Setup *********************************/
#define MQTT_SERVER ""
#define MQTT_SERVERPORT 1883 // use 8883 for SSL
//#define MQTT_USERNAME ""
//#define MQTT_PASSWORD ""

46
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 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

26
platformio.ini Normal file
View file

@ -0,0 +1,26 @@
; 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:d1_mini]
platform = espressif8266
board = d1_mini
framework = arduino
monitor_speed=115200
upload_speed = 921600
build_flags =
'-D MQTT_BASETOPIC="wifips01"'
lib_deps =
adafruit/Adafruit MQTT Library@^2.5.8
mathertel/OneButton@^2.0.3

295
src/main.cpp Normal file
View file

@ -0,0 +1,295 @@
#include <Arduino.h>
#include "ESP8266WiFi.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <OneButton.h>
#include "credentials.h"
#define PIN_BTN_1 D5 //white
#define PIN_BTN_2 D6 //orange
#define PIN_BTN_3 D7 //black
//const int RSSI_MAX =-50;// define maximum strength of signal in dBm
//const int RSSI_MIN =-100;// define minimum strength of signal in dBm
const int displayEnc=1;// set to 1 to display Encryption or 0 not to display
#define MAX_STATIONS 64
#define BSSID_LEN 6
uint8_t target_mac[BSSID_LEN];
uint8_t stations_found=0;
String scan_SSID[MAX_STATIONS];
int32_t scan_RSSI[MAX_STATIONS];
String scan_BSSID[MAX_STATIONS];
//Buttons
OneButton button1(PIN_BTN_1, true,true);
void click1();
void doubleclick1();
void longPressStart1();
OneButton button2(PIN_BTN_2, true,true);
void click2();
void doubleclick2();
void longPressStart2();
OneButton button3(PIN_BTN_3, true,true);
void click3();
void doubleclick3();
void longPressStart3();
//MQTT
WiFiClient client;
#ifdef MQTT_USERNAME
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);
#else
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT);
#endif
Adafruit_MQTT_Publish statusPub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/status");
Adafruit_MQTT_Publish batteryPub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/battery");
Adafruit_MQTT_Publish scanPub = Adafruit_MQTT_Publish(&mqtt, MQTT_BASETOPIC"/scan");
bool MQTT_connect();
bool wifi_connect();
void scan();
void setup() {
pinMode(LED_BUILTIN,OUTPUT);
pinMode(PIN_BTN_1,OUTPUT);
pinMode(PIN_BTN_2,OUTPUT);
pinMode(PIN_BTN_3,OUTPUT);
pinMode(A0,INPUT);
Serial.begin(115200);
button1.attachClick(click1);
button1.attachDoubleClick(doubleclick1);
button1.attachLongPressStart(longPressStart1);
button2.attachClick(click2);
button2.attachDoubleClick(doubleclick2);
button2.attachLongPressStart(longPressStart2);
button3.attachClick(click3);
button3.attachDoubleClick(doubleclick3);
button3.attachLongPressStart(longPressStart3);
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(2000);
Serial.println("Setup done");
digitalWrite(LED_BUILTIN,LOW);
analogRead(A0);
Serial.print("Battery:"); Serial.println(analogRead(A0));
}
void loop() {
button1.tick();
button2.tick();
button3.tick();
scan();
bool mqtt_send=false;
bool wificonnectresult=wifi_connect();
if (wificonnectresult) {
mqtt_send=true;
}
while(mqtt_send) {
bool mqttconnectresult=MQTT_connect();
if (mqttconnectresult) {
mqtt.processPackets(10);
int bat=analogRead(A0);
batteryPub.publish(bat);
Serial.print("batteryPub:"); Serial.println(bat);
for (uint8_t i=0;i<stations_found;i++) {
Serial.print(scan_BSSID[i]); Serial.print(", ");
Serial.print(scan_SSID[i]); Serial.print(", ");
Serial.print(scan_RSSI[i]);
Serial.println();
String pubstring="";
pubstring.concat(scan_BSSID[i]);
pubstring.concat(";");
pubstring.concat(scan_SSID[i]);
pubstring.concat(";");
pubstring.concat(scan_RSSI[i]);
char pubarray[128];
pubstring.toCharArray(pubarray, pubstring.length() + 1);
scanPub.publish(pubarray);
delay(50);
}
}
mqtt_send=false;
digitalWrite(LED_BUILTIN,HIGH);
mqtt.disconnect();
while(mqtt.connected()) {
Serial.println("Waiting for mqtt to disconnect");
delay(100);
}
Serial.println("Going to deep sleep");
Serial.flush();
delay(10);
ESP.deepSleep(60e6); //0=infinite, 30e6 =30 seconds
}
while(true) {
delay(2000);
Serial.println("Finished");
}
}
bool wifi_connect(){
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
#define TIMEOUT_WIFI_CONNECT 30000
unsigned long wifi_connect_start_time=millis();
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(LED_BUILTIN,!digitalRead(LED_BUILTIN));
delay(500);
Serial.print(".");
if (millis()-wifi_connect_start_time > TIMEOUT_WIFI_CONNECT) { //timeout
return false;
}
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
digitalWrite(LED_BUILTIN,HIGH);
return true;
}
bool MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return true;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
return false;
}
}
Serial.println("MQTT Connected!");
statusPub.publish("online");
return true;
}
void scan() {
Serial.println("Wifi scan started");
// WiFi.scanNetworks will return the number of networks found
Serial.println("Wifi scan ended");
stations_found=WiFi.scanNetworks();
if (stations_found == 0) {
Serial.println("no networks found");
} else {
Serial.print(stations_found);
Serial.println(" networks found");
for (int i = 0; i < stations_found; ++i) {
scan_SSID[i]=WiFi.SSID(i);
scan_RSSI[i]=WiFi.RSSI(i);
uint8_t* bssid = WiFi.BSSID(i);
memcpy(target_mac, bssid, BSSID_LEN);
String stringmac="";
for (int i = 0; i < BSSID_LEN; i++) {
stringmac.concat(String(target_mac[i], HEX));
if (i < BSSID_LEN - 1) {
stringmac.concat(":");
}
}
scan_BSSID[i]=stringmac;
/*if(WiFi.encryptionType(i) == ENC_TYPE_NONE)*/
delay(10);
}
}
Serial.println("");
// Wait a bit before scanning again
WiFi.scanDelete();
}
void click1() {
Serial.println("Button 1 click.");
}
void doubleclick1() {
Serial.println("Button 1 doubleclick.");
}
void longPressStart1() {
Serial.println("Button 1 longPress start");
}
void click2() {
Serial.println("Button 2 click.");
}
void doubleclick2() {
Serial.println("Button 2 doubleclick.");
}
void longPressStart2() {
Serial.println("Button 2 longPress start");
}
void click3() {
Serial.println("Button 3 click.");
}
void doubleclick3() {
Serial.println("Button 3 doubleclick.");
}
void longPressStart3() {
Serial.println("Button 3 longPress start");
}

11
test/README Normal file
View 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