Difference between revisions of "Micro-controller based sensors"

From phyphox
Jump to navigation Jump to search
Line 32: Line 32:
 
//#############################################################################
 
//#############################################################################
 
//In this sketch a BLE server with one service and one characeristic is created.
 
//In this sketch a BLE server with one service and one characeristic is created.
//The analog input (pin 36) is readout periodically.  
+
//The analog input (pin A0) is readout periodically with the timer1 library.
 +
//After each period cycle, the timer1 interrupt set a flag to start the adc.
 
//#############################################################################
 
//#############################################################################
  
#include <BLEDevice.h>
+
#include <BLEPeripheral.h>
#include <BLEUtils.h>
+
#include <TimerOne.h>
#include <BLEServer.h>
+
#include <SPI.h>
#include <esp_system.h>
+
 
 +
 
 +
//Those pin numbers can be different if you use another bluetooth low energy shield.
 +
//For example:  REQ,RDY,RST
 +
//RedBearLab      9,  8,  7
 +
//Bluefruit LE  10,  2,  9
 +
BLEPeripheral blePeripheral = BLEPeripheral(9, 8, 7);
  
 
//uuid's can be generated on https://www.uuidgenerator.net/
 
//uuid's can be generated on https://www.uuidgenerator.net/
#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
+
//create a BLEService and a Characteristic where one can store a Float value
#define CHARACTERISTIC_UUID "59f51a40-8852-4abe-a50f-2d45e6bd51ac"
+
//uuid for the BLE Service is set
BLECharacteristic *aCharacteristic;
+
BLEService analogService = BLEService("137e939e-dd26-4dec-a262-0608d382549f"); 
 +
//uuid for the characeristic is set. This uuid is used in the phyphox file
 +
BLEFloatCharacteristic analogCharacteristic =    BLEFloatCharacteristi<c("59f51a40-8852-4abe-a50f-2d45e6bd51ac", BLERead | BLENotify);
  
uint8_t analogValue;
+
bool readA0;
 
 
void setup() {
 
void setup() {
   Serial.begin(9600);
+
   //######################################################################
 +
  // The Bluetooth server will be initialized.
 +
  //######################################################################
 +
 
 +
  blePeripheral.setLocalName("Arduino");
 +
  blePeripheral.setAdvertisedServiceUuid(analogService.uuid());
 +
  blePeripheral.addAttribute(analogService);
 +
  blePeripheral.addAttribute(analogCharacteristic);
 +
  blePeripheral.begin();
  
   //BLE server is being initialized
+
   //######################################################################
   //create one BLEService and one Characteristic
+
  // The timer1 interrupt will be initialized.
   BLEDevice::init("MyESP32");
+
  //######################################################################
   BLEServer *aServer = BLEDevice::createServer();
+
   // The interrupt will be every 0.2 seconds.
 +
   Timer1.initialize(0.2 * 1000000);
 +
   Timer1.attachInterrupt(timerHandler);
 
    
 
    
  //uuid for the BLE service is set
+
}
  BLEService *aService = aServer->createService(SERVICE_UUID);
+
 
 +
void loop() {
 
    
 
    
   //uuid for the BLE characteristic is set
+
   blePeripheral.poll();
  //the characteristics properties are defined
 
  aCharacteristic = aService->createCharacteristic(
 
                      CHARACTERISTIC_UUID,
 
                      BLECharacteristic::PROPERTY_READ  |
 
                      BLECharacteristic::PROPERTY_WRITE  |
 
                      BLECharacteristic::PROPERTY_NOTIFY
 
                    );
 
  
   //BLE server is being started
+
   if(readA0){
  aService->start();
+
    setAnalogValue();
  BLEAdvertising *aAdvertising = aServer->getAdvertising();
+
   }
   aAdvertising->start();
 
  
 
}
 
}
  
void loop() {
+
void setAnalogValue() {
 
+
   // readout analog input and write the value into the characeristic
   //analog Input 36 is read out and saved into the 8bit unsigned int "analogValue"
+
   int analogValue = analogRead(A0);
   analogValue = analogRead(36);
+
   analogCharacteristic.setValue(analogValue);
   //the measured value is written to the characteristic
+
   readA0=false;
  aCharacteristic->setValue(&analogValue,1);
+
}
   aCharacteristic->notify();
 
  delay(100);
 
  
 +
void timerHandler() {
 +
  readA0 = true;
 
}
 
}
 
</pre>
 
</pre>
Line 126: Line 137:
 
//#############################################################################
 
//#############################################################################
 
//In this sketch a BLE server with one service and one characeristic is created.
 
//In this sketch a BLE server with one service and one characeristic is created.
//The analog input (pin A0) is readout periodically with the timer1 library.
+
//The analog input (pin 36) is readout periodically.  
//After each period cycle, the timer1 interrupt set a flag to start the adc.
 
 
//#############################################################################
 
//#############################################################################
  
#include <BLEPeripheral.h>
+
#include <BLEDevice.h>
#include <TimerOne.h>
+
#include <BLEUtils.h>
#include <SPI.h>
+
#include <BLEServer.h>
 
+
#include <esp_system.h>
 
 
//Those pin numbers can be different if you use another bluetooth low energy shield.
 
//For example:  REQ,RDY,RST
 
//RedBearLab      9,  8,  7
 
//Bluefruit LE  10,  2,  9
 
BLEPeripheral blePeripheral = BLEPeripheral(9, 8, 7);
 
  
 
//uuid's can be generated on https://www.uuidgenerator.net/
 
//uuid's can be generated on https://www.uuidgenerator.net/
//create a BLEService and a Characteristic where one can store a Float value
+
#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
//uuid for the BLE Service is set
+
#define CHARACTERISTIC_UUID "59f51a40-8852-4abe-a50f-2d45e6bd51ac"
BLEService analogService = BLEService("137e939e-dd26-4dec-a262-0608d382549f"); 
+
BLECharacteristic *aCharacteristic;
//uuid for the characeristic is set. This uuid is used in the phyphox file
 
BLEFloatCharacteristic analogCharacteristic =    BLEFloatCharacteristi<c("59f51a40-8852-4abe-a50f-2d45e6bd51ac", BLERead | BLENotify);
 
  
bool readA0;
+
uint8_t analogValue;
 +
 
void setup() {
 
void setup() {
   //######################################################################
+
   Serial.begin(9600);
  // The Bluetooth server will be initialized.
 
  //######################################################################
 
  
  blePeripheral.setLocalName("Arduino");
+
   //BLE server is being initialized
  blePeripheral.setAdvertisedServiceUuid(analogService.uuid());
+
   //create one BLEService and one Characteristic
  blePeripheral.addAttribute(analogService);
+
   BLEDevice::init("MyESP32");
  blePeripheral.addAttribute(analogCharacteristic);
+
   BLEServer *aServer = BLEDevice::createServer();
  blePeripheral.begin();
 
 
 
   //######################################################################
 
  // The timer1 interrupt will be initialized.
 
  //######################################################################
 
   // The interrupt will be every 0.2 seconds.
 
   Timer1.initialize(0.2 * 1000000);
 
   Timer1.attachInterrupt(timerHandler);
 
 
    
 
    
}
+
  //uuid for the BLE service is set
 
+
  BLEService *aService = aServer->createService(SERVICE_UUID);
void loop() {
 
 
    
 
    
   blePeripheral.poll();
+
   //uuid for the BLE characteristic is set
 +
  //the characteristics properties are defined
 +
  aCharacteristic = aService->createCharacteristic(
 +
                      CHARACTERISTIC_UUID,
 +
                      BLECharacteristic::PROPERTY_READ  |
 +
                      BLECharacteristic::PROPERTY_WRITE  |
 +
                      BLECharacteristic::PROPERTY_NOTIFY
 +
                    );
  
   if(readA0){
+
   //BLE server is being started
    setAnalogValue();
+
  aService->start();
   }
+
  BLEAdvertising *aAdvertising = aServer->getAdvertising();
 +
   aAdvertising->start();
  
 
}
 
}
  
void setAnalogValue() {
+
void loop() {
   // readout analog input and write the value into the characeristic
+
 
   int analogValue = analogRead(A0);
+
   //analog Input 36 is read out and saved into the 8bit unsigned int "analogValue"
   analogCharacteristic.setValue(analogValue);
+
   analogValue = analogRead(36);
   readA0=false;
+
   //the measured value is written to the characteristic
}
+
  aCharacteristic->setValue(&analogValue,1);
 +
   aCharacteristic->notify();
 +
  delay(100);
  
void timerHandler() {
 
  readA0 = true;
 
 
}
 
}
 
</pre>
 
</pre>

Revision as of 17:27, 29 October 2018

With phyphox version 1.1.0 external sensors via Bluetooth Low Energy (BLE) are supported, making it possible to develop sensor modules based on a micro-controller. So far we have used two different micro-controllers. On the one hand an Arduino with a BLE shield and on the other hand the ESP32 which already supports BLE. Of course there are many other micro-controllers which support BLE that can also be used.

In this article we provide one example for each micro-controller. Both examples are quite similar to each other. The differences in the sketch occur due to the fact that two different libraries are used. Both micro-controllers are programmed with the official Arduino-IDE. The goal of these examples is to send the value of the analog input to phyphox.

Warning: This tutorial requires basic knowledge in programming of a micro-controller. If you have never used an Arduino you should do a basic Arduino tutorial first.


Using a Arduino:

The Arduino itself does not have BLE on board. To support BLE, a BLE shield has to be used. In this case we use the „BLE Shield“ by RedBearLab in combination with the Arduino Library „arudino-BLEPeripheral“ by sandeepmistry from github.

[https://github.com/sandeepmistry/arduino-BLEPeripheral ]

Furthermore the library “Timer1” is used which can be found here:

[https://playground.arduino.cc/code/timer1 ]

Since the BLE library is also used for other BLE shields like the “Bluefruit LE” by Adafruit the tutorial for this shield can also be used. Ideally, just the port numbers for the specific shield have to be changed, as can be seen on github under the topic „Pinouts“.

These zip files can be included directly via:

(In the Arduino IDE) Sketch > Include Library > Add .ZIP Library... > select the downloaded file > Open

After both libraries are installed the following sketch can be used to initialize a Bluetooth Low Energy server with one service and one characteristic. In this sketch the analog input A0 is readout and the value is written into the characteristic. The additional phyphox file subscribes to notifications from this characteristic and plots the received values.

File:BLE Arduino.ino

File:AnalogReadTutorial.phyphox

//#############################################################################
//In this sketch a BLE server with one service and one characeristic is created.
//The analog input (pin A0) is readout periodically with the timer1 library.
//After each period cycle, the timer1 interrupt set a flag to start the adc.
//#############################################################################

#include <BLEPeripheral.h>
#include <TimerOne.h>
#include <SPI.h>


//Those pin numbers can be different if you use another bluetooth low energy shield.
//For example:  REQ,RDY,RST
//RedBearLab      9,  8,  7
//Bluefruit LE   10,  2,  9
BLEPeripheral blePeripheral = BLEPeripheral(9, 8, 7); 

//uuid's can be generated on https://www.uuidgenerator.net/
//create a BLEService and a Characteristic where one can store a Float value
//uuid for the BLE Service is set
BLEService analogService = BLEService("137e939e-dd26-4dec-a262-0608d382549f");  
//uuid for the characeristic is set. This uuid is used in the phyphox file
BLEFloatCharacteristic analogCharacteristic =     BLEFloatCharacteristi<c("59f51a40-8852-4abe-a50f-2d45e6bd51ac", BLERead | BLENotify);  

bool readA0;
void setup() {
  //######################################################################
  // The Bluetooth server will be initialized.
  //######################################################################

  blePeripheral.setLocalName("Arduino");
  blePeripheral.setAdvertisedServiceUuid(analogService.uuid());
  blePeripheral.addAttribute(analogService);
  blePeripheral.addAttribute(analogCharacteristic);
  blePeripheral.begin();

  //######################################################################
  // The timer1 interrupt will be initialized.
  //######################################################################
  // The interrupt will be every 0.2 seconds.
  Timer1.initialize(0.2 * 1000000);
  Timer1.attachInterrupt(timerHandler);
  
}

void loop() {
  
  blePeripheral.poll();

  if(readA0){
    setAnalogValue();
  }

}

void setAnalogValue() {
  // readout analog input and write the value into the characeristic
  int analogValue = analogRead(A0);
  analogCharacteristic.setValue(analogValue);
  readA0=false;
}

void timerHandler() {
  readA0 = true;
}

Using a ESP32:

In this example we use the HIMALAYA ESP32 Dev Board. Since the ESP32 isn't an official Arduino Board, a library to support this micro-controller in the Arduino-IDE has to be used. To do so start the Arduino-IDE and open: File > Preferences Then past the following link into the Field “Additional Board Manager URLs:”

https://dl.espressif.com/dl/package_esp32_index.json

Afterwards open the Board Manager:

Tools > Board > Boardmanager

Boardsmanager.png

Search for ESP32 platform and install the library. Choose the Board DOIT ESP32 DEVKIT V1

Tools > Board: ”DOIT ESP32 DEVKIT V1”

To check if the library is installed correctly, flash one of the basic examples on the micro-controller:

File > Examples > Basic > Blink

After the sketch is flashed on the ESP32 the LED should start flashing. Afterwards a library has to be installed in order to use BLE on the ESP32. For Example the BLE library by Neil Kolban which can be found on github:

[https://github.com/nkolban/ESP32_BLE_Arduino ]

Download the library as a zip file. These zip file can be included directly via

(In the Arduino IDE) Sketch > Include Library > Add .ZIP Library... > select the downloaded file > Open

After both libraries are installed the following sketch can be used to initialize a Bluetooth Low Energy server with one service and one characteristic. In this sketch the analog input on Pin 36 is readout and the value is written into the characteristic. The additional phyphox file subscribes to notifications from this characteristic and plots the received values.

File:BLE ESP32.ino

File:AnalogReadTutorial.phyphox

//#############################################################################
//In this sketch a BLE server with one service and one characeristic is created.
//The analog input (pin 36) is readout periodically. 
//#############################################################################

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <esp_system.h>

//uuid's can be generated on https://www.uuidgenerator.net/
#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "59f51a40-8852-4abe-a50f-2d45e6bd51ac"
BLECharacteristic *aCharacteristic;

uint8_t analogValue;
 
void setup() {
  Serial.begin(9600);

  //BLE server is being initialized
  //create one BLEService and one Characteristic
  BLEDevice::init("MyESP32");
  BLEServer *aServer = BLEDevice::createServer();
  
  //uuid for the BLE service is set
  BLEService *aService = aServer->createService(SERVICE_UUID);
  
  //uuid for the BLE characteristic is set
  //the characteristics properties are defined
  aCharacteristic = aService->createCharacteristic(
                      CHARACTERISTIC_UUID,
                      BLECharacteristic::PROPERTY_READ   |
                      BLECharacteristic::PROPERTY_WRITE  |
                      BLECharacteristic::PROPERTY_NOTIFY 
                    );

  //BLE server is being started
  aService->start();
  BLEAdvertising *aAdvertising = aServer->getAdvertising();
  aAdvertising->start();

}

void loop() {

  //analog Input 36 is read out and saved into the 8bit unsigned int "analogValue"
  analogValue = analogRead(36);
  //the measured value is written to the characteristic
  aCharacteristic->setValue(&analogValue,1);
  aCharacteristic->notify();
  delay(100);

}