Create/modify example code and main code base
This commit is contained in:
parent
795ac28caa
commit
1a92127161
|
@ -0,0 +1,157 @@
|
||||||
|
/*********
|
||||||
|
Rui Santos
|
||||||
|
Complete project details at https://randomnerdtutorials.com
|
||||||
|
*********/
|
||||||
|
|
||||||
|
#include <WiFi.h>
|
||||||
|
#include <PubSubClient.h>
|
||||||
|
#include <Wire.h>
|
||||||
|
#include <Adafruit_BME280.h>
|
||||||
|
#include <Adafruit_Sensor.h>
|
||||||
|
|
||||||
|
// Replace the next variables with your SSID/Password combination
|
||||||
|
const char* ssid = "REPLACE_WITH_YOUR_SSID";
|
||||||
|
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
|
||||||
|
|
||||||
|
// Add your MQTT Broker IP address, example:
|
||||||
|
//const char* mqtt_server = "192.168.1.144";
|
||||||
|
const char* mqtt_server = "YOUR_MQTT_BROKER_IP_ADDRESS";
|
||||||
|
|
||||||
|
WiFiClient espClient;
|
||||||
|
PubSubClient client(espClient);
|
||||||
|
long lastMsg = 0;
|
||||||
|
char msg[50];
|
||||||
|
int value = 0;
|
||||||
|
|
||||||
|
//uncomment the following lines if you're using SPI
|
||||||
|
/*#include <SPI.h>
|
||||||
|
#define BME_SCK 18
|
||||||
|
#define BME_MISO 19
|
||||||
|
#define BME_MOSI 23
|
||||||
|
#define BME_CS 5*/
|
||||||
|
|
||||||
|
Adafruit_BME280 bme; // I2C
|
||||||
|
//Adafruit_BME280 bme(BME_CS); // hardware SPI
|
||||||
|
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
|
||||||
|
float temperature = 0;
|
||||||
|
float humidity = 0;
|
||||||
|
|
||||||
|
// LED Pin
|
||||||
|
const int ledPin = 4;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
// default settings
|
||||||
|
// (you can also pass in a Wire library object like &Wire2)
|
||||||
|
//status = bme.begin();
|
||||||
|
if (!bme.begin(0x76)) {
|
||||||
|
Serial.println("Could not find a valid BME280 sensor, check wiring!");
|
||||||
|
while (1);
|
||||||
|
}
|
||||||
|
setup_wifi();
|
||||||
|
client.setServer(mqtt_server, 1883);
|
||||||
|
client.setCallback(callback);
|
||||||
|
|
||||||
|
pinMode(ledPin, OUTPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup_wifi() {
|
||||||
|
delay(10);
|
||||||
|
// We start by connecting to a WiFi network
|
||||||
|
Serial.println();
|
||||||
|
Serial.print("Connecting to ");
|
||||||
|
Serial.println(ssid);
|
||||||
|
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(500);
|
||||||
|
Serial.print(".");
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("");
|
||||||
|
Serial.println("WiFi connected");
|
||||||
|
Serial.println("IP address: ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
}
|
||||||
|
|
||||||
|
void callback(char* topic, byte* message, unsigned int length) {
|
||||||
|
Serial.print("Message arrived on topic: ");
|
||||||
|
Serial.print(topic);
|
||||||
|
Serial.print(". Message: ");
|
||||||
|
String messageTemp;
|
||||||
|
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
Serial.print((char)message[i]);
|
||||||
|
messageTemp += (char)message[i];
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void reconnect() {
|
||||||
|
// Loop until we're reconnected
|
||||||
|
while (!client.connected()) {
|
||||||
|
Serial.print("Attempting MQTT connection...");
|
||||||
|
// Attempt to connect
|
||||||
|
if (client.connect("ESP8266Client")) {
|
||||||
|
Serial.println("connected");
|
||||||
|
// Subscribe
|
||||||
|
client.subscribe("esp32/output");
|
||||||
|
} else {
|
||||||
|
Serial.print("failed, rc=");
|
||||||
|
Serial.print(client.state());
|
||||||
|
Serial.println(" try again in 5 seconds");
|
||||||
|
// Wait 5 seconds before retrying
|
||||||
|
delay(5000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void loop() {
|
||||||
|
if (!client.connected()) {
|
||||||
|
reconnect();
|
||||||
|
}
|
||||||
|
client.loop();
|
||||||
|
|
||||||
|
long now = millis();
|
||||||
|
if (now - lastMsg > 5000) {
|
||||||
|
lastMsg = now;
|
||||||
|
|
||||||
|
// Temperature in Celsius
|
||||||
|
temperature = bme.readTemperature();
|
||||||
|
// Uncomment the next line to set temperature in Fahrenheit
|
||||||
|
// (and comment the previous temperature line)
|
||||||
|
//temperature = 1.8 * bme.readTemperature() + 32; // Temperature in Fahrenheit
|
||||||
|
|
||||||
|
// Convert the value to a char array
|
||||||
|
char tempString[8];
|
||||||
|
dtostrf(temperature, 1, 2, tempString);
|
||||||
|
Serial.print("Temperature: ");
|
||||||
|
Serial.println(tempString);
|
||||||
|
client.publish("esp32/temperature", tempString);
|
||||||
|
|
||||||
|
humidity = bme.readHumidity();
|
||||||
|
|
||||||
|
// Convert the value to a char array
|
||||||
|
char humString[8];
|
||||||
|
dtostrf(humidity, 1, 2, humString);
|
||||||
|
Serial.print("Humidity: ");
|
||||||
|
Serial.println(humString);
|
||||||
|
client.publish("esp32/humidity", humString);
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,60 +24,96 @@ void setup() {
|
||||||
// Initalize buzzer pin
|
// Initalize buzzer pin
|
||||||
pinMode(buzzer, OUTPUT);
|
pinMode(buzzer, OUTPUT);
|
||||||
digitalWrite(buzzer, LOW);
|
digitalWrite(buzzer, LOW);
|
||||||
|
|
||||||
|
// Initalize motor directions
|
||||||
|
pinMode(motorReverse, OUTPUT);
|
||||||
|
digitalWrite(motorReverse, LOW);
|
||||||
|
pinMode(motorForward, OUTPUT);
|
||||||
|
digitalWrite(motorForward, LOW);
|
||||||
|
|
||||||
|
// Initalize Sense Pin
|
||||||
|
pinMode(sense, INPUT);
|
||||||
|
// use "digitalRead(sense);" to read
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup_wifi() {
|
void setup_wifi() {
|
||||||
delay(10);
|
delay(10);
|
||||||
// We start by connecting to a WiFi network
|
|
||||||
Serial.println();
|
|
||||||
WiFi.hostname("ESP-" WINDOW_NAME);
|
|
||||||
Serial.print("Connecting to ");
|
|
||||||
Serial.println(WIFI_SSID);
|
|
||||||
|
|
||||||
|
// Set our hostname
|
||||||
|
WiFi.hostname("ESP-" WINDOW_NAME);
|
||||||
|
|
||||||
|
// Connect to the wifi!
|
||||||
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
||||||
|
|
||||||
|
// For as long as we're not connected..
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
delay(950);
|
delay(950); // Wait...
|
||||||
tone(buzzer, 1200, 50);
|
tone(buzzer, 1200, 50); // Beep...
|
||||||
Serial.println("Not yet connected.. Waiting 1s to check again..");
|
//Serial.println("Not yet connected.. Waiting 1s to check again..");
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("");
|
//Serial.println("");
|
||||||
Serial.println("WiFi connected");
|
//Serial.println("WiFi connected");
|
||||||
Serial.println("IP address: ");
|
//Serial.println("IP address: ");
|
||||||
Serial.println(WiFi.localIP());
|
//Serial.println(WiFi.localIP());
|
||||||
}
|
}
|
||||||
|
|
||||||
void reconnect() {
|
void reconnect() {
|
||||||
// Loop until we're reconnected
|
// Loop until we're reconnected
|
||||||
while (!client.connected()) {
|
while (!client.connected()) {
|
||||||
Serial.print("Attempting MQTT connection...");
|
//Serial.print("Attempting MQTT connection...");
|
||||||
// Attempt to connect
|
// Attempt to connect
|
||||||
if (client.connect("ESP8266Client", MQTT_USER, MQTT_PASS)) {
|
if (client.connect("ESP8266Client", MQTT_USER, MQTT_PASS)) {
|
||||||
Serial.println("connected");
|
//Serial.println("connected");
|
||||||
|
;
|
||||||
} else {
|
} else {
|
||||||
Serial.print("failed, rc=");
|
//Serial.print("failed, rc=");
|
||||||
Serial.print(client.state());
|
//Serial.print(client.state());
|
||||||
Serial.println(" try again in 5 seconds");
|
//Serial.println(" try again in 5 seconds");
|
||||||
// Wait 5 seconds before retrying
|
// Wait 5 seconds before retrying
|
||||||
delay(5000);
|
delay(5000);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void callback(char* topic, byte* payload, unsigned int length) {
|
void callback(char* topic, byte* message, unsigned int length) {
|
||||||
Serial.print("Message arrived [");
|
//Serial.print("Message arrived on topic: ");
|
||||||
Serial.print(topic);
|
//Serial.print(topic);
|
||||||
Serial.print("] ");
|
//Serial.print(". Message: ");
|
||||||
|
String messageString;
|
||||||
|
|
||||||
|
// Reconstruct a string from the mqtt contents.
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
char receivedChar = (char)payload[i];
|
messageString += (char)message[i];
|
||||||
Serial.print(receivedChar);
|
}
|
||||||
if (receivedChar == '0')
|
|
||||||
//digitalWrite(in_led, LOW);
|
if (String(topic) == MQTT_ROOT "/request_state") {
|
||||||
if (receivedChar == '1')
|
//Serial.print("Changing output to ");
|
||||||
//digitalWrite(in_led, HIGH);
|
if(messageString == "open"){
|
||||||
|
//Serial.println("Opening window.");
|
||||||
|
digitalWrite(motorForward, HIGH);
|
||||||
|
digitalWrite(motorReverse, LOW);
|
||||||
|
tone(buzzer, 2000, 200);
|
||||||
|
delay(200);
|
||||||
|
tone(buzzer, 2000, 200);
|
||||||
|
delay(200);
|
||||||
|
tone(buzzer, 2000, 200);
|
||||||
|
delay(200);
|
||||||
|
client.publish(MQTT_ROOT "/current_state", "open", true);
|
||||||
|
}
|
||||||
|
else if(messageString == "close"){
|
||||||
|
//Serial.println("Closing window");
|
||||||
|
digitalWrite(motorForward, LOW);
|
||||||
|
digitalWrite(motorReverse, HIGH);
|
||||||
|
tone(buzzer, 2000, 200);
|
||||||
|
delay(200);
|
||||||
|
tone(buzzer, 2000, 200);
|
||||||
|
delay(200);
|
||||||
|
tone(buzzer, 2000, 200);
|
||||||
|
delay(200);
|
||||||
|
client.publish(MQTT_ROOT "/current_state", "close", true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Serial.println();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
@ -89,10 +125,6 @@ void loop() {
|
||||||
// Run the client loop
|
// Run the client loop
|
||||||
client.loop();
|
client.loop();
|
||||||
|
|
||||||
// Publishes a random 0 and 1 like someone switching off and on randomly (random(2))
|
client.subscribe(MQTT_ROOT "/request_state");
|
||||||
client.publish(MQTT_ROOT "/random", String(random(2)).c_str(), true);
|
|
||||||
delay(1000);
|
|
||||||
client.subscribe(MQTT_ROOT "/in");
|
|
||||||
tone(buzzer, 2000, 500);
|
|
||||||
delay(1000);
|
delay(1000);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue