main/config.h

Fri, 11 Oct 2019 13:12:34 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 11 Oct 2019 13:12:34 +0200
changeset 8
c6bbd1380f22
parent 2
c0184362d48c
child 9
a85995941d0d
permissions
-rw-r--r--

Added alarm flag for units.

/**
 * @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_sleep.h"
#include "esp_log.h"
#include "esp_spiffs.h"
#include "esp_vfs.h"
#include "esp_system.h"
#include "esp_wifi.h"
//#include "esp_wifi_types.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.h"
#include "nvs_flash.h"
#include "lwip/apps/sntp.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 "xutil.h"


/**
 * @brief Mainloop 1 timed actions.
 */
typedef enum
{
    MAIN_LOOP1_INIT = 0,		///< Init fase
    MAIN_LOOP1_CONNECT,			///< Connect WiFi
    MAIN_LOOP1_MQTT_CONNECT,		///< Connect MQTT if WiFi
    MAIN_LOOP1_WAITCON,			///< Wait for connection and measurements
    MAIN_LOOP1_SEND,			///< Send MQTT node and units messages
    MAIN_LOOP1_WAITACK,			///< MQTT messages received
    MAIN_LOOP1_MQTT_DISCONNECT,		///< MQTT disconnect
    MAIN_LOOP1_DISCONNECT,		///< Disconnect WiFi
    MAIN_LOOP1_WIFI_OFF,		///< WiFi power off
    MAIN_LOOP1_DONE			///< All done
} MAIN_LOOP1;



/**
 * @brief Mainloop 2 user actions.
 */
typedef enum
{
    MAIN_LOOP2_INIT = 0,
    MAIN_LOOP2_UNIT1,			///< Unit 1
    MAIN_LOOP2_UNIT2,			///< Unit 2
    MAIN_LOOP2_UNIT3,			///< Unit 3
    MAIN_LOOP2_UNIT4,			///< Unit 4
    MAIN_LOOP2_SET_WIFI,		///< WiFi stations setup
    MAIN_LOOP2_SET_NETWORK,		///< Network setup
    MAIN_LOOP2_SET_MQTT,		///< MQTT setup
    MAIN_LOOP2_UPDATE,			///< Update
    MAIN_LOOP2_INACTIVE,		///< Inactive reached, cleanup
    MAIN_LOOP2_DONE			///< All done
} MAIN_LOOP2;



/**
 * @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];                  ///< Last connected station.
    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        hide;                           ///< 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		product_uuid[37];		///< Beer product uuid
    char		product_code[33];		///< Beer product code
    char		product_name[129];		///< Beer product name
    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 in mV.
    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