Differences

This shows you the differences between two versions of the page.

Link to this comparison view

iothings:laboratoare:2022:lab7 [2023/11/20 21:37]
dan.tudose [Sparrow BLE Service]
iothings:laboratoare:2022:lab7 [2023/11/20 22:15] (current)
dan.tudose [Web BLE Application]
Line 127: Line 127:
 #include <​BLEUtils.h>​ #include <​BLEUtils.h>​
 #include <​BLE2902.h>​ #include <​BLE2902.h>​
-#​include ​<​Adafruit_Sensor.h+#​include ​"​Zanshin_BME680.h" ​
-#include <​Adafruit_BME280.h>​+
  
 //BLE server name //BLE server name
-#define bleServerName "ESP32_BME280"+#define bleServerName "Sparrow_BME680"
  
 // Default UUID for Environmental Sensing Service // Default UUID for Environmental Sensing Service
Line 151: Line 150:
  
 // Create a sensor object // Create a sensor object
-Adafruit_BME280 bme; +BME680_Class BME680;
- +
-// Init BME280 +
-void initBME(){ +
-  if (!bme.begin(0x76)) { +
-    Serial.println("​Could not find a valid BME280 sensor, check wiring!"​);​ +
-    while (1); +
-  } +
-}+
  
 bool deviceConnected = false; bool deviceConnected = false;
Line 180: Line 171:
  
   // Start BME sensor   // Start BME sensor
-  ​initBME();+  ​while (!BME680.begin(I2C_STANDARD_MODE)) {  // Start BME680 using I2C, use first device found 
 +    Serial.print(F("​- ​ Unable to find BME680. Trying again in 5 seconds.\n"​));​ 
 +    delay(5000);​ 
 +  }  // of loop until device is located 
 +  Serial.print(F("​- Setting 16x oversampling for all sensors\n"​));​ 
 +  BME680.setOversampling(TemperatureSensor,​ Oversample16); ​ // Use enumerated type values 
 +  BME680.setOversampling(HumiditySensor,​ Oversample16); ​    // Use enumerated type values 
 +  BME680.setOversampling(PressureSensor,​ Oversample16); ​    // Use enumerated type values 
 +  Serial.print(F("​- Setting IIR filter to a value of 4 samples\n"​));​ 
 +  BME680.setIIRFilter(IIR4); ​ // Use enumerated type values 
 +  Serial.print(F("​- Setting gas measurement to 320\xC2\xB0\x43 for 150ms\n"​)); ​ // "​degC"​ symbols 
 +  BME680.setGas(320,​ 150);  // 320 deg.C for 150 milliseconds
  
   // Create the BLE Device   // Create the BLE Device
Line 212: Line 214:
 void loop() { void loop() {
   if (deviceConnected) {   if (deviceConnected) {
-    // Read temperature as Celsius ​(the default+    ​ 
-    float t = bme.readTemperature(); +  static int32_t ​ temp, hum, pres, gas;  ​// BME readings 
-    // Read humidity +   
-    float h = bme.readHumidity(); +  BME680.getSensorData(temp, hum, pres, gas);  // Get readings 
-    // Read pressure +  ​ 
-    float p = bme.readPressure()/100.0F;+  float t = (float)((int8_t)(temp / 100)); 
 +  t += (float)((uint8_t)(temp % 100))/100.0; 
 +  
 +  ​float h = (float)((int8_t)(hum / 1000)); 
 +  h += (float)((uint16_t)(hum % 1000)))/1000.0; 
 +  
 +  float p = (float)((int16_t)(pres / 100)); 
 +  p += (float)((uint8_t)(pres % 100))/100.0;
     ​     ​
     //Notify temperature reading     //Notify temperature reading
Line 251: Line 260:
 </​code>​ </​code>​
  
 +Upload the code to your board. After uploading, open the Serial Monitor, and restart the Sparrow by pressing the RST/EN button. ​
 +
 +You should get a //"​Waiting a client connection to notify..."//​ message in the Serial Monitor.
 +
 +Then, go to your smartphone, open the nRF Connect app from Nordic, and start scanning for new devices. You should find a device called **Sparrow_BME680**,​ this is the BLE server name you defined earlier.
 +
 +Connect to it. You’ll see that it displays the Environmental Sensing service with the temperature,​ humidity, and pressure characteristics. Click on the down arrows to activate the notifications.
 +
 +Then, click on the second icon (the one that looks like a " mark) at the left to change the format. You can change to unsigned int for all characteristics. You’ll start seeing the temperature,​ humidity, and pressure values being reported every 10 seconds.
 +
 +===== Web BLE Application =====
 +
 +Follow the tutorial [[https://​randomnerdtutorials.com/​esp32-web-bluetooth/​| here]] to learn how to create a Web application that connects directly to your Sparrow ESP32 board. You can use the web app just like a normal phone application to send and receive information over BLE from your device. ​
 +
 +<note important>​Web BLE is not currently supported by iOS phones </​note>​
 +
 +Build the application in the tutorial and deploy the web page in your GitHub account. ​
 +
 +=== Assignment ===
 +<​note>​ Modify the web page and the BLE app to display the BME680 sensor data (temperature,​ pressure and humidity). </​note>​
iothings/laboratoare/2022/lab7.1700509059.txt.gz · Last modified: 2023/11/20 21:37 by dan.tudose
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0