main/config.h

Sat, 14 Mar 2020 13:07:02 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 14 Mar 2020 13:07:02 +0100
changeset 47
1ab1f4a8c328
parent 38
46e2c385e9de
child 52
90a36619b07b
permissions
-rw-r--r--

Version 0.2.2 Changed to use a permanent network and WiFi connection. Removed three mainloop stages. Removed MQTT sequence counter that was not used. Update WiFi rssi status during eacht measure cycle. Changed FreeRTOS schedulng to 500 Hz.

/**
 * @file config.h
 * @brief The 'co2meter' configuration data. These are stored in the 
 *        spiffs filesystem in a flash partition.
 */

#ifndef _CONFIG_H
#define _CONFIG_H

// Global includes for the project

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#include <errno.h>
#include <sys/unistd.h>
#include <sys/fcntl.h>
#include <sys/time.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/event_groups.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#include "driver/adc.h"
#include "driver/rtc_io.h"
#include "soc/sens_periph.h"
#include "soc/rtc.h"
#include "esp_adc_cal.h"
#include "esp_log.h"
#include "esp_spiffs.h"
#include "esp_vfs.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_wpa2.h"
#include "esp_event.h"
#include "esp_spi_flash.h"
#include "esp_ota_ops.h"
#include "esp_http_client.h"
#include "nvs_flash.h"
#include "lwip/sockets.h"
#include "lwip/dns.h"
#include "lwip/netdb.h"
#include "tcpip_adapter.h"
#include "mqtt_client.h"

#include "rotary_encoder.h"
#include "u8g2_esp32_hal.h"

#include "task_ds18b20.h"
#include "task_adc.h"
#include "task_wifi.h"
#include "task_mqtt.h"
#include "task_user.h"
#include "xutil.h"
#include "updates.h"


/**
 * @brief Mainloop 1 timed actions.
 */
typedef enum
{
    ML1_INIT = 0,		///< Init fase
    ML1_CONNECT,		///< Connect WiFi
    ML1_MQTT_CONNECT,		///< Connect MQTT if WiFi
    ML1_WAITCON,		///< Wait for connection and measurements
    ML1_SEND,			///< Send MQTT node and units messages
    ML1_WAITACK,		///< MQTT messages received
    ML1_DONE			///< All done
} ML1;



/**
 * @brief Mainloop 2 user actions.
 */
typedef enum
{
    ML2_INIT = 0,
    ML2_USER,			///< User mainmenu
    ML2_UNIT1,			///< Unit 1
    ML2_UNIT2,			///< Unit 2
    ML2_UNIT3,			///< Unit 3
    ML2_WIFI,			///< WiFi status
    ML2_NETWORK,		///< Network status
    ML2_MQTT,			///< MQTT status
    ML2_UPDATE,			///< Update
    ML2_SETUP_UNIT1,		///< Unit 1 setup
    ML2_SETUP_UNIT2,		///< Unit 2 setup
    ML2_SETUP_UNIT3,		///< Unit 3 setup
    ML2_ZERO_UNIT1,		///< Unit 1 set zero
    ML2_ZERO_UNIT2,		///< Unit 2 set zero
    ML2_ZERO_UNIT3,		///< Unit 3 set zero
    ML2_SEL_SENSOR1,		///< Unit 1 select sensor
    ML2_SEL_SENSOR2,		///< Unit 2 select sensor
    ML2_SEL_SENSOR3,		///< Unit 3 select sensor
    ML2_SETUP_NETWORK,		///< Network setup
    ML2_SETUP_MQTT,		///< MQTT setup
    ML2_SETUP_WIFI,		///< WiFi setup
    ML2_LIST_APS,		///< WiFi list APs
    ML2_EDIT_AP,		///< WiFi edit AP
    ML2_INACTIVE,		///< Inactive reached, cleanup
    ML2_DONE			///< All done
} ML2;



/**
 * @brief Global configuration. File /spiffs/config.conf
 */
struct strConfig {
    uint8_t		Version;			///< Record version number for updates.
    char                ntp_server[32];                 ///< Preffered NTP server.
    char                hostname[32];                   ///< Our hostname.
    char                uuid[37];                       ///< Sort of uuid code.
    char                xlastSSID[32];                  ///< Not used anymore.
    char		mqtt_server[32];		///< MQTT server.
    uint16_t		mqtt_port;			///< MQTT TCP port.
    char		mqtt_user[32];			///< MQTT user name if needed.
    char		mqtt_pwd[62];			///< MQTT password.
} config;                                               ///< Config record.


/**
 * @brief Write configuration to disk.
 */
void write_config(void);

/**
 * @brief Read configuration file. If it doesn't exist create and
 *        write a new configuration file with sane defaults.
 */
void read_config(void);



/**
 * @brief Records with WiFi stations we have succesfully connected.
 */
struct strStations {
    char        SSID[32];                       ///< Station SSID
    char        Password[64];                   ///< Station password
    bool        xhide;                          ///< Hide from AP scan.
} wifiStation;                                  ///< Station record.

/**
 * @brief Add a new station record.
 * @param SSID The SSID
 * @param Password The password for this SSID
 * @return The record number, or -1 if error.
 */
int add_station(uint8_t *SSID, uint8_t *Password);

/**
 * @brief Read station info record.
 * @param SSID Search for the SSID and load the record if found.
 * @return Return -1 if not found, else the record number and the wifiStation record is filled.
 */
int read_station(uint8_t *SSID);

/**
 * @brief Remove station record.
 * @param SSID The SSID to remove.
 */
void remove_station(uint8_t *SSID);



/**
 * @brief Records that describes the carbonation units,
 */
typedef struct strUnit {
    char		uuid[37];			///< Unit uuid
    char		alias[33];			///< Alias name 'unit1'
    int			temperature_state;		///< Reading status
    char		temperature_rom_code[17];	///< DS18B20 address
    uint32_t		temperature;			///< Temperature in C * 1000
    int			pressure_state;			///< Reading status
    uint8_t		pressure_channel;		///< ADC channel
    uint32_t		pressure_voltage;		///< Measured voltage in mV.
    uint32_t		pressure_zero;			///< Zero offset promille of the full scale.
    uint32_t		pressure;			///< Pressure in bar * 1000;
    int			mode;				///< Unit mode
    uint32_t		alarm;				///< Alarm bits
} unit_t;

#define ALARM_UNIT_PRESSURE		0x0001		///< Unit pressure sensor error
#define ALARM_UNIT_TEMPERATURE		0x0002		///< Unit temperature sensor error
#define	ALARM_SYS_TEMPERATURE		0x0004		///< System temperature sensor error


/**
 * @brief Write units to disk.
 */
void write_units(void);

/** @brief Read units file. If it doesn't exist create and
 *         write a new units file with sane defaults.
 */
void read_units(void);



#endif

mercurial