hello, i want to design MIT app, basically, the data is from the sensor of my prototype mini project, and i used the Bluetooth HC05 to send the data from the sensor to the mit apps. the Bluetooth is connected but the data not come out to the screen, i dont know either my coding or the block was not correct, please help me as soon as possible because i want to present by prototype by this week, huhuhuhu..
btw, i used STM32duino for my coding.
(this is my block)
this is my coding set up
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <DHT.h>
// === LCD Setup ===
LiquidCrystal_I2C lcd(0x27, 16, 2);
// === DHT11 Setup ===
#define DHTPIN PC1
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// === Ultrasonic Pins ===
const int trig1 = PA0;
const int echo1 = PA1;
const int trig2 = PC10;
const int echo2 = PC11;
const int trig3 = PA6;
const int echo3 = PA7;
// === LED Pins ===
const int led_green = PB0;
const int led_red = PB1;
// === Servo & Button ===
const int servoPin = PB6;
const int buttonPin = PC0;
// === Buzzer ===
const int BUZZER_PIN = PC2;
// === Thresholds ===
const float TEMP_LOW = 10.0;
const float TEMP_HIGH = 40.0;
const float HUMID_LOW = 55.0;
const float HUMID_HIGH = 90.0;
// === Variables ===
Servo myservo;
bool doorOpened = false;
bool lastButtonState = HIGH;
float distance1, distance2, distance3;
// === Custom Characters (✓ and ✗) ===
byte checkmark[8] = {
0b00000,
0b00001,
0b00010,
0b10100,
0b01000,
0b00000,
0b00000,
0b00000
};
byte cross[8] = {
0b00000,
0b10001,
0b01010,
0b00100,
0b01010,
0b10001,
0b00000,
0b00000
};
// === HC-05 via UART2 ===
HardwareSerial &BTSerial = Serial2;
void setup() {
Serial.begin(9600); // Debug ke PC (USB)
BTSerial.begin(9600); // HC-05 via Serial2 (PA2=TX, PA3=RX)
// LCD
lcd.init();
lcd.backlight();
lcd.createChar(0, checkmark); // ✓
lcd.createChar(1, cross); // ✗
// DHT11
dht.begin();
pinMode(trig1, OUTPUT); pinMode(echo1, INPUT);
pinMode(trig2, OUTPUT); pinMode(echo2, INPUT);
pinMode(trig3, OUTPUT); pinMode(echo3, INPUT);
// LED setup
pinMode(led_green, OUTPUT);
pinMode(led_red, OUTPUT);
// Servo & button
myservo.attach(servoPin);
pinMode(buttonPin, INPUT_PULLUP);
myservo.write(0); // start closed
// Buzzer
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
}
float getDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH, 30000);
if (duration == 0) return 999;
return (duration * 0.034) / 2;
}
void buzzWarning() {
for (int i = 0; i < 3; i++) {
digitalWrite(BUZZER_PIN, HIGH);
delay(200);
digitalWrite(BUZZER_PIN, LOW);
delay(200);
}
}
void loop() {
distance1 = getDistance(trig1, echo1);
distance2 = getDistance(trig2, echo2);
distance3 = getDistance(trig3, echo3);
// === LED logic ===
bool isEmpty = (distance1 > 5 && distance2 > 5 && distance3 > 5);
digitalWrite(led_green, !isEmpty);
digitalWrite(led_red, isEmpty);
// === Button buka/tutup servo ===
bool currentButtonState = digitalRead(buttonPin);
if (lastButtonState == HIGH && currentButtonState == LOW) {
doorOpened = !doorOpened;
myservo.write(doorOpened ? 90 : 0);
delay(100);
}
lastButtonState = currentButtonState;
// === Baca suhu & kelembapan ===
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (!isnan(temperature) && !isnan(humidity)) {
bool tempAlert = (temperature < TEMP_LOW || temperature > TEMP_HIGH);
bool humidAlert = (humidity < HUMID_LOW || humidity > HUMID_HIGH);
if (tempAlert || humidAlert) {
buzzWarning();
} else {
digitalWrite(BUZZER_PIN, LOW);
}
// === Hantar data ke HC-05 (Bluetooth) ===
BTSerial.println(String(temperature,1) + "," +String(humidity,1) + "," +(isEmpty ? "EMPTY" : "AVAILABLE"));
Serial.print("TEMP: ");
Serial.print(temperature);
Serial.print("C, HUMID: ");
Serial.print(humidity);
Serial.print("%, STATUS: ");
Serial.println(isEmpty ? "EMPTY" : "AVAILABLE");
} else {
Serial.println(" Gagal baca sensor DHT!");
}
// === Papar ke LCD ===
lcd.clear();
if (isEmpty) {
lcd.setCursor(0, 0); lcd.print("no food");
lcd.setCursor(0, 1); lcd.print("right now");
} else {
lcd.setCursor(0, 0); lcd.print("please serve yourself");
lcd.setCursor(0, 1);
lcd.print(" B1:"); lcd.write(distance1 < 5 ? 0 : 1);
lcd.print(" B2:"); lcd.write(distance2 < 5 ? 0 : 1);
lcd.print(" B3:"); lcd.write(distance3 < 5 ? 0 : 1);
}
delay(1000);
}