diff --git a/ESP32_Sensors/.vscode/settings.json b/ESP32_Sensors/.vscode/settings.json new file mode 100644 index 00000000..b227edc7 --- /dev/null +++ b/ESP32_Sensors/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "idf.portWin": "COM3", + "idf.flashType": "UART" +} \ No newline at end of file diff --git a/ESP32_Sensors/platformio.ini b/ESP32_Sensors/platformio.ini index c4b493e6..7fe46403 100644 --- a/ESP32_Sensors/platformio.ini +++ b/ESP32_Sensors/platformio.ini @@ -12,3 +12,10 @@ platform = espressif32 board = esp32doit-devkit-v1 framework = arduino +monitor_speed = 115200 +lib_deps = + adafruit/Adafruit BME280 Library@^2.2.2 + adafruit/Adafruit BMP280 Library@^2.6.6 + +[platformio] +description = Temperature and pressure sensors diff --git a/ESP32_Sensors/src/config.h b/ESP32_Sensors/src/config.h new file mode 100644 index 00000000..710ad2f8 --- /dev/null +++ b/ESP32_Sensors/src/config.h @@ -0,0 +1,135 @@ +//#include "secrets.h" + +// Uncomment modules as required +//#define RTC +//#define MICRO_SD +#define BMP280 // Adafruit BMP280; temp & pressure +//#define BME280 // Generic BME280; temp, pressure & humidity +//#define AWSIoT + +#define MQTT_SSL +//#define HOTSPOT +//#define MQTT + + +/* +######################################################################### + + MQTT + +######################################################################### +*/ +// MQTT Default Channels +#define MQTT_IOT_CHANNEL_TEMPERATURE "esp32/sensors/temperature" +#define MQTT_IOT_CHANNEL_PRESSURE "esp32/sensors/pressure" +#define MQTT_IOT_CHANNEL_HUMIDITY "esp32/sensors/humidity" +// MQTT Channel specific for each node +// [NODE]/sensors/sensor +// IoT ID +#define IoT_ID "node1" +#define MQTT_NODE_TEMPERATURE "/sensors/temperature" +#define MQTT_NODE_PRESSURE "/sensors/pressure" +#define MQTT_NODE_HUMIDITY "/sensors/humidity" +#define MQTT_NODE_PULSE "/pulse" +#define MQTT_NODE_SWITCH_1 "/sw1" +#define MQTT_NODE_SWITCH_2 "/sw2" +#define MQTT_NODE_OUTPUT_PWM_1 "/pwm1" +// Standardize switches channels +#define MQTT_IOT_CHANNEL_OUTPUT_PULSE "node1/output/pulse" +#define MQTT_IOT_CHANNEL_OUTPUT_SWITCH_1 "node1/output/sw1" +#define MQTT_IOT_CHANNEL_OUTPUT_SWITCH_2 "node1/sw2" +#define MQTT_IOT_CHANNEL_OUTPUT_PWM_1 "node1/pwm1" + +// Uncomment corresponding board +#define devkit_30pin_001 +//#define devkit_36pin_001 + +#ifdef devkit_30pin_001 +/* +#### ESP32 DEVKIT V1.1 DIY MALL #### +#### 30 PINS #### +#### DEVELOPMENT BOARD SUPPORTED PIN OUTS #### +---------------------------------------------- +GPIO | Physical | Description +Pin | | +---------------------------------------------- + 2 Built-in LED +D15 3 +D2 4 => Built-in LED +D4 5 +D5 8 +D18 9 +D19 10 +D21 11 +D22 14 +D23 15 +D34 19 Input only +D35 20 Input only +D32 21* +D33 22* +D25 23* => Assigned to DAC +D26 24* => Assigned to DAC +D27 25 +D14 26* => Assigned to Switch 1 +D12 27* => Assigned to Switch 2 +D13 28 => Assigned to read RPM +---------------------------------------------- +*/ +#define PING_PIN 33 // GPIO 33 pin # of audio ping +#define LED_PIN 32 +#define SWITCH_1 14 // GPIO 14 +#define SWITCH_2 12 // GPIO 12 +#define DAC_CH1 25 // GPIO 25 +#define DAC_CH2 26 // GPIO 26 +#define FAN_RPM 13 // GPIO 13 +//uint8_t pins[] = {2,4,5,12,13,14,15,18,19,21,22,23,25,26,27,32,33}; // 20 GPIO pins +#endif + +/* +#### ESP32 DEVKIT V1.1 DIY MALL #### +#### 36 PINS #### +#### DEVELOPMENT BOARD SUPPORTED PIN OUTS #### +---------------------------------------------- +GPIO | Physical | Description +Pin | | +---------------------------------------------- + 2 Built-in LED +D0 5 +D15 6 +D2 7 Same as built-in LED +D4 8 +D5 11 +D18 12 +D19 13 +D21 14 +D22 17 +D23 18 +D34 22 +D35 23 +D32 24 +D33 25 +D25 26 +D26 27 +D27 28 +D17 29 +D14 30 +D12 31 +D13 32 +---------------------------------------------- +*/ +#ifdef devkit_36pin_001 +#define PING_PIN 33 // D33 pin # of audio ping +#define LED_PIN 2 // pin # of LED controlled by light sensor +//uint8_t pins[] = {}; +#endif +/* +############################################## +*/ + +//#define LIGHT_SENSOR_PIN 34 // analog in pin # for a light sensor +//#define LED_PIN 32 // pin # of LED controlled by light sensor +#define ANALOG_THRESHOLD 1800 // threshhold for analog input when logical 0 should become logical 1 +// RGB LED +#define RGB_R_PIN 11 // D14 +#define RGB_B_PIN 13 // D13 +#define RGB_G_PIN 12 // D12 \ No newline at end of file diff --git a/ESP32_Sensors/src/main.cpp b/ESP32_Sensors/src/main.cpp index cb9fbba4..a284c10e 100644 --- a/ESP32_Sensors/src/main.cpp +++ b/ESP32_Sensors/src/main.cpp @@ -1,15 +1,73 @@ #include +#include "config.h" + +#ifdef BMP280 +#include +#endif +#ifdef BME280 +#include +#endif + +// BME280 +#ifdef BME280 +// WaveShare BME280 +Adafruit_BME280 bme; +#define SEALEVELPRESSURE_HPA (1013.25) +#endif +// BMP280 +#ifdef BMP280 +#define BMP_SCK (18) +#define BMP_MISO (19) +#define BMP_MOSI (23) +#define BMP_CS (5) +Adafruit_BMP280 bmp(BMP_CS); +#endif // put function declarations here: int myFunction(int, int); -void setup() { - // put your setup code here, to run once: - int result = myFunction(2, 3); +struct { + float humidity = 0.0; + float pressure = 0.0; + float temperature = 0.0; +} sensors_values; + +void setup() +{ + Serial.begin(115200); + Serial.println(); + + // BMP280 + #ifdef BMP280 + unsigned status_bmp280; + status_bmp280 = bmp.begin(); + if (!status_bmp280) { + Serial.println("Could not find BMP280"); + Serial.println(bmp.sensorID(),16); + while (1); + } + else { + Serial.println(bmp.sensorID(),16); + } + #endif + } void loop() { - // put your main code here, to run repeatedly: + #ifdef BMP280 + Serial.println("\n==== BMP-280 ============="); + sensors_values.temperature = (float)bmp.readTemperature(); + sensors_values.pressure = (float)bmp.readPressure(); + #endif + + // Display sensors values + Serial.print("Temperature = "); + Serial.println(sensors_values.temperature); + Serial.print("Pressure = "); + Serial.print(sensors_values.pressure / 100.0F); + Serial.println(" Pa"); + delay(1000); + } // put function definitions here: