Home > Other >Special Application Circuits > Design a BLE thermos cup using M5Stack

Design a BLE thermos cup using M5Stack

Source: InternetPublisher:失踪的猫 Keywords: Sensors Bluetooth Protocol ESP32 Updated: 2024/12/17

    This thermos bottle senses the internal temperature and sends the information to a receiver via Bluetooth.

    In this article, I will show you how to transmit the Bluetooth protocol of a sensor between two ESP32 boards.

pYYBAGKN61aATlc_AAPKy41aNZ0183.png

    Things you need:

    M5Stick C

    M5Stamp Pico

    LM35 Temperature Sensor

    M5 Rod C:

pYYBAGKN7iGAejPIAAJFFSSEDUA961.png

    M5StickC is a mini M ESP32 provided. It is an open source IoT that can realize your Stack55. What does it do? M5stickC is one of the core devices of the M5Stack product series and helps you to carry out product prototyping in the process.

Power switch operation:

    Guide: Press and hold the power button for 2 seconds

    TV: Press and hold the power button for 6 seconds

    Notice:

    Baud rates supported by M5StickC: 1200~115200, 250K, 500K, 750K, 1500K

    Features

    Based on ESP32

    Combined 6-axis IMU

    Red LED

Infrared transmitter

    microphone

    Button, LCD (0.96 inches)

    Compressed lithium battery

    Scalable

    Wearable and wall-mounted

    Game multi-platform development:

    UIFlow

    MicroPython

    Arduino

    .NET Nano Framework

    M5Stamp Pico:

poYBAGKN7i6AdNyyAAN6rE7Xxc0376.png

    Its STAMP-P ultra-designed size is as precise as a LXLX 6 microprocessor, 40MHz printer with a low 32-bit ICOX interface on a PCB. Such as wearable devices, medical devices, sensors and other IoT devices.

    Multiple installation methods, unlimited possibilities! (SMT, DIP, flying wire interface), high temperature resistant plastic shell, 3D antenna and No. 5 antenna can be better protected.

    LOW-CODE DEV OPMENT: STAMP-MP-programming, scripted programming platform; comprehensive Arduino programming-free 3-2-micro graphics development and other development platforms, quickly support Python and other development, Python and other various application development.

    HIGH: 1P-ICO5V STA->3V DC/MP-1DC/1P-ICO5V STA-3V DC/12GR, full RGB lights, interface x1x1, xx circuit, stable communication, providing reliable wireless.

    Powerful scalability: Access to the M5 software and hardware ecosystem: rich selection of sensors, execution, functions, accessories, and extremely fast system access modules.

    Features:

Chipset: ESP32-PICO-D4 (2.4GHz Wi-Fi dual-mode)

    Support UIFlow graphical programming

    Multiple IO pinouts, supporting various application forms (SMT, DIP, fly-by-wire)

    Integrated embedded RGB LED and buttons

    Micro Module

    LM35 Temperature Sensor:

poYBAGKN7jyAVGtvAAFAVcmerus761.png

    LM35 35 analog temperature analog output and proportional to 35, its sensor is connected to LM35 or LM timing speaker separately. Specially designed for LM35 external light source or forward LM automatic accurate output, providing normal output and appropriate characteristics 35 analog temperature and output and proportional to 35. Control circuit connection.

    Hardware Connection:

pYYBAGKN7kmAN4aRAAJ9jsF9_OI308.png

    Connect the output terminal of GND to the 36th Stamp terminal of LM3, and connect the power supply of LM3 to 5 V and GND of LM3.

    VOUT -> G36

    Grounding point -> Grounding point

    VCC -> 5V

    To program the transmitter:

    In this article, the micro transmitter launches the transmitter into the middle and connects the transmitter to emit infrared transmitter to the transmitter.

    #include

    // LED count
#define NUM_LEDS 1
#define DATA_PIN 27

    //Define LED array
CRGB leds[NUM_LEDS];

    #define Button 39
//BLE server name
#define bleServerName "M5"

    //LM35
#define ADC_VREF_mV 3300.0 // in millivolts
#define ADC_RESOLUTION 4096.0
#define PIN_LM35 36 // ESP32 pin GIOP36 (ADC0) connected to LM35

    float temp;
float temp F;
float hum;

    // Timer variable
unsigned long lastTime = 0;
unsigned long timer delay = 30000;

    boolean deviceConnected = false;

    #define SERVICE_UUID "91bad492-b950-4226-aa2b-4ede9fa42f59"

    // Temperature characteristic and descriptor
BLECharacteristic bmeTemperatureCelsiusCharacteristics("cba1d466-344c-4be3-ab3f-189f80dd7518", BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor bmeTemperatureCelsiusDescriptor(BLEUUID((uint16_t)0x2902));

    //Set callbacks onConnect and onDisconnect
classes MyServerCallbacks: public BLEServerCallbacks { void onConnect(BLEServer* pServer) {   deviceConnected = true;  };
 void onDisconnect(BLEServer* pServer)  {   deviceConnected = false;  } };






void setup() {
// Start serial communication
Serial.begin(115200);

    FastLED.addLeds

    pinMode(button, INPUT);

    // Create BLE device
BLEDevice::init(bleServerName);

    // Create a BLE server
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());

    // Create BLE service
BLEService *bmeService = pServer->createService(SERVICE_UUID);

    // Create BLE characteristic and create BLE descriptor
bmeService->addCharacteristic(&bmeTemperatureCelsiusCharacteristics);
bmeTemperatureCelsiusDescriptor.setValue("BME temperature in Celsius");
bmeTemperatureCelsiusCharacteristics.addDescriptor(&bmeTemperatureCelsiusDescriptor);


// Start the service
bmeService->start();

    // Start advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pServer->getAdvertising()->start();
Serial.println("Waiting for client connection notification...");
}

    void loop() {
static boolean state;
state = digitalRead(button);
if (State == 0)
{
 ESP.restart();
}
if (deviceConnected) {
 if ((millis() - lastTime) > timerDelay) {

    // Turn the LED on, then pause
  leds[0] = 0xf00000;
  FastLED.show();
  delay(2000);

    // Read the ADC value from the temperature sensor
  int adcVal = analogRead(PIN_LM35);
  // Convert the ADC value to a voltage in millivolts
  float milliVolt = adcVal * (ADC_VREF_mV / ADC_RESOLUTION);
  // Convert the voltage to temperature in °C
  temp = milliVolt / 10;

    Static character temperatureCTemp[6];
  dtostrf(temp, 6, 2, temperatureCTemp);
  //Set the temperature characteristic value and notify the connected client
  bmeTemperatureCelsiusCharacteristics.setValue(temperatureCTemp);
  bmeTemperatureCelsiusCharacteristics.notify();

    // Now turn the LED off, then pause
  leds[0] = 0x00f000;
  FastLED.show();
 
  Serial.print("Temperature in Celsius: ")
  ;
  Serial.println("ºC");

    finalTime = milliseconds();
 }
}


In this code, if you need to change the BLE server name, change this line as needed:

poYBAGKN7lmAGyI6AABGDmW1f5k292.png

    The base code for the LM35 includes the lines for the readouts, do not change these.

poYBAGKN7mWAKIJgAAD4DysjYoc788.png

    This is the BLE service ID of the transmitter:

pYYBAGKN7nGAM8vnAABpEoAugOk083.png

    This is the service ID for the temperature data:

pYYBAGKN7n2AQ75iAACpNulSWbg324.png

    The M5 doesn't use any reset button, but it has a button defined here and I reset the board Pico.

poYBAGKN7omAWYWNAAB_SBAdd2k548.png

    Finally, upload the code to the M5Stamp Pico, making sure you have the correct board settings selected:

poYBAGKN7pWAVhn_AAGf4RFgeVk889.png

    Forecast:

    In this paper, 5 of the transmitters are used to receive the temperature from the transmitter.

    #include

    //BLE server name
#define bleServerName "M5"

    /* UUID of the service we want to read the characteristic*/
// BLE service
static BLEUUID bmeServiceUUID("91bad492-b950-4226-aa2b-4ede9fa42f59");

    //Temperature Celsius characteristic
static BLEUUID temperatureCharacteristicUUID("cba1d466-344c-4be3-ab3f-189f80dd7518");


// Flag indicating whether the connection should be started and whether the connection is OK
static boolean doConnect = false;
static boolean connect = false;

    //The address of the peripheral. The address will be found during the scan...
static BLEAddress *pServerAddress;

    //The characteristic value we want to read
static BLERemoteCharacteristic* temperatureCharacteristic;

    //Activate notification
const uint8_t notificationOn[] = {0x1, 0x0};
const uint8_t notificationOff[] = {0x0, 0x0};



//Variable char* temperatureChar to store temperature and humidity ;

    // Flag to check if new temperature and humidity readings are available
boolean newTemperature = false;


//Connect to a BLE server with name, service and characteristics
bool connectToServer(BLEAddress pAddress) {
BLEClient* pClient = BLEDevice::createClient();

    // Connect to the removed BLE server.
pClient->connect(pAddress);
Serial.println(" - Connected to server");

    // Get a reference to the service we're after in the remote BLE server.
BLERemoteService* pRemoteService = pClient->getService(bmeServiceUUID);
if (pRemoteService == nullptr) {
 Serial.print("Failed to find our service UUID:");
 Serial.println(bmeServiceUUID.toString().c_str());
 return (false);
}

    // Get the characteristic reference in the remote BLE server service.
temperatureCharacteristic = pRemoteService->getCharacteristic(temperatureCharacteristicUUID);

    if (temperatureCharacteristic == nullptr) {
 Serial.print("Failed to find our characteristic UUID");
 return false;
}
Serial.println(" - Found our characteristic");

    //Assign callback function to characteristic
temperatureCharacteristic->registerForNotify(temperatureNotifyCallback);
return true;
}

    //Callback function called when an advertisement is received from another device
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
 void onResult(BLEAdvertisedDevice advertisedDevice) {
  if (advertisedDevice.getName() == bleServerName) { //Check if the advertiser's name matches
the advertising device.getScan()->stop(); //Can stop scanning, we found
what we were looking for pServerAddress = new BLEAddress(advertisedDevice.getAddress()); //The advertiser's address is
the one we need doConnect = true; //Set indicator that we're ready to connect
Serial.println("Device found. Connecting!");
  }
 }
};

    //When the BLE Server sends a new temperature reading with the notify attribute
static void temperatureNotifyCallback(BLERemoteCharacteristic* pBLERemoteCharacteristic,
  uint8_t* pData, size_t length, bool isNotify) {
//Store the temperature value
temperatureChar = (char*)pData;
newTemperature = true;
}


//Function to print the latest sensor readings in the OLED display
void printReadings() {
M5.update();

    // Erase the previous content in the display
M5.Lcd.fillScreen(BLACK);

    M5.Lcd.setTextColor(red);
M5.Lcd.setCursor(20, 18);
M5.Lcd.setTextSize(1.8);
M5.Lcd.print("[");
M5.Lcd.setTextColor(green);
M5.Lcd.print("BLE Smart Flask");
M5.Lcd.setTextColor(red);
M5.Lcd.print(" ]");
M5.Lcd.setCursor(18, 40);
M5.Lcd.setTextColor(green);
Serial.print("Temperature:");
M5.Lcd.print("Temperature:");
Serial.println(temperature character);
M5.Lcd.print(temperature character);
M5.Lcd.print("C");

    M5.Lcd.setCursor(15, 55);
M5.Lcd.setTextColor(red);
M5.Lcd.print("---------");

    float x = atof(temperature_char);
if (x >= 35.0)
{
 Serial.println("May be overheated");
 M5.Lcd.setCursor(15, 65);
 M5.Lcd.setTextColor(red);

    M5.Lcd.print("May be overheated");
}
else {
 Serial.println("You can drink");
 M5.Lcd.setCursor(30, 65);
 M5.Lcd.setTextColor(green);
 M5.Lcd.print("You can drink");
}

    M5.Lcd.setCursor(15, 75);
M5.Lcd.setTextColor(red);
M5.Lcd.print("---------");
}
void setup() {

    M5.begin();
M5.Lcd.setRotation(3);
M5.Axp.ScreenBreath(10);
M5.Lcd.setTextColor(RED);
M5.Lcd.setCursor(10, 35);
M5.Lcd.setTextSize( 1.8);

    //Start serial communication
Serial.begin(115200);
Serial.println("Starting Arduino BLE Client application...");

    //Init BLE device
BLEDevice::init("");

    BLEScan* pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
pBLEScan->start(30);
}

    void loop() {

if (doConnect == true) {
 if (connectToServer(*pServerAddress)) {
  Serial.println("We are now connected to the BLE server.");
  M5.Lcd.print("Connected to BLE server");
  //Activate the Notify property of each Characteristic
  temperatureCharacteristic->getDescriptor(BLEUUID((uint16_t)0x2902))->writeValue((uint8_t*)notificationOn, 2, true);
  connected = true;
 } else {
  Serial.println("We failed to connect to the server; restart your device to scan for nearby BLE servers again.");
  M5.Lcd.print("Not connected");
 }
 connected = false;
}
//If a new temperature reading is available, print it in the OLED
if (newTemperature) {
 newTemperature = false;
 printReading();
}
}
Make sure you rename this service name to the same name as tf and match it in the device name and temperature data ID.

pYYBAGKN7qWAAj3jAAGZZI9kO-I811.png

    Some lines of code to display I added to the TFT display.

pYYBAGKN7raAA95RAAJ5gu4WpI4351.png

    Once completed, make the desired changes and upload the code to the M5Stick C.

pYYBAGKN7sOAIFZhAAHk2mWIEK8739.png

poYBAGKN7tOANxAhAAEoUeoPI-0843.png

    After uploading the code once, you can click buttons on the transmitter and monitor output.

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号