Compare commits
2 Commits
master
..
a8ec86f2ef
| Author | SHA1 | Date | |
|---|---|---|---|
|
a8ec86f2ef
|
|||
|
4233fefd2e
|
@@ -1,26 +0,0 @@
|
|||||||
CompileFlags:
|
|
||||||
Add: [
|
|
||||||
-DSSIZE_MAX,
|
|
||||||
-DLWIP_NO_UNISTD_H=1,
|
|
||||||
-Dssize_t=long,
|
|
||||||
-D_SSIZE_T_DECLARED,
|
|
||||||
-Wno-unknown-warning-option
|
|
||||||
]
|
|
||||||
Remove: [
|
|
||||||
-mlong-calls,
|
|
||||||
-fno-tree-switch-conversion,
|
|
||||||
-mtext-section-literals,
|
|
||||||
-mlongcalls,
|
|
||||||
-fstrict-volatile-bitfields,
|
|
||||||
-free,
|
|
||||||
-fipa-pta,
|
|
||||||
-march=*,
|
|
||||||
-mabi=*,
|
|
||||||
-mcpu=*
|
|
||||||
]
|
|
||||||
Diagnostics:
|
|
||||||
Suppress:
|
|
||||||
- pp_including_mainfile_in_preamble
|
|
||||||
- pp_expr_bad_token_start_expr
|
|
||||||
- redefinition_different_typedef
|
|
||||||
- main_returns_nonint
|
|
||||||
+16
-12
@@ -1,10 +1,12 @@
|
|||||||
* Muraqib
|
* Muraqib
|
||||||
|
|
||||||
CO2 sensor that collects and sends its data via MQTT to a server. The board of the sensor is the Wemos D1 mini ESP32 and the sensor is a SCD41.
|
Temperature and humidity sensor that sends its data via MQTT. The project is implemented using a Wemos D1 mini board with the following components:
|
||||||
|
|
||||||
|
- DHT22 temperature and humidity sensor
|
||||||
|
- Battery shield
|
||||||
|
- 3.7V 1000mAh Lithium battery
|
||||||
|
|
||||||
#+ATTR_HTML: :width 40%
|
The sensor captures the data every 5 minutes and sends it to a MQTT broker and then enters deep sleep, it is important to save power as the device runs off a battery. This sensor is part of a project to grow mushrooms in a controlled environment.
|
||||||
[[./board.jpg]]
|
|
||||||
|
|
||||||
** Pinout of the board
|
** Pinout of the board
|
||||||
|
|
||||||
@@ -12,8 +14,9 @@ CO2 sensor that collects and sends its data via MQTT to a server. The board of t
|
|||||||
[[./pinout.png]]
|
[[./pinout.png]]
|
||||||
|
|
||||||
** Dependencies
|
** Dependencies
|
||||||
- [[https://github.com/Sensirion/arduino-i2c-scd4x][Sensirion I²C SCD4X Arduino Library]]
|
- [[https://github.com/adafruit/DHT-sensor-library][Adafruit DHT sensor library]]
|
||||||
- [[https://github.com/marvinroger/async-mqtt-client][Async MQTT client]]
|
- [[https://github.com/adafruit/Adafruit_Sensor][Adafruit Unified Sensor Driver]]
|
||||||
|
- [[https://github.com/knolleary/pubsubclient][PubSubClient]]
|
||||||
- [[https://github.com/bblanchon/ArduinoJson][ArduinoJSON]]
|
- [[https://github.com/bblanchon/ArduinoJson][ArduinoJSON]]
|
||||||
** Configuration
|
** Configuration
|
||||||
|
|
||||||
@@ -36,19 +39,20 @@ The file needs to be renamed to config.json
|
|||||||
|
|
||||||
The software uses the Arduino framework and the development environment of [[https://platformio.org/][PlatformIO]], which offers better tooling than the official Arduino IDE.
|
The software uses the Arduino framework and the development environment of [[https://platformio.org/][PlatformIO]], which offers better tooling than the official Arduino IDE.
|
||||||
|
|
||||||
*** Generate compilation database
|
1. Upload the configuration file to the board:
|
||||||
#+begin_src sql
|
|
||||||
pio run -t compiledb
|
|
||||||
#+end_src
|
|
||||||
*** Upload the configuration file to the board
|
|
||||||
#+begin_src shell
|
#+begin_src shell
|
||||||
pio run -t uploadfs
|
pio run -t uploadfs
|
||||||
#+end_src
|
#+end_src
|
||||||
*** Compile the project
|
|
||||||
|
2. Compile the project
|
||||||
|
|
||||||
#+begin_src shell
|
#+begin_src shell
|
||||||
pio run -t compile
|
pio run -t compile
|
||||||
#+end_src
|
#+end_src
|
||||||
*** Upload firmware
|
|
||||||
|
3. Upload firmware
|
||||||
|
|
||||||
#+begin_src shell
|
#+begin_src shell
|
||||||
pio run -t upload
|
pio run -t upload
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"ssid": "Zingarata",
|
||||||
|
"psk": "shlomoshekelson42",
|
||||||
|
"mqtt_host": "192.168.13.2",
|
||||||
|
"mqtt_user": "homeostasis",
|
||||||
|
"mqtt_password": "8fbb169b0ca25ef66dfec6f1cbc6076e95960369",
|
||||||
|
"mqtt_port": 1883,
|
||||||
|
"mqtt_topic": "homeostasis/room",
|
||||||
|
"device_id": "homeostasis",
|
||||||
|
"sleep_time": 15,
|
||||||
|
"connection_attempts": 120
|
||||||
|
}
|
||||||
+2
-1
@@ -7,5 +7,6 @@
|
|||||||
"mqtt_port": 1883,
|
"mqtt_port": 1883,
|
||||||
"mqtt_topic": "",
|
"mqtt_topic": "",
|
||||||
"device_id": "",
|
"device_id": "",
|
||||||
"sleep_time": 30
|
"sleep_time": 30,
|
||||||
|
"connection_attempts": 60
|
||||||
}
|
}
|
||||||
|
|||||||
Generated
+3
-3
@@ -2,11 +2,11 @@
|
|||||||
"nodes": {
|
"nodes": {
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1773046814,
|
"lastModified": 1744646643,
|
||||||
"narHash": "sha256-3CEw64UyzEk5QjfbcXNIl4TfmIpa2oY+duuo6aiawcU=",
|
"narHash": "sha256-gswCvHOZvcHtY/5fIWYlz/VxMAIUQ6XFRZB/rxlH3OA=",
|
||||||
"owner": "NixOS",
|
"owner": "NixOS",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "0c6c0dd2469abaa216599bb19bbf77a328af6564",
|
"rev": "a6165272f0074877f7c0c45fe6eba86d1e18e80d",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
|||||||
+14
-11
@@ -1,24 +1,27 @@
|
|||||||
#ifndef CONFIG_H_
|
#ifndef CONFIG_H_
|
||||||
#define CONFIG_H_
|
#define CONFIG_H_
|
||||||
|
|
||||||
|
#include "FS.h"
|
||||||
|
#include "LittleFS.h"
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
const char *ssid;
|
const char *ssid;
|
||||||
const char *psk;
|
const char *psk;
|
||||||
const char *mqtt_host;
|
const char *mqtt_host;
|
||||||
const char *mqtt_user;
|
const char *mqtt_user;
|
||||||
const char *mqtt_password;
|
const char *mqtt_password;
|
||||||
const char *topic;
|
const char *topic;
|
||||||
const char *device_id;
|
const char *device_id;
|
||||||
int mqtt_port;
|
int mqtt_port;
|
||||||
unsigned long sleep_time;
|
long sleep_time;
|
||||||
|
int connection_attempts;
|
||||||
} Config;
|
} Config;
|
||||||
|
|
||||||
void initialize_config(Config *config, JsonDocument json);
|
void initialize_config(Config *config, StaticJsonDocument<512> json);
|
||||||
|
|
||||||
bool load_config_file(const char *file_path, Config *config);
|
bool load_config_file(const char *file_path, Config *config);
|
||||||
|
|
||||||
long minutes_to_milliseconds(int minutes);
|
long minutes_to_microseconds(int minutes);
|
||||||
|
|
||||||
#endif // CONFIG_H_
|
#endif // CONFIG_H_
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
#ifndef SENSOR_H_
|
|
||||||
#define SENSOR_H_
|
|
||||||
|
|
||||||
#include <SensirionI2cScd4x.h>
|
|
||||||
|
|
||||||
void handle_error(int code, char *msg);
|
|
||||||
|
|
||||||
void initialize_sensor(SensirionI2cScd4x &sensor, int error_code,
|
|
||||||
char *error_msg);
|
|
||||||
|
|
||||||
void read_values(SensirionI2cScd4x &sensor, float *data, int error_code,
|
|
||||||
char *error_msg);
|
|
||||||
|
|
||||||
#endif // SENSOR_H_
|
|
||||||
+8
-23
@@ -2,30 +2,15 @@
|
|||||||
#define WLAN_H
|
#define WLAN_H
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include <AsyncMqttClient.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <Ticker.h>
|
#include <PubSubClient.h>
|
||||||
#include <WiFi.h>
|
|
||||||
|
|
||||||
static AsyncMqttClient mqtt_client;
|
|
||||||
static Ticker mqtt_connection_timer, wlan_connection_timer;
|
|
||||||
extern Config *config;
|
|
||||||
|
|
||||||
void initialize_wlan();
|
|
||||||
|
|
||||||
void initialize_mqtt();
|
|
||||||
|
|
||||||
void connect_wlan();
|
|
||||||
|
|
||||||
void connect_mqtt();
|
|
||||||
|
|
||||||
void wlan_connection_handler(WiFiEvent_t event);
|
|
||||||
|
|
||||||
void on_mqtt_connection(bool session);
|
|
||||||
|
|
||||||
void on_mqtt_disconnection(AsyncMqttClientDisconnectReason reason);
|
|
||||||
|
|
||||||
|
void initial_connection(const char *ssid, const char *psk);
|
||||||
|
void connect_wlan(Config *config);
|
||||||
|
void connect_mqtt(PubSubClient &client, Config *config);
|
||||||
|
void disconnect_mqtt(PubSubClient &client, const char *topic);
|
||||||
size_t construct_json(float *data, char *buffer, int buffer_size);
|
size_t construct_json(float *data, char *buffer, int buffer_size);
|
||||||
|
void mqtt_transfer(PubSubClient &client, Config *config, float *data);
|
||||||
void mqtt_transfer(float *data);
|
void enter_deep_sleep(bool wifi_timeout, int sleep_time);
|
||||||
|
|
||||||
#endif /* WLAN_H */
|
#endif /* WLAN_H */
|
||||||
|
|||||||
+6
-7
@@ -9,13 +9,12 @@
|
|||||||
; https://docs.platformio.org/page/projectconf.html
|
; https://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
[env:d1_mini]
|
[env:d1_mini]
|
||||||
platform = espressif32
|
platform = espressif8266
|
||||||
board = wemos_d1_mini32
|
board = d1_mini
|
||||||
board_build.filesystem = littlefs
|
board_build.filesystem = littlefs
|
||||||
framework = arduino
|
framework = arduino
|
||||||
monitor_filters = esp32_exception_decoder
|
|
||||||
lib_deps =
|
lib_deps =
|
||||||
sensirion/Sensirion I2C SCD4x@^1.1.0
|
adafruit/DHT sensor library@^1.4.4
|
||||||
marvinroger/AsyncMqttClient@^0.9.0
|
adafruit/Adafruit Unified Sensor@^1.1.9
|
||||||
bblanchon/ArduinoJson@^7.4.1
|
knolleary/PubSubClient@^2.8
|
||||||
|
bblanchon/ArduinoJson@^6.21.1
|
||||||
|
|||||||
+23
-23
@@ -1,30 +1,30 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "LittleFS.h"
|
|
||||||
|
|
||||||
void initialize_config(Config *config, JsonDocument json) {
|
void initialize_config(Config *config, StaticJsonDocument<512> json) {
|
||||||
config->ssid = strdup(json["ssid"]);
|
config->ssid = strdup(json["ssid"]);
|
||||||
config->psk = strdup(json["psk"]);
|
config->psk = strdup(json["psk"]);
|
||||||
config->mqtt_host = strdup(json["mqtt_host"]);
|
config->mqtt_host = strdup(json["mqtt_host"]);
|
||||||
config->mqtt_user = strdup(json["mqtt_user"]);
|
config->mqtt_user = strdup(json["mqtt_user"]);
|
||||||
config->mqtt_password = strdup(json["mqtt_password"]);
|
config->mqtt_password = strdup(json["mqtt_password"]);
|
||||||
config->topic = strdup(json["mqtt_topic"]);
|
config->topic = strdup(json["mqtt_topic"]);
|
||||||
config->device_id = strdup(json["device_id"]);
|
config->device_id = strdup(json["device_id"]);
|
||||||
config->mqtt_port = json["mqtt_port"];
|
config->mqtt_port = json["mqtt_port"];
|
||||||
config->sleep_time = minutes_to_milliseconds(json["sleep_time"]);
|
config->sleep_time = minutes_to_microseconds(json["sleep_time"]);
|
||||||
|
config->connection_attempts = json["connection_attempts"];
|
||||||
}
|
}
|
||||||
|
|
||||||
bool load_config_file(const char *file_path, Config *config) {
|
bool load_config_file(const char *file_path, Config *config) {
|
||||||
if (!LittleFS.begin())
|
if (!LittleFS.begin())
|
||||||
return false;
|
return false;
|
||||||
File config_file = LittleFS.open(file_path, "r");
|
File config_file = LittleFS.open(file_path, "r");
|
||||||
if (!config_file)
|
if (!config_file)
|
||||||
return false;
|
return false;
|
||||||
JsonDocument json;
|
StaticJsonDocument<512> json;
|
||||||
DeserializationError err = deserializeJson(json, config_file);
|
DeserializationError err = deserializeJson(json, config_file);
|
||||||
if (err)
|
if (err)
|
||||||
return false;
|
return false;
|
||||||
initialize_config(config, json);
|
initialize_config(config, json);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
long minutes_to_milliseconds(int minutes) { return (minutes * 6e4); }
|
long minutes_to_microseconds(int minutes) { return (minutes * 6e7); }
|
||||||
|
|||||||
+25
-24
@@ -1,37 +1,38 @@
|
|||||||
#include "sensor.h"
|
#include "config.h"
|
||||||
#include "wlan.h"
|
#include "wlan.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <DHT.h>
|
||||||
|
|
||||||
SensirionI2cScd4x sensor;
|
#define DHTTYPE DHT22
|
||||||
|
#define DHTPIN 4
|
||||||
int error_code;
|
DHT dht(DHTPIN, DHTTYPE);
|
||||||
char error_msg[64];
|
|
||||||
|
|
||||||
const char *config_file_path = "/config.json";
|
const char *config_file_path = "/config.json";
|
||||||
Config *config;
|
Config *config;
|
||||||
|
WiFiClient wifi_client;
|
||||||
|
PubSubClient mqtt_client(wifi_client);
|
||||||
|
|
||||||
float data[3];
|
bool check_valid_value(float value) {
|
||||||
unsigned long previous_millis = 0;
|
return (!isnan(value) && value >= 0 && value <= 100);
|
||||||
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
config = (Config *)malloc(sizeof(Config));
|
dht.begin();
|
||||||
|
config = (Config *)malloc(sizeof(Config));
|
||||||
if (!load_config_file(config_file_path, config))
|
if (!load_config_file(config_file_path, config))
|
||||||
Serial.println("ERROR: The config file could not be loaded");
|
Serial.println("ERROR: The config file could not be loaded");
|
||||||
|
connect_wlan(config);
|
||||||
initialize_wlan();
|
|
||||||
initialize_mqtt();
|
|
||||||
connect_wlan();
|
|
||||||
initialize_sensor(sensor, error_code, error_msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
unsigned long current_millis = millis();
|
float temperature = dht.readTemperature();
|
||||||
|
float humidity = dht.readHumidity();
|
||||||
if (current_millis - previous_millis >= config->sleep_time) {
|
float data[2] = {temperature, humidity};
|
||||||
previous_millis = current_millis;
|
if (check_valid_value(temperature) && check_valid_value(humidity)) {
|
||||||
read_values(sensor, data, error_code, error_msg);
|
mqtt_transfer(mqtt_client, config, data);
|
||||||
mqtt_transfer(data);
|
}
|
||||||
}
|
disconnect_mqtt(mqtt_client, config->topic);
|
||||||
|
free(config);
|
||||||
|
enter_deep_sleep(false, config->sleep_time);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,49 +0,0 @@
|
|||||||
#include "sensor.h"
|
|
||||||
#include <Wire.h>
|
|
||||||
|
|
||||||
// TODO Trigger a reset when an error occurs
|
|
||||||
void handle_error(int code, char *msg) {
|
|
||||||
if (code) {
|
|
||||||
errorToString(code, msg, sizeof msg);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void initialize_sensor(SensirionI2cScd4x &sensor, int error_code,
|
|
||||||
char *error_msg) {
|
|
||||||
Wire.begin();
|
|
||||||
sensor.begin(Wire, SCD41_I2C_ADDR_62);
|
|
||||||
Serial.println("Starting sensor initialization");
|
|
||||||
|
|
||||||
error_code = sensor.wakeUp();
|
|
||||||
handle_error(error_code, error_msg);
|
|
||||||
error_code = sensor.stopPeriodicMeasurement();
|
|
||||||
handle_error(error_code, error_msg);
|
|
||||||
error_code = sensor.reinit();
|
|
||||||
handle_error(error_code, error_msg);
|
|
||||||
error_code = sensor.startPeriodicMeasurement();
|
|
||||||
handle_error(error_code, error_msg);
|
|
||||||
|
|
||||||
Serial.println("The sensor was initialized properly");
|
|
||||||
};
|
|
||||||
|
|
||||||
void read_values(SensirionI2cScd4x &sensor, float *data, int error_code,
|
|
||||||
char *error_msg) {
|
|
||||||
bool data_available = false;
|
|
||||||
short unsigned int co2_concentration = 0.0;
|
|
||||||
|
|
||||||
error_code = sensor.getDataReadyStatus(data_available);
|
|
||||||
handle_error(error_code, error_msg);
|
|
||||||
|
|
||||||
while (!data_available) {
|
|
||||||
delay(100);
|
|
||||||
error_code = sensor.getDataReadyStatus(data_available);
|
|
||||||
handle_error(error_code, error_msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
Serial.println("Data fetched from the sensor");
|
|
||||||
|
|
||||||
error_code = sensor.readMeasurement(co2_concentration, data[1], data[2]);
|
|
||||||
data[0] = co2_concentration;
|
|
||||||
handle_error(error_code, error_msg);
|
|
||||||
}
|
|
||||||
+48
-47
@@ -1,61 +1,62 @@
|
|||||||
#include "wlan.h"
|
#include "wlan.h"
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
void initialize_wlan() { WiFi.onEvent(wlan_connection_handler); }
|
void initial_connection(const char *ssid, const char *psk) {
|
||||||
|
WiFi.begin(ssid, psk);
|
||||||
void initialize_mqtt() {
|
WiFi.persistent(true);
|
||||||
mqtt_client.onConnect(on_mqtt_connection);
|
WiFi.setAutoConnect(true);
|
||||||
mqtt_client.onDisconnect(on_mqtt_disconnection);
|
WiFi.setAutoReconnect(true);
|
||||||
mqtt_client.setServer(config->mqtt_host, config->mqtt_port);
|
|
||||||
mqtt_client.setCredentials(config->mqtt_user, config->mqtt_password);
|
|
||||||
Serial.println("MQTT initialization complete");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void connect_wlan() { WiFi.begin(config->ssid, config->psk); }
|
void connect_wlan(Config *config) {
|
||||||
|
if (WiFi.SSID() != config->ssid)
|
||||||
void connect_mqtt() {
|
initial_connection(config->ssid, config->psk);
|
||||||
Serial.println("Connecting to MQTT");
|
int retries = 0;
|
||||||
mqtt_client.connect();
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
if (retries == config->connection_attempts)
|
||||||
|
enter_deep_sleep(true, config->sleep_time);
|
||||||
|
retries++;
|
||||||
|
delay(1000);
|
||||||
|
Serial.print(".");
|
||||||
|
}
|
||||||
|
Serial.println("WiFi connected");
|
||||||
}
|
}
|
||||||
|
|
||||||
void wlan_connection_handler(WiFiEvent_t event) {
|
void connect_mqtt(PubSubClient &client, Config *config) {
|
||||||
Serial.printf("[WiFi-event] event: %d\n", event);
|
if (!client.connected())
|
||||||
switch (event) {
|
client.setServer(config->mqtt_host, config->mqtt_port);
|
||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
if (client.connect(config->device_id, config->mqtt_user,
|
||||||
Serial.println("WiFi connected");
|
config->mqtt_password)) {
|
||||||
connect_mqtt();
|
Serial.println("MQTT connected");
|
||||||
break;
|
client.subscribe(config->topic);
|
||||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
}
|
||||||
Serial.println("WiFi disconnected");
|
|
||||||
mqtt_connection_timer.detach();
|
|
||||||
wlan_connection_timer.once(2, connect_wlan);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void on_mqtt_connection(bool session) { Serial.println("MQTT connected"); };
|
void disconnect_mqtt(PubSubClient &client, const char *topic) {
|
||||||
|
Serial.println("Disconnecting MQTT");
|
||||||
void on_mqtt_disconnection(AsyncMqttClientDisconnectReason reason) {
|
client.unsubscribe(topic);
|
||||||
Serial.println("MQTT disconnected");
|
client.disconnect();
|
||||||
|
|
||||||
if (WiFi.isConnected()) {
|
|
||||||
mqtt_connection_timer.once(2, connect_mqtt);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t construct_json(float *data, char *buffer, int buffer_size) {
|
size_t construct_json(float *data, char *buffer, int buffer_size) {
|
||||||
JsonDocument json;
|
StaticJsonDocument<100> json;
|
||||||
json["co2_concentration"] = data[0];
|
json["temperature"] = data[0];
|
||||||
json["temperature"] = data[1];
|
json["humidity"] = data[1];
|
||||||
json["humidity"] = data[2];
|
size_t payload_size = serializeJson(json, buffer, buffer_size);
|
||||||
size_t payload_size = serializeJson(json, buffer, buffer_size);
|
return payload_size;
|
||||||
return payload_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void mqtt_transfer(float *data) {
|
void mqtt_transfer(PubSubClient &client, Config *config, float *data) {
|
||||||
char buffer[100];
|
char buffer[100];
|
||||||
size_t payload_size = construct_json(data, buffer, 100);
|
connect_mqtt(client, config);
|
||||||
uint16_t response =
|
size_t payload_size = construct_json(data, buffer, 100);
|
||||||
mqtt_client.publish(config->topic, 2, true, buffer, payload_size);
|
client.publish(config->topic, buffer, payload_size);
|
||||||
if (response)
|
Serial.println("Data transferred successfully");
|
||||||
Serial.println("Data transferred successfully");
|
}
|
||||||
|
|
||||||
|
void enter_deep_sleep(bool wifi_timeout, int sleep_time) {
|
||||||
|
Serial.println("Entering deep sleep");
|
||||||
|
if (wifi_timeout)
|
||||||
|
WiFi.disconnect();
|
||||||
|
ESP.deepSleep(sleep_time, WAKE_RF_DEFAULT);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user