Now Reading
Constructing an occupancy sensor with a $5 ESP32 and a serverless DB

Constructing an occupancy sensor with a $5 ESP32 and a serverless DB

2023-11-13 11:17:14

Have you ever ever needed to design a full end-to-end software program answer to gather occupancy information throughout a university campus?

I did not suppose I might both, however right here we’re.

The inspiration

Throughout my first yr in faculty, we had Sodexo as our eating supplier. That they had a contract with Bluefox, who offers occupancy sensors to report the variety of folks inside a eating corridor. I might wish to suppose they used this information to enhance eating corridor operations. I could not let you know what they really used it for. I can say that after some FOIA requests a buddy made that returned PDF’s with terrible kerning, these units work by counting smartphone MAC addresses from Bluetooth promoting packets. This was a reasonably cool approach for me to keep away from crowds within the eating corridor – toss the API name into Grafana, and you’ve got a dwell chart of how busy the eating halls are.

The downfall

Sadly, the college switched eating corridor suppliers to Aramark. Aramark doesn’t contract Bluefox to offer the identical occupancy counts, which meant no extra occupancy information, which meant no extra skipping busy hours.

The climb again

The concept of monitoring occupancy metrics with a bluetooth beacon was caught in my head. What design choices and concerns would it’s essential make?

  • How correct is BLE beacon depend, as a proxy for occupancy?
    • Some folks carry round headphones, smartwatches, and so forth – however some do not carry any units in any respect (or maintain Bluetooth off on their telephone).
  • How correct is BLE beacon availability time, as a proxy for dwell time?
    • Can we use distinctive MAC addresses’ churn to detect this?
    • Is the built-in MAC tackle randomization that’s widespread throughout many completely different manufacturers going to impression this? (Regardless that this privateness characteristic has flaws)
  • How can we talk the outcomes again to a central server?
    • WiFi appears the plain alternative for me. Not each location may have easy-to-access WiFi, although.
    • LoRa may very well be an possibility, relying on the distribution of beacons. Here is some range testing numbers, although that is affected by the antenna’s acquire and areas (this is another test with a far larger vary).
  • How can we accumulate the information?
    • Ought to we use a time collection database?
  • How can we analyze the information?
    • Can we predict traits within the longer time spans, excluding particular occasions like homecoming weekend, finals week, and so forth?

I discovered these questions tumbling round in my head, so I started with some preliminary testing – writing some easy code to depend the variety of units detected on my laptop computer’s Bluetooth adapter. Success – it was surprisingly simple to jot down code to scan for x seconds each y seconds and reserve it to a SQLite database at common intervals. So, I carried my laptop computer to eating halls, Chick-Fil-A, Starbucks, and so forth and waited.

And waited.

And waited.

I spent numerous time gathering information whereas sipping on coffees and milkshakes. You realize, for information assortment functions. That is all very tutorial, after all.

​No different motive.

​Anyway, accuracy – In smaller areas (like a single-room Starbucks), I discovered the depend to be fairly correct. On the very least it mirrored traits in occupancy in a short time. When extra folks arrived, the charts rapidly climbed.

In bigger areas like eating halls, the counts appeared correct by my guesstimate. There is no approach for me to depend everybody in a eating corridor, with difficult layouts and completely different seating areas, particularly once I’m not sure of my bluetooth adapter’s vary (is it selecting up folks on the terrace by the partitions? and so forth). Nevertheless it most undoubtedly matched the traits round class modifications – when courses bought out, folks went to eat, which quickly elevated the variety of folks I noticed, and the variety of beacons my laptop computer detected.

Long run deployment

Okay, sounds nice, it seems to be like we’ve got some sort of methodology validation. However I do not plan on cloning myself in each eating corridor and sitting 24/7, so what can we do to create a small machine to gather the identical information?

Raspberry Pi – Perhaps?

My first thought was – Raspberry Pi Zero W. It is small and low cost, has Wi-Fi and Bluetooth, and undoubtedly is in inventory someplace on Earth.

I rewrote my easy code (in Rust, no much less!) to deal with every part gracefully (reboots, no community, adapter loss, and so forth). Linux Bluetooth is extremely painful to deal with in a headless approach. Binding to DBus requires cross-compiler magic and never even Cross was getting me out of it. After struggling sufficient by 1,000,000 completely different compiler flags, an influence outage that prompted me to lose my progress on the Makefile (additionally, sure I take advantage of Make with Rust), and eventually organising a QEMU bridge, I used to be in a position to get my binary to run on my Pi. I even wrote all the patching magic to make it hook up with Wi-Fi (attempt doing THAT headlessly, when there is a portal you need to signal into!), set up the required libraries on start-up, make a service to run my executable, and automagically replace once I push a brand new replace. Okay, that is numerous transferring components. Let’s boot it up and hope it really works…

Nothing.

That is proper, completely nothing labored. Not even the automagic wifi connection by way of Mac tackle fiddling hacks.

Shifting on

Should you’re smarter than me, you might have realized that is a bit WAY an excessive amount of complexity. We actually don’t want a complete Linux kernel in any respect. We’d like two issues – dependable Wi-Fi, and dependable Bluetooth. Okay, so shelve the Pi Zero W and Orange Pi Zero W I purchased. What’s this nonsense a few machine that may do these two issues at an excellent cheaper value and smaller footprint?…

ESP32?

ESP32C3

On paper, it seems to be nice – Wi-Fi, Bluetooth, extraordinarily low energy utilization (🌱🌍♻️🌿🌞💚), very low cost, and really tiny.

I bought one off of Amazon since I did not need to look ahead to abroad transport (not dropping momentum in a nerd snipe like that is CRITICAL). I purchased a random ESP32-WROOM-32 with an OLED display, since I believed it could be cool to show the information on the display screen dwell. I rewrote my information assortment code in C++ type (away from Rust!) for the reason that Rust ecosystem for ESP32 shouldn’t be all the way in which there but.

After fiddling with the show code sufficient to get it working (SSD1306Wire show(0x3c, 5, 4); for those who’re questioning), it labored nice. I requested campus IT to whitelist the MAC tackle, wrote up some Cloudflare features right into a D1 database as my information ingest, and got down to work.

Deployment

I hid positioned my information assortment machine in my campus library on a crisp fall morning, sat myself down at my laptop computer, noticed the information rolling in, and did a silent celebration. Off to my Ideas class to find out about scope guidelines then…

A picture showing my chart rapidly falling as it peaks
Why did everybody go away Swem library and by no means come again???

Obstacles

One main downside that I bumped into was the poor specs of the random ESP32 machine I had.

The above concern exhibits the machine crashing at about 250 units. At first, I used to be fearful this was a bug with the consequence depend being saved in a 1-byte quantity, like a u8 (thus capping at ~255 units). A fast Serial.print made me understand it could crash additionally at about 249, 265, and so forth – randomly round this space. So, not a bit-overflow concern (no less than, not within the integer half!).

Our library fills up rapidly with studious twamps – it would not final a minute in finals season if it could not deal with any greater than that.

The issue – recognized

By saving the outcomes throughout a scan into a knowledge construction till the tip of the scan, it piled up information in regards to the machine’s scan energy, marketed providers, producer ID, and so forth. Whereas this looks like nice information to gather, I had one factor in thoughts – the variety of distinctive units (for now).

By debugging the heap dimension consistently, I spotted that the scan outcomes was filling up the small quantity of RAM it had. This was dangerous information – at first, I did not see a method to nonetheless accumulate the information whereas not really gathering the information.

Decision

I made a decision to brush up on my C++ information constructions programming and write a small hashset. In spite of everything, the information construction for the scan outcomes was very bloated – if I may override that, I might have the ability to management the heap dimension extra intently, proper?

On each callback, then, we would insert the MAC tackle right into a hashset, then clear the built-in consequence construction to permit for extra reminiscence.

Code

Sadly, this isn’t ideally suited – if and provided that there’s some outcomes to verify for duplicates, the callback is barely known as on new units. Once we clear this each time, we give it amnesia, thus each single BLE commercial packet causes a callback. We do verify for duplicates in addToSet (it’s a hashset!), however this can undoubtedly trigger a whole bunch of duplicate callbacks to our hashset, and heap thrashing since we’re allocating and deallocating the consequence construction each single callback. That is okay (for now), it is higher to repeatedly verify a hashmap with not more than 1000 entries (a really fast process) fairly often, than have our capability restricted to 250 folks.

Extra obstacles

Okay, we now have an ideal method to scan for units for lengthy durations of time. Oh would you have a look at the calendar – it is fall break! I am going to go away it within the library and have it report again so I can see how packed it’s over break (sure, there will likely be learning accomplished on campus over break – we’re a nerdy faculty). Good, even some long-term testing over the 5 day weekend! Absolutely nothing dangerous will happ-

A picture showing my chart rapidly falling again
That is about 400 units earlier than the crash – on a fall time without work in a university. As talked about – nerdy.

Okay, superior. One other concern, one which manifests itself after 3 hours of use with zero indication of points. After combating with the debugger for lengthy sufficient, and even tossing in periodic reboots (it boots very fast so this provides virtually no time in any respect), I chalked this as much as a nasty board/BT adapter – it appeared to run, nevertheless it immediately returned zero units on each scan. That is okay – whereas I used to be messing with this Amazon one, I purchased fairly just a few others, together with the remainder I’ve purchased over the course of the entire undertaking.

A picture showing several devices I've purchased over the course of this project
Seeed Studio XIAO ESP32S3/C3, WaveShare ESP32S3 Zero, Unbranded ESP32-WROOM with OLED, Orange Pi Zero W (untouched), Raspberry Pi Zero W (L->R, T->D)

After testing all of those, the one one dependable to work for lengthy durations of time (one month presently) was the XIAO ESP32C3/S3. Each work acceptably, however I made a decision to go together with the C3, because it’s RISC-V, which is superior (for my beliefs), and because it’s cheaper, which can also be superior (for my pockets).

One other advantage of switching to a greater producer is extra SRAM – I used to be in a position to change away from my hand-written hashmap implementation, for the reason that RAM was in a position to maintain the outcomes information construction significantly better with out crashing. I’ve seen as excessive as 1000 units detected with no signal of slowing down. This in all probability reduces CPU utilization as nicely – no extra heap and callback churn!

After I discovered a tool that labored much better, I moved my deployment location to my dorm room window so I might have a better deployment cycle – right here it’s with quite a few tutorial buildings within the background.

A picture showing a XIAO ESP32C3 turned on and scanning
My RISC-V primarily based ESP32C3, scanning from my dorm room window in entrance of Washington Corridor, throughout homecoming weekend.

Remaining information assortment

Now that I’ve gotten my information assortment working efficiently, let’s take a look at the information for at some point.

A screenshot of a chart showing occupancy stats - it shows peaks at times where you'd expect there to be
Knowledge assortment for at some point. Discover the peaks? That is proper in regards to the time that courses change.

There’s one thing to notice about this. The machine could be in my dorm, nevertheless it largely shouldn’t be restricted to the dorm’s personal inhabitants. In any other case, it could be at its max within the early morning and drop because the day went on. If I needed it to measure completely dorm inhabitants, I might in all probability place it extra centrally (as a substitute of out a window), nevertheless it nonetheless wouldn’t be nice at this, since dorms are the worst at permeating bluetooth indicators by many partitions.

That is okay, I am going to simply maintain that in thoughts as I analyze the information – it is largely selecting up college students as they go into the 2 nearest tutorial buildings, not the dorm inhabitants.

The peaks begin up round 7:50, proper earlier than the 8 am courses begin in Ewell and Washington halls. I believe that is detecting the scholars initially leaving dorms, shuffling alongside to their courses, and the drop is as college students enter the buildings. Then, the height at 8:50 might be as college students go away their 8 AMs and go to their 9 AMs, coming into the vary of the machine solely to instantly go away it because the numbers drop at 9 AM. Identical for 9:50/10 AM, and 10:50/11 AM. These are all class change instances.

These all level to methodology validation – it looks like this machine actually is nice at monitoring traits within the motion of scholars round it. The antenna can also be seemingly very excessive vary – I did not count on it to succeed in the ~160ft into Washington Corridor, and the ~100ft into Ewell. The altitude of being on the third ground in all probability helps with it, although.

Time collection forecasting?

This looks like the proper goal for time collection forecasting, like with NeuralProphet. The information is chock-full of hourly, every day, and weekly traits. I added the performance to foretell these traits and to date, it is superb at predicting every day traits; the longer (week, month, and season-long) traits will doubtless converge after sufficient information is collected.

Additional ideas

After all, this isn’t a solved undertaking. I’ve written the code to parse this right into a Cloudflare DB, into Grafana, and a few forecasting, however there’s extra to be accomplished.

I carried out an unlimited quantity of literature evaluate over the course of this undertaking – studying dozens of analysis papers on the subject, discovering out what works and what did not, what I might try to what I might keep away from. Many questions are raised in these papers, that I nonetheless need to reply.

A photograph of a stack of research papers, annotated with sticky notes
Hours of doc evaluate

For instance, how nicely of a proxy is a BLE beacon depend for precise inhabitants depend?

  • Does this rely upon the demographics? How will we discover a correction issue (like for x beacons, there’s roughly 0.7x folks, given the a number of units folks carry round)?
  • For instance, within the laptop science constructing, is the linear fee to inhabitants larger, since we feature so many devices round? Or is it decrease, as a result of we all know to show off Bluetooth when not utilizing it (BT firmware zero days, you see)?
  • Or within the employees constructing, is it decrease in comparison with college students, since they’re much less prone to carry round a bunch of tech?
  • Perhaps the speed is decrease in a eating corridor, since not many individuals are utilizing a number of units like they might in a lecture corridor (laptop computer, iPad, and so forth for notes)?

Different questions got here to thoughts, like:

  • Can we enhance the accuracy by setting an RSSI minimal, for which units weaker than it don’t depend, to make sure solely those that are actually close by get counted?
  • Can we enhance the accuracy by filtering by producer ID, so it is solely Apple + widespread Android producers? Would this assist, since Apple Watches, AirPods, MacBooks, and so forth would all nonetheless add to the depend?
  • What sorts of privateness accomodations do I would like to bear in mind? I already solely observe pure beacon numbers. I do not observe precise MAC addresses like Bluefox did, however do I would like so as to add noise to the information? Is the prevailing information noisy sufficient to keep away from deanonymization? Is it practical to establish a single particular person within the information with out something however the variety of units?
  • What’s one of the best scan length? Too fast, and it will not discover all the a whole bunch of units that exist. Too lengthy, and we danger inaccuracy (counting units which have since left, information displaying giant drifts inside quick durations of time, and so forth).
    • Is a dynamic scan size finest? (Take into consideration cooking a bag of popcorn – when a time frame passes with none change, you are accomplished)

Numerous inquiries to reply. I plan on validating my information with real-world inhabitants information collected in a spot the place it is easy to get the “floor fact”. Perhaps I’ll attain out to a spot on campus who both already tracks occupancy (the health club with swipe in/out), or will examine extra totally in a spot the place it’s trivial to take action myself (restricted entry/exit, like a eating corridor, or a Starbucks).

Additional work

I’m not positive whether or not I will likely be taking this additional – I am presently speaking to some professors in regards to the use circumstances for some college committees, or maybe additional tutorial (and hopefully publish-worthy) analysis. I’m additionally contemplating promoting it to brick-and-mortar companies that need to measure occupancy traits. It is a fairly packaged up answer – every part from the front-end to the back-end is constructed already. That is vaguely what the setup seems to be like for any given deployment:

  • Arrange Wi-Fi in config (both have community whitelist the MAC if it is a portal, or hook up with open/password protected Wi-Fi on boot)
  • Change machine and web site IDs in config
  • Plug machine/units into outlet in central/handy areas
  • Arrange Grafana dashboard to learn every machine from backend
  • Arrange Grafana dashboard to learn predicted traits as nicely, in separate charts

Should you’re concerned with any of this, please let me know! I might love to listen to from you.

Conclusion

I hope you loved studying my weblog publish about analyzing occupancy traits and embedded growth. When you’ve got any ideas, feedback, questions, or indignant fists, you’ll be able to e mail me at maesposito@wm.edu.

Assume this was cool? Hiring software program engineering interns for Summer time 2024? Check out my resume here. I am very enthusiastic about software program growth (I did all of this with out the promise of any outcomes – all in my spare time as a result of I beloved doing it!), and I am all the time able to embark on new coding adventures.

Bibliography


Bibliography

See Also

Ahmad, J., Larijani, H., Emmanuel, R., Mannion, M., & Javed, A. (2020). Occupancy detection in non-residential buildings – A survey and novel privateness preserved occupancy monitoring answer. Utilized Computing and Informatics, 17(2), 279–295. https://doi.org/10.1016/j.aci.2018.12.001

Apolónia, F., Ferreira, P. M., & Cecílio, J. (2021). Buildings Occupancy Estimation: Preliminary Outcomes Utilizing Bluetooth Alerts and Synthetic Neural Networks. In M. Kamp, I. Koprinska, A. Bibal, T. Bouadi, B. Frénay, L. Galárraga, J. Oramas, L. Adilova, Y. Krishnamurthy, B. Kang, C. Largeron, J. Lijffijt, T. Viard, P. Welke, M. Ruocco, E. Aune, C. Gallicchio, G. Schiele, F. Pernkopf, … G. Graça (Eds.), Machine Studying and Ideas and Observe of Information Discovery in Databases (pp. 567–579). Springer Worldwide Publishing. https://doi.org/10.1007/978-3-030-93733-1_42

Baronti, P., Barsocchi, P., Chessa, S., Mavilia, F., & Palumbo, F. (2018). Indoor Bluetooth Low Vitality Dataset for Localization, Monitoring, Occupancy, and Social Interplay. Sensors, 18(12), Article 12. https://doi.org/10.3390/s18124462

Barsocchi, P., Crivello, A., Girolami, M., Mavilia, F., & Palumbo, F. (2017). Occupancy detection by multi-power bluetooth low vitality beaconing. 2017 Worldwide Convention on Indoor Positioning and Indoor Navigation (IPIN), 1–6. https://doi.org/10.1109/IPIN.2017.8115946

Billah, M. F. R. M., & Campbell, B. (2019). Unobtrusive Occupancy Detection with FastGRNN on Useful resource-Constrained BLE Units. Proceedings of the first ACM Worldwide Workshop on Gadget-Free Human Sensing, 1–5. https://doi.org/10.1145/3360773.3360874

Chen, Z., Jiang, C., & Xie, L. (2018). Constructing occupancy estimation and detection: A evaluate. Vitality and Buildings, 169, 260–270. https://doi.org/10.1016/j.enbuild.2018.03.084

Demrozi, F., Turetta, C., Chiarani, F., Kindt, P. H., & Pravadelli, G. (2021). Estimating Indoor Occupancy By way of Low-Value BLE Units. IEEE Sensors Journal, 21(15), 17053–17063. https://doi.org/10.1109/JSEN.2021.3080632

Ding, Y., Han, S., Tian, Z., Yao, J., Chen, W., & Zhang, Q. (2022). Assessment on occupancy detection and prediction in constructing simulation. Constructing Simulation, 15(3), 333–356. https://doi.org/10.1007/s12273-021-0813-8

Dodier, R. H., Henze, G. P., Tiller, D. Ok., & Guo, X. (2006). Constructing occupancy detection by sensor perception networks. Vitality and Buildings, 38(9), 1033–1043. https://doi.org/10.1016/j.enbuild.2005.12.001

Feng, C., Mehmani, A., & Zhang, J. (2020). Deep Studying-Primarily based Actual-Time Constructing Occupancy Detection Utilizing AMI Knowledge. IEEE Transactions on Good Grid, 11(5), 4490–4501. https://doi.org/10.1109/TSG.2020.2982351

Filippoupolitis, A., Oliff, W., & Loukas, G. (2016). Occupancy Detection for Constructing Emergency Administration Utilizing BLE Beacons. In T. Czachórski, E. Gelenbe, Ok. Grochla, & R. Lent (Eds.), Laptop and Data Sciences (pp. 233–240). Springer Worldwide Publishing. https://doi.org/10.1007/978-3-319-47217-1_25

Mashuk, M. S., Pinchin, J., Siebers, P.-O., & Moore, T. (2018). A wise telephone primarily based multi-floor indoor positioning system for occupancy detection. 2018 IEEE/ION Place, Location and Navigation Symposium (PLANS), 216–227. https://doi.org/10.1109/PLANS.2018.8373384

Meyn, S., Surana, A., Lin, Y., Oggianu, S. M., Narayanan, S., & Frewen, T. A. (2009). A sensor-utility-network methodology for estimation of occupancy in buildings. Proceedings of the 48h IEEE Convention on Resolution and Management (CDC) Held Collectively with 2009 twenty eighth Chinese language Management Convention, 1494–1500. https://doi.org/10.1109/CDC.2009.5400442

Oliff, W., Filippoupolitis, A., & Loukas, G. (2017). Evaluating the impression of malicious spoofing assaults on Bluetooth low vitality primarily based occupancy detection programs. 2017 IEEE fifteenth Worldwide Convention on Software program Engineering Analysis, Administration and Purposes (SERA), 379–385. https://doi.org/10.1109/SERA.2017.7965755

Pratama, A. R., Widyawan, W., Lazovik, A., & Aiello, M. (2018). Multi-Consumer Low Intrusive Occupancy Detection. Sensors, 18(3), Article 3. https://doi.org/10.3390/s18030796

Rahaman, M. S., Pare, H., Liono, J., Salim, F. D., Ren, Y., Chan, J., Kudo, S., Rawling, T., & Sinickas, A. (2019). OccuSpace: In the direction of a Strong Occupancy Prediction System for Exercise Primarily based Office. 2019 IEEE Worldwide Convention on Pervasive Computing and Communications Workshops (PerCom Workshops), 415–418. https://doi.org/10.1109/PERCOMW.2019.8730762

Rueda, L., Agbossou, Ok., Cardenas, A., Henao, N., & Kelouwani, S. (2020). A complete evaluate of approaches to constructing occupancy detection. Constructing and Atmosphere, 180, 106966. https://doi.org/10.1016/j.buildenv.2020.106966

Sayed, A. N., Himeur, Y., & Bensaali, F. (2022). Deep and switch studying for constructing occupancy detection: A evaluate and comparative evaluation. Engineering Purposes of Synthetic Intelligence, 115, 105254. https://doi.org/10.1016/j.engappai.2022.105254

Shen, W., & Newsham, G. (2016). Good telephone primarily based occupancy detection in workplace buildings. 2016 IEEE twentieth Worldwide Convention on Laptop Supported Cooperative Work in Design (CSCWD), 632–636. https://doi.org/10.1109/CSCWD.2016.7566063

Sikeridis, D., Papapanagiotou, I., & Devetsikiotis, M. (2019). BLEBeacon: A Actual-Topic Trial Dataset from Cellular Bluetooth Low Vitality Beacons (arXiv:1802.08782). arXiv. https://doi.org/10.48550/arXiv.1802.08782

Tekler, Z. D., Low, R., & Blessing, L. (2019). An alternate strategy to watch occupancy utilizing bluetooth low vitality expertise in an workplace atmosphere. Journal of Physics: Convention Collection, 1343(1), 012116. https://doi.org/10.1088/1742-6596/1343/1/012116

V., M. P. J., de Souza, B. J. O., Lamenza, T. de S., & Endler, M. (2022). Sensible Challenges And Pitfalls Of Bluetooth Mesh Knowledge Assortment Experiments With Esp-32 Microcontrollers (arXiv:2211.10696). arXiv. https://doi.org/10.48550/arXiv.2211.10696

Valks, B., Arkesteijn, M. H., Koutamanis, A., & den Heijer, A. C. (2021). In the direction of a sensible campus: Supporting campus choices with Web of Issues purposes. Constructing Analysis & Data, 49(1), 1–20. https://doi.org/10.1080/09613218.2020.1784702

Yoshimura, Y., Krebs, A., & Ratti, C. (2017). Noninvasive Bluetooth Monitoring of Guests’ Size of Keep on the Louvre. IEEE Pervasive Computing, 16(2), 26–34. https://doi.org/10.1109/MPRV.2017.33

Zim, M. Z. H. (2021). TinyML: Evaluation of Xtensa LX6 microprocessor for Neural Community Purposes by ESP32 SoC. https://doi.org/10.13140/RG.2.2.28602.11204

Zoto, J., La, R. J., Hamedi, M., & Haghani, A. (2012). Estimation of Common Car Speeds Touring on Heterogeneous Lanes Utilizing Bluetooth Sensors. 2012 IEEE Vehicular Know-how Convention (VTC Fall), 1–5. https://doi.org/10.1109/VTCFall.2012.6399146

Source Link

What's Your Reaction?
Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0
View Comments (0)

Leave a Reply

Your email address will not be published.

2022 Blinking Robots.
WordPress by Doejo

Scroll To Top