瀏覽代碼

Setting up project

Thomas Chef 2 周之前
父節點
當前提交
13f8b8fbd4
共有 13 個文件被更改,包括 234 次插入186 次删除
  1. 2 2
      CMakeLists.txt
  2. 1 1
      Makefile
  3. 7 3
      main/CMakeLists.txt
  4. 44 1
      main/Kconfig.projbuild
  5. 2 4
      main/config.h
  6. 0 59
      main/fridgeDoor.c
  7. 0 9
      main/fridgeDoor.h
  8. 29 6
      main/main.c
  9. 2 3
      main/mqtt.c
  10. 138 0
      main/readTemps.c
  11. 9 0
      main/readTemps.h
  12. 0 84
      main/sound.c
  13. 0 14
      main/sound.h

+ 2 - 2
CMakeLists.txt

@@ -1,8 +1,8 @@
 # The following five lines of boilerplate have to be in your project's
 # The following five lines of boilerplate have to be in your project's
 # CMakeLists in this exact order for cmake to work correctly
 # CMakeLists in this exact order for cmake to work correctly
-cmake_minimum_required(VERSION 3.5)
+cmake_minimum_required(VERSION 3.8.0...4.1.0)
 
 
 #set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/led_strip)
 #set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/led_strip)
 
 
 include($ENV{IDF_PATH}/tools/cmake/project.cmake)
 include($ENV{IDF_PATH}/tools/cmake/project.cmake)
-project(fridgeSensor)
+project(andreasPoolTempSensor)

+ 1 - 1
Makefile

@@ -3,7 +3,7 @@
 # project subdirectory.
 # project subdirectory.
 #
 #
 
 
-PROJECT_NAME := fridgeSensor
+PROJECT_NAME := andreasPoolTempSensor
 
 
 #EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/led_strip
 #EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/led_strip
 
 

+ 7 - 3
main/CMakeLists.txt

@@ -1,4 +1,8 @@
-idf_component_register(SRCS "mqtt.c" "fridgeDoor.c" "sound.c" "main.c" "wifi.c"
-                    INCLUDE_DIRS "."
-                    )
+#idf_component_register(SRCS "mqtt.c" "fridgeDoor.c" "sound.c" "main.c" "wifi.c"
+#                    INCLUDE_DIRS "."
+#                    )
 
 
+set(COMPONENT_SRCDIRS ".")
+set(COMPONENT_ADD_INCLUDEDIRS ".")
+
+register_component()

+ 44 - 1
main/Kconfig.projbuild

@@ -1,4 +1,4 @@
-menu "Fridge Sensor Application Configuration"
+menu "Andreas Pool Temp Sensor Configuration"
 
 
     config ESP_WIFI_SSID
     config ESP_WIFI_SSID
         string "WiFi SSID"
         string "WiFi SSID"
@@ -48,5 +48,48 @@ menu "Fridge Sensor Application Configuration"
             GPIO number (IOxx) to blink on and off or the RMT signal for the addressable LED.
             GPIO number (IOxx) to blink on and off or the RMT signal for the addressable LED.
             Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to blink.
             Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used to blink.
 
 
+        config ONE_WIRE_GPIO
+            int "OneWire GPIO number"
+            range 0 33
+            default 4
+            help
+                GPIO number (IOxx) to access One Wire Bus.
+
+                Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used.
+
+                GPIOs 34-39 are input-only so cannot be used to drive the One Wire Bus.
+
+        config ENABLE_STRONG_PULLUP_GPIO
+            bool "Enable strong pull-up controlled by GPIO (MOSFET)"
+            default n
+            help
+                An external circuit can be used to provide a strong pull-up to the One Wire Bus.
+                This is useful when the bus has parasitic-powered devices and extra current is
+                required to power them, such as during temperature ADC conversions.
+
+                An example of such a circuit for the ESP32 is a P-channel MOSFET (such as the BS250)
+                connected Source-to-Drain between a current-limiting resistor (e.g. 270ohm for 12mA
+                max at 3.3V), itself connected to VCC, and the One Wire Bus data line. The Gate is
+                connected to the GPIO specified here.
+
+        config STRONG_PULLUP_GPIO
+            int "Strong pull-up GPIO number"
+            range 0 33
+            default 5
+            help
+                GPIO number (IOxx) to control the strong pull-up on the One Wire Bus, perhaps
+                via a P-channel MOSFET between VCC and the One Wire Bus data line.
+
+                This GPIO will be set as an output and driven high during temperature conversion.
+                This would enable the MOSFET providing current to the devices.
+
+                At all other times it will be driven low, switching off the MOSFET and allowing
+                the One Wire Bus to operate normally.
+
+                Some GPIOs are used for other purposes (flash connections, etc.) and cannot be used.
+
+                GPIOs 34-39 are input-only so cannot be used to drive the One Wire Bus.
+            depends on ENABLE_STRONG_PULLUP_GPIO
+
 
 
 endmenu
 endmenu

+ 2 - 4
main/config.h

@@ -6,14 +6,12 @@
 
 
 #define SW_VERSION "1.0"
 #define SW_VERSION "1.0"
 
 
-#define PHOTO_SENSOR_PIN GPIO_NUM_18
 #define BLINK_GPIO CONFIG_BLINK_GPIO
 #define BLINK_GPIO CONFIG_BLINK_GPIO
 
 
-#define SOUND_ENABLED
-#define DOOR_SENSOR_ENABLED
 
 
+#define ENABLE_DS18B20
 
 
-#define WIFI_ENABLED
+//#define WIFI_ENABLED
 //#define MQTT_ENABLED
 //#define MQTT_ENABLED
 
 
 
 

+ 0 - 59
main/fridgeDoor.c

@@ -1,59 +0,0 @@
-#include <stdio.h>
-#include "freertos/FreeRTOS.h"
-#include "freertos/task.h"
-#include "driver/gpio.h"
-#include "esp_log.h"
-#include <stdbool.h>
-#include "sdkconfig.h"
-#include "fridgeDoor.h"
-#include "config.h"
-#include "sound.h"
-#include "mqtt.h"
-
-#ifdef DOOR_SENSOR_ENABLED
-
-static void read_photo_sensor_task(void *pvParameters) {
-    static bool prevState = false;
-    while (1) {
-        bool fridgeDoorClosed = gpio_get_level(PHOTO_SENSOR_PIN) == 0 ? false : true;
-        if (fridgeDoorClosed != prevState) {
-            prevState = fridgeDoorClosed;
-            ESP_EARLY_LOGI("PHOTO", "Triggered! State: %s", fridgeDoorClosed == true ? "CLOSED" : "OPEN");
-            if( fridgeDoorClosed == false ) {
-                gpio_set_level(BLINK_GPIO, 1);
-            }
-            else {
-                gpio_set_level(BLINK_GPIO, 0);
-            }
-
-            char mqtt_s[50];
-            char value_s[10];
-
-            sprintf(mqtt_s,"kitchen/fridge/doorState");
-            sprintf(value_s,"%s",fridgeDoorClosed ? "closed" : "open");
-            sendMQTTMessage(mqtt_s, value_s);
-        }
-
-        vTaskDelay(pdMS_TO_TICKS(50)); // Delay 50ms (20 times per second)
-
-    }
-}
-
-void setup_photo_sensor() {
-    gpio_config_t io_conf = {
-        .intr_type = GPIO_INTR_ANYEDGE,  // Trigger on both rising & falling edges
-        .pin_bit_mask = (1ULL << PHOTO_SENSOR_PIN),
-        .mode = GPIO_MODE_INPUT,
-        .pull_up_en = GPIO_PULLUP_DISABLE,  
-        .pull_down_en = GPIO_PULLDOWN_DISABLE
-    };
-    gpio_config(&io_conf);
-
-    xTaskCreate(read_photo_sensor_task, "read_sensor_task", 2048, NULL, 10, NULL);
-
-    ESP_LOGI("PHOTO", "Photomicrosensor configured on GPIO %d", PHOTO_SENSOR_PIN);
-}
-
-#else
-void setup_photo_sensor() {};
-#endif

+ 0 - 9
main/fridgeDoor.h

@@ -1,9 +0,0 @@
-#ifndef __FRIDGE_H__
-#define __FRIDGE_H__
-
-#include <stdbool.h>
-
-
-void setup_photo_sensor();
-
-#endif

+ 29 - 6
main/main.c

@@ -5,11 +5,12 @@
 #include "esp_log.h"
 #include "esp_log.h"
 #include <stdbool.h>
 #include <stdbool.h>
 #include "sdkconfig.h"
 #include "sdkconfig.h"
-#include "sound.h"
 #include "config.h"
 #include "config.h"
-#include "fridgeDoor.h"
 #include "wifi.h"
 #include "wifi.h"
 #include "mqtt.h"
 #include "mqtt.h"
+#include "readTemps.h"
+
+esp_chip_info_t chip_info;
 
 
 static void configure_led(void)
 static void configure_led(void)
 {
 {
@@ -19,17 +20,39 @@ static void configure_led(void)
     gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
     gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
 }
 }
 
 
+void getChipInfo(void) {
+    esp_chip_info_t chip_info;
+    esp_chip_info(&chip_info);
+
+    ESP_LOGI("MAIN", "Model: %d = %s", chip_info.model,chip_info.model == CHIP_ESP32 ? "ESP32" : "Unknown");
+    ESP_LOGI("MAIN", "Features: WiFi%s%s, %d cores",
+             (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
+             (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "",
+             chip_info.cores);
+    ESP_LOGI("MAIN", "Revision: %d", chip_info.revision);
+}
+
 void app_main(void)
 void app_main(void)
 {
 {
 
 
+    getChipInfo();
+
+#ifdef WIFI_ENABLED
     initWifi();             // Init WIFI
     initWifi();             // Init WIFI
-    //mqtt_init();
+#endif
+
+#ifdef MQTT_ENABLED
+    mqtt_init();
+#endif
+
+#ifdef ENABLE_DS18B20
+    initTempReadings();
+#endif
+
 
 
     /* Configure the peripheral according to the LED type */
     /* Configure the peripheral according to the LED type */
     configure_led();
     configure_led();
-    //setup_photo_sensor();
-    //initSound();
-
+ 
     while(1) {
     while(1) {
         gpio_set_level(BLINK_GPIO, 1); // Turn the LED on (1 is high)
         gpio_set_level(BLINK_GPIO, 1); // Turn the LED on (1 is high)
         vTaskDelay(500 / portTICK_PERIOD_MS);
         vTaskDelay(500 / portTICK_PERIOD_MS);

+ 2 - 3
main/mqtt.c

@@ -29,7 +29,6 @@
 
 
 #include "esp_log.h"
 #include "esp_log.h"
 #include "mqtt_client.h"
 #include "mqtt_client.h"
-#include "sound.h"
 
 
 #include "wifi.h"
 #include "wifi.h"
 
 
@@ -70,7 +69,7 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
             ESP_LOGI(TAG, "MQTT_EVENT_DATA");
             ESP_LOGI(TAG, "MQTT_EVENT_DATA");
             printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
             printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
             printf("DATA=%.*s\r\n", event->data_len, event->data);
             printf("DATA=%.*s\r\n", event->data_len, event->data);
-            if (strncmp(event->topic, "kitchen/alarm/set", event->topic_len) == 0) {
+            /*if (strncmp(event->topic, "kitchen/alarm/set", event->topic_len) == 0) {
                 if (strncmp(event->data, "off", event->data_len) == 0) {
                 if (strncmp(event->data, "off", event->data_len) == 0) {
                     ESP_LOGI("MQTT", "Fridge alarm disabled");
                     ESP_LOGI("MQTT", "Fridge alarm disabled");
                     setAlarmState(false);
                     setAlarmState(false);
@@ -89,7 +88,7 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
             }
             }
             else {
             else {
                 ESP_LOGI(TAG, "Unknown topic");
                 ESP_LOGI(TAG, "Unknown topic");
-            }
+            }*/
 
 
             break;
             break;
         case MQTT_EVENT_ERROR:
         case MQTT_EVENT_ERROR:

+ 138 - 0
main/readTemps.c

@@ -0,0 +1,138 @@
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+#include "driver/gpio.h"
+#include "esp32/rom/ets_sys.h"
+#include "esp_log.h"
+#include "config.h"
+#include "readTemps.h"
+#include "mqtt.h"
+
+#include "owb.h"
+#include "owb_rmt.h"
+#include "ds18b20.h"
+
+#ifdef ENABLE_DS18B20
+
+#define GPIO_DS18B20_0       (CONFIG_ONE_WIRE_GPIO)
+#define MAX_DEVICES          (8)
+#define DS18B20_RESOLUTION   (DS18B20_RESOLUTION_12_BIT)
+#define SAMPLE_PERIOD        (1000)   // milliseconds
+
+DS18B20_Info * devices[MAX_DEVICES] = {0};
+int num_devices = 0;
+
+OneWireBus * owb;
+owb_rmt_driver_info rmt_driver_info;
+
+void initTempReadings() {
+
+    ESP_LOGI("TEMPS", "Init temp readings.");
+
+    
+    owb = owb_rmt_initialize(&rmt_driver_info, GPIO_DS18B20_0, RMT_CHANNEL_1, RMT_CHANNEL_0);
+    owb_use_crc(owb, true);  // enable CRC check for ROM code
+
+    // Find all connected devices
+    ESP_LOGI("TEMPS", "Find devices:");
+    OneWireBus_ROMCode device_rom_codes[MAX_DEVICES] = {0};
+    OneWireBus_SearchState search_state = {0};
+    bool found = false;
+    owb_search_first(owb, &search_state, &found);
+
+    while (found)
+    {
+        char rom_code_s[17];
+        owb_string_from_rom_code(search_state.rom_code, rom_code_s, sizeof(rom_code_s));
+        ESP_LOGI("TEMPS", "  %d : %s", num_devices, rom_code_s);
+        device_rom_codes[num_devices] = search_state.rom_code;
+        ++num_devices;
+        owb_search_next(owb, &search_state, &found);
+    }
+    ESP_LOGI("TEMPS", "Found %d device%s", num_devices, num_devices == 1 ? "" : "s");
+
+    // Create DS18B20 devices on the 1-Wire bus
+    for (int i = 0; i < num_devices; ++i)
+    {
+        DS18B20_Info * ds18b20_info = ds18b20_malloc();  // heap allocation
+        devices[i] = ds18b20_info;
+
+        ds18b20_init(ds18b20_info, owb, device_rom_codes[i]); // associate with bus and device
+        ds18b20_use_crc(ds18b20_info, true);           // enable CRC check on all reads
+        ds18b20_set_resolution(ds18b20_info, DS18B20_RESOLUTION);
+    }
+
+    // Start the TEMPS task
+    xTaskCreatePinnedToCore(readAndSendTemps, "TEMPS-Task", 1024*10, NULL, 2, NULL,0);
+}
+
+void readAndSendTemps(void *pvParameters) {
+
+    ESP_LOGI("TEMPS", "Read temperature task. Core:%d",xPortGetCoreID());
+
+    TickType_t vLastWakeTime = xTaskGetTickCount();
+
+    while(true)
+    {
+   
+
+        // Read temperatures more efficiently by starting conversions on all devices at the same time
+        int errors_count[MAX_DEVICES] = {0};
+        if (num_devices > 0)
+        {
+            ds18b20_convert_all(owb);
+
+            // In this application all devices use the same resolution,
+            // so use the first device to determine the delay
+            const float sampleTime = ds18b20_wait_for_conversion(devices[0]);
+
+            vTaskDelay(500 / portTICK_PERIOD_MS);
+
+            // Read the results immediately after conversion otherwise it may fail
+            // (using printf before reading may take too long)
+            float readings[MAX_DEVICES] = { 0 };
+            DS18B20_ERROR errors[MAX_DEVICES] = { 0 };
+
+            for (int i = 0; i < num_devices; ++i)
+            {
+                errors[i] = ds18b20_read_temp(devices[i], &readings[i]);
+            }
+
+            ESP_LOGI("TEMPS","Sample time:%.0fms",sampleTime);
+
+            // Print results in a separate loop, after all have been read
+            for (int i = 0; i < num_devices; ++i)
+            {
+                if (errors[i] != DS18B20_OK)
+                {
+                    ++errors_count[i];
+                }
+                
+                char rom_code_s[25];
+                char mqtt_s[50];
+                char value_s[10];
+                owb_string_from_rom_code(devices[i]->rom_code, rom_code_s, sizeof(rom_code_s));
+
+                sprintf(mqtt_s,"garage/temps/%s", rom_code_s);
+                sprintf(value_s,"%.1f",readings[i]);
+                ESP_LOGI("TEMPS","%s %s", mqtt_s, value_s);
+
+                sendMQTTMessage(mqtt_s, value_s);
+            }
+
+        }
+        else
+        {
+            ESP_LOGE("TEMPS", "No DS18B20 devices detected!");
+        }
+
+        vTaskDelayUntil( &vLastWakeTime, 20000 / portTICK_PERIOD_MS );
+    
+    }
+    vTaskDelete(NULL);
+}
+
+
+
+
+
+#endif

+ 9 - 0
main/readTemps.h

@@ -0,0 +1,9 @@
+#ifndef READ_TEMPS_H
+#define READ_TEMPS_H
+
+
+void initTempReadings();
+void readAndSendTemps(void *pvParameters);
+
+
+#endif

+ 0 - 84
main/sound.c

@@ -1,84 +0,0 @@
-#include "sound.h"
-#include <stdio.h>
-#include "freertos/FreeRTOS.h"
-#include "freertos/task.h"
-//#include "driver/gpio.h"
-#include "esp_log.h"
-#include "sdkconfig.h"
-//#include "driver/dac.h"
-#include "math.h"
-#include "config.h"
-#include "mqtt.h"
-
-#ifdef SOUND_ENABLED
-
-#define BUZZ_PIN          GPIO_NUM_25   // GPIO pin for DAC output GPIO25=Channel 1
-#define BUZZ_SHORT_DURATION     200          // Duration of the buzz in ms
-#define QUIET_DURATION         1000          // Duration of the quiet period in ms
-
-static bool alarmEnabled = false; // Tracks the state of the normal alarm
-
-static void activate_buzzer(int on_time, int off_time, int repetitions) {
-    
-    for (int i = 0; i < repetitions; i++) {
-        gpio_set_level(BUZZ_PIN, 1); // Set GPIO high
-        vTaskDelay(pdMS_TO_TICKS(on_time)); // Wait for the specified on time
-        gpio_set_level(BUZZ_PIN, 0); // Set GPIO low
-        vTaskDelay(pdMS_TO_TICKS(off_time)); // Wait for the specified off time
-    }
-}
-
-static void setupSound() {
-
-    // Configure GPIO25 as output and set it to inactive (low) by default
-    gpio_config_t io_conf = {
-        .pin_bit_mask = (1ULL << BUZZ_PIN),
-        .mode = GPIO_MODE_OUTPUT,
-        .pull_up_en = GPIO_PULLUP_DISABLE,
-        .pull_down_en = GPIO_PULLDOWN_DISABLE,
-        .intr_type = GPIO_INTR_DISABLE
-    };
-    gpio_config(&io_conf);
-
-    // Set GPIO25 to low (inactive)
-    gpio_set_level(BUZZ_PIN, 0);
-
-    ESP_LOGI("SOUND", "Sound setup");
-}
-
-static void sound_task(void *pvParameters) {
-    ESP_LOGI("SOUND", "Sound task starts....");
-
-    setupSound();
-
-    activate_buzzer(50, 0, 1); // Initial short beep to indicate sound system is ready
-
-    while (1) {
-        while( alarmEnabled ) {
-            activate_buzzer(BUZZ_SHORT_DURATION, QUIET_DURATION, 1);
-        }
-        vTaskDelay(pdMS_TO_TICKS(100)); // Check alarm state every second
-    }
-}
-
-void setAlarmState(bool enabled) {
-    alarmEnabled = enabled;
-    //ESP_LOGI("SOUND", "Alarm state set to: %s", enabled ? "ENABLED" : "DISABLED");
-}
-
-void triggerNotificationSound() {
-    ESP_LOGI("SOUND", "Triggering short notification sound");
-    activate_buzzer(100, 0, 1); // 100ms beep, no quiet time, single iteration
-}
-
-void initSound() {
-    xTaskCreate(sound_task, "sound_task", 2048, NULL, 10, NULL);
-    ESP_LOGI("SOUND", "Sound initialized");
-}
-
-#else
-void initSound() {};
-void triggerNotificationSound() {};
-void setAlarmState(bool enabled) {};
-#endif
-

+ 0 - 14
main/sound.h

@@ -1,14 +0,0 @@
-#ifndef __SOUND_H__
-#define __SOUND_H__
-
-#include <stdbool.h>
-
-// Enable or disable the normal alarm
-void setAlarmState(bool enabled);
-
-// Trigger a short notification sound (100ms)
-void triggerNotificationSound();
-
-void initSound();
-
-#endif