-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
623 additions
and
3 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* LoboMQ Broker Example | ||
* This broker example has both topic and subscriber persistence and | ||
* whitelist access control enabled. The log messages are stored on the SD card. | ||
* | ||
* https://github.com/rubnium/LoboMQ | ||
*/ | ||
|
||
#include <LoboMQ/Broker.h> | ||
|
||
void setup() { | ||
Serial.begin(9600); | ||
|
||
//Create whitelist of the devices allowed to connect to the broker | ||
MACAddrList *whitelist = new MACAddrList(); | ||
whitelist->addToList({0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}); //add 1 by 1 | ||
whitelist->addArrayToList((std::vector<String>){ //or multiple at once | ||
"08:B6:1F:BA:04:F8", | ||
"C0:49:EF:CB:99:10" | ||
}); | ||
|
||
//Choose logger: | ||
//a. Serial monitor logger | ||
//Elog *logger = initializeSerialLogger(BROKER, DEBUG); | ||
//b. SD logger. In this case, pins are: | ||
// - CD/Chip Detect: 22 (not used) | ||
// - DO/MISO: 19 | ||
// - SCK/CLK: 18 | ||
// - DI/MOSI: 23 | ||
// - CS: 5 | ||
// - VCC: 3.3V | ||
// - GND: GND | ||
Elog *logger = initializeSDLogger(BROKER, 5, 18, 19, 23, DEBUG); | ||
//c. No logger | ||
//Elog *logger = disableLogger(); | ||
|
||
//Initialize broker | ||
initBroker(whitelist, logger, true, 5); | ||
//Default values are available: | ||
//initBroker(BRO_DEFAULT_WHITELIST, BRO_DEFAULT_LOGGER, BRO_DEFAULT_PERSISTENCE, BRO_DEFAULT_CS_SD_PIN); | ||
} | ||
|
||
void loop() { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.pio | ||
.vscode/.browse.c_cpp.db* | ||
.vscode/c_cpp_properties.json | ||
.vscode/launch.json | ||
.vscode/ipch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* LoboMQ Publisher Example | ||
* This publisher example publishes a random number every 2 seconds. | ||
* | ||
* https://github.com/rubnium/LoboMQ | ||
*/ | ||
|
||
#include <LoboMQ/PubSub.h> | ||
|
||
uint8_t brokerAddr[] = {0x24, 0xDC, 0xC3, 0x9C, 0x7E, 0x40}; //Broker MAC destination | ||
|
||
Elog *_logger; | ||
|
||
void setup() { | ||
Serial.begin(9600); | ||
randomSeed(analogRead(0)); //to generate random numbers | ||
|
||
//Choose logger: | ||
_logger = initializeSerialLogger(PUBLISHER, DEBUG); | ||
//_logger = initializeSDLogger(...); | ||
//_logger = disableLogger(); | ||
|
||
_logger->log(INFO, "Started publisher board on %s!", WiFi.macAddress().c_str()); | ||
} | ||
|
||
void loop() { | ||
int numberExample = random(101); //generates random number | ||
publish(brokerAddr, "topic1", &numberExample, sizeof(numberExample), _logger); | ||
sleep(2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[platformio] | ||
src_dir = . | ||
|
||
[env:run_test] | ||
platform = espressif32 | ||
board = esp32doit-devkit-v1 | ||
framework = arduino | ||
|
||
lib_ldf_mode = deep | ||
lib_deps = | ||
adafruit/Adafruit Unified Sensor@^1.1.14 | ||
adafruit/DHT sensor library@^1.4.6 | ||
symlink://../.. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.pio | ||
.vscode/.browse.c_cpp.db* | ||
.vscode/c_cpp_properties.json | ||
.vscode/launch.json | ||
.vscode/ipch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* LoboMQ Sensor Demo - Broker | ||
* This example broker has topic and subscriber persistence enabled and | ||
* whitelist access control disabled. The log messages are printed on the serial | ||
* monitor. | ||
* | ||
* SD module pins: | ||
* - CD/Chip Detect: 22 (not used) | ||
* - DO/MISO: 19 | ||
* - SCK/CLK: 18 | ||
* - DI/MOSI: 23 | ||
* - CS: 5 | ||
* - VCC: 3.3V | ||
* - GND: GND | ||
* | ||
* https://github.com/rubnium/LoboMQ | ||
*/ | ||
|
||
#include <LoboMQ/Broker.h> | ||
|
||
void setup() { | ||
Serial.begin(9600); | ||
Elog *logger = initializeSerialLogger(BROKER, DEBUG); | ||
|
||
initBroker(BRO_DEFAULT_WHITELIST, logger, true, 5); | ||
} | ||
|
||
void loop() { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[platformio] | ||
src_dir = . | ||
|
||
[env:sensordemo_bro] | ||
platform = espressif32 | ||
board = esp32doit-devkit-v1 | ||
framework = arduino | ||
|
||
lib_ldf_mode = deep | ||
lib_deps = | ||
symlink://../.. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.pio | ||
.vscode/.browse.c_cpp.db* | ||
.vscode/c_cpp_properties.json | ||
.vscode/launch.json | ||
.vscode/ipch |
118 changes: 118 additions & 0 deletions
118
library/examples/SensorDemo_Publisher/SensorDemo_Publisher.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* LoboMQ Sensor Demo - Publisher | ||
* This publisher example reads temperature, humidity, and potentiometer values | ||
* every second. If any of these values change from the previous reading, the | ||
* new values are packaged and published to the broker under their respective | ||
* topics. | ||
* | ||
* Pins: | ||
* - DHT11: 32 | ||
* - Potentiometer: 34 | ||
* | ||
* https://github.com/rubnium/LoboMQ | ||
*/ | ||
|
||
#include <LoboMQ/PubSub.h> | ||
#include <DHT.h> | ||
|
||
//Sensors configuration | ||
|
||
#define DHTPIN 32 | ||
#define DHTTYPE DHT11 | ||
DHT dht(DHTPIN, DHTTYPE); | ||
#define POT 34 | ||
|
||
//Structs of the different payloads in the messages | ||
//Must be the same on the subscriber | ||
|
||
typedef struct { | ||
int temperature100; | ||
int humidity100; | ||
} DHTValues; | ||
|
||
typedef struct { | ||
int potValue; | ||
} PotValue; | ||
|
||
uint8_t brokerAddr[] = {0x24, 0xDC, 0xC3, 0x9C, 0x7E, 0x40}; //Broker MAC destination | ||
char *topicDht = "sensor/dht"; //Topic of temperature and humidity | ||
char *topicPot = "sensor/pot"; //Topic of potentiometer | ||
|
||
Elog *_logger; | ||
int lastTemp100; //Last stored temperature value | ||
int lastHum100; //Last stored humidity value | ||
int lastPotVal; //Last stored potentiometer value | ||
|
||
void setup() { | ||
Serial.begin(9600); | ||
_logger = initializeSerialLogger(PUBLISHER, INFO); | ||
|
||
//Initialize sensors | ||
dht.begin(); | ||
|
||
//Initialize last values | ||
lastTemp100 = -1; | ||
lastHum100 = -1; | ||
lastPotVal = -1; | ||
|
||
WiFi.mode(WIFI_STA); | ||
//Initialize ESP-NOW | ||
if (esp_now_init() != ESP_OK) { | ||
_logger->log(ERROR, "Couldn't initialize ESP-NOW"); | ||
exit(1); | ||
} | ||
|
||
_logger->log(INFO, "Started sensor publisher on %s!", WiFi.macAddress().c_str()); | ||
} | ||
|
||
void loop() { | ||
//Read DHT value | ||
float temperature = dht.readTemperature(); //reads temperature in Celsius | ||
float humidity = dht.readHumidity(); | ||
|
||
//If the values aren't correct, DHT sensor isn't working | ||
if (isnan(temperature) || isnan(humidity)) { | ||
_logger->log(ERROR, "Failed to read from DHT sensor"); | ||
} else { | ||
//Round the values to 2 decimals and multiply by 100 to store as integers | ||
int temperature100 = round(temperature * 100); | ||
int humidity100 = round(humidity * 100); | ||
bool dhtChanged = false; | ||
//If the temperature is different than the previous, it is processed | ||
if (fabs(temperature100 - lastTemp100) >= 10) { | ||
_logger->log(INFO, "Temperature: %.2fºC", (temperature100/100.0)); | ||
lastTemp100 = temperature100; //Stores the new temperature | ||
dhtChanged = true; | ||
} | ||
//If the humidity is different than the previous, it is processed | ||
if (fabs(humidity100 - lastHum100) >= 10) { | ||
_logger->log(INFO, "Humidity: %.2f%%", (humidity100/100.0)); | ||
lastHum100 = humidity100; //Stores the new humidity | ||
dhtChanged = true; | ||
} | ||
//If some of the values changed, they are published | ||
if (dhtChanged) { | ||
DHTValues dhtValues; | ||
dhtValues.temperature100 = temperature100; | ||
dhtValues.humidity100 = humidity100; | ||
if (publish(brokerAddr, topicDht, &dhtValues, sizeof(dhtValues), _logger) != LMQ_ERR_SUCCESS) | ||
_logger->log(ERROR, "Failed to publish DHT values"); | ||
|
||
Serial.println(sizeof(dhtValues)); | ||
} | ||
} | ||
|
||
int potVal = analogRead(POT); //Read potentiometer value in range [0, 4095] | ||
potVal = map(potVal, 0, 4095, 0, 100); //Transforms to range [0,100] | ||
//If the value is different than the previous, it is processed | ||
if (fabs(potVal - lastPotVal) >= 2) { | ||
lastPotVal = potVal; | ||
_logger->log(INFO, "Value: %d%%", potVal); | ||
PotValue potValue; | ||
potValue.potValue = potVal; | ||
if (publish(brokerAddr, topicPot, &potValue, sizeof(potValue), _logger) != LMQ_ERR_SUCCESS) | ||
_logger->log(ERROR, "Failed to publish potentiometer value"); | ||
} | ||
|
||
delay(1000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[platformio] | ||
src_dir = . | ||
|
||
[env:sensordemo_pub] | ||
platform = espressif32 | ||
board = esp32doit-devkit-v1 | ||
framework = arduino | ||
|
||
lib_ldf_mode = deep | ||
lib_deps = | ||
adafruit/Adafruit Unified Sensor@^1.1.14 | ||
adafruit/DHT sensor library@^1.4.6 | ||
symlink://../.. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.pio | ||
.vscode/.browse.c_cpp.db* | ||
.vscode/c_cpp_properties.json | ||
.vscode/launch.json | ||
.vscode/ipch |
Oops, something went wrong.