commit dec05df25140f35cb2b8f18c1af5cda065cea4ba Author: Philipp Kramer Date: Fri Jan 10 16:03:21 2025 +0100 add existing files diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -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" + ] +} diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -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 diff --git a/include/credentials.h b/include/credentials.h new file mode 100644 index 0000000..fd4c030 --- /dev/null +++ b/include/credentials.h @@ -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 "" diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..2593a33 --- /dev/null +++ b/lib/README @@ -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 +#include + +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 diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..7b35387 --- /dev/null +++ b/platformio.ini @@ -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 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..40afae4 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,295 @@ +#include +#include "ESP8266WiFi.h" + + +#include "Adafruit_MQTT.h" +#include "Adafruit_MQTT_Client.h" + +#include + +#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 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"); +} \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -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