Home > Communication Circuits >Wireless Communication > How to use M5Stick and M5Stamp to trigger Bluetooth operation BLE communication

How to use M5Stick and M5Stamp to trigger Bluetooth operation BLE communication

Source: InternetPublisher:D先生 Keywords: Controller Bluetooth ESP32 Updated: 2024/08/02

In this article, I will show you how to trigger Bluetooth actions using the M5Stamp PICO and M5Stick C using BLE communication, a very cheap and efficient ESP32 controller.

Components required:

M5Stick C

M5Stamp PICO

What are M5Stick and M5Stamp?

M5Stack is a technology company that designs and manufactures development kits, including hardware, programming platforms, and IoT solutions. It was founded by Jimmy Lai in 2017 and is headquartered in Shenzhen, China.

M5Stack created M5Stick and M5Stamps, both of which are M5 batteries based on ESP32. M5Stick C has built-in TFT objects and objects, while M5Stamp connects small icons that can be used in various products.

Follow these instructions to set up the M5Stamp and M5Stick using the Arduino IDE.

M5Stick C turns the M5Stamp into a BLE transmitter, while PICO turns the M5 Stamp into a BLE receiver. When button A on the M5Stamp is clicked, the LED on the M5Stamp turns green.

Transmitter code:

#include

BLEAdvertising *pAdvertising; // BLE advertising
typestruct timeval now;
#define GPIO_DEEP_SLEEP_DURATION 1 // sleep for x seconds then wake
upRTC_DATA_ATTR static time_t last; // remember last boot in RTC
memoryRTC_DATA_ATTR static uint32_t bootcount; // remember boot count in RTC memory
#define BEACON_UUID "87b99b2c-90fd-11e9-bc42-526af7764f64" // UUID 1 128 bits (you can use linux tool uuidgen or get a random number from https://www.uuidgenerator.net/)void
setBeacon()
{

BLEBeacon oBeacon = BLEBeacon();
oBeacon.setManufacturerId(0x4C00);
oBeacon.setProximityUUID(BLEUUID(BEACON_UUID));
oBeacon.setMajor((bootcount & 0xFFFF0000) >> 16);
oBeacon.setMinor(bootcount & 0xFFFF);
BLEAdvertisementData oAdvert isementData = BLEAdvertisementData();
BLEAdvertisementData oScanResponseData = BLEAdvertisementData();

oAdvertisementData.setFlags(0x04); // BR_EDR_NOT_SUPPORTED 0x04

std::string strServiceData = "";

strServiceData += (char)26; // Len
strServiceData += (char)0xFF; //
Input strServiceData += oBeacon.getData();
oAdvertisementData.addData(strServiceData);

pAdvertising->setAdvertisementData(oAdvertisementData);
pAdvertising->setScanResponseData(oScanResponseData);
}

void setup(){
M5.begin();
Serial.Begin(115200);
M5.Lcd.setTextColor(yellow); //Set the font color to yellow. Font setting color is
M5.Lcd.setRotation(3);
M5.Axp.ScreenBreath(10);
M5.Lcd.setTextColor(red);
M5.Lcd.setCursor(3, 10);
M5.Lcd.setTextSize(2);
}

void loop(){

M5.update();
M5.Lcd.setCursor(3, 10);
if (M5.BtnA.wasReleased()) {
 M5.Lcd.setTextColor(GREEN);
 M5.Lcd.println("Trigger");
 Serial.println("Trigger method");
 // Create BLE device
 BLEDevice::init("BLE Receiver 01");
 // Create BLE server
 BLEServer *pServer = BLEDevice::createServer(); // <-- No need to instantiate BLEServer anymore, less flash and memory usage
 pAdvertising = BLEDevice::getAdvertising();
 BLEDevice::startAdvertising();
 Set beacon();
 // Start advertising
 pAdvertising->start();
 Serial.println("Advertising started...");
 delay(1000);
 pAdvertising->stop();
 M5.Lcd.fillScreen(black);
 M5.Lcd.setCursor(3, 10);
}
}

Upload the above code to your M5Stick C controller.

Receiver Encoding:

#include "Arduino.h"
#include

// How many LEDs are there in your strip?
#define NUM_LEDS 1
#define DATA_PIN 27

//Define LED array
CRGB leds[NUM_LEDS];

string knownBLEAddresses[] = {"24:a1:60:53:06:3e"};
int RSSI_THRESHOLD = -100;
bool device_found;
int scan_time = 1; // in seconds
BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
 void onResult(BLEAdvertisedDevice advertisedDevice) {
  for (int i = 0; i < (sizeof(knownBLEAddresses) / sizeof(knownBLEAddresses[0])); i++)
  {
if (strcmp(advertisedDevice.getAddress().toString().c_str(), knownBLEAddresses[i].c_str()) == 0)
{
device_found = true;
rest;
}
else
device_found = false;
  }
  Serial.printf("Advertised device: %s", AdvertisementdDevice.toString().c_str());
 }
};
void setup() {
Serial.begin(115200); // Enable UART on ESP32
FastLED.addLeds

for (int i = 0; i < foundDevices.getCount(); i++)
{
 BLEAdvertisedDevice device = foundDevices.getDevice(i);
 int rssi = device.getRSSI();
 Serial.print("RSSI : ");
 Serial.println(rssi);
 if (rssi > RSSI_THRESHOLD && device_found == true)
 {
  Serial.println("Triggred");
  leds[0] = 0xf00000;
  FastLED.show();
  delay(200);
 }

}
// Now turn the LED off, then pause
leds[0] = 0x00f000;
FastLED.show();
delay(200);
pBLEScan->clearResults(); // Remove results from the BLEScan buffer to free up memory
}

NOTE: In the above code, you have to change the BLE address of the M5Stick C.

Here I have added the BLE address of my M5Stick C, you will need to change this address according to your specific situation.

Upload the result codes of the transmitter and receiver to the M5Stamp

On, connect the M5Stamp to the PC and then display to you, then click A on the M5Stick C, the TFT will continue to upload the display.

Whenever you see a message on your M5Stick C, check the search results.

The M5Stamp's LED indicators will also be displayed in the ESP sender's name, RSS, and player.

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号