Compare commits
82 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 03adefeee7 | |||
| 60d7752721 | |||
| 514f4d5d69 | |||
| d8a29c30d4 | |||
| b108d57c12 | |||
| af8e330460 | |||
| 46fcb137b4 | |||
| 2a3eaf2acc | |||
| 3d841230bf | |||
| 26bbe5b518 | |||
| 68e5a67fb7 | |||
| 50fbe9e9eb | |||
| 5dff5da328 | |||
| 72974c41a7 | |||
| 9fab98af61 | |||
| aba6253e70 | |||
| 6bfdc19fa8 | |||
| 184a0eed27 | |||
| 1bbd73ff49 | |||
| cd36415807 | |||
| dc5c2c751b | |||
| 137814206c | |||
| 2b8bbd703b | |||
| 1c21ddee89 | |||
| 14fc015d1b | |||
| b9c8a5d2a7 | |||
| f961cc62a0 | |||
| 0d0750b746 | |||
| 626bbe9dbb | |||
| 63c358fd87 | |||
| 1a893547b6 | |||
| 06a672d1f7 | |||
| b1a6dfddd3 | |||
| 2e6e829829 | |||
| 6a2a933002 | |||
| aa395c381b | |||
| eaad4469bc | |||
| 4e78a5f2ed | |||
| c53783072e | |||
| 9bad1790a7 | |||
| 4cd9a3210c | |||
| 2c7951c2bb | |||
| 88c7635ed9 | |||
| b0059f6e9f | |||
| 985642e0fe | |||
| fb98ce800a | |||
| a332c37e72 | |||
| 567fca2916 | |||
| 04b5fb0a7c | |||
| 6fd2d2523d | |||
| 9143bd0f76 | |||
| c6c4011a47 | |||
| 213f6b399f | |||
| 0a418805a4 | |||
| 3670b58f8e | |||
| 5dd2c00ac0 | |||
| 2fcc3172aa | |||
| 665125e07f | |||
| 401c610a58 | |||
| 1ef2476f78 | |||
| 28ac3f44b3 | |||
| 29ad9350a3 | |||
| 455ab94449 | |||
| 49cf49db87 | |||
| 50dd39fd60 | |||
| a02d3e3294 | |||
| 3a91791180 | |||
| 71106c2542 | |||
| 4f0dd9faaa | |||
| cd4d2658b9 | |||
| a1781af096 | |||
| 2be65a0b76 | |||
| 620eb57920 | |||
| b28b20f46a | |||
| 1721be5e66 | |||
| 2a36d1356c | |||
| 0f0408df9b | |||
| 2d9a4f3bb5 | |||
| 2d6643601d | |||
| 74aab766fc | |||
| 6c48ebb65f | |||
| 40a5ad5a35 |
@@ -440,6 +440,7 @@
|
|||||||
"single_global_label": "ignore",
|
"single_global_label": "ignore",
|
||||||
"unannotated": "error",
|
"unannotated": "error",
|
||||||
"unconnected_wire_endpoint": "warning",
|
"unconnected_wire_endpoint": "warning",
|
||||||
|
"undefined_netclass": "error",
|
||||||
"unit_value_mismatch": "error",
|
"unit_value_mismatch": "error",
|
||||||
"unresolved_variable": "error",
|
"unresolved_variable": "error",
|
||||||
"wire_dangling": "error"
|
"wire_dangling": "error"
|
||||||
|
|||||||
+736
-409
File diff suppressed because it is too large
Load Diff
@@ -13,7 +13,7 @@ O_FLAG = -Os
|
|||||||
BUILD_DIR = build
|
BUILD_DIR = build
|
||||||
OBJ_DIR = $(BUILD_DIR)/obj
|
OBJ_DIR = $(BUILD_DIR)/obj
|
||||||
|
|
||||||
SRCS_C = source/adc.c source/moisture.c source/main.c source/pump.c source/eeprom.c
|
SRCS_C = source/adc.c source/moisture.c source/main.c source/pump.c source/eeprom.c source/twi.c
|
||||||
OBJS = $(addprefix $(OBJ_DIR)/, $(notdir $(SRCS_C:.c=.o)))
|
OBJS = $(addprefix $(OBJ_DIR)/, $(notdir $(SRCS_C:.c=.o)))
|
||||||
|
|
||||||
CFLAGS = -Wall $(O_FLAG) -DF_CPU=$(F_CPU) -mmcu=$(MCU) -Iinclude
|
CFLAGS = -Wall $(O_FLAG) -DF_CPU=$(F_CPU) -mmcu=$(MCU) -Iinclude
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
inline void eeprom_init() {
|
inline void eeprom_init() {
|
||||||
#define DICT_SIZE 4
|
#define DICT_SIZE 4
|
||||||
|
|
||||||
|
// EEPROM addresses
|
||||||
#define TWI_ADDR 0x22
|
#define TWI_ADDR 0x22
|
||||||
#define SENSOR_READINGS 0x23
|
#define SENSOR_READINGS 0x23
|
||||||
#define MOISTURE_MIN 0x24
|
#define MOISTURE_MIN 0x24
|
||||||
@@ -39,8 +40,9 @@ inline void eeprom_init() {
|
|||||||
MOISTURE_MAX
|
MOISTURE_MAX
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// actual values
|
||||||
uint16_t values[DICT_SIZE] = {
|
uint16_t values[DICT_SIZE] = {
|
||||||
0x20,
|
0x30,
|
||||||
5,
|
5,
|
||||||
400,
|
400,
|
||||||
200
|
200
|
||||||
@@ -51,4 +53,6 @@ inline void eeprom_init() {
|
|||||||
for (int i = 0; i < DICT_SIZE; i++) if (eepromRead(keys[i]) == 0) eepromWrite(keys[i], values[i]);
|
for (int i = 0; i < DICT_SIZE; i++) if (eepromRead(keys[i]) == 0) eepromWrite(keys[i], values[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern volatile uint16_t *pLastMoistureVal;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -44,7 +44,6 @@ static const int8_t sensorPins[] = {
|
|||||||
extern volatile bool readSensors;
|
extern volatile bool readSensors;
|
||||||
|
|
||||||
void sensorsInit(void);
|
void sensorsInit(void);
|
||||||
inline void triggerMoistureRead(void);
|
|
||||||
uint16_t moistureRead(void);
|
uint16_t moistureRead(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
#ifndef TWI_H
|
||||||
|
#define TWI_H
|
||||||
|
|
||||||
|
extern volatile uint16_t *pLastMoistureVal;
|
||||||
|
|
||||||
|
void twiInit(void);
|
||||||
|
void twiListen(void);
|
||||||
|
void twiRespond(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -3,15 +3,20 @@
|
|||||||
#include <avr/sleep.h>
|
#include <avr/sleep.h>
|
||||||
#include <avr/wdt.h>
|
#include <avr/wdt.h>
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
|
#include "config.h"
|
||||||
#include "adc.h"
|
#include "adc.h"
|
||||||
#include "moisture.h"
|
#include "moisture.h"
|
||||||
#include "pump.h"
|
#include "pump.h"
|
||||||
|
#include "twi.h"
|
||||||
|
|
||||||
ISR(WDT_vect) {
|
volatile uint16_t lastMoistureVal = 0;
|
||||||
triggerMoistureRead();
|
volatile uint16_t *pLastMoistureVal = &lastMoistureVal;
|
||||||
|
|
||||||
|
ISR(TWI_vect) {
|
||||||
|
twiListen();
|
||||||
}
|
}
|
||||||
|
|
||||||
void watchdogs(void) {
|
static void watchdogs(void) {
|
||||||
cli();
|
cli();
|
||||||
MCUSR &= ~(1 << WDRF);
|
MCUSR &= ~(1 << WDRF);
|
||||||
WDTCSR |= (1 << WDCE) | (1 << WDE);
|
WDTCSR |= (1 << WDCE) | (1 << WDE);
|
||||||
@@ -19,15 +24,16 @@ void watchdogs(void) {
|
|||||||
sei();
|
sei();
|
||||||
}
|
}
|
||||||
|
|
||||||
void enterSleep(void) {
|
static inline void enterSleep(void) {
|
||||||
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
|
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
|
||||||
sleep_enable();
|
sleep_enable();
|
||||||
sleep_cpu();
|
sleep_cpu();
|
||||||
sleep_disable();
|
sleep_disable();
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup(void) {
|
static void setup(void) {
|
||||||
adcInit();
|
adcInit();
|
||||||
|
twiInit();
|
||||||
sensorsInit();
|
sensorsInit();
|
||||||
pumpsInit();
|
pumpsInit();
|
||||||
watchdogs();
|
watchdogs();
|
||||||
@@ -36,16 +42,13 @@ void setup(void) {
|
|||||||
int main(void) {
|
int main(void) {
|
||||||
setup();
|
setup();
|
||||||
|
|
||||||
while(true) {
|
pLastMoistureVal = &lastMoistureVal;
|
||||||
if (readSensors) {
|
|
||||||
uint16_t result = moistureRead();
|
|
||||||
//espSend(VASE_NUM, result)
|
|
||||||
}
|
|
||||||
// if(espGet("ask if i need to water plants") == true) {waterPlant()}
|
|
||||||
// or maybe it's better to do this with interrupts?
|
|
||||||
|
|
||||||
readSensors = false;
|
while(true) {
|
||||||
|
if ((*pLastMoistureVal = moistureRead()) < MOISTURE_MIN) waterPlant();
|
||||||
|
twiRespond();
|
||||||
enterSleep();
|
enterSleep();
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,10 +17,6 @@ void sensorsInit(void) {
|
|||||||
PORTC &= ~sensorMask;
|
PORTC &= ~sensorMask;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void triggerMoistureRead(void) {
|
|
||||||
readSensors = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint16_t moistureAverage(uint8_t sensorPin) {
|
static uint16_t moistureAverage(uint8_t sensorPin) {
|
||||||
uint16_t sum = 0;
|
uint16_t sum = 0;
|
||||||
for (uint8_t i = 0; i < SENSOR_READINGS; i++) {
|
for (uint8_t i = 0; i < SENSOR_READINGS; i++) {
|
||||||
|
|||||||
@@ -0,0 +1,101 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <avr/io.h>
|
||||||
|
#include "config.h"
|
||||||
|
#include "pump.h"
|
||||||
|
#include "twi.h"
|
||||||
|
|
||||||
|
#define BUFFER_SIZE 4
|
||||||
|
|
||||||
|
static uint8_t twiBuffer[BUFFER_SIZE] = {0};
|
||||||
|
static uint8_t bufferIndex = 0;
|
||||||
|
bool respondFlag = false;
|
||||||
|
|
||||||
|
static inline void twiReset(void) {
|
||||||
|
// Enable TWI; enable ACK; clear interrupt flag; enable twi external interrupt
|
||||||
|
TWCR = (1 << TWEN) | (1 << TWEA) | (1 << TWINT) | (1 << TWIE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void twiInit(void) {
|
||||||
|
TWAR = ((eepromRead(TWI_ADDR)) << 1); //address definition found in config.h
|
||||||
|
twiReset();
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void twiSendAck(void) {
|
||||||
|
TWCR = (1 << TWEA) | (1 << TWINT);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void twiSendNack(void) {
|
||||||
|
TWCR = (TWCR & ~(1 << TWEA)) | (1 << TWINT);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void twiSendData(uint8_t data) {
|
||||||
|
TWDR = data;
|
||||||
|
twiSendAck();
|
||||||
|
}
|
||||||
|
|
||||||
|
void twiRespond(void) {
|
||||||
|
if (!respondFlag) return;
|
||||||
|
|
||||||
|
uint8_t cmd = twiBuffer[0];
|
||||||
|
uint8_t param = twiBuffer[1];
|
||||||
|
|
||||||
|
switch(cmd) {
|
||||||
|
// READ
|
||||||
|
case 0x01:
|
||||||
|
twiSendData(*pLastMoistureVal);
|
||||||
|
break;
|
||||||
|
case 0x02:
|
||||||
|
twiSendData(eepromRead(SENSOR_READINGS));
|
||||||
|
break;
|
||||||
|
case 0x03:
|
||||||
|
twiSendData(eepromRead(MOISTURE_MIN));
|
||||||
|
break;
|
||||||
|
case 0x04:
|
||||||
|
twiSendData(eepromRead(MOISTURE_MAX));
|
||||||
|
break;
|
||||||
|
// WRITE
|
||||||
|
case 0x05:
|
||||||
|
eepromWrite(SENSOR_READINGS, param);
|
||||||
|
twiSendAck();
|
||||||
|
break;
|
||||||
|
case 0x06:
|
||||||
|
eepromWrite(MOISTURE_MIN, param);
|
||||||
|
twiSendAck();
|
||||||
|
break;
|
||||||
|
case 0x07:
|
||||||
|
eepromWrite(MOISTURE_MAX, param);
|
||||||
|
twiSendAck();
|
||||||
|
break;
|
||||||
|
case 0x08:
|
||||||
|
waterPlant();
|
||||||
|
twiSendAck();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
twiSendNack();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void twiListen(void) {
|
||||||
|
switch (TWSR & 0xF8) {
|
||||||
|
case 0x60: // SLA+W received
|
||||||
|
bufferIndex = 0;
|
||||||
|
twiSendAck();
|
||||||
|
break;
|
||||||
|
case 0x80: // data received
|
||||||
|
if(bufferIndex < BUFFER_SIZE) {
|
||||||
|
twiBuffer[bufferIndex++] = TWDR;
|
||||||
|
twiSendAck();
|
||||||
|
} else twiSendNack();
|
||||||
|
break;
|
||||||
|
case 0xA0: // stop condition
|
||||||
|
respondFlag = true;
|
||||||
|
twiReset();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
twiSendNack();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
cmake_minimum_required(VERSION 3.16)
|
cmake_minimum_required(VERSION 3.16)
|
||||||
|
|
||||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||||
project(plant-manager)
|
project(plant-thing)
|
||||||
|
|||||||
@@ -45,4 +45,4 @@ RUN rm -rf build && /bin/bash -c "source $IDF_PATH/export.sh && idf.py set-targe
|
|||||||
RUN mkdir -p /usr/src/build && \
|
RUN mkdir -p /usr/src/build && \
|
||||||
cp build/bootloader/bootloader.bin /usr/src/build/ && \
|
cp build/bootloader/bootloader.bin /usr/src/build/ && \
|
||||||
cp build/partition_table/partition-table.bin /usr/src/build/ && \
|
cp build/partition_table/partition-table.bin /usr/src/build/ && \
|
||||||
cp build/plant-manager.bin /usr/src/build/
|
cp build/plant-thing.bin /usr/src/build/
|
||||||
|
|||||||
@@ -22,6 +22,6 @@ esptool --chip esp32c3 --port /dev/tty.usbmodem101 --baud 115200 write-flash \
|
|||||||
--flash-size 4MB \
|
--flash-size 4MB \
|
||||||
0x0000 "$DEST_DIR/bootloader.bin" \
|
0x0000 "$DEST_DIR/bootloader.bin" \
|
||||||
0x8000 "$DEST_DIR/partition-table.bin" \
|
0x8000 "$DEST_DIR/partition-table.bin" \
|
||||||
0x10000 "$DEST_DIR/plant-manager.bin"
|
0x10000 "$DEST_DIR/plant-thing.bin"
|
||||||
|
|
||||||
screen -fn /dev/tty.usbmodem101 115200
|
screen -fn /dev/tty.usbmodem101 115200
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
#ifndef CONFIG_H
|
||||||
|
#define CONFIG_H
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
// WI-FI
|
||||||
|
#define SSID "your_wifi_ssid"
|
||||||
|
#define PASSWORD "your_wifi_password"
|
||||||
|
#define AUTH_MODE WIFI_AUTH_WPA2_PSK
|
||||||
|
|
||||||
|
// MQTT
|
||||||
|
#define MQTT_ADDR "mqtt://yourmqttserver"
|
||||||
|
#define MQTT_PORT 1883
|
||||||
|
#define MQTT_USER "your_username"
|
||||||
|
#define MQTT_PASS "your_password"
|
||||||
|
|
||||||
|
#define CMD_GET_MOISTURE 0x01
|
||||||
|
#define CMD_GET_SENSOR_READS 0x02
|
||||||
|
#define CMD_GET_MOISTURE_MIN 0x03
|
||||||
|
#define CMD_GET_MOISTURE_MAX 0x04
|
||||||
|
#define CMD_SET_SENSOR_READS 0x05 // [cmd, value]
|
||||||
|
#define CMD_SET_PARAMS 0x06 // [cmd, min, max, reads]
|
||||||
|
#define CMD_FORCE_PUMP 0x07 // [cmd]
|
||||||
|
|
||||||
|
// Change based on how many vases max to manage [1 to ~100]
|
||||||
|
// larger numberss will require larger arrays stored in memory
|
||||||
|
// see AVR limitation on TWI addresses
|
||||||
|
#define SLAVES_NUMBER 8
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef I2C_HPP
|
||||||
|
#define I2C_HPP
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
|
#include "freertos/task.h"
|
||||||
|
|
||||||
|
extern TaskHandle_t i2c_task;
|
||||||
|
|
||||||
|
void i2cTask(void *pvParameters);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,9 +1,15 @@
|
|||||||
#ifdef __cplusplus
|
#ifndef MAIN_HPP
|
||||||
extern "C" {
|
#define MAIN_HPP
|
||||||
#endif
|
|
||||||
|
|
||||||
extern volatile bool criticalErrorFlag;
|
#pragma once
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#include "freertos/FreeRTOS.h"
|
||||||
}
|
#include "freertos/event_groups.h"
|
||||||
#endif
|
|
||||||
|
extern volatile bool criticalErrorFlag;
|
||||||
|
extern EventGroupHandle_t connectivity_event_group;
|
||||||
|
|
||||||
|
#define WIFI_CONNECTED_BIT BIT0
|
||||||
|
#define MQTT_CONNECTED_BIT BIT1
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef MQTT_HPP
|
||||||
|
#define MQTT_HPP
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
void mqttTask(void *pvParameters);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
#ifndef WIFI_CREDENTIALS_H
|
|
||||||
#define WIFI_CREDENTIALS_H
|
|
||||||
|
|
||||||
#define SSID "your_wifi_ssid"
|
|
||||||
#define PASSWORD "your_wifi_password"
|
|
||||||
#define AUTH_MODE WIFI_AUTH_WPA2_PSK
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
#ifndef WIFI_HPP
|
#ifndef WIFI_HPP
|
||||||
#define WIFI_HPP
|
#define WIFI_HPP
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
void wifiTask(void *pvParameters);
|
void wifiTask(void *pvParameters);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
idf_component_register(SRCS "./main.cpp"
|
idf_component_register(
|
||||||
|
SRCS "./main.cpp"
|
||||||
"./wifi.cpp"
|
"./wifi.cpp"
|
||||||
|
"./i2c.cpp"
|
||||||
|
"./mqtt.cpp"
|
||||||
INCLUDE_DIRS "../include"
|
INCLUDE_DIRS "../include"
|
||||||
REQUIRES esp_wifi
|
REQUIRES esp_wifi
|
||||||
nvs_flash
|
nvs_flash
|
||||||
@@ -7,4 +10,7 @@ idf_component_register(SRCS "./main.cpp"
|
|||||||
esp_hw_support
|
esp_hw_support
|
||||||
esp_system
|
esp_system
|
||||||
freertos
|
freertos
|
||||||
log)
|
log
|
||||||
|
mqtt
|
||||||
|
esp_driver_i2c
|
||||||
|
)
|
||||||
|
|||||||
@@ -0,0 +1,100 @@
|
|||||||
|
#include <cstdint>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
|
#include "freertos/event_groups.h"
|
||||||
|
#include "freertos/task.h"
|
||||||
|
#include "freertos/queue.h"
|
||||||
|
#include "esp_task_wdt.h"
|
||||||
|
#include "driver/i2c_master.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "config-erick.h"
|
||||||
|
|
||||||
|
#define I2C_TASK_TIMEOUT_MS 4500
|
||||||
|
|
||||||
|
// for ESP32-C3
|
||||||
|
#define I2C_MASTER_SCL_IO ((gpio_num_t)9)
|
||||||
|
#define I2C_MASTER_SDA_IO ((gpio_num_t)8)
|
||||||
|
|
||||||
|
#define DATA_LENGTH 100
|
||||||
|
|
||||||
|
static const char* TAG = "I2C";
|
||||||
|
|
||||||
|
TaskHandle_t i2c_task = NULL;
|
||||||
|
|
||||||
|
i2c_master_bus_handle_t bus_handle;
|
||||||
|
static i2c_master_dev_handle_t slave_handles[SLAVES_NUMBER];
|
||||||
|
uint8_t slave_count = 0;
|
||||||
|
uint8_t slave_addrs[SLAVES_NUMBER];
|
||||||
|
|
||||||
|
uint8_t data_rd[DATA_LENGTH];
|
||||||
|
|
||||||
|
static void register_slave(uint8_t addr) {
|
||||||
|
i2c_device_config_t slave_dev_cfg = {};
|
||||||
|
slave_dev_cfg.dev_addr_length = I2C_ADDR_BIT_LEN_7;
|
||||||
|
slave_dev_cfg.device_address = addr;
|
||||||
|
slave_dev_cfg.scl_speed_hz = 100000;
|
||||||
|
ESP_ERROR_CHECK(i2c_master_bus_add_device(bus_handle, &slave_dev_cfg, &slave_handles[slave_count]));
|
||||||
|
slave_addrs[slave_count++] = addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void i2c_init(void) {
|
||||||
|
i2c_master_bus_config_t i2c_mst_config = {};
|
||||||
|
i2c_mst_config.i2c_port = I2C_NUM_0;
|
||||||
|
i2c_mst_config.scl_io_num = I2C_MASTER_SCL_IO;
|
||||||
|
i2c_mst_config.sda_io_num = I2C_MASTER_SDA_IO;
|
||||||
|
i2c_mst_config.clk_source = I2C_CLK_SRC_DEFAULT;
|
||||||
|
i2c_mst_config.glitch_ignore_cnt = 7;
|
||||||
|
i2c_mst_config.intr_priority = 1;
|
||||||
|
i2c_mst_config.trans_queue_depth = 4;
|
||||||
|
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_mst_config, &bus_handle));
|
||||||
|
|
||||||
|
register_slave(0x30);
|
||||||
|
register_slave(0x31);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void i2c_send(uint32_t packet) {
|
||||||
|
uint8_t cmd = (packet >> 24) & 0xFF;
|
||||||
|
uint8_t slave_addr = (packet >> 16) & 0xFF;
|
||||||
|
uint8_t param = packet & 0xFF;
|
||||||
|
|
||||||
|
i2c_master_dev_handle_t dev = NULL;
|
||||||
|
for(uint8_t i = 0; i < slave_count; i++) {
|
||||||
|
if(slave_addrs[i] == slave_addr) {
|
||||||
|
dev = slave_handles[i];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!dev) { ESP_LOGE(TAG,"Slave 0x%02X not found", slave_addr); return; }
|
||||||
|
|
||||||
|
uint8_t tx[2] = {cmd, param};
|
||||||
|
esp_err_t err;
|
||||||
|
switch(cmd) {
|
||||||
|
case CMD_GET_MOISTURE:
|
||||||
|
case CMD_GET_SENSOR_READS:
|
||||||
|
case CMD_GET_MOISTURE_MIN:
|
||||||
|
case CMD_GET_MOISTURE_MAX: {
|
||||||
|
uint8_t rx;
|
||||||
|
err = i2c_master_transmit_receive(dev, tx, 2, &rx, 1, 250);
|
||||||
|
if(err == ESP_OK) ESP_LOGI(TAG,"I2C READ cmd=0x%02X val=%u", cmd, rx);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
err = i2c_master_transmit(dev, tx, 2, 250);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(err != ESP_OK) ESP_LOGE(TAG,"I2C transaction failed: %s", esp_err_to_name(err));
|
||||||
|
}
|
||||||
|
|
||||||
|
void i2cTask(void *pvParameters) {
|
||||||
|
i2c_init();
|
||||||
|
ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
|
||||||
|
|
||||||
|
uint32_t packet;
|
||||||
|
for(;;) {
|
||||||
|
if(xTaskNotifyWait(0, 0, &packet, pdMS_TO_TICKS(I2C_TASK_TIMEOUT_MS)) == pdPASS) {
|
||||||
|
i2c_send(packet);
|
||||||
|
}
|
||||||
|
ESP_ERROR_CHECK(esp_task_wdt_reset());
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(150));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
dependencies:
|
||||||
|
espressif/mqtt: "*"
|
||||||
@@ -6,6 +6,12 @@
|
|||||||
#include "esp_pm.h"
|
#include "esp_pm.h"
|
||||||
#include "esp_task_wdt.h"
|
#include "esp_task_wdt.h"
|
||||||
#include "wifi.hpp"
|
#include "wifi.hpp"
|
||||||
|
#include "i2c.hpp"
|
||||||
|
#include "mqtt.hpp"
|
||||||
|
|
||||||
|
#define WATCHDOG_KEEPALIVE_MS 8000
|
||||||
|
|
||||||
|
EventGroupHandle_t connectivity_event_group;
|
||||||
|
|
||||||
static const char* TAG = "MAIN";
|
static const char* TAG = "MAIN";
|
||||||
|
|
||||||
@@ -22,18 +28,16 @@ static void watchdogTask(void *pvParameters) {
|
|||||||
|
|
||||||
ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
|
ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
|
||||||
|
|
||||||
const TickType_t keepAlivePeriod = pdMS_TO_TICKS(8000);
|
|
||||||
|
|
||||||
for(;;) {
|
for(;;) {
|
||||||
if (criticalErrorFlag) {
|
if(criticalErrorFlag) {
|
||||||
ESP_LOGE(TAG, "Critical error flag raised. Rebooting.");
|
ESP_LOGE(TAG, "Critical error flag raised. Rebooting.");
|
||||||
esp_restart();
|
esp_restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_ERROR_CHECK(esp_task_wdt_reset());
|
ESP_ERROR_CHECK(esp_task_wdt_reset());
|
||||||
ESP_LOGI(TAG, "WDT reset");
|
ESP_LOGV(TAG, "WDT reset");
|
||||||
|
|
||||||
vTaskDelay(keepAlivePeriod);
|
vTaskDelay(pdMS_TO_TICKS(WATCHDOG_KEEPALIVE_MS));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,6 +50,10 @@ extern "C" void app_main(void)
|
|||||||
};
|
};
|
||||||
esp_pm_configure(&pm_cfg);
|
esp_pm_configure(&pm_cfg);
|
||||||
|
|
||||||
|
connectivity_event_group = xEventGroupCreate();
|
||||||
|
|
||||||
xTaskCreate(watchdogTask, "watchdogs", 2048, NULL, configMAX_PRIORITIES-1, NULL);
|
xTaskCreate(watchdogTask, "watchdogs", 2048, NULL, configMAX_PRIORITIES-1, NULL);
|
||||||
xTaskCreate(wifiTask, "wifi", 4096, NULL, configMAX_PRIORITIES-4, NULL);
|
xTaskCreate(wifiTask, "wifi", 4096, NULL, configMAX_PRIORITIES-4, NULL);
|
||||||
|
xTaskCreate(i2cTask, "i2c", 4096, NULL, configMAX_PRIORITIES-6, &i2c_task);
|
||||||
|
xTaskCreate(mqttTask, "mqtt", 2048, NULL, configMAX_PRIORITIES-8, NULL);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,156 @@
|
|||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
|
#include "freertos/event_groups.h"
|
||||||
|
#include "freertos/task.h"
|
||||||
|
#include "freertos/queue.h"
|
||||||
|
#include "esp_task_wdt.h"
|
||||||
|
#include "esp_event.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "string.h"
|
||||||
|
#include "mqtt_client.h"
|
||||||
|
#include "main.hpp"
|
||||||
|
#include "i2c.hpp"
|
||||||
|
#include "config.h"
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#define MQTT_TASK_TIMEOUT_MS 3000
|
||||||
|
#define DEFAULT_SLAVE_ADDR 0x30
|
||||||
|
|
||||||
|
static const char* TAG = "MQTT";
|
||||||
|
|
||||||
|
static esp_mqtt_client_handle_t mqtt_client = NULL;
|
||||||
|
|
||||||
|
static char rx_buffer[50];
|
||||||
|
|
||||||
|
static void mqtt_subscribe(void) {
|
||||||
|
esp_mqtt_client_subscribe(mqtt_client, "/plant/moisture/qos0", 0);
|
||||||
|
esp_mqtt_client_subscribe(mqtt_client, "/plant/humidity/qos0", 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
|
||||||
|
esp_mqtt_event_handle_t event =(esp_mqtt_event_handle_t)event_data;
|
||||||
|
|
||||||
|
switch(event->event_id) {
|
||||||
|
case MQTT_EVENT_CONNECTED:
|
||||||
|
xEventGroupSetBits(connectivity_event_group, MQTT_CONNECTED_BIT);
|
||||||
|
ESP_LOGV(TAG, "Connected to broker");
|
||||||
|
mqtt_subscribe();
|
||||||
|
ESP_LOGV(TAG, "Subscribed to topic");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MQTT_EVENT_DISCONNECTED:
|
||||||
|
xEventGroupClearBits(connectivity_event_group, MQTT_CONNECTED_BIT);
|
||||||
|
ESP_LOGW(TAG, "Disconnected from broker");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MQTT_EVENT_DATA:
|
||||||
|
{
|
||||||
|
ESP_LOGD(TAG, "Data received");
|
||||||
|
|
||||||
|
int len = event->data_len;
|
||||||
|
if(len > sizeof(rx_buffer) - 1) len = sizeof(rx_buffer) - 1;
|
||||||
|
|
||||||
|
memcpy(rx_buffer, event->data, len);
|
||||||
|
rx_buffer[len] = '\0';
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case MQTT_EVENT_ERROR:
|
||||||
|
ESP_LOGE(TAG, "Error event");
|
||||||
|
if(event->error_handle->error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) ESP_LOGE(TAG, "Network Error: %s", strerror(event->error_handle->esp_transport_sock_errno));
|
||||||
|
else if(event->error_handle->error_type == MQTT_ERROR_TYPE_CONNECTION_REFUSED) ESP_LOGE(TAG, "Connection Refused! Reason code: 0x%x", event->error_handle->connect_return_code);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mqtt_init(void) {
|
||||||
|
esp_mqtt_client_config_t mqtt_cfg = {};
|
||||||
|
mqtt_cfg.broker.address.uri = MQTT_ADDR;
|
||||||
|
mqtt_cfg.credentials.username = MQTT_USER;
|
||||||
|
mqtt_cfg.credentials.authentication.password = MQTT_PASS;
|
||||||
|
|
||||||
|
mqtt_client = esp_mqtt_client_init(&mqtt_cfg);
|
||||||
|
if(mqtt_client == NULL) {
|
||||||
|
ESP_LOGE(TAG, "esp_mqtt_client_init returned NULL");
|
||||||
|
criticalErrorFlag = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_mqtt_client_register_event(mqtt_client, (esp_mqtt_event_id_t)ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
|
||||||
|
|
||||||
|
esp_mqtt_client_start(mqtt_client);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mqtt_publish(void) {}
|
||||||
|
|
||||||
|
void mqttTask(void *pvParameters) {
|
||||||
|
ESP_LOGV(TAG, "Task started");
|
||||||
|
|
||||||
|
ESP_LOGV(TAG, "Waiting for Wi-Fi before initializing server");
|
||||||
|
xEventGroupWaitBits(connectivity_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdTRUE, portMAX_DELAY);
|
||||||
|
ESP_LOGV(TAG, "Server initialized");
|
||||||
|
|
||||||
|
mqtt_init();
|
||||||
|
ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
|
||||||
|
|
||||||
|
for(;;) {
|
||||||
|
ESP_LOGV(TAG, "Checking Wi-Fi connection...");
|
||||||
|
EventBits_t bits = xEventGroupWaitBits(connectivity_event_group, WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT, pdFALSE, pdTRUE, pdMS_TO_TICKS(MQTT_TASK_TIMEOUT_MS));
|
||||||
|
|
||||||
|
if ((bits & (WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) == (WIFI_CONNECTED_BIT | MQTT_CONNECTED_BIT)) {
|
||||||
|
ESP_LOGV(TAG, "Wi-Fi and MQTT up");
|
||||||
|
|
||||||
|
uint8_t params[3] = {0};
|
||||||
|
uint8_t param_len = 0;
|
||||||
|
|
||||||
|
uint8_t i2c_cmd = 0;
|
||||||
|
uint8_t slave_addr = DEFAULT_SLAVE_ADDR;
|
||||||
|
|
||||||
|
char *action = strtok(rx_buffer, ":");
|
||||||
|
char *target = strtok(NULL, ":");
|
||||||
|
char *value = strtok(NULL, ":");
|
||||||
|
char *slave = strtok(NULL, ":");
|
||||||
|
|
||||||
|
if(slave) slave_addr = (uint8_t)strtol(slave, NULL, 0);
|
||||||
|
|
||||||
|
if(action && target) {
|
||||||
|
if (strcmp(action, "READ") == 0) {
|
||||||
|
if (strcmp(target, "moisture") == 0) i2c_cmd = CMD_GET_MOISTURE;
|
||||||
|
else if (strcmp(target, "eeprom") == 0 && value) {
|
||||||
|
if (strcmp(value, "sensor_reads") == 0) i2c_cmd = CMD_GET_SENSOR_READS;
|
||||||
|
else if (strcmp(value, "moisture_min") == 0) i2c_cmd = CMD_GET_MOISTURE_MIN;
|
||||||
|
else if (strcmp(value, "moisture_max") == 0) i2c_cmd = CMD_GET_MOISTURE_MAX;
|
||||||
|
}
|
||||||
|
} else if (strcmp(action, "WRITE") == 0) {
|
||||||
|
if (strcmp(target, "force") == 0) i2c_cmd = CMD_FORCE_PUMP;
|
||||||
|
else if (strcmp(target, "eeprom") == 0 && value) {
|
||||||
|
if (strncmp(value, "params:", 7) == 0) {
|
||||||
|
sscanf(value + 7, "%hhu,%hhu,%hhu", ¶ms[0], ¶ms[1], ¶ms[2]);
|
||||||
|
i2c_cmd = CMD_SET_PARAMS;
|
||||||
|
param_len = 3;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
uint8_t v = (uint8_t)atoi(value);
|
||||||
|
|
||||||
|
if (strcmp(value, "sensor_reads") == 0) {
|
||||||
|
i2c_cmd = CMD_SET_SENSOR_READS;
|
||||||
|
params[0] = v;
|
||||||
|
param_len = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
uint32_t packet = (i2c_cmd<<24) | (slave_addr<<16) | params[0];
|
||||||
|
xTaskNotify(i2c_task, packet, eSetValueWithOverwrite);
|
||||||
|
} else ESP_LOGW(TAG, "Wi-Fi or MQTT down");
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK(esp_task_wdt_reset());
|
||||||
|
ESP_LOGV(TAG, "Task reset");
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(3000));
|
||||||
|
}
|
||||||
|
ESP_LOGW(TAG, "Task exited");
|
||||||
|
}
|
||||||
@@ -6,55 +6,51 @@
|
|||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
#include "main.hpp"
|
#include "main.hpp"
|
||||||
#include "wifi-credentials.h"
|
#include "config.h"
|
||||||
|
|
||||||
#define WIFI_TIMEOUT_TICKS pdMS_TO_TICKS(8000)
|
#define WIFI_TIMEOUT_MS 4500
|
||||||
|
|
||||||
static const char* TAG = "WIFI";
|
static const char* TAG = "WIFI";
|
||||||
const int WIFI_CONNECTED_BIT = BIT0;
|
|
||||||
|
|
||||||
EventGroupHandle_t wifi_event_group;
|
|
||||||
|
|
||||||
static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
|
static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) {
|
||||||
if (event_base == WIFI_EVENT) {
|
if(event_base == WIFI_EVENT) {
|
||||||
switch (event_id) {
|
switch(event_id) {
|
||||||
case WIFI_EVENT_STA_START:
|
case WIFI_EVENT_STA_START:
|
||||||
ESP_LOGI(TAG, "Wi-Fi process started, connecting...");
|
ESP_LOGV(TAG, "Process started, connecting...");
|
||||||
|
|
||||||
esp_wifi_connect();
|
esp_wifi_connect();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WIFI_EVENT_STA_DISCONNECTED: {
|
case WIFI_EVENT_STA_DISCONNECTED: {
|
||||||
wifi_event_sta_disconnected_t* disconn = (wifi_event_sta_disconnected_t*) event_data;
|
wifi_event_sta_disconnected_t* disconn =(wifi_event_sta_disconnected_t*) event_data;
|
||||||
ESP_LOGW(TAG, "Disconnected, reason: %d", disconn->reason);
|
ESP_LOGW(TAG, "Disconnected, reason: %d", disconn->reason);
|
||||||
|
|
||||||
xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT);
|
xEventGroupClearBits(connectivity_event_group, WIFI_CONNECTED_BIT);
|
||||||
|
|
||||||
esp_wifi_connect();
|
esp_wifi_connect();
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(2000));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
else if(event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||||
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
|
ip_event_got_ip_t* event =(ip_event_got_ip_t*) event_data;
|
||||||
ESP_LOGI(TAG, "Got IP: " IPSTR, IP2STR(&event->ip_info.ip));
|
ESP_LOGD(TAG, "Got IP: " IPSTR, IP2STR(&event->ip_info.ip));
|
||||||
xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
|
|
||||||
|
xEventGroupSetBits(connectivity_event_group, WIFI_CONNECTED_BIT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void wifi_init_sta(void) {
|
static void wifi_init_sta(void) {
|
||||||
esp_err_t nvs_err = nvs_flash_init();
|
esp_err_t nvs_err = nvs_flash_init();
|
||||||
|
|
||||||
if (nvs_err == ESP_ERR_NVS_NO_FREE_PAGES || nvs_err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
if(nvs_err == ESP_ERR_NVS_NO_FREE_PAGES || nvs_err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||||
ESP_ERROR_CHECK(nvs_flash_init());
|
ESP_ERROR_CHECK(nvs_flash_init());
|
||||||
}
|
}
|
||||||
|
|
||||||
wifi_event_group = xEventGroupCreate();
|
|
||||||
|
|
||||||
ESP_ERROR_CHECK(esp_netif_init());
|
ESP_ERROR_CHECK(esp_netif_init());
|
||||||
|
|
||||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||||
|
|
||||||
esp_netif_create_default_wifi_sta();
|
esp_netif_create_default_wifi_sta();
|
||||||
@@ -77,16 +73,15 @@ static void wifi_init_sta(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void wifiTask(void *pvParameters) {
|
void wifiTask(void *pvParameters) {
|
||||||
ESP_LOGI(TAG, "WiFi task started");
|
ESP_LOGV(TAG, "Task started");
|
||||||
|
|
||||||
wifi_init_sta();
|
wifi_init_sta();
|
||||||
ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
|
ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
|
||||||
|
|
||||||
for(;;) {
|
for(;;) {
|
||||||
EventBits_t bits = xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, pdFALSE, pdTRUE, WIFI_TIMEOUT_TICKS);
|
|
||||||
|
|
||||||
ESP_ERROR_CHECK(esp_task_wdt_reset());
|
ESP_ERROR_CHECK(esp_task_wdt_reset());
|
||||||
ESP_LOGI(TAG, "Wi-Fi task reset");
|
ESP_LOGV(TAG, "Task reset");
|
||||||
vTaskDelay(pdMS_TO_TICKS(4000));
|
vTaskDelay(pdMS_TO_TICKS(WIFI_TIMEOUT_MS));
|
||||||
}
|
}
|
||||||
|
ESP_LOGW(TAG, "Task exited");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user