GithubHelp home page GithubHelp logo

ble-sensing's Introduction

Ble-Sensing

A Crowd Sensor using ESP32 BLE capabilities and Arduino IDE

Main Achievements

  1. A BLE advertising counter for crowd sensing using ESP32;
  2. MQTT integration;
  3. Struct to JSON, being then the JsonObject sent to a MQTT broker;
  4. Timers and newSightings detection functions;
  5. Interaction with Serial Monitor via commands.

Setup Notes

Unfortunately, the availability (and documentation) of libraries for the ESP32 is significantly lower when compared to the ESP8266. Therefore, the effort to setup both the board and the libraries may be somehow annoying! The following lines describe the main steps:

  1. Firstly, you are required to install Arduino ESP32 support on Windows. Fortunately, all the steps, including how to update to the latest code, are clearly described in https://github.com/espressif/arduino-esp32/blob/master/docs/arduino-ide/windows.md ;
  2. As we are using several distinct libraries, when uploading the sketch to the board you will surely receive an error message saying there isn't enough memory to deploy it! As workaround, you will need to increase the amount of space available for the program storage. To do that you will need to navigate to <ARDUINO_ESP32>/tools/partitions (in my case ARDUINO_ESP32 corresponds to C:\Users\user\Documents\Arduino\hardware\espressif\esp32), backup the default.csv file and edit the partitions size.

alt text

Then, navigate to <ARDUINO_ESP32>/boards.txt and edit the ESP32 Dev Module maximum size entry. A detailed step-by-step is available in https://desire.giesecke.tk/index.php/2018/04/20/change-partition-size-arduino-ide/ ;

alt text

  1. Now that ESP32 is working in Windows and that we have enough memory for big heavy sketches, on the Tools tab define the correct options (board, port, flash, ...);

alt text

  1. It is now time to install all the required librabries (you will probably just need to install ArduinoJson and PubSubClient, the others should be already installed). Unfortunately (I know, again!), there is a final tweak: the PubSubClient library limits the payload to 128 bytes on the MQTT_MAX_PACKET_SIZE variable. As we may sense several devices, the JSON may get quite big and this space will prevent the payload from being sent to the MQTT broker! You must navigate to ..\libraries\PubSubClient\src, open the file entitled as PubSubClient.h and increase the MQTT_MAX_PACKET_SIZE value (I've changed it 4096);
  2. Finally, redefine some variables on the sketch, namely the MQTTSERVER, MQTTPORT, MQTTUSER, MQTTPASSWORD, STATION_NETWORK and STATION_PASSWORD, and upload the sketch! Turn on the Serial Monitor for detailed information!

Additional Notes:

  • Officially, PubSubClient library only supports ESP8266. Nonetheless, it seems to work fine in ESP32;
  • PubSubClient publish() works with a QoS of 0: there are no guarantees data will reach the broker;
  • If no WiFi is available then the board will still scan for BLE devices on your request (Scan command);
  • If WiFi is available then the timer will start automatically (and will wake up all sendTimer seconds), with a scan time of scanTime seconds;
  • As MQTT broker you can use CloudMQTT (they have a free plan) and as client you can use, for example, HiveMQ or even CloudMQTT.

ESP32 restrictions: As I have said before, ESP32 is still on an embrionary stage. Just to let you know, my first approach was to send the sensed data to Firebase. However, after many battles with different libraries, many problems were arising and I decided to find a new alternative. I decided to send the data over an HTTP POST request to a server running php so that I could store the data. Many problems again, mainly due to BLE and WiFi libraries compatibility (BLEDevice::init was breaking the HTTP request and I couldn't find the reason why). Finally, as last attempt, I decided to send the data to a MQTT Broker, which is now working!

Available Commands

You may open the Serial Monitor to get detailed information. It also accepts the following commands:

  • Stop - stops the handlers that capture the data
  • Start - starts the handlers if they have been stopped previously
  • Scan - starts a BLE scan
  • Count - prints the number of distinct detected devices
  • Print - prints the entire list of detected devices
  • Clear - clears the entire list of detected devices
  • Send - publishes the list of detected devices in JSON to the specified MQTT server
  • Start Timer - starts a timer that sends the list of detected devices every sendTimer seconds, in JSON, to the specified MQTT server
  • Stop Timer - stops the timer

ble-sensing's People

Contributors

brunofmf avatar

Watchers

Rafael Magalhães avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.