ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches
example_irrigation.cpp
Go to the documentation of this file.
1
8#include <Arduino.h>
9#include <ArduinoLog.h>
10#include <WiFi.h>
11#include <Wire.h>
12#include <sys/time.h> // For time functions
13
14#include "module_handler.hpp"
16#include "modules/microsd.hpp"
17#include "modules/wifi.hpp"
18
20static const uint8_t dev_addr = 0x20;
22static const int sda_pin = 0;
24static const int scl_pin = 1;
25
26const std::string ssid = "HARE_Lab";
27const std::string password = "";
28
29// create wifi module
31
32// Create irrigation module
33static ModuleIrrigation irrigation;
34
35// External declarations for time initialization flags (defined in
36// irrigation.cpp)
39
45void onReceive(int len) {
46 Log.traceln("onReceive(%d)", len);
47 mh.OnReceive(len);
48}
49
55void onRequest() {
56 Log.traceln("onRequest");
57 mh.OnRequest();
58}
59
61void setup() {
62 // Start serial interface
63 Serial.begin(115200);
64
65 // Create logging interface
66 Log.begin(LOG_LEVEL_TRACE, &Serial);
67 Log.noticeln("ESP32 Starting...");
68
69 // Connect to WiFi
70 Log.noticeln("Connecting to WiFi: %s", ssid.c_str());
71 WiFi.begin(ssid.c_str(), password.c_str());
72
73 int wifi_retries = 0;
74 const int max_wifi_retries = 20; // 10 seconds total
75
76 while (WiFi.status() != WL_CONNECTED && wifi_retries < max_wifi_retries) {
77 delay(500);
78 Serial.print(".");
79 wifi_retries++;
80 }
81
82 if (WiFi.status() != WL_CONNECTED) {
83 Log.errorln("WiFi connection failed!");
84 // Continue anyway - time sync will fail but other functions should work
85 } else {
86 Log.noticeln("");
87 Log.noticeln("WiFi connected");
88 Log.noticeln("IP address: %s", WiFi.localIP().toString().c_str());
89
90 // Configure NTP time synchronization after WiFi is connected
91 Log.noticeln("Configuring NTP time...");
92 configTzTime("UTC", "pool.ntp.org", "time.nist.gov", "time.google.com");
93
94 // Wait a moment for time to sync
95 delay(2000);
96
97 // Check if time is synchronized
98 time_t now = time(nullptr);
99 if (now > 1000000000) {
100 Log.noticeln("Time synchronized: %s", ctime(&now));
102 } else {
103 Log.errorln("Time not synchronized, using system time");
104 time_sync_successful = false;
105 }
106 time_initialized = true;
107 }
108
109 // Setup Web server (doesn't need time sync)
110 SetupServer();
111 Log.noticeln("Web server started");
112
113 Log.noticeln("ents-node esp32 firmware, compiled at %s %s", __DATE__,
114 __TIME__);
115 Log.noticeln("Git SHA: %s", GIT_REV);
116
117 Log.noticeln("Starting i2c interface...");
118
119 // Register irrigation module
120 mh.RegisterModule(&irrigation);
121
122 // start i2c interface
123 Wire.onReceive(onReceive);
124 Wire.onRequest(onRequest);
125 bool i2c_status = Wire.begin(dev_addr, sda_pin, scl_pin, 100000);
126
127 if (i2c_status) {
128 Log.noticeln("Success!");
129 } else {
130 Log.noticeln("Failed!");
131 }
132}
133
135void loop() {
136 HandleClient();
137 irrigation.CheckAutoIrrigation();
138 delay(20);
139}
Interface for the module handler.
void OnReceive(size_t num_bytes)
Arduino I2C onRecieve.
void RegisterModule(Module *module)
Register a module with a message type.
void OnRequest(void)
Arduino I2C onRequest.
Irrigation module for the esp32.
void CheckAutoIrrigation()
bool time_sync_successful
void setup()
bool time_initialized
void onReceive(int len)
Callback for onReceive.
void onRequest()
Callback for onRequest.
const std::string ssid
const std::string password
void loop()
Module for controling irrigation valve.
Module for accessing the microSD card.
Interface for controlling the esp32.
void SetupServer()
Setup endpoints for the webserver.
void HandleClient()
Handle requests from the webserver.
Module for WiFi communication.