commit 60b5c945b538abe5e9e3707f52de5828861ef347 Author: Luis Rodil-Fernandez Date: Sat Feb 12 04:44:08 2022 +0100 needed before anything happens diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b9c2d11 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch + +src/credentials* +mcukit/* diff --git a/How to press a button digitally with a transistor.png b/How to press a button digitally with a transistor.png new file mode 100644 index 0000000..d1174cb Binary files /dev/null and b/How to press a button digitally with a transistor.png differ 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/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /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 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 +#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/mcukit b/mcukit new file mode 160000 index 0000000..e06e54c --- /dev/null +++ b/mcukit @@ -0,0 +1 @@ +Subproject commit e06e54c09a925a7a0285504f98919ea78ce149f6 diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..588b7cb --- /dev/null +++ b/platformio.ini @@ -0,0 +1,28 @@ +; 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 + +[common] +lib_deps_external = + https://github.com/adafruit/DHT-sensor-library/archive/refs/tags/1.4.3.zip + https://github.com/knolleary/pubsubclient/archive/refs/tags/v2.8.zip + + +[env:lolin32] +platform = espressif32 +board = lolin32 +framework = arduino + +upload_speed = 115200 +monitor_speed = 115200 + +lib_extra_dirs = ./mcukit +lib_deps = ${common.lib_deps_external} + +upload_port = /dev/cu.SLAB_USBtoUART diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..5cad6d6 --- /dev/null +++ b/src/config.h @@ -0,0 +1,8 @@ + +// pin configuration + +#define PIN_DHT 2 +#define PIN_ATOMIZER 4 + +#define MQTT_BROKER "YOUR_MQTT_BROKER_IP_ADDRESS" + diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..c31daef --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,180 @@ +#include +#include +#include +#include +#include +#include +#include // mqtt stuff +#include + +#include +#include + +#include "credentials.h" +#include "config.h" + +DHT dht(2, DHT11); +float h, t, f; +float hif, hic; + + +USquare duty; // function generator for duty cycle +Trigger fogOff( &duty, 1.0, ON_ASCENT ); +Trigger fogOn( &duty, 0.0, ON_DESCENT ); + +WiFiClient espClient; +PubSubClient client(espClient); + +void setup() { + Serial.begin(115200); + LOG_INIT(); + + pinMode(PIN_ATOMIZER, OUTPUT); + + duty.setFrequency(0.05); + duty.setAmplitude(0.5); + duty.setPhase(0); + duty.setYShift(0.5); + + df_header_print(); + + if( wifi_connect_as_client(WIFI_SSID, WIFI_PASSWORD) ) { + wifi_print_mode(); + wifi_print_ip(); + } else { + Serial.print("Failed to connect to wifi "); + Serial.print( WIFI_SSID ); + Serial.println(); + Serial.println("Brace! Brace! trying to continue without wifi..."); + } + + Serial.println("Initializing mqtt data stream..."); + client.setServer(MQTT_BROKER, 1883); + client.setCallback(callback); + + Serial.println("Initializing DHT sensor..."); + dht.begin(); +} + +void dht_report_reading() { + Serial.print(F("Humidity: ")); + Serial.print(h); + Serial.print(F("% Temperature: ")); + Serial.print(t); + Serial.print(F("°C ")); + Serial.print(f); + Serial.print(F("°F Heat index: ")); + Serial.print(hic); + Serial.print(F("°C ")); + Serial.print(hif); + Serial.println(F("°F")); +} + +void callback(char* topic, byte* message, unsigned int length) { + Serial.print("Message arrived on topic: "); + Serial.print(topic); + Serial.print(". Message: "); + String messageTemp; + + for (int i = 0; i < length; i++) { + Serial.print((char)message[i]); + messageTemp += (char)message[i]; + } + Serial.println(); + + // // Feel free to add more if statements to control more GPIOs with MQTT + + // // If a message is received on the topic esp32/output, you check if the message is either "on" or "off". + // // Changes the output state according to the message + // if (String(topic) == "esp32/output") { + // Serial.print("Changing output to "); + // if(messageTemp == "on"){ + // Serial.println("on"); + // digitalWrite(ledPin, HIGH); + // } + // else if(messageTemp == "off"){ + // Serial.println("off"); + // digitalWrite(ledPin, LOW); + // } + // } +} + +void reconnect() { + // Loop until we're reconnected + while (!client.connected()) { + Serial.print("Attempting MQTT connection..."); + // Attempt to connect + if (client.connect("kodama")) { + Serial.println("connected"); + // Subscribe + client.subscribe("esp32/output"); + } else { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" try again in 5 seconds"); + // Wait 5 seconds before retrying + delay(5000); + } + } +} + +// bool dht_read() { //(float &_h, float &_t) { +// bool retval = true; + +// // Reading temperature or humidity takes about 250 milliseconds! +// // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) +// h = dht.readHumidity(); +// // Read temperature as Celsius (the default) +// t = dht.readTemperature(); + +// // Check if any reads failed and exit early (to try again). +// if (isnan(h) || isnan(t) ) { +// retval = false; +// } + +// return retval; +// } + +void atomizer_switch() { + digitalWrite(PIN_ATOMIZER, HIGH); + delay(300); + digitalWrite(PIN_ATOMIZER, LOW); +} + +void loop() { + + // float t = millis() * 0.001; + // float v = duty.square(t); + // Serial.print(t); Serial.print("@ "); Serial.println(v); + + // if ( !dht_read() ) { //(h, t) ) { + // LOG("Failed to read from DHT sensor!"); + // LOG_NEW_LINE + // } + + if( fogOff.bang() ) { + Serial.print(t); Serial.println(" >>>>>>>>>> FOG OFF!"); + atomizer_switch(); + } + + if( fogOn.bang() ) { + Serial.print(t); Serial.println(" <<<<<<<<<< FOG ON!"); + atomizer_switch(); + } + + // if(v >= 1) { + // // Reading temperature or humidity takes about 250 milliseconds! + // // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) + // h = dht.readHumidity(); + // // Read temperature as Celsius (the default) + // t = dht.readTemperature(); + + // // Compute heat index in Celsius (isFahreheit = false) + // hic = dht.computeHeatIndex(t, h, false); + // dht_report_reading(); + + // Serial.print(">> "); Serial.print(v); Serial.println(); + // } + + delay(50); +} diff --git a/test/README b/test/README new file mode 100644 index 0000000..b94d089 --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Unit Testing 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/page/plus/unit-testing.html