Browse Source
cleaned up the code, made a library for the atomizer, made mqtt dispatcher for humidity and temperature
master
cleaned up the code, made a library for the atomizer, made mqtt dispatcher for humidity and temperature
master
Luis Rodil-Fernandez
3 years ago
5 changed files with 178 additions and 107 deletions
-
39src/atomizer.h
-
7src/config.h
-
148src/main.cpp
-
80src/mqtt.cpp
-
9src/mqtt.h
@ -0,0 +1,39 @@ |
|||
#include <Arduino.h> |
|||
|
|||
/** |
|||
* Simple controller library for an ultrasonic atomizer |
|||
* used in atomizers, scent dispensers, etc. |
|||
* |
|||
* studio derfunke (c) 2022 |
|||
*/ |
|||
class Atomizer { |
|||
int pinSwitch; |
|||
int pinSensor; |
|||
u_int lastToggle; // since last toggle action |
|||
|
|||
public: |
|||
Atomizer(int pin) : pinSwitch(pin) { |
|||
} |
|||
|
|||
bool debug() { |
|||
int ldr = analogRead(pinSensor); |
|||
Serial.print(ldr); |
|||
} |
|||
|
|||
bool is_on() { |
|||
int ldr = analogRead(pinSensor); |
|||
return ldr > 300; |
|||
} |
|||
|
|||
u_int elapsed() { |
|||
return millis() - lastToggle; |
|||
} |
|||
|
|||
void toggle() { |
|||
digitalWrite(pinSwitch, HIGH); |
|||
delay(300); |
|||
digitalWrite(pinSwitch, LOW); |
|||
lastToggle = millis(); |
|||
} |
|||
|
|||
}; // class |
@ -1,8 +1,11 @@ |
|||
|
|||
// pin configuration |
|||
|
|||
#define PIN_DHT 2 |
|||
#define PIN_DHT 16 |
|||
#define PIN_ATOMIZER 4 |
|||
|
|||
#define MQTT_BROKER "YOUR_MQTT_BROKER_IP_ADDRESS" |
|||
// mqtt://interact:c9FjEDUnRgDTalBR@interact.cloud.shiftr.io |
|||
#define MQTT_BROKER "interact.cloud.shiftr.io" |
|||
#define MQTT_TOKEN "interact" |
|||
#define MQTT_PASSWD "c9FjEDUnRgDTalBR" |
|||
|
@ -0,0 +1,80 @@ |
|||
#include "mqtt.h"
|
|||
#include "config.h"
|
|||
|
|||
WiFiClient espClient; |
|||
PubSubClient broker(espClient); |
|||
|
|||
String byte2str(byte* message, unsigned int length) { |
|||
String retval; |
|||
|
|||
for (int i = 0; i < length; i++) { |
|||
Serial.print((char)message[i]); |
|||
retval += (char)message[i]; |
|||
} |
|||
|
|||
return retval; |
|||
} |
|||
|
|||
void mqtt_callback(char* _topic, byte* _message, unsigned int _length) { |
|||
|
|||
String topic = String(_topic); |
|||
String payload = byte2str( _message, _length ); |
|||
|
|||
Serial.print("MQTT << "); |
|||
Serial.print(topic); |
|||
Serial.print(" ["); |
|||
Serial.print(payload); |
|||
Serial.print("]"); |
|||
Serial.println(); |
|||
|
|||
|
|||
// // Feel free to add more if statements to control more GPIOs with MQTT
|
|||
|
|||
// // If a message is received on the topic esp32/output, you check if the message is either "on" or "off".
|
|||
// // Changes the output state according to the message
|
|||
// if (String(topic) == "esp32/output") {
|
|||
// Serial.print("Changing output to ");
|
|||
// if(messageTemp == "on"){
|
|||
// Serial.println("on");
|
|||
// digitalWrite(ledPin, HIGH);
|
|||
// }
|
|||
// else if(messageTemp == "off"){
|
|||
// Serial.println("off");
|
|||
// digitalWrite(ledPin, LOW);
|
|||
// }
|
|||
// }
|
|||
} |
|||
|
|||
bool mqtt_init() { |
|||
Serial.println("Initializing mqtt data stream..."); |
|||
broker.setServer(MQTT_BROKER, 1883); |
|||
broker.setCallback(mqtt_callback); |
|||
} |
|||
|
|||
void mqtt_reconnect() { |
|||
// Loop until we're reconnected
|
|||
while (!broker.connected()) { |
|||
Serial.print("Attempting MQTT connection..."); |
|||
// Attempt to connect
|
|||
if (broker.connect("kodama-AC234", MQTT_TOKEN, MQTT_PASSWD)) { |
|||
Serial.println("connected"); |
|||
// Subscribe
|
|||
broker.subscribe("kodama/incoming"); |
|||
} else { |
|||
Serial.print("failed, rc="); |
|||
Serial.print(broker.state()); |
|||
Serial.println(" try again in 5 seconds"); |
|||
// Wait 5 seconds before retrying
|
|||
delay(5000); |
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
void mqtt_pump() { |
|||
if (!broker.connected()) { |
|||
mqtt_reconnect(); |
|||
} |
|||
broker.loop(); |
|||
} |
|||
|
@ -0,0 +1,9 @@ |
|||
#include <PubSubClient.h> // mqtt stuff |
|||
#include <WiFi.h> |
|||
|
|||
extern PubSubClient broker; |
|||
|
|||
void mqtt_callback(char* topic, byte* message, unsigned int length); |
|||
bool mqtt_init(); |
|||
void mqtt_reconnect(); |
|||
void mqtt_pump(); |
Write
Preview
Loading…
Cancel
Save
Reference in new issue