Difference between revisions of "Micro-controller based sensors"

From phyphox
Jump to navigation Jump to search
 
(16 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
----
 +
'''Important! The information on this page is not exactly outdated, but for most users there is now a much easier solution to connect a microcontroller to phyphox by using our [[Arduino library]]. The information on this page is only relevant as a reference if you want to work with a board not supported by our library or if you want to do something unusual.'''
 +
----
 +
 
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.
 
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.
  
Line 4: Line 8:
  
 
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.
 
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.
 +
 +
 +
This sketch was used succecfully with the following setups (UPDATED):
 +
* Arduino Leonardo and RedBearLab BLE Shield
 +
* Arduino Mega 2560 + Bluefruit LE (nRF8001)
 +
* ESP32
  
  
 
== Using a Arduino: ==
 
== Using a Arduino: ==
 +
 +
'''This Sketch does not work with the newest Arduino IDE currently! Please use the Arduino-IDE 1.8.5 (other versions can maybe work too)'''
 +
 
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.
 
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.
  
Line 23: Line 36:
 
''(In the Arduino IDE) Sketch > Include Library > Add .ZIP Library... > select the downloaded file > Open''
 
''(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 notifies this characteristic and plots the received values.
+
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.
 +
 
 +
[[Media:analogReadTutorial.phyphox]]
  
 
[[File:BLE_Arduino.ino]]
 
[[File:BLE_Arduino.ino]]
  
[[File:analogReadTutorial.phyphox]]
+
<pre>
 +
//#############################################################################
 +
//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 =    BLEFloatCharacteristic("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;
 +
}
 +
</pre>
  
 
== Using a ESP32: ==
 
== Using a ESP32: ==
Line 60: Line 141:
 
''(In the Arduino IDE) Sketch > Include Library > Add .ZIP Library... > select the downloaded file > Open''
 
''(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 notifies this characteristic and plots the received values.
+
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.
 +
 
 +
 
 +
[[Media:analogReadTutorial.phyphox]]
 +
 
 +
[[File:BLE-ESP32.ino]]
 +
 
 +
<pre>
 +
//#############################################################################
 +
//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>
 +
#include <BLE2902.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;
 +
 
 +
void setup() {
 +
Serial.begin(9600);
 +
 
 +
//BLE server is being initialized
 +
//create one BLEService and one Characteristic
 +
BLEDevice::init("Arduino");
 +
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 
 +
                  );
 +
aCharacteristic->addDescriptor(new BLE2902());
 +
 
 +
//BLE server is being started
 +
aService->start();
 +
BLEAdvertising *aAdvertising = aServer->getAdvertising();
 +
aAdvertising->start();
 +
 
 +
}
 +
 
 +
void loop() {
 +
 
 +
//analog Input 36 is read out and the measured voltage is calculated
 +
 +
float voltage = 3.3*analogRead(36)/4095;
  
[[File:BLE_ESP32.ino]]
+
aCharacteristic->setValue(voltage);
 +
aCharacteristic->notify();
 +
delay(100);
  
[[File:analogReadTutorial.phyphox]]
+
}
 +
</pre>

Latest revision as of 21:18, 25 August 2020


Important! The information on this page is not exactly outdated, but for most users there is now a much easier solution to connect a microcontroller to phyphox by using our Arduino library. The information on this page is only relevant as a reference if you want to work with a board not supported by our library or if you want to do something unusual.


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.


This sketch was used succecfully with the following setups (UPDATED):

  • Arduino Leonardo and RedBearLab BLE Shield
  • Arduino Mega 2560 + Bluefruit LE (nRF8001)
  • ESP32


Using a Arduino:

This Sketch does not work with the newest Arduino IDE currently! Please use the Arduino-IDE 1.8.5 (other versions can maybe work too)

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.

Media:analogReadTutorial.phyphox

File:BLE Arduino.ino

//#############################################################################
//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 =     BLEFloatCharacteristic("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.


Media:analogReadTutorial.phyphox

File:BLE-ESP32.ino

//#############################################################################
//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>
#include <BLE2902.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;

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

 //BLE server is being initialized
 //create one BLEService and one Characteristic
 BLEDevice::init("Arduino");
 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  
                   );
 aCharacteristic->addDescriptor(new BLE2902());

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

}

void loop() {

 //analog Input 36 is read out and the measured voltage is calculated
 
 float voltage = 3.3*analogRead(36)/4095;

 aCharacteristic->setValue(voltage);
 aCharacteristic->notify();
 delay(100);

}