1
0
Prechádzať zdrojové kódy

Added WiFi and config

Thomas Chef 3 rokov pred
rodič
commit
09a6c05e8e
8 zmenil súbory, kde vykonal 238 pridanie a 2 odobranie
  1. 1 1
      main/CMakeLists.txt
  2. 22 0
      main/Kconfig.projbuild
  3. 4 0
      main/config.h
  4. 2 0
      main/led_test_inout.c
  5. 4 0
      main/main.c
  6. 10 1
      main/pcnt_functions.c
  7. 182 0
      main/wifi.c
  8. 13 0
      main/wifi.h

+ 1 - 1
main/CMakeLists.txt

@@ -1,2 +1,2 @@
-idf_component_register(SRCS "pcnt_functions.c" "main.c" "led_test_inout.c" "pcnt_functions.c"
+idf_component_register(SRCS "wifi.c" "pcnt_functions.c" "main.c" "led_test_inout.c" "pcnt_functions.c" "wifi.c"
                     INCLUDE_DIRS "")

+ 22 - 0
main/Kconfig.projbuild

@@ -0,0 +1,22 @@
+menu "HomeEnergyMeter Application Configuration"
+
+    config ESP_WIFI_SSID
+        string "WiFi SSID"
+        default "Hemnet3"
+        help
+            SSID (network name) to connect to.
+
+    config ESP_WIFI_PASSWORD
+        string "WiFi Password"
+        default "mypassword"
+        help
+            WiFi password (WPA or WPA2) to use.
+
+    config ESP_PULSES_PER_KWH
+        string "Pulses per kWh"
+        default "10000"
+        help
+            Number of pulses per kWh
+
+
+endmenu

+ 4 - 0
main/config.h

@@ -1,6 +1,10 @@
 
 #define PULSES_PER_KWH      10000
 
+// These defines configures which code to generate (to save download time during development)
+//#define CCFG_GEN_PULSE      // For testing purposes only (LED-Pulse 14Hz)
+//#define CCFG_PCNT           // pcnt-code that counts pulses
+
 
 #define PCNT_INPUT_SIG_IO   4  // Pulse Input GPIO
 #define PCNT_INPUT_CTRL_IO  5  // Control GPIO HIGH=count up, LOW=count down

+ 2 - 0
main/led_test_inout.c

@@ -13,6 +13,7 @@
  */
 void ledc_init(void)
 {
+#ifdef CCFG_GEN_PULSE
     // Prepare and then apply the LEDC PWM timer configuration
     ledc_timer_config_t ledc_timer;
     ledc_timer.speed_mode       = LEDC_LOW_SPEED_MODE;
@@ -37,4 +38,5 @@ void ledc_init(void)
     ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, 409)); // Set duty to 5%. ((2 ** 13) - 1) * 50% = 409
     // Update duty to apply the new value
     ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0));
+#endif
 }

+ 4 - 0
main/main.c

@@ -10,6 +10,7 @@
 #include "esp_attr.h"
 #include "esp_log.h"
 #include "config.h"
+#include "wifi.h"
 
 // Externs
 extern void ledc_init(void);
@@ -34,6 +35,9 @@ void app_main(void)
 
     init_pcnt_unit();
     ledc_init();
+#ifdef WIFI_ENABLED
+    initWifi();             // Init WIFI
+#endif
 
     float kWh;
 

+ 10 - 1
main/pcnt_functions.c

@@ -9,6 +9,8 @@
 
 #include "soc/dport_reg.h"
 
+#ifdef CCFG_PCNT
+
 // Example-code: https://github.com/espressif/esp-idf/blob/master/examples/peripherals/pcnt/pulse_count_event/main/pcnt_event_example_main.c
 
 // Counts full kWh
@@ -74,4 +76,11 @@ void init_pcnt_unit()
 
     /* Everything is set up, now go to counting */
     pcnt_counter_resume(pcntUnit);
-}
+}
+
+#else
+
+void init_pcnt_unit() { }
+float getkWh() { return 0.0f; }
+
+#endif

+ 182 - 0
main/wifi.c

@@ -0,0 +1,182 @@
+/* WiFi station Example
+
+   This example code is in the Public Domain (or CC0 licensed, at your option.)
+
+   Unless required by applicable law or agreed to in writing, this
+   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
+   CONDITIONS OF ANY KIND, either express or implied.
+*/
+
+
+#include <string.h>
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+#include "freertos/event_groups.h"
+#include "esp_system.h"
+#include "esp_log.h"
+#include "driver/gpio.h"
+
+#include "wifi.h"
+
+
+#ifdef WIFI_ENABLED
+
+#include "nvs_flash.h"
+#include "esp_wifi.h"
+#include "esp_event.h"
+#include "lwip/err.h"
+#include "lwip/sys.h"
+
+
+// These are configured in idf.py menuconfig
+#define DEFAULT_SSID CONFIG_ESP_WIFI_SSID
+#define DEFAULT_PWD CONFIG_ESP_WIFI_PASSWORD
+
+
+static const char *TAG = "wifi";
+
+
+int latestRSSIValue = 0;
+int latestWiFiChannel = -1;
+
+/*******
+ * 0: Not initialized
+ * 5: Waiting for a connection, or other event
+ * 10: WIFI_EVENT_STA_START
+ * 15: Waiting for a connection
+ * 20: WIFI_EVENT_STA_DISCONNECTED
+ * 30: IP_EVENT_STA_GOT_IP
+ * 35: All is up and running
+ */
+
+static int wifiState = 0; // Local wifi-state
+
+int commIsUpAndRunning = 0; // Global info
+
+static void event_handler(void* arg, esp_event_base_t event_base,int32_t event_id, void* event_data)
+{
+    ESP_LOGI(TAG, "Got event: %s ID:%d",event_base, event_id);
+    if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
+        wifiState = 10;
+    }
+    else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
+        wifiState = 20;
+
+    }
+    else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
+        wifiState = 30;
+    }
+}
+
+static void setupAndConfigureWiFi(void)
+{
+    ESP_ERROR_CHECK(esp_netif_init());
+    ESP_ERROR_CHECK(esp_event_loop_create_default());
+
+    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
+    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
+
+    ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
+    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
+
+    // Initialize default station as network interface instance (esp-netif)
+    esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
+    assert(sta_netif);
+
+    // Initialize and start WiFi
+    wifi_config_t wifi_config = {
+        .sta = {
+            .ssid = DEFAULT_SSID,
+            .password = DEFAULT_PWD,
+            .scan_method = WIFI_ALL_CHANNEL_SCAN,
+            .sort_method = WIFI_CONNECT_AP_BY_SIGNAL,
+            .threshold.rssi = -127,
+            .threshold.authmode = WIFI_AUTH_OPEN,
+        },
+    };
+    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
+    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
+    ESP_ERROR_CHECK(esp_wifi_start());
+}
+
+void    wifiTask(void *pvParameters)
+{
+    static TickType_t lastLogTime = 0;
+
+    while(1) {
+
+        switch (wifiState)
+        {
+        case 0:
+            // Time to setup everything
+            ESP_LOGI(TAG, "wifiTask state 0. Init-phase");
+            wifiState = 5;
+            setupAndConfigureWiFi();
+            break;
+        
+        case 10:
+            // Try to connect
+            ESP_LOGI(TAG, "wifiTask state 10: Try to scan and connect");
+            wifiState = 15;
+            esp_wifi_connect();
+            break;          
+        
+        case 20:
+            // We got disconnected ??
+            commIsUpAndRunning = 0;
+            ESP_LOGI(TAG, "wifiTask state 20: Disconnected");
+            wifiState = 15;
+            esp_wifi_connect();
+            break;          
+       
+        case 30:
+            // We got IP
+            commIsUpAndRunning = 1;
+            wifiState = 35;
+            ESP_LOGI(TAG, "wifiTask state 30. We got IP");
+            break;
+        
+        case 35:
+            // Everything is fine !
+
+            if( (xTaskGetTickCount()*portTICK_PERIOD_MS) > (lastLogTime + 500) ) {
+                static int logCounter = 5;
+                lastLogTime = xTaskGetTickCount() * portTICK_PERIOD_MS;
+                wifi_ap_record_t wifidata;
+                if (esp_wifi_sta_get_ap_info(&wifidata)==0){
+                    if( --logCounter == 0 ) {
+                        //ESP_LOGI(TAG, "WiFi RSSI:%d  Ch:%d",wifidata.rssi, wifidata.primary);
+                        logCounter=60;
+                    }
+                    latestRSSIValue = wifidata.rssi;
+                    latestWiFiChannel = wifidata.primary;
+                }
+            }
+            break;
+
+        case 5:
+        case 15:
+        default:
+            break;
+        }
+
+        vTaskDelay(250 / portTICK_PERIOD_MS);
+    }
+
+    vTaskDelete(NULL);
+}
+
+void initWifi(void)
+{
+  //Initialize NVS
+  esp_err_t ret = nvs_flash_init();
+  if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
+    ESP_ERROR_CHECK(nvs_flash_erase());
+    ret = nvs_flash_init();
+  }
+  ESP_ERROR_CHECK(ret);
+
+  xTaskCreate(wifiTask, "WiFi-Task", 1024*10, NULL, 2, NULL);
+}
+
+#endif

+ 13 - 0
main/wifi.h

@@ -0,0 +1,13 @@
+#ifndef __WIFI__
+#define __WIFI__
+
+void initWifi(void);
+
+extern int latestRSSIValue;
+extern int commIsUpAndRunning; // Global info
+extern int latestWiFiChannel;
+
+//#define WIFI_ENABLED
+
+
+#endif