esp_garden git · master
Distributed ESP sensor and actuator system (garden)
C++ 70.7% Markdown 18% C 10.7%git clone https://git.christianimmanuel.de/embedded/esp_garden.gitwget https://git.christianimmanuel.de/embedded/esp_garden/archive/esp_garden.tar.gzclient_sensor/main/OLD.client_sensor.ino raw
#include "ClientSensor.h"
#include "esp_system.h"
extern "C" esp_err_t esp_base_mac_addr_set(const uint8_t mac[6]);
// this is going to be a sensor read value!
// RTC_DATA_ATTR is not required than!
RTC_DATA_ATTR uint32_t fake_value_to_send = 0;
// ----------------------------------------------------------------------------
// Setup
// ----------------------------------------------------------------------------
void setup() {
uint32_t setup_start = millis();
DBG_BEGIN();
esp_base_mac_addr_set(PEER_SENSOR);
setup_Timezone();
init_WiFi();
init_EspNow();
DBG_PRINTLN("Requesting time sync...");
if (request_TimeSync()) DBG_PRINTLN("done");
else DBG_PRINTLN("failed (timeout)");
restore_SensorConfigs();
DBG_PRINTF("----------------------\n");
// Add all known configs to an array so we can request thoose from server
if (!all_init_configs_received)
get_AllSensorConfigs();
DBG_PRINTF("----------------------\n");
ensure_Peers();
g_setup_duration = millis() - setup_start;
DBG_PRINTF("Setup duration: %u ms\n", g_setup_duration);
}
// ----------------------------------------------------------------------------
// Loop
// ----------------------------------------------------------------------------
void loop() {
DBG_PRINTF("## Unix time: %lld\n", get_CurrentUnix());
for (size_t config_id = 0; config_id < SENSOR_CONFIG_COUNT; config_id++) {
SensorConfig_s& peer = sensor_configs[config_id];
DBG_PRINTF("LOOP checking config: %s\n", peer.sensor_name);
print_SensorConfig(peer);
if ( is_TimeToReadSensor(config_id) ) {
DBG_PRINTLN("ITS TIME FOR THIS SENSOR!");
/*
* read sensor value
* default sensor configurations can be found in globals.cpp sensor_configs
* the struct SensorConfig_s is defined in globals.h
*/
uint32_t value_to_send = fake_value_to_send;
// set last read time
peer.last_sensor_read = get_CurrentUnix();
peer.next_sensor_read =
get_NextSensorReadFor(sensor_configs[config_id].sensor_name);
// set to true if value says we should pump
bool shall_pump = fake_value_to_send % 2;
send_DataToPumpAndServer(config_id, value_to_send, shall_pump);
fake_value_to_send++;
}
print_SensorConfig(peer);
}
if (g_pending_sensor_configs_count > 0)
process_PendingSensorConfigs();
/*
* Tell server some infos
* how long am i gonna sleep, which configurations have i updated
*/
int64_t next_read = get_NextSensorReadsUnixTime();
send_NextSensorReadToPeer(PEER_SERVER, next_read);
sleep_UntilNextSend(next_read);
}