Kontrolery ESP posiadają wbudowany moduł komunikacyjny Bluetooth z obsługą Low Energy. Umożliwia on komunikację z urządzeniami korzystającymi z tego standardu.
Program Beacon Bluetooth
Beacon to funkcja, która rozgłaszanie swoją nazwę poprzez Bluetooth.
Program odczytuje temperaturę z czujnika DS18B20 i ją rozgłasza.
Schemat
Wymagany rezystor 4,7kΩ.
Pobranie adresu czujnika
#include <OneWire.h>
#define ONE_WIRE_BUS 15
OneWire pin(ONE_WIRE_BUS);
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
byte i;
byte addr[8];
if (!pin.search(addr)) {
Serial.println();
pin.reset_search();
delay(250);
return;
}
Serial.print(" ROM =");
for (i = 0; i < 8; i++) {
Serial.write(' ');
Serial.print(addr[i], HEX);
}
delay(5000);
}
Monitor portu szeregowego
Kod programu
Efektem pracy programu, jest widoczny odczyt temperatury w smartfonach, znajdujących się w pobliżu modułu ESP.
#include "SimpleBLE.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 15
SimpleBLE beacon;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
//Adres sensora
DeviceAddress sensor = { 0x28, 0xFF, 0x19, 0x4D, 0xB3, 0x17, 0x1, 0x3E };
void setup(void){
Serial.begin(9600);
sensors.begin();
}
void loop(void){
//Pobranie temperatury z sensora
sensors.requestTemperatures();
//Rozgłoszenie nazwy poprzez Bluetooth
beacon.begin("Temperatura: " + String(sensors.getTempC(sensor)) + "^C" );
delay(10000);
}
Program wysłanie danych (serial Bluetooth)
Program odczytuje temperaturę z czujnika DS18B20 i wysyła ją poprzez Bluetooth do aplikacji "Serial Bluetooth". Schemat podłączenia jest taki sam jak w Beacon Bluetooth.
Aplikacja "Serial Bluetooth"
Kod Programu
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 15
#include "BluetoothSerial.h"
BluetoothSerial bluetooth;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Adres sensora
DeviceAddress sensor = { 0x28, 0xFF, 0x19, 0x4D, 0xB3, 0x17, 0x1, 0x3E };
void setup(void){
Serial.begin(9600);
bluetooth.begin("Bluetooth Terminal");
sensors.begin();
}
void loop(void){
//Pobranie temperatury z sensora
sensors.requestTemperatures();
//Wysłanie danych poprzez Bluetooth Serial SPP
bluetooth.println("Temperatura: " + String(sensors.getTempC(sensor)) + "^C" );
delay(2000);
}
Program sterowanie diodami (serial Bluetooth)
Sterowanie diodą w programie odbywa się za pomocą aplikacji "Serial Bluetooth". Wysyła ona poprzez terminal komunikaty "LED-ON" oraz "LED-OFF". W programie są one przetwarzane, co w rezultacie odpowiada za sterowanie pinem cyfrowym GPIO22.
Schemat
Wymagany rezystor 220Ω.
Aplikacja "Serial Bluetooth"
Aplikacja daje możliwość przypisania do klawiszy domyślnie nazwanych "M1" - "M7" wartości wysyłane przez port.
Kod
#include "BluetoothSerial.h"
//Pin diody LED
#define ledPin 22
BluetoothSerial bluetooth;
String messageBT = "";
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
bluetooth.begin("Połączono");
}
void loop() {
if (bluetooth.available()){
//odczytanie serial blutoth
char receivedChar = bluetooth.read();
//Przetworzenie char na string
if (receivedChar != '\n'){
messageBT += String(receivedChar);
}
else{
messageBT = "";
}
Serial.write(receivedChar);
}
//Zapalenie gaszenie diody LED
if (messageBT =="LED-ON"){
digitalWrite(ledPin, HIGH);
bluetooth.begin("GPIO22 - HIGH");
}
else if (messageBT =="LED-OFF"){
digitalWrite(ledPin, LOW);
bluetooth.begin("Połączono");
bluetooth.begin("GPIO22 - LOW");
}
delay(50);
}