ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches
configuration.cpp
Go to the documentation of this file.
1#include "configuration.hpp"
2
3#include <ArduinoLog.h>
5#include <string.h>
6
8static UserConfiguration config = UserConfiguration_init_default;
9
10void setConfig(const UserConfiguration& new_config) {
11 // deeeeep copy
12 config.logger_id = new_config.logger_id;
13 config.cell_id = new_config.cell_id;
14 config.Upload_method = new_config.Upload_method;
15 config.Upload_interval = new_config.Upload_interval;
16 config.enabled_sensors_count = new_config.enabled_sensors_count;
17 memcpy(config.enabled_sensors, new_config.enabled_sensors,
18 sizeof(config.enabled_sensors));
19 config.Voltage_Slope = new_config.Voltage_Slope;
20 config.Voltage_Offset = new_config.Voltage_Offset;
21 config.Current_Slope = new_config.Current_Slope;
22 config.Current_Offset = new_config.Current_Offset;
23 strncpy(config.WiFi_SSID, new_config.WiFi_SSID, sizeof(config.WiFi_SSID));
24 strncpy(config.WiFi_Password, new_config.WiFi_Password,
25 sizeof(config.WiFi_Password));
26 strncpy(config.API_Endpoint_URL, new_config.API_Endpoint_URL,
27 sizeof(config.API_Endpoint_URL));
28 config.API_Endpoint_Port = new_config.API_Endpoint_Port;
29
30 // Check that the user config can be encoded/decoded?? (John)
31 uint8_t buffer[UserConfiguration_size];
32 size_t message_length = EncodeUserConfiguration(&config, buffer);
33 UserConfiguration decoded_config = UserConfiguration_init_zero;
34
35 if (message_length != -1) {
36 // printEncodedData(buffer, message_length);
37
38 if (DecodeUserConfiguration(buffer, message_length, &decoded_config) == 0) {
39 // printDecodedConfig(&decoded_config);
40 } else {
41 Log.errorln("Failed to decode the configuration");
42 }
43 } else {
44 Log.errorln("Failed to encode the configuration");
45 }
46}
47
48const UserConfiguration& getConfig() { return config; }
49
50String getConfigJson() {
51 const UserConfiguration& config = getConfig();
52
53 String json = "{";
54 json += "\"logger_id\":" + String(config.logger_id) + ",";
55 json += "\"cell_id\":" + String(config.cell_id) + ",";
56 json += "\"upload_method\":\"" +
57 String(config.Upload_method == Uploadmethod_WiFi ? "WiFi" : "LoRa") +
58 "\",";
59 json += "\"upload_interval\":" + String(config.Upload_interval) + ",";
60
61 // Enabled sensors
62 json += "\"enabled_sensors\":[";
63 for (uint8_t i = 0; i < config.enabled_sensors_count; i++) {
64 if (i > 0) json += ",";
65 switch (config.enabled_sensors[i]) {
66 case EnabledSensor_Voltage:
67 json += "\"Voltage\"";
68 break;
69 case EnabledSensor_Current:
70 json += "\"Current\"";
71 break;
72 case EnabledSensor_Teros12:
73 json += "\"Teros12\"";
74 break;
75 case EnabledSensor_Teros21:
76 json += "\"Teros21\"";
77 break;
78 case EnabledSensor_BME280:
79 json += "\"BME280\"";
80 break;
81 case EnabledSensor_Phytos31:
82 json += "\"Phytos31\"";
83 break;
84 case EnabledSensor_SEN0308:
85 json += "\"SEN0308\"";
86 break;
87 case EnabledSensor_SEN0257:
88 json += "\"SEN0257\"";
89 break;
90 case EnabledSensor_YFS210C:
91 json += "\"YFS210C\"";
92 break;
93 case EnabledSensor_PCAP02:
94 json += "\"PCAP02\"";
95 break;
96 default:
97 break;
98 }
99 }
100 json += "],";
101
102 char calib_buffer[32];
103 snprintf(calib_buffer, sizeof(calib_buffer), "%g", config.Voltage_Slope);
104 json += "\"calibration_v_slope\":" + String(calib_buffer) + ",";
105 snprintf(calib_buffer, sizeof(calib_buffer), "%g", config.Voltage_Offset);
106 json += "\"calibration_v_offset\":" + String(calib_buffer) + ",";
107 snprintf(calib_buffer, sizeof(calib_buffer), "%g", config.Current_Slope);
108 json += "\"calibration_i_slope\":" + String(calib_buffer) + ",";
109 snprintf(calib_buffer, sizeof(calib_buffer), "%g", config.Current_Offset);
110 json += "\"calibration_i_offset\":" + String(calib_buffer) + ",";
111
112 // WiFi settings
113 json += "\"wifi_ssid\":\"" + String(config.WiFi_SSID) + "\",";
114 // Do not send password
115 // json += "\"wifi_password\":\"" + String(config.WiFi_Password) + "\",";
116 json += "\"api_endpoint_url\":\"" + String(config.API_Endpoint_URL) + "\"";
117
118 json += "}";
119
120 return json;
121}
122
123void printConfig(const UserConfiguration& pconfig) {
124 Log.noticeln(" ============ Configuration Details ============");
125 Log.noticeln(" Logger ID: %u", pconfig.logger_id);
126 Log.noticeln(" Cell ID: %u", pconfig.cell_id);
127 Log.noticeln(" Upload Method: %s",
128 pconfig.Upload_method == Uploadmethod_LoRa ? "LoRa" : "WiFi");
129 Log.noticeln(" Upload Interval: %u sec", pconfig.Upload_interval);
130
131 Log.noticeln(" Enabled Sensors (%d):", pconfig.enabled_sensors_count);
132 for (int i = 0; i < pconfig.enabled_sensors_count; i++) {
133 const char* sensor_name = "Unknown";
134 switch (pconfig.enabled_sensors[i]) {
135 case EnabledSensor_Voltage:
136 sensor_name = "Voltage";
137 break;
138 case EnabledSensor_Current:
139 sensor_name = "Current";
140 break;
141 case EnabledSensor_Teros12:
142 sensor_name = "Teros12";
143 break;
144 case EnabledSensor_Teros21:
145 sensor_name = "Teros21";
146 break;
147 case EnabledSensor_BME280:
148 sensor_name = "BME280";
149 break;
150 case EnabledSensor_Phytos31:
151 sensor_name = "Phytos31";
152 break;
153 case EnabledSensor_SEN0308:
154 sensor_name = "SEN0308";
155 break;
156 case EnabledSensor_SEN0257:
157 sensor_name = "SEN0257";
158 break;
159 case EnabledSensor_YFS210C:
160 sensor_name = "YFS210C";
161 break;
162 case EnabledSensor_PCAP02:
163 sensor_name = "PCAP02";
164 break;
165 }
166 Log.noticeln(" - %s", sensor_name);
167 }
168
169 // NOTE(jtmadden): This doesn't print all the precision
170 Log.noticeln(" Calibration Data:");
171 Log.noticeln(" Voltage Slope: %D", pconfig.Voltage_Slope);
172 Log.noticeln(" Voltage Offset: %D", pconfig.Voltage_Offset);
173 Log.noticeln(" Current Slope: %D", pconfig.Current_Slope);
174 Log.noticeln(" Current Offset: %D", pconfig.Current_Offset);
175
176 Log.noticeln(" WiFi Settings:");
177 Log.noticeln(" SSID: %s", pconfig.WiFi_SSID);
178 Log.noticeln(" Password: %s", pconfig.WiFi_Password);
179 Log.noticeln(" API Endpoint: %s", pconfig.API_Endpoint_URL);
180 Log.noticeln(" =============================");
181}
182
void printReceivedConfig()
Print the current user configuration to the log.
void printConfig(const UserConfiguration &pconfig)
Print a user config.
String getConfigJson()
Get the current configuration as a JSON string.
void setConfig(const UserConfiguration &new_config)
Set the user configuration.
const UserConfiguration & getConfig()
Get a copy of the current user configuration.
uint8_t buffer[32]
uint8_t i
int DecodeUserConfiguration(const uint8_t *data, const size_t len, UserConfiguration *config)
Decodes user configuration data.
Definition transcoder.c:376
size_t EncodeUserConfiguration(const UserConfiguration *config, uint8_t *buffer)
Encodes user configuration data.
Definition transcoder.c:361
Library for encoding/decoding protobuf messages.