#include <WiFi.h>
extern "C" {
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
}
#include <AsyncMqttClient.h>
const int trig_pin = 25;
const int echo_pin = 26;
// Sound speed in air
#define SOUND_SPEED 340
#define TRIG_PULSE_DURATION_US 10
float ultrason_duration;
unsigned long distance_cm;
// Change the credentials below, so your ESP32 connects to your router
#define WIFI_SSID "TP-Link_BIOMED"
#define WIFI_PASSWORD "B!0medical"
// Change the MQTT_HOST variable to your Raspberry Pi IP address,
// so it connects to your Mosquitto MQTT broker
#define MQTT_HOST IPAddress(192, 168, 0, 108)
#define MQTT_PORT 1883
// Create objects to handle MQTT client
AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;
// Stores last time height was published
const long interval = 10000; // interval at which to publish sensor readings
const int ledPin = 25; // GPIO where the LED is connected to
int ledState = LOW; // the current state of the output pin
// GPIO where the DS18B20 is connected to
const int oneWireBus = 27;
// Setup a oneWire instance to communicate with any OneWire devices
const int buttonPin = 32; // Define GPIO where the pushbutton is connected
int buttonState; // current reading from the input pin (pushbutton)
int lastButtonState = LOW; // previous reading from the input pin (pushbutton)
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flicker
void connectToWifi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}
void WiFiEvent(WiFiEvent_t event) {
Serial.printf("[WiFi-event] event: %d\n", event);
switch(event) {
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
connectToMqtt();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
xTimerStart(wifiReconnectTimer, 0);
break;
}
}
// Add more topics that want your ESP32 to be subscribed to
void onMqttConnect(bool sessionPresent) {
Serial.println("Connected to MQTT.");
Serial.print("Session present: ");
Serial.println(sessionPresent);
// ESP32 subscribed to esp32/bodyTemp topic
uint16_t packetIdSub = mqttClient.subscribe("esp32/marcell", 0);
Serial.print("Subscribing at QoS 0, packetId: ");
Serial.println(packetIdSub);
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println("Disconnected from MQTT.");
if (WiFi.isConnected()) {
xTimerStart(mqttReconnectTimer, 0);
}
}
void onMqttSubscribe(uint16_t packetId, uint8_t qos) {
Serial.println("Subscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
Serial.print(" qos: ");
Serial.println(qos);
}
void onMqttUnsubscribe(uint16_t packetId) {
Serial.println("Unsubscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
void onMqttPublish(uint16_t packetId) {
Serial.println("Publish acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
// You can modify this function to handle what happens when you receive a certain message in a specific topic
void onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
String messageTemp;
for (int i = 0; i < len; i++) {
//Serial.print((char)payload[i]);
messageTemp += (char)payload[i];
}
// Check if the MQTT message was received on topic esp32/bodyTemp
if (strcmp(topic, "esp32/marcell") == 0) {
// If the LED is off turn it on (and vice-versa)
if (messageTemp == "on") {
digitalWrite(ledPin, HIGH);
}
else if (messageTemp == "off") {
digitalWrite(ledPin, LOW);
}
}
Serial.println("Publish received.");
Serial.print(" message: ");
Serial.println(distance_cm);
Serial.print(" topic: ");
Serial.println(topic);
Serial.print(" qos: ");
Serial.println(properties.qos);
Serial.print(" dup: ");
Serial.println(properties.dup);
Serial.print(" retain: ");
Serial.println(properties.retain);
Serial.print(" len: ");
Serial.println(len);
Serial.print(" index: ");
Serial.println(index);
Serial.print(" total: ");
Serial.println(total);
}
void setup() {
// Start the DS18B20 sensor
//sensors.begin();
// Define LED as an OUTPUT and set it LOW
pinMode(trig_pin, OUTPUT);
digitalWrite(echo_pin, LOW);
// Define buttonPin as an INPUT
pinMode(buttonPin, INPUT);
Serial.begin(115200);
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
WiFi.onEvent(WiFiEvent);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onSubscribe(onMqttSubscribe);
mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onMessage(onMqttMessage);
mqttClient.onPublish(onMqttPublish);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
connectToWifi();
}
int value = 0;
void loop() {
// Set up the signal
digitalWrite(trig_pin, LOW);
delayMicroseconds(2);
// Create a 10 µs impulse
digitalWrite(trig_pin, HIGH);
delayMicroseconds(TRIG_PULSE_DURATION_US);
digitalWrite(trig_pin, LOW);
// Return the wave propagation time (in µs)
ultrason_duration = pulseIn(echo_pin, HIGH);
//distance calculation
distance_cm = ultrason_duration * SOUND_SPEED/2 * 0.0001;
// We print the distance on the serial port
Serial.print("Distance (cm): ");
Serial.println(distance_cm);
// Read the state of the pushbutton and save it in a local variable
int reading = digitalRead(buttonPin);
// If the pushbutton state changed (due to noise or pressing it), reset the timer
if (reading != lastButtonState) {
// Reset the debouncing timer
lastDebounceTime = millis();
}
delay(1000);
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.