How can I display the DHT11 values from my ESP32 onto my MIT App Inventor? My serial monitor shows the correct values, so I believe the code is working. What block code should I use in MIT App Inventor to show these values?
MIT_app_inventor1.aia (284.9 KB)
Arduino code:
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <dht11.h> // Library for DHT11 Sensor
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h> // Include the Adafruit GFX library
// DHT11 Configuration
#define DHT11PIN 19
dht11 DHT11;
// OLED Configuration
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // -1 if no reset pin
#define LED 2
#define BUZZER_PIN 18 // Buzzer pin definition
bool deviceConnected = false;
bool oldDeviceConnected = false;
float temperature = 0; // Temperature from DHT11
float humidity = 0; // Humidity from DHT11
BLEServer *pServer; // Declare pServer as a pointer to BLEServer
BLECharacteristic *pCharacteristic; // Declare the BLE characteristic
// Define UUIDs for Service and Characteristic
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX1 "6E400004-B5A3-F393-E0A9-E50E24DCCA9E"
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
oldDeviceConnected = false;
Serial.println("Device connected");
}
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
Serial.println("Device disconnected");
pServer->startAdvertising(); // Restart advertising immediately
Serial.println("Advertising restarted");
}
};
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT); // Initialize buzzer pin
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (1);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print(F("Initializing..."));
display.display();
// Create the BLE Device
BLEDevice::init("UART Service");
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create the BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_READ
);
// Add descriptors (optional, can be used to set proper read/write permissions)
pCharacteristic->addDescriptor(new BLE2902()); // Enable notifications
// Start the service
pService->start();
// Start advertising
pServer->getAdvertising()->start();
Serial.println("Waiting for a client connection to notify...");
// Initialize DHT11 sensor
pinMode(DHT11PIN, INPUT);
}
void loop() {
if (deviceConnected) {
// Read DHT11 sensor data
int chk = DHT11.read(DHT11PIN);
if (chk == DHTLIB_OK) {
temperature = DHT11.temperature;
humidity = DHT11.humidity;
} else {
Serial.println("DHT11 read failed!");
return;
}
// Print values to the serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" C ");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
// Update OLED display with the values
display.clearDisplay();
display.setCursor(0, 0);
display.print("Temperature: ");
display.print(temperature);
display.println(" C");
display.print("Humidity: ");
display.print(humidity);
display.println(" %");
display.display(); // Display the data on the OLED screen
// Prepare data to send to the characteristic
String data = "Temperature: " + String(temperature) + "C, Humidity: " + String(humidity) + "%";
pCharacteristic->setValue(data.c_str());
pCharacteristic->notify(); // Notify the connected client
// Buzzer logic for temperature and humidity
if (humidity >= 80 && temperature >= 30) {
tone(BUZZER_PIN, 2000); // High pitch if both temperature and humidity are high
delay(200);
noTone(BUZZER_PIN);
delay(200);
} else if (humidity > 80 || temperature > 30) {
tone(BUZZER_PIN, 500); // Low pitch if either temperature or humidity is high
delay(1000);
noTone(BUZZER_PIN);
delay(250);
} else {
digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer if the conditions are normal
}
delay(1000); // Delay to avoid congestion
}
// Handle disconnection
if (!deviceConnected && oldDeviceConnected) {
delay(500); // Give the stack time to reset
pServer->startAdvertising(); // Restart advertising
Serial.println("Start advertising");
oldDeviceConnected = deviceConnected;
}
// Handle new connection
if (deviceConnected && !oldDeviceConnected) {
Serial.println("Device connected, ready to notify");
oldDeviceConnected = deviceConnected;
}
}