[HN Gopher] Building an occupancy sensor with a $5 ESP32 and a s...
       ___________________________________________________________________
        
       Building an occupancy sensor with a $5 ESP32 and a serverless DB
        
       Author : sodality2
       Score  : 631 points
       Date   : 2023-11-13 17:17 UTC (1 days ago)
        
 (HTM) web link (matthew.science)
 (TXT) w3m dump (matthew.science)
        
       | filterfiber wrote:
       | This is an excellent write up!
       | 
       | > 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 these, the only one
       | reliable to work for long periods of time (one month currently)
       | was the XIAO ESP32C3/S3.
       | 
       | I suspect they may be having power issues? For the ESP32's
       | specifically I highly recommend adding a beefy capacitor over the
       | power rails, as those can be rather sensitive to voltage
       | fluctuations especially when transmitting. Both the RPi and ESP's
       | can be finicky depending on the power supply/cable/cable length
       | too, and the RPi's sdcard does tend to fail from sudden power
       | loss. They should all be capable of at least a month, my pi's and
       | esp's have gone several months.
       | 
       | I'd be curious to see the results from other ESP32's (or even the
       | pi) with a larger capacitor added.
        
         | sodality2 wrote:
         | It's definitely not a reliability issue, with the platform -
         | ESP32 in general is very popular so it's surely an issue with
         | my usage. I'm fairly certain it's just a cheap manufacturer
         | issue I ran into. I bought essentially a no-name ESP32 at first
         | and that's the one that refused to last. Proper manufacturer
         | solved that issue for me and now ESP32 works just fine.
         | 
         | The raspberry pi actually never worked, largely because I tried
         | to shoehorn way too much complexity in and manage too many
         | things. It's just a BT scan and a HTTP call.
         | 
         | I don't think it's a power issue as it's getting 5V1A from a
         | power outlet directly to USB-C into the device. Though that's
         | definitely something I need to look into, as the Xiao ESP32-S3
         | I have also had intermittent issues, and IIRC the XTensa cores
         | are more power-hungry than its RISC-V based sibling, the
         | ESP32-C3.
        
           | filterfiber wrote:
           | > I don't think it's a power issue as it's getting 5V1A from
           | a power outlet directly to USB-C into the device.
           | 
           | It's not the total voltage/wattage the PSU can provide, but
           | the voltage at the processor.
           | 
           | The ESP's varying current draw notoriously causes too much
           | noise and a lot of boards don't have large enough decoupling
           | capacitors so the voltage drops too much and it glitches out.
           | Also a warning that USB PSU's can very MASSIVELY in quality
           | (I'd suggest an apple one for testing if you have one handy).
           | 
           | I think you're right that the RISC-V processor is either
           | better behaved and draws power more consistently, or the
           | board has shorter traces to it's bypass capacitor or a larger
           | bypass capacitor.
        
             | stavros wrote:
             | This is correct, I thought it wasn't and was going crazy
             | trying to debug my Pi 3 resetting my 3D printer every time,
             | with any of ten power supplies I tried, including a
             | configurable bench PSU.
             | 
             | I switched to the official Pi PSU after someone said "trust
             | me on this" online, and yep, zero issues in all the years
             | since.
        
               | numpad0 wrote:
               | It's voltage for Pi 3. They forgot to account for voltage
               | drop due to protection or something. Official as well as
               | third-party Pi 3 specific adapters are rated at 5.1V to
               | mitigate that problem.
        
               | nottheengineer wrote:
               | Early alexa devices also have power supplies that are
               | rated at 5.2V, those could be an alternative. I also had
               | a good experience with a 30W oneplus warp charger after
               | some power issues with cheap 5V2A ones.
        
             | iicc wrote:
             | >don't have large enough decoupling capacitors
             | 
             | Is this something you can eyeball to guess at quality?
        
           | londons_explore wrote:
           | Are you sure that the system isn't still running, but it has
           | just disconnected from wifi and is failing to reconnect? I
           | have a few status LED's flashing to show the main CPU is
           | still running, and it often is, even when the device has
           | fallen off the network.
           | 
           | I find that happens fairly frequently unfortunately, and
           | haven't managed to track down the cause.
        
             | sodality2 wrote:
             | Hm, that's a good call. I have a callback on the connection
             | dropped to reconnect but it's possible it needs a full
             | reboot? I'm going to have to sit in on the serial monitor
             | and wait until something happens and see. Thanks for the
             | tip!
        
               | londons_explore wrote:
               | I live in a very wifi-busy area (hundreds of networks),
               | and I suspect that the wifi logic somehow hangs itself
               | waiting for some packet/message/state which gets lost and
               | never comes.
        
           | sokoloff wrote:
           | I chased a reliability problem with an RPi3 for a while
           | before figuring out it was a poor quality USB cable that I
           | was using for power.
           | 
           | Not saying that's your issue obviously, but I spent a couple
           | more hours than I really should have swapping USB power
           | bricks but not the cable (because it was threaded through the
           | printer enclosure and laziness prevailed).
        
             | sodality2 wrote:
             | I didn't consider that as highly as the outlet adapter
             | since that's what pushes the amperage and wattage, but
             | thanks, I'll definitely be investigating that next!
        
               | alias_neo wrote:
               | I've found cable quality to be quite important for
               | Raspberry Pi, particularly if you're not using an
               | official adapter, the head room can be low and the
               | resistance of the cable can tip it over.
               | 
               | I haven't had major issues with ESP devices but I try to
               | keep them on <30cm cables of decent quality and ensure
               | they're shielded. Though I haven't tested experimentally,
               | I'd expect low quality, poorly shielded cables to have
               | some effect on the signal quality.
        
           | numpad0 wrote:
           | Just put a huge low-ESR cap on the 3V3 rail. Newer WiSoCs
           | like ESP32 draws too much for too short that regulators can't
           | catch up. I've once had brownouts with an Uno R3 style ESP32
           | board, and it didn't stop until I added a 0.22F EDLC from my
           | junk bin, which is absurd and not a recommended usage of
           | EDLC, but I think even 330uF electrolytic from same bin
           | didn't cut it.
        
         | jetbalsa wrote:
         | I'm a huge fan of using Alpine with the RPi due to reducing
         | writes to the SDCard. it will only save data to disk with a lbu
         | commit -d and by default boots into a kind of tempfs.
        
           | alias_neo wrote:
           | I have been using Alpine for a while but didn't know this!
           | 
           | At the moment, I'm looking for a way to make my storage more
           | resilient on a Pi I'm using to host Hashicorp Vault, for
           | secrets in Filesystem mode.
           | 
           | First I looked at high-resilience SD cards, but I don't
           | really want them stored on the OS card anyway, also
           | considered just getting a high-quality USB stick and mounting
           | the secrets storage to that; It sounds like a combination of
           | one of these (or perhaps another, similar option) alongside
           | Alpine to reduce writes to the OS SD could be the ticket.
           | 
           | I have a couple of Pis on my network edge running things like
           | WireGuard, with the root mounted read-only to try and help
           | with the issue of SD card resilience; I've killed dozens upon
           | dozens of SD cards in Pis over the years.
        
         | alias_neo wrote:
         | I have a bunch of ESP32s performing critical-infrastructure
         | tasks in my Home Automation; they've been running flawlessly
         | for about a year now; I update them occasionally (ESPHome) but
         | haven't had any issues.
         | 
         | Some of them perform multiple tasks, e.g. one of them is
         | Bluetooth proxy in my home office, but it also runs the light
         | strip along my Skadis wall boards.
         | 
         | I'm using some random brand ones from a company called
         | "AZDelivery" on Amazon.
         | 
         | I haven't done anything special to them; they sit in 3D printed
         | cases, and are plugged in with relatively short cables (usually
         | <30cm where outlets allow) into at least 5V/1A adapters.
        
           | hellweaver666 wrote:
           | I have a number of similar devices but I use dirt cheap Wemos
           | D1 Mini boards from Aliexpress, they usually cost around a
           | buck each (and they come in ESP8266 and ESP32 variants).
        
       | lesuorac wrote:
       | > Linux Bluetooth is incredibly painful to handle in a headless
       | way. Binding to DBus requires cross-compiler magic and not even
       | Cross was getting me out of it.
       | 
       | That was my experience as well. I was doing something different
       | so ultimately I just decided I'd put both the devices on the same
       | WiFi network and then scan for "pi.local".
       | 
       | Do wish I knew how all those other crates work though where it
       | crashes at runtime if you don't have a library instead of
       | refusing to build.
        
         | wutwutwat wrote:
         | Home assistant does headless bt presence detection in ~200
         | lines of python I think the author had a painful experience
         | because they wanted to do embedded and had to interface at a
         | much lower level, where it's a long ago solved problem in
         | higher level languages. Considering the tracker ended up in
         | their door next to their computer, they could have stuck with
         | the rpi that ran python and been fine
         | 
         | https://github.com/home-assistant/core/blob/dev/homeassistan...
         | 
         | https://www.home-assistant.io/integrations/bluetooth_tracker...
        
           | lesuorac wrote:
           | I don't know the author's code but mine was at the same level
           | as home assistant's. I believe I was using Bluer (BlueZ) [1]
           | and afaik the problem is that the dependency needs an armv7
           | version of DBUS when it compiles which my non-armv7 laptop
           | did not provide.
           | 
           | If I do the home assistant route and just copy my source code
           | over to the raspi and build the binary there it all
           | compiles+runs fine. Except for the part where I can't compile
           | it locally and so I don't know of a build issue until its
           | been transferred + built.
           | 
           | [1]: https://github.com/bluez/bluer
        
       | Animats wrote:
       | And with more of these, you could track where everyone is all the
       | time!
       | 
       | Here's a camera based video people counter.[1] This is a bit less
       | intrusive.
       | 
       | [1] https://github.com/saimj7/People-Counting-in-Real-Time
        
         | sodality2 wrote:
         | Given manufacturer's trends of enabling the privacy feature of
         | randomizing the MAC address every n minutes, it would take some
         | serious effort and analysis to reverse these general trends
         | into individuals. I've linked a relevant article in the
         | bibliography about these kinds of attacks [1] but in general,
         | at least with user numbers in the hundreds, it would be very
         | difficult.
         | 
         | [1]: https://ieeexplore.ieee.org/document/9369628
        
           | michaelmior wrote:
           | Just skimming that paper, it doesn't look like they made use
           | of any kind of fingerprinting. The paper below shows how BLE
           | devices can be profiled to extract a potentially unique
           | fingerprint. I don't know how well this would scale to large
           | numbers of devices, but I'd imagine if you used such a
           | fingerprint, you could dramatically improve the association
           | between randomized MAC addresses even if the fingerprint is
           | not 100% unique.
           | 
           | https://inria.hal.science/hal-02359914/document
        
       | reidjs wrote:
       | is something like this possible for an outdoor setting like a
       | beach?
        
         | sodality2 wrote:
         | Definitely, if there's a solar panel and a battery hooked up +
         | some sort of internet connection. Though I would worry about
         | durability outdoors, probably needs to be sealed pretty tightly
        
       | dylan604 wrote:
       | I've heard talks of the toll tag companies wanting to be able to
       | detect occupants in a car so they can properly charge the correct
       | amount of fees when the occupancy changes the amount. I never
       | looked into how that might happen, but I'm guessing Bluetooth
       | would not be a solve here as it's too easy to circumvent if
       | passengers disable bluetooth
        
         | kurthr wrote:
         | Or (shudder) have more or less than the requisite single phone.
        
           | dylan604 wrote:
           | Obviously a soccer mom taking her bus load of kids somewhere
           | would ruin the bluetooth use case. I never did hear more
           | about how they would actually do it. I just met someone
           | randomly at a holiday gathering a couple of years ago that
           | said that was something his company was working on. The
           | conversation just didn't allow for follow up.
        
         | HumblyTossed wrote:
         | Cameras work well for front occupants. They're already being
         | used to check if commercial drivers are wearing seat belts.
        
           | dylan604 wrote:
           | if a toll tag required me to power it and allow it to record
           | video of the interior of my car at all times, then FUCK THAT
           | NONSENSE!!!! even if they had it setup so that the camera was
           | only turned on briefly by a signal as you approached and then
           | turned off again as you passed the gates, that's still a hard
           | no from me.
        
             | alx__ wrote:
             | Uh you're over thinking it. They just have cameras in the
             | toll booths that snap a photo when the car crosses the
             | sensor.
        
               | dylan604 wrote:
               | uh, what about the kids sitting in the back rows that
               | can't be seen?
        
         | sodality2 wrote:
         | That's much worse of an automobile usage case than the ones I
         | read about (measuring vehicle travel speeds to improve travel
         | time estimates)!
         | 
         | Occupancy based pricing for vehicles seems too hard to enforce
         | anyway, change the pricing scheme to something more practical
         | IMO
        
           | aidenn0 wrote:
           | Right now occupancy based pricing involves moving a switch on
           | your toll device to indicate if your car qualifies for the
           | high-occupancy discount (e.g.
           | https://www.e-zpassiag.com/about-e-zpass/e-zpass-flex)
        
       | Dowwie wrote:
       | This would be an interesting way to measure crowds in general--
       | in concerts, attending political rallies, attending protests,
       | etc. A handful of journalists attending an event could spread out
       | and share their max value.
        
         | Smoosh wrote:
         | This makes me wonder if phone companies would have the ability
         | to provide figures for mass gatherings. Would they only be able
         | to count devices on their own network, or do they "see" all
         | phones in range and then filter out the other-network ones?
        
       | SCUSKU wrote:
       | Awesome write up, I liked how accessible the writing is, as well
       | as the fun tone.
       | 
       | I have a problem where I will need to monitor the soil moisture
       | of 20 trees in San Francisco. My current thought is to strap an
       | ESP32, Lora module, battery, and capacitive soil moisture sensor
       | and to send data once every 6 hours or so.
       | 
       | I was wondering if off the cuff you have any suggestions or
       | pointers on how to best approach this. Thanks and once again,
       | thank you for sharing this!
        
         | mwint wrote:
         | Do you need an ESP32 vs an 8266? I believe the 8266's are more
         | energy efficient.
         | 
         | In either case, you can put them in a deep sleep that uses
         | minuscule amounts of energy and can wake back up on a timer.
         | I'd start there.
        
           | SCUSKU wrote:
           | I think I am largely MCU agnostic, although preferably
           | something where I can use Arduino since I am familiar with
           | that. Good to know that ESP8266 is more energy efficient. I
           | have seen this video which demonstrates that the ESP8266 can
           | run off 2 AA batteries for 425 days [1].
           | 
           | Another thought I was having was to write a mobile app that
           | as volunteers walk by the sensor would send data to their
           | phone, which would then forward it to my backend. I suppose
           | this is a mesh network of sorts? Not sure if this is feasible
           | or a good idea though :/
           | 
           | [1] - https://youtu.be/IYuYTfO6iOs?si=oLuJiGxdQ8VHyu29&t=837
        
             | sodality2 wrote:
             | Another option would be LoRa - it's pretty long range. The
             | problem would be that could add a lot to power usage.
             | Though 6h should be plenty of time - wake up, send a small
             | packet, sleep for 6 hours.
             | 
             | You would have a central LoRa receiver in a place that can
             | reach all of the sensors. How far apart are the trees?
             | 
             | Other than that, I would suggest testing out lots of
             | different hardware. I bought several different boards
             | before any decisions were made, and I'm glad I didn't stick
             | to just one. Costed me maybe $30 to buy a gamut of devices
             | and now I've settled on a single one that does what I need.
             | 
             | Also, make sure you're flashing with the largest image size
             | you can, if your code becomes too large. For me I had to
             | enable an option in the board settings to use the full
             | memory space.
             | 
             | Lastly, one thing you should do is the low power option.
             | See which board supports it the best. Then, what I would do
             | at startup is turn on all the sensors, collect and send
             | data right away, then turn all your sensors off. Do ultra
             | deep sleep for 6 hours. then when it wakes up, don't bother
             | doing any kind of loop to the beginning - hard reboot. That
             | avoids any problems with memory allocation becoming too
             | large somehow over the course of the program, since it
             | always starts fresh (which is all you need!). I'm not sure
             | the trade off here but for me, I reboot every 12 hours and
             | it works great. (I would do it after every scan but I don't
             | delay between scans for more constant data, so it would be
             | a lot of set up every single minute).
        
               | SCUSKU wrote:
               | The trees are all pretty close to each other, about 3-4
               | city block radius (map of trees [1]). The issue is that
               | as far as I can tell there is no existing LoraWAN gateway
               | on The Things Network I can tap into [2], which means I
               | will have to setup my own gateway. Which is fine, but it
               | may prove to be difficult to get this onto a tall
               | building since I am just an apartment dweller. At the
               | very least I can put the gateway in my apartment window.
               | 
               | [1] -- https://www.google.com/maps/d/u/0/viewer?mid=1e7K_
               | VdEEYkxuAy... [2] --
               | https://www.thethingsnetwork.org/community/san-francisco/
        
         | filterfiber wrote:
         | There are esp-lora boards including lipo power management you
         | can start with.
         | 
         | The ESP has a few deep sleep modes, and there's a lot you can
         | do to optimize them.
         | 
         | I highly recommend Andreas Spiess on youtube.
         | 
         | EDIT: Heads up that moisture sensors have reliability issues,
         | Andreas's video 463 talks about them.
        
           | SCUSKU wrote:
           | So my thought was to utilize the capacitive soil moisture
           | sensor and then coat the sides of the PCB with a
           | waterproofing epoxy as Andreas recommended. My hope being
           | that even if it craps out, I can just swap out the sensor,
           | and since they are so cheap it won't be a big deal.
           | 
           | Also, great to know that there esp-lora board w/ lipo power
           | included!
        
         | morphle wrote:
         | If it is only 20 trees, you could use off the shelf sensors.
         | 
         | Its much better and cheaper to design from scatch, use an $0.04
         | ultra-low power Arm microcontroller with ADC or a Padauk, add a
         | single layer pcb with two well proportioned pcb traces in a
         | plastic bag as a capacitive moisture censor (better than
         | resistive sensor) and a $0.12 solar cell or rechargable
         | battery. Single sensor cost around $0.41 excluding labor, mass
         | produced $0.08 including labor and sensor network
        
           | SCUSKU wrote:
           | That's a great point. I have experience w/ Arduino and have
           | made a carrier board one time [1], but am not familiar with
           | full custom PCB + MCU designs. Do you have any suggestions on
           | where to get started for someone with my medium level of
           | experience here? And also, how difficult would you anticipate
           | a custom MCU to be?
           | 
           | [1] -- https://zachbellay.com/projects/odaf/#iteration-3-phas
           | e-2-wi...
        
             | morphle wrote:
             | A custom MCU+pcb is at least 3 months work: downloading an
             | design for an FPGA, testing it, using openlane to make an
             | custom chip, taping it out, writting boot code and the main
             | loop ( I would do that in assembly, much simpler than using
             | C and some vendors library). The big issue is taping it out
             | to a chip FAB, around $25K, see my comment below.
             | 
             | I would get started with an off the shelf MCU with good
             | documentation, writing the software on a good development
             | platform with the same assembly as the final
             | microcontroller. For the $0,04 ARM microcontroller I
             | mentioned above as final target with analog to digital
             | controller (ADC) I would first write the program (the main
             | loop) on a Mac on a high level language like Squeak or
             | Python and use the $4 raspberry pi pico as ADC. I would
             | then rewrite the working software in ARM assembly and test
             | it directly on the pi pico. Then I would flash it on the
             | $0.04 arm microcontroller wired on a breadboard to the
             | moisture sensor. Then I would debug it again, with simple
             | flashing leds to see where your software goes wrong.
             | morphle at ziggo dot nl for more questions like how to find
             | a 4 cent arm chip. I would start at lcsc.com , use their
             | sister company and eda tool to design the pcb only with
             | lcsc.com parts and order it fully assembled. Then I would
             | open source it so others can directly order assembled
             | boards
             | 
             | I would avoid Arduino like the plaque, in essence its just
             | a precanned overpriced bloated C library on an overpriced
             | microcontroller. You always get better results writing from
             | scratch. See 'is it complex or did we make it complicated',
             | a lecture by Alan Kay at Quallcom on Vimeo or Youtube, on
             | designing systems from scratch or buy vendor stuff:
             | https://vimeo.com/82301919
        
           | morphle wrote:
           | I designed my own chip for EUR,03 , a 180nm 8 core
           | microcontroller with builtin moisture and other sensors.
           | Minimal volume is 800.000 chips.
        
         | Kevin09210 wrote:
         | check this out
         | 
         | https://dynamax.com/products
        
         | antoniuschan99 wrote:
         | Either a dfrobot: https://www.dfrobot.com/product-1385.html or
         | M5Stack: https://shop.m5stack.com/products/earth-sensor-unit
         | https://shop.m5stack.com/products/watering-unit-with-mositur...
        
         | chasd00 wrote:
         | look at Adafruit's Feather line up. they have lipo support +
         | charging and then an easy connector to use for i2c which you'll
         | probably be using to interface to the sensor. You can probably
         | find one with Lora included.
        
         | Mumps wrote:
         | Would the Pimoroni Grow help you out?
         | https://learn.pimoroni.com/article/plant-monitoring-with-env...
         | 
         | grab that, a pi pico, and some batteries (which should last
         | months they claim), and set up an MQTT for data.
         | 
         | It is pretty expensive at scale. But if you want "no fuss" I
         | think it would do the trick for you.
        
       | mariocesar wrote:
       | Interesting DIY project! It reminds me of a startup called
       | OccuSpace (https://occuspace.io/), which offers a similar
       | occupancy solution but as a service. They may be leveraging
       | similar technologies to provide real-time occupancy data, which
       | is incredibly useful for managing space in offices, public
       | venues, etc. It's a good example of how these concepts are
       | scaling up commercially.
       | 
       | I wonder How well can your DIY sensor or OccuSpace figure out
       | what kind of devices are in the room or who's using them. Can
       | they tell different users or gadgets apart?
        
       | me_again wrote:
       | Cool!
       | 
       | I didn't totally follow the issues with keeping the data in
       | memory, and it sounds like it is solved now - but you could
       | probably use a cardinality estimation algorithm to estimate the
       | number of unique beacon IDs while only using constant space.
       | https://en.wikipedia.org/wiki/Count-distinct_problem
        
         | sodality2 wrote:
         | That's a really good point. I've been meaning to go to a
         | football game recently and bring it, to stress test it with the
         | largest gathering I can quickly access. If it can easily scan a
         | couple thousand without a sweat, I'd consider it pretty solid
         | anyway. But a fixed-size estimating data structure would be
         | really cool to research. I will surely look into it, thank you
         | for the suggestion!
         | 
         | Side note: Definitely one of my favorite parts of this project,
         | that I get to investigate more in-depth and interesting CS
         | concepts without having to worry about doing the easiest
         | solution. I'm scratching an itch, not developing a solution to
         | deliver ASAP.
        
       | lormayna wrote:
       | Can you use the variation of RSSI to track movement? I know thst
       | it's multipath and cannot be really reliable, but maybe it can
       | provide some useful information. Or maybe using 2 tracking
       | devices and correlate the results.
        
         | sodality2 wrote:
         | Instead of triangulation like that, which could get real
         | complicated, you could also simply place enough in the areas
         | you're counting and limit the RSSI cut-off enough to measure
         | those right next to it. That would work for specific areas you
         | want to measure trends in, instead of general 3d spatial
         | triangulation.
         | 
         | though some of the papers I read did this via measuring the
         | time of flight of BT packets and how they were changed when
         | people walked between them, and that really blew my mind!
        
         | dividuum wrote:
         | I do something like that for my home assistant: I have a few
         | shelly smart plugs that can scan BLE. They filter for apple
         | watches and send the MAC and RSSI to my home assistant where a
         | small neural net then tries to detect the location within my
         | apartment. It works better than I expected given the high
         | variance of signal strength, even when standing still.
        
       | fullstop wrote:
       | ESPresence [1] is an interesting project, and it works with the
       | ESP32-C3.
       | 
       | It is more designed to figure out which room you are in.
       | 
       | 1. https://espresense.com/
        
       | summm wrote:
       | Theres this using ESP32 and LoRa, where you can order readymade
       | hardware: https://cyberman54.github.io/ESP32-Paxcounter/
        
       | Havoc wrote:
       | You can also get pretty check 24ghz radar that does reliable
       | sensing till like 3m. Much much more accurate than the IR based
       | stuff
        
         | foxtrottbravo wrote:
         | While yes, these devices are cool and I use them in a couple of
         | projects (mostly centered around home automation), I think
         | there is no good way to count people that way (other than
         | placing them above ingress and egress points).
         | 
         | I think the original use case is not to know whether a dining
         | hall is occupied at all but about how occupied it is (and if he
         | should go to lunch now or wait another 20 minutes).
        
       | epcoa wrote:
       | > and heap thrashing since we're allocating and deallocating the
       | result structure every single callback.
       | 
       | This should not an issue. It is entirely possible to write this
       | without any dynamic allocation (other than stack) at all, or it
       | could be done be done with an arena. Consider with something as
       | powerful as an esp32 a hash table is not even strictly necessary,
       | surely for a single purpose use case like this.
        
         | sodality2 wrote:
         | I figure the internal data structure that already exists in the
         | BT library probably uses something similar to a hash table, so
         | I emulated it with less of the data being saved (just a MAC).
         | You're right though, there are smaller choices for data
         | structures. I ended up upgrading to a better board with more
         | memory, and just used the existing structure in the BT library.
         | It fixed a number of other problems at the same time.
        
       | davidw wrote:
       | How do people "productize" their ESP32 projects? (Edit: not
       | talking about mass-producing something, I just want a self-
       | contained unit rather than a board with a bunch of wires sticking
       | out)
       | 
       | I see a lot of pages where they show some bare-looking board, but
       | in the real world you want to package that up.
       | 
       | If you're a software guy like me, you would likely prefer to pay
       | a bit extra to just have that tidied up for you rather than
       | fiddle around with stuff.
        
         | lostapathy wrote:
         | 3d printed enclosure?
        
           | davidw wrote:
           | That sounds like one of those "I have a problem... now I have
           | two problems" jokes.
           | 
           | I wanted something simple and cheap and now I need to go deal
           | with this whole other thing I don't know much about and
           | either rent or buy into it?
        
             | krasin wrote:
             | JLCPCB would 3d print high-quality enclosures from a
             | variety of UV resins: https://3d.jlcpcb.com/ and decently
             | cheap.
             | 
             | The next best option is injection molding with PCBWay (or
             | other similar provider): https://www.pcbway.com/rapid-
             | prototyping/manufacture/?type=4...
             | 
             | An alternative is a sheet metal enclosure:
             | https://www.pcbway.com/rapid-
             | prototyping/manufacture/?type=4...
             | 
             | But 3d printing with JLCPCB is really the most convenient
             | way unless you need a lot of parts (thousands) or the
             | material properties of the UV resins would not be enough
             | (high-temperature, outdoors, etc).
        
             | Zetobal wrote:
             | You can also pay someone to do it.
        
             | lostapathy wrote:
             | As somebody else mentioned, you can hire it out.
             | 
             | There's a lot of files running around with existing models
             | for common projects, so you probably don't need to design
             | your own enclosures a lot of the time unless you want to.
             | 
             | And printers have gotten cheap, like $99 gets you a jumping
             | off point into the hobby.
        
               | davidw wrote:
               | Do people not just sell like "complete package" things?
               | 
               | The board in the article is $13.49 (
               | https://www.amazon.com/dp/B072HBW53G ). If you could get
               | an enclosed, complete thing for $20, including the board
               | and whatever you need for it to run, that'd be much less
               | of a PITA than "brand new hobby" or "find someone to 3D
               | print stuff for me locally and deal with that whole
               | hassle".
        
               | kps wrote:
               | There are things like M5Stack (also ESP32-based) that
               | suit some purposes.
        
               | davidw wrote:
               | Yes! That looks pretty cool and more like what I'm
               | talking about.
        
               | kube-system wrote:
               | The thing about enclosures is that they are pretty design
               | dependent. Nobody knows what you are hooking up to your
               | board or what the intended purpose is. Some boards do
               | have generic type cases, e.g. raspberry pi cases.
        
               | szundi wrote:
               | $99 is just enough to jump into the hobby of fixing your
               | printer all the time - and that's what hobbyists are
               | actually looking for
               | 
               | solving something would be like $700 calibrated out of
               | the box, like the old prusa 3 something or the bambu
               | p1something.
        
               | lostapathy wrote:
               | I'm well aware. I think of the $99 printer as a filter -
               | either it's going to catch your interest enough to
               | justify jumping to a $600+ printer, or 3d printing isn't
               | for you and you aren't out much.
        
             | bluGill wrote:
             | That is why so many amateur projects look ugly: making a
             | pretty package is a whole new skill set. 3d printing is the
             | easiest acceptable solution, but it isn't the best. Best
             | would be a custom designed injection molded housing, but
             | that is even more work to design.
             | 
             | Nothing about making a nice presentation is beyond what
             | amateurs can do. However it is a lot more effort and so
             | most decide it isn't worth it.
        
               | davidw wrote:
               | I don't care if it's "beautiful" or ultra-compact or
               | looks as nice as some Apple device. All I want is to buy
               | one thing without wires and an exposed board.
        
               | bluGill wrote:
               | There are not enough of you to make it worth the effort
               | most likely. If you disagree then you should invest in
               | the business opportunity.
        
             | fnordpiglet wrote:
             | There are plenty of enclosures available on AliExpress and
             | Amazon. But a cheap reprap 3d printer is a general tool
             | like a screw driver or hammer, albeit more complex for
             | sure. But the learning curve isn't absurd, it's basically
             | buying something plugging it in and loading filament then
             | printing a downloaded STL. It gets harder when it needs
             | calibration and maintaining, but it's not an unknown dark
             | art - there are a billion helpful discord groups.
             | 
             | But once you have it and know how to maintain it, anything
             | where you're like "man wouldn't it be nice if I had a
             | physical object like X" becomes a relatively solvable
             | problem. There are lot of materials with many different
             | properties. When you skip into a zone of material or print
             | you can't afford the equipment for, there are a bazillion
             | print shops.
             | 
             | The harder skill frankly is the 3d modeling. But I find
             | openscad is sufficient for almost everything and then for
             | the rest I use Build123. They're both parametric code
             | driven design tools so fit well inside a software project
             | (better than GUI tools, and more easy to be precise with).
             | Openscad is absurdly easy, build123 is more advanced and
             | requires Python skills as well as effort to learn the
             | conceptual model.
             | 
             | I sort of felt the way you seem to once upon a time until I
             | said screw it and figured it out one day. Now I feel like I
             | lived most my life hobbled for want of this tool.
             | 
             | So, yeah, overkill for printing an esp32 enclosure. But
             | doing so unlocks a lifetime of possibilities.
        
             | Johnny555 wrote:
             | If you're looking for an enclosure for your hobby ESP32
             | project, there are lots of off the shelf, small enclosures
             | available:
             | 
             | https://www.amazon.com/s?k=esp32+enclosure
             | 
             | If you want to sell a more finished commercial quality
             | project and don't want to print your own, there are places
             | that will fabricate one for you (but in low quantities,
             | having them 3d print it probably cheaper)
        
             | dekhn wrote:
             | The simplest I can think of is this:
             | https://www.digikey.com/en/products/detail/sparkfun-
             | electron... use a step drill or other large-diameter bit to
             | cut holes for cable glands, dremels to cut rectangular
             | holes for power switches, and drill and tap holes for
             | standard brass standoffs. IMHO everybody should have at
             | least one cordless drill and drill bits, as well as an M3
             | tap. Then use Cable glands for wires going in/out, and
             | nylon sheathing for the cables outside the box.
             | 
             | If you take the time to make the holes nicely, the result
             | often looks good enough that non-makers will think you are
             | a god of manufacturing.
        
         | bluGill wrote:
         | There are lots of options. Most of what you see they don't:
         | this is a one off project for the hacker and if others find it
         | useful great, but not the goal. As such packaging isn't needed,
         | sometimes you shove it in a box.
         | 
         | If you want something nice, that can be done, now you need to
         | pay someone (could be yourself!) to design a package. ESP32 is
         | designed to be easy to put into your own products, this means
         | design a circuit board and case to fit each other, and so on.
         | Then you buy the ESP32 chips in bulk and assemble them onto the
         | circuit board. The board you buy for a prototype is officially
         | only a reference board and not what they expect you to ship
         | (though shipping the board is common: they are small, cheap,
         | and someone else did the hard board design)
        
           | davidw wrote:
           | I'm not really talking about producing something at scale,
           | just the bare minimum of not having a board with wires coming
           | out of it.
           | 
           | Say I want to do some one-off project and put it outside
           | somewhere, out of the weather, with some batteries to power
           | it.
           | 
           | Or an indoor thing with an AC hookup of some kind.
           | 
           | Doesn't have to be perfect or beautiful or as tiny as
           | possible; just sort of a complete package.
        
             | qwertox wrote:
             | I just buy new small breadboards and jumper wires and make
             | it so that it looks nice. Jumper wires of the proper
             | length, not jumper cables.
        
             | amenghra wrote:
             | You can buy project boxes. Some of them come with
             | breadboards/perfboard that properly fit with screws (e.g. h
             | ttps://discountbloc.ru/archive/products/xwzz1130z33x.html).
             | Alternative is to 3D print a box.
             | 
             | Altoids box is a classic. Food plastic boxes also works
             | depending how hot the components run.
        
               | XorNot wrote:
               | AliExpress has a number of companies selling the "generic
               | black rectangle" that looks a bit like an Apple TV now.
               | I've been meaning to buy a couple and see what they're
               | like to work with, because that's the overall effect I
               | really want - something that looks like a router or
               | generic company product.
        
             | HeyLaughingBoy wrote:
             | I mentioned M5Stack above. An alternative is to buy an off
             | the shelf display, e.g., from Polycase -- an outfit I've
             | used a lot and mount your board inside.
             | 
             | There are literally tens of thousands of off the shelf
             | electronics enclosures for every likely possibility. From
             | massive control panel boxes measured in multiple feet, to
             | watch-sized enclosures with straps for items designed to be
             | worn on your wrist. You'll find something suitable no
             | matter what.
        
             | anigbrowl wrote:
             | Time for me to shill m5stack.com again
             | 
             | I don't work for them, I just really like their products
             | and own about 10 of them. Downside, the documentation is
             | rudimentary so you should enjoy steep learning curves (but
             | not too steep - The ESP-IDF and MicroPython docs will get
             | you through most problems).
        
         | mikub wrote:
         | I look if if I can find some already assembled things and try
         | to build an project around it. For example I used an
         | pictureframe to build a nice neopixel thing, I also documented
         | it on my website, https://mikub.me/post/(1)/
         | 
         | So what I do is I usually look around what everyday objects
         | could become nice enclosures for microcontrollers and such
         | things.
        
         | morphle wrote:
         | You shouldn't productise anything with ESP32, its just an
         | overprized bloated hobby pcb.
         | 
         | See my comment below on how to productize this: use a 4 cent
         | off the shelf microcontroller on a 10 cent pcb or much better:
         | make a custom chip with all the sensors on board but with $25K
         | minimum Capex but tiny Opex
        
           | chpatrick wrote:
           | There are thousands of ESP32-based IoT devices on AliExpress
           | that would disagree with you. It's an SoC chip not a "pcb".
        
           | morphle wrote:
           | As others point out: you could hire someone to do it. Based
           | on my custom 1 cent ASIC (custom chip) I would design, test
           | and build your (moisture or other) sensor for EUR3000
           | excluding tax and price of all the needed tools. Usually more
           | like $6000 because they want to outsource all the problems
           | (like mass production and sales) to me
           | 
           | morphle@ziggo.nl if you want me to design it
        
             | szundi wrote:
             | Mine would be 0.3 cent per chip and would need $50k
             | investment.
             | 
             | Jokes aside, if you know how to design a custom chip for 3k
             | USD, I would like to know about it.
        
               | morphle wrote:
               | it is not a joke, you can do as low as 1.10870 cent per
               | custom chip or 2 cent per off the shelf chip. 0.03 cent
               | is impossible, the minimum size of a chip is .625 mm2 and
               | that sets the minimum price.
               | 
               | Designing one for $3K is possible because I already have
               | a tested chip design that I just have to modify for you.
               | 
               | A master student project is usually a free design with a
               | free tapeout at Google MPW, Mosys of Europractice.
               | 
               | I also design bigger chips and $500 wafer scale
               | integration with 10000 cores :
               | https://vimeo.com/731037615
        
               | anigbrowl wrote:
               | You seem like a very smart and capable person, and for an
               | industrial buyer your proposal makes a lot of sense. For
               | a hacker (who is often a hobbyist or amateur looking to
               | build something simple quickly) a versatile SoC at
               | $5/unit also makes a lot of sense.
               | 
               | I have to say you are hurting yourself with your
               | communication style, because it comes off as very
               | dismissive and arrogant. I bookmarked your presentation
               | in case I need it in the future, but a 4 hour video is
               | not a great way to hook people who are not sure if they
               | are interested or not.
        
               | morphle wrote:
               | if you like to know more about it: morphle at ziggo dot
               | nl to make a video conf appointment and get a demo. Or
               | ask email questions, but that takes more effort for us
               | both.
        
             | sweetjuly wrote:
             | You're selling yourself drastically short asking for just
             | 3k unless your plan is to wrap the entire project in a
             | week. I don't think I've ever managed to even setup a new
             | tech library in under a week, let alone design and verify
             | anything.
        
           | dgacmu wrote:
           | This is silly and reductive. You make different design for
           | manufacturing choices at different steps of product
           | development and volume.
           | 
           | In some cases, you may luck out and find that a widely
           | available dev board is actually a really good match for your
           | needs, in which case, run with it. You'll cut out a lot of
           | NRE and get your product in customers hands faster. Once
           | you've shown that you can sell in volume, do a DFM pass for
           | V2 to minimize cost. OP probably fits this case, as all they
           | need is Wi-Fi and Bluetooth, depending on their eventual
           | power solution.
           | 
           | If you find that you're taking a dev board and soldering a
           | lot of extra components to it, then I agree, you want to move
           | to a custom PCB before doing any real volume. But you still
           | might make a dozen first cuts with this and a 3d printed case
           | to see how some product beta testers respond.
        
           | tiagod wrote:
           | I'm certain that an ASIC wouldn't be cost effective for all
           | but very large operations. Even if you could get it at the
           | same price, it would massively increase time to market, and
           | if you need to scale production fast you're shit out of luck.
           | Not to mention you'll probably need multiple iterations,
           | especially if you're sticking a Bluetooth modem and a bunch
           | of sensors in it.
           | 
           | Using an off the shelf MCU and off the shelf sensors saves
           | you all those headaches.
           | 
           | Also, ESP32 is not a "pcb".
        
         | serial_dev wrote:
         | You get a "product designer" (?) who designs a case. If your
         | product is "standard enough", you might skip this step.
         | 
         | Then, you reach out to local manufacturers and ones from bigger
         | markets. You get some samples, you check if things work like
         | they should. If you like it, order a smaller batch. Design a
         | quality assurance workflow, or find a specialist who can help
         | you with that so that the manufacturer doesn't ship you 20%
         | faulty products.
         | 
         | Get your products onto the shelves of a brick and mortar store,
         | and/or create a website, sell on Amazon and other portals.
         | 
         | Talk to your customers, listen to their feedback, improve every
         | step a little.
         | 
         | It sounds overwhelming.
         | 
         | I worked for a small smart home startup, and I learned that,
         | for example, in Shenzhen, there is a whole industry ready to
         | help entrepreneurs realize their dream products, and at every
         | step of the way, you can contract someone to help you. In our
         | experience, the Chinese service was both much better, cheaper,
         | faster than European offers.
        
         | greggsy wrote:
         | Smaller dev boards and shrink wrap.
        
         | _jal wrote:
         | I like wood. It is pretty much everything you want for
         | something like this - super durable, very easy to work with,
         | and depending on what you pick, cheap and pretty, too.
         | 
         | Plus, woodworking is very satisfying by itself. And also easy
         | to get started with - for small box-like things like this you
         | only need a few cheap hand tools.
        
         | morphle wrote:
         | You shouldn't productise anything with ESP32, its just an
         | overprized bloated hobby pcb, very unprofessional.
         | 
         | See my comment below on how to productize this: use a 4 cent
         | off the shelf microcontroller on a 10 cent pcb or much better:
         | make a custom chip with all the sensors on board but with $25K
         | minimum Capex but tiny Opex
        
           | AnotherGoodName wrote:
           | The esp is a cheap microcontroller.
           | 
           | You shouldn't productionize an esp32 dev kit but you should
           | absolutely productionize an esp32 on a board you've designed
           | that has the specif features to do your thing (whatever that
           | is).
        
             | morphle wrote:
             | no, the ESP32 is a SoC, a bunch of chips in a package on a
             | PCB. not a bare microcontroller chip. It is already a
             | product with several resellers: ip, fab, packaging,
             | exporter, distributor, retailer and a very high profit
             | overhead of at least 2.7 times the costprice
        
               | k0k0 wrote:
               | You're absolutely wrong on a number of points. A SoC
               | stands for "system on a chip", it refers to a single die
               | (if you want to get pedantic, there are multi-die
               | packages but this does not apply here) package, a "bare
               | chip" if you will.
               | 
               | https://www.espressif.com/en/products/socs/esp32
               | 
               | The ESP32 is a SoC. It's available in QFN packaging (Quad
               | Flat Pak No Lead).
               | 
               | The ESP32 is available included with a number of
               | "modules" (and of course devkits). These modules are
               | designed for production use and it can be economical to
               | do so. You clearly don't have the foggiest idea about
               | these product lines so don't seem to be in a good
               | position to comment on the economics.
               | 
               | https://en.wikipedia.org/wiki/ESP32
        
               | morphle wrote:
               | You should call the company, order a few million units,
               | negotiate and sign their NDA and then they will tell you
               | it is not a single bare die
        
               | k0k0 wrote:
               | Why would you need to sign an NDA? There are decaps
               | readily available. Most ESP32 models have a separate die
               | for the flash memory, but everything but the flash (that
               | is uC, WiFi, BLE, and peripherals) is on a single die,
               | which sounds like a SoC to me (The definition of SoC
               | devices have always included devices with off-die RAM and
               | Flash). These aren't "a bunch of components on printed
               | circuit boards" as you initially claimed.
               | 
               | https://electronupdate.blogspot.com/2018/08/espressif-
               | esp32-...
        
               | emj wrote:
               | There are some nice images of the ESP32 die here
               | https://zeptobars.com/en/read/Espressif-ESP32-Wi-Fi-
               | Bluetoot... and yes does not look like a multi die chip.
        
           | HeyLaughingBoy wrote:
           | Not everything is going to be sold @ qty 100,000. I deal with
           | the other end of the spectrum: 1 unit to maybe 25/year. At
           | those quantities, speed of development and assembly is the
           | most important criteria. Unit price is not.
        
         | mkingston wrote:
         | I mount the board on some nylon standoffs through a takeaway
         | box lid. The rest of the takeaway box is the enclosure. I drill
         | some holes through the takeaway box for wire ingress and
         | egress.
         | 
         | I use a tupperware container if I'm feeling fancy.
        
         | boustrophedon wrote:
         | There are a lot of companies that make electronics enclosures.
         | Some of them are waterproof. Usually they have a couple pre-
         | drilled mounting flanges to screw in the board as well, and
         | then you'd drill your own external holes for input/output as
         | necessary. Here's a couple manufacturers, and you can usually
         | find stuff on e.g. adafruit and digikey as well to save on
         | shipping if you're buying dev boards or other parts from them
         | already.
         | 
         | https://www.hammfg.com/ https://www.budind.com/ http://takachi-
         | enclosure.com/ https://www.adafruit.com/product/903
        
         | HeyLaughingBoy wrote:
         | I am a huge fan of M5Stack. Take a look at the Tough or Core
         | units. You get an ESP32 with couple of peripherals (speaker,
         | buzzer, real-time clock) and a TFT touchscreen all in a case.
         | Water-resistant in the case of the M5Stack Tough.
         | 
         | I've shipped a couple of consulting products based on them and
         | it really cuts down on the work I need to do in order to build
         | product.
        
           | davidw wrote:
           | That's exactly the kind of thing I'm talking about. You could
           | take those and plug them together and be basically ready to
           | go, rather than "just" investing in a bunch of 3D printing
           | and soldering and lord only knows what else. Maybe it costs a
           | bit more, but that's fine - you get something that you can
           | use somewhere besides sitting on your desk with a bunch of
           | exposed wires.
        
         | jpcfl wrote:
         | You can design both a circuit board and a case with Fusion360,
         | which is free for personal use. Use JLCPCB to assemble the
         | board for you. You can 3D print the case yourself.
         | 
         | It took me about 20hr to learn how to use Fusion360 and make a
         | simple circuit and case this way.
        
         | martinsnow wrote:
         | Usually a black abs box will do
        
         | dragontamer wrote:
         | If you're handy with wood, you use wood.
         | 
         | If you've got a 3d printer, you make a 3d print.
         | 
         | If you're more of a acrylic guy, then you take your laser-
         | cutter, cut out some shapes, drill a few holes and bam you've
         | got a box.
         | 
         | If you got none of these and just want a bare-necessities box,
         | you use an Altoids Tin that costs about $25 for 12 tins... and
         | they have these kinda useless mints in them though. So you buy
         | those boxes, then you throw away the mints and shove your
         | project in there.
         | 
         | If you want slightly better than the Altoids tin, you buy a
         | Hammon Enclosure. (https://www.hammfg.com/electronics/small-
         | case).
         | 
         | If you're like me, you wrap it in electrical tape, and then
         | wrap that in duct tape or something. Maybe color on it with
         | permanent marker.
        
         | shoelessone wrote:
         | I am by no means an expert, but I wrote up a bunch of words on
         | how I did this personally for a family Xmas present here. The
         | tl;dr; is a custom PCB in my case:
         | https://kevinmitchell.io/blog/mitchine-esp8266-wooden-thinki...
        
         | bobsmooth wrote:
         | I designed a carrier board for my module to slot into. You can
         | also solder the header pins directly to some perfboard and
         | build off that.
        
         | dekhn wrote:
         | I wouldn't call it "productize", but when I wanted to give an
         | ESP32 project to a friend who would be using it, I did these
         | things: designed and printed a case for the ESP32. The ESP32 is
         | soldered into a perma-proto and all wire connections are
         | soldered to the ESP32's terminals (I don't use a solderless
         | breadboard, or Dupont wires, once I'm done with the design).
         | The perma-proto is screwed into posts in the case (as are
         | several daughterboards). The user-controlled potentiometer is
         | also soldered into the perma-proto and is tightly screwed into
         | the case. There's a power jack screwed into the case, and all
         | wiring in/out of the case goes through a "cable gland" and the
         | cables, outside the case, and wrapped into nylon sleeve with
         | strain relief. And an on/off switch.
         | 
         | example: https://www.printables.com/model/72839-stable-and-
         | waterproof.... If I am using screw terminals, wires are
         | terminated with ferrules.
         | 
         | For some projects that use 120V AC directly, I've purchased
         | nice receptacle with fuse, and grounded the metal casing. But I
         | don't feel comfortable distributing those.
         | 
         | I have also been known to encase an entire project in clear
         | epoxy (the first "maker" project I did, thanks to an
         | enlightened shop teacher in the 1980s, was a nightlight using
         | only 3 components, encased in epoxy; they work some 35 years
         | later!)
         | 
         | Some things I can think of to go further: designing my own PCB
         | instead of using perma-proto. FInding better temporary
         | connectors (duponts are not reliable). Eliminating the ESP32,
         | replacing it with simpler logic circuits when possible.
        
         | qiqitori wrote:
         | If it uses Bluetooth you're theoretically on the hook for
         | thousands of dollars if you want to sell your ESP32 project as
         | a product. (IANAL; it may take a lawyer to truly understand if
         | you have to pay or not.)
        
           | sweetjuly wrote:
           | ESP32 modules (read: not just the packaged ICs) are pre-
           | certified in most markets. This is the main advantage of
           | shelling out for these more expensive modules instead of just
           | integrating it all yourself. It's also why many of the IOT
           | products which use ESP32s use modules and waste a lot of area
           | rather than the much smaller ICs: certification is a
           | nightmare for small scale products!
        
         | tbyehl wrote:
         | If you're not wiring anything but power, LilyGo T-Dongle-S3 is
         | an ESP in a USB-A thumb drive form factor complete with a shell
         | and optionally a 160x80 color LCD.
        
       | barkingcat wrote:
       | I like the inclusion of the bibliography. I should do that with
       | all my blog posts too (even if it's links to other posts, etc,
       | the references still help to understand the post)
        
       | post_break wrote:
       | This paired with a TPMS scanner would be great for occupancy for
       | the entire campus. Scan the TPMS at the major traffic zones, then
       | compare with bluetooth signatures.
        
       | fnordpiglet wrote:
       | While I don't discourage learning a new language the immediate
       | toss aside of rust on esp32 was perplexing. It sounds like a lot
       | of the memory issues would have been mitigated by sticking with a
       | language better understood. Rust's esp32c3 support is pretty good
       | IMO, that's all I use. Writing in C++ makes me feel dirty in ways
       | that even COBOL doesn't.
       | 
       | I also think I would have implemented presence by TTL, updating
       | the TTL for each MAC when I see it and purging a TTL heap before
       | updating my remote statistics. I didn't fully grok the approach
       | taken for maintaining the hash map, in a taxi off a long flight,
       | but it didn't seem super robust on my quick read.
        
         | sodality2 wrote:
         | The hash map was definitely not robust enough, that's why I
         | ultimately switched back to the default behavior and increased
         | the memory available on the board!
         | 
         | Also, I am a Rust fanatic. I do nearly everything in it! I gave
         | a talk on it at our last hackathon! I even wear a Rust t-shirt
         | regularly! So rest assured I will be exploring my options with
         | Rust. I would love to get rid of my dependence on Arduino. But
         | C++ was easiest to get off the ground with, believe it or not,
         | just because I have experience flashing Arduino boards.
        
           | fnordpiglet wrote:
           | No that's awesome. Experiment and enjoy. But I would check
           | out the esp33 toolchain for rust. It's remarkably mature.
           | Awesome project btw.
        
           | kramerger wrote:
           | Is it possible to program this device without the espresso
           | SDK and the arduino stuff?
           | 
           | Whether I'll use C++ or Rust for my next project depends on
           | which one I can first get working on this board with the
           | standard toolchain (GCC and Rustc respectively)
        
       | j1elo wrote:
       | Always surprised to see people running around with their
       | Bluetooth needlessly enabled.
       | 
       | Maybe I worry to much about battery usage, but I just got used to
       | always enable WiFi, Bluetooth, or NFC, only at the moment of
       | usage, and disabled afterwards.
        
         | szundi wrote:
         | Google/Apple obfuscating these mac addresses do not help
         | anymore?
        
         | ThrowAway1922A wrote:
         | I have a smartwatch and airpods, turning off Bluetooth would
         | cause both not to work.
         | 
         | I'm more surprised that anyone ever turns it off, battery use
         | is negligible compared to the cellular modem as I browse the
         | web/listen to music.
        
           | landemva wrote:
           | I use Bluetooth maybe once or twice a year because I don't
           | want to be tracked everwhere. Wired headphones. Phone in
           | airplane mode much of the day. I suppose I am a privacy
           | outlier, and I'm also not a slave responding to calls/texts.
        
             | ThrowAway1922A wrote:
             | > and I'm also not a slave responding to calls/texts.
             | 
             | You do you, but I enjoy talking with my friends and family
             | and I don't see responding to them as "slavery".
        
               | landemva wrote:
               | Yes, I enjoy experience of life in real life.
        
               | ThrowAway1922A wrote:
               | You're also very judgmental. My family lives far away,
               | thankfully my phone allows me to maintain contact.
        
           | j1elo wrote:
           | Well, I said needlessly because of the amount of times I saw
           | people leaving Bluetooth enabled while not using any BT
           | devices. Their phone enabled it by default, and that's how it
           | stays for its lifetime. Even if it's small amounts, it must
           | be chipping battery away!
           | 
           | Of course if you got a smartwatch you'll want to leave BT
           | enabled to get calls messages and such. At least that's a
           | good reason to leave it on.
        
         | greggsy wrote:
         | I feel like you're an extreme outlier. It's not at all
         | surprising, given the convenience in a range of modern day
         | activities like driving a car, listening to music, being in a
         | workplace, or simply being at home.
        
           | j1elo wrote:
           | But that's what I meant, using it when there is a purpose
           | makes a lot of sense of course. I'd just enable BT, drive,
           | then leave the car and disable BT before putting the phone in
           | my pocket. Why leave it enabled during times that it's not
           | paired with any other device?
           | 
           | To me it sounds like leaving home and keeping WiFi enabled.
           | It eats battery because it's still sending beacons and trying
           | to find some known device to connect to.
        
       | gyf304 wrote:
       | Did something very similar years ago -
       | https://blog.yifangu.com/tag/library-occupancy-project/
        
         | sodality2 wrote:
         | Wow, very cool! I would have used WiFi but unfortunately our
         | beacons either separate or merge devices (so you either get
         | just the users on the nearest beacon, which is not a set range
         | of distance, or every user on that region's subnet, which could
         | be entire dorm complexes). But that seems like a really good
         | solution!
        
       | qwertox wrote:
       | My alarm clock consists of an ESP32 and has a somewhat big LED-
       | array connected to it (powered by a transistor), a buzzer and a
       | PIR sensor. It doesn't show the time, but buzzes (and flashes)
       | and takes care of slowly brightening the room, or at night slowly
       | dimming it into darkness some time after all the lights have been
       | turned off.
       | 
       | One added benefit I discovered afterwards is that even though the
       | AP is not between me and the ESP, I seem to reflect enough RF so
       | that logging the RSSI provides information not only about when
       | I'm in the bed, but also when I change position at night.
       | 
       | https://imgur.com/a/VixOlu5 (edit: no erotic content despite the
       | warning)
       | 
       | The green one in the middle is the RSSI, the yellow at the bottom
       | the PIR sensor, the top one a mix of the data from my Mi Band 3
       | plus an annotation of when I was in bed (yes I did spend a lot of
       | time reading news before falling asleep and before getting up)
        
         | picture wrote:
         | What was your stack like for data collection and visualization?
        
           | punkybr3wster wrote:
           | Ditto! I'd also love to know what is being used for the
           | visualizations.
        
             | outworlder wrote:
             | Not OP but the screenshot looks identical to Grafana.
        
           | qwertox wrote:
           | Sent via UDP to a small custom Python server which feeds it
           | into InfluxDB, then Grafana for visualization.
        
         | bayat wrote:
         | Very cool! Thank you for sharing this. I would love to see a
         | picture the aforementioned alarm clock setup as well.
        
       | caycep wrote:
       | applications for cities w/ housing availability issues and vacant
       | "investor properties" to apply/enforce a vacancy tax?
        
       | neilv wrote:
       | Since this was done by a student at a university, and at some
       | level this involves tracking people, that's an opportunity to ask
       | a professor about what human subjects protocols you'd need if
       | this was research.
       | 
       | (A professor might be easier to ask than the university human
       | subjects board -- who might refuse to talk with you because this
       | isn't under their jurisdiction, or might get alarmed because you
       | didn't talk with them sooner.)
       | 
       | There might also be campus IT privacy&security policies that
       | apply. Of course people are getting violated much harder by
       | ordinary Web sites, apps, and IoT devices, but sometimes
       | universities aspire to be better than the outer world.
        
         | sodality2 wrote:
         | Yep, I'm talking to a professor about exactly this. Didn't want
         | to include that in case it doesn't work out though!
        
         | elijahwright wrote:
         | People lie to their IRBs all the time, unfortunately.
        
         | alias_neo wrote:
         | I wonder what the "rules" are for things like tracking beacons.
         | They're being broadcast, and they're almost certainly collected
         | without consent everywhere we go.
         | 
         | I happen to live at a junction where my home is regularly
         | surrounded by large numbers of Bluetooth devices; I had
         | considered setting up a BLE tracker purely for "fun" to see
         | what I could collect and discern from the data.
         | 
         | For example, one thing I'm certain of, is a correlation between
         | poor air quality in my home and the arrival of these Bluetooth
         | devices in the area.
        
           | wutwutwat wrote:
           | > So a hidden industry of third-party location-marketing
           | firms has proliferated in response. These companies take
           | their beacon tracking code and bundle it into a toolkit
           | developers can use. The makers of many popular apps, such as
           | those for news or weather updates, insert these toolkits into
           | their apps
           | 
           | > Location marketing aims to understand "online-offline
           | attribution." If a Starbucks coffee ad is sent to your email,
           | for example, marketers want to know if you actually went
           | there and bought a coffee. The only way to know is to monitor
           | your online and offline habits at all times.
           | 
           | > Beacons are also being used for smart cities initiatives.
           | The location company Gimbal provided beacons for LinkNYC
           | kiosks that provoked privacy concerns about tracking passers-
           | by. Beacon initiatives have been started in other cities,
           | including Amsterdam (in partnership with Google), London and
           | Norwich.
           | 
           | https://web.archive.org/web/20231027211008/https://www.nytim.
           | ..
           | 
           | It's the wild west. We're illegally surveilled every day and
           | the government uses the same data in product that allow them
           | to track people without warrants
           | 
           | https://en.wikipedia.org/wiki/Fog_Reveal
           | 
           | There are no "rules" against receiving a signal. All you need
           | is a radio that can pick them up. The rules apply to those
           | broadcasting things afaik
        
             | jon-wood wrote:
             | While I feel like the beacon tracking SDKs in apps are as
             | slimy as going the other way round, those SDKs aren't
             | watching for your device's beacon. They listen for BLE
             | beacons with known addresses the company has place on their
             | premises and use that to determine you've been to that
             | particular location. I'm sure some of them are doing it at
             | a larger scale and tracking all the beacons you see to
             | provide a view on all the places you've been.
             | 
             | Almost all devices you're carrying with you will regularly
             | rotate the ID they transmit in order to avoid correlation,
             | so reading those beacons in a location can't really be used
             | for much beyond rough numbers of devices.
        
           | numpad0 wrote:
           | Depends on local laws and jurisdiction. US famously has no
           | communication secrecy laws, some countries states broadcast
           | is free to listen, etc.
           | 
           | But I think the point made in GP is tracking people, although
           | indirectly, could qualify as `human subject research`, the
           | same technical category as Stanford prison experiment,
           | MKULTRA project, standard website A/B testing, etc. I don't
           | think it's going to be a problem per se, but can be an
           | opportunity to discuss and learn about ethics and processes.
        
           | rokkamokka wrote:
           | I don't understand your last paragraph. Do you mean it
           | correlates to lots of internal combustion engines nearby
           | (e.g. pollution)? How else would this possibly affect your
           | air quality?
        
       | UncleEntity wrote:
       | Bluetooth is fun.
       | 
       | A while ago I was playing around with libble++[0] to read the
       | data from some cheap temp/humidity sensors which broadcast their
       | data as an advertising packet. Pretty simple, considering.
       | 
       | Did some other playing to turn my laptop into a BLE beacon (which
       | worked pretty well with some random app on my phone) and also
       | tried (IIRC successfully) to emulate one of those apple airtags
       | though that code is lost somewhere on said laptop.
       | 
       | A quick glance at my temperature reading code and I'd say that
       | counting the number of seen advertisements would be trivial using
       | that library, I'm currently letting it do all the real work and
       | just filtering the results to the devices I'm interested in.
       | 
       | [0] https://github.com/edrosten/libblepp
       | 
       | --edit--
       | 
       | I also seem to have started writing a python C wrapper around
       | libble++ but obviously never followed through so its completeness
       | is doubtful.
        
       | raajg wrote:
       | > not losing momentum in a nerd snipe like this is CRITICAL
       | 
       | This line got me laughing! My nerd brain has tried so many times
       | to convince me to splurge money with this exact line!
        
         | alias_neo wrote:
         | It's funny, but it's also true. There are definitely times when
         | I'm more "in the mood" for projects; In the winter
         | particularly, I work more on my electronic projects, and do
         | more gaming (my Gaming "season" just started again last week).
         | 
         | Of course the dramatic shift from "what do I feel like doing
         | today" to "what am I in the mood for / have time for this
         | season" has a lot to do with me being older and having children
         | now, but with such huge gaps between me being willing/able to
         | do such projects, losing momentum could mean coming back to
         | something an entire year later, if ever.
         | 
         | I started an electronics project, at least 10 years ago now.
         | It's "infamous" in my family, particularly when my wife needs
         | something to poke at with my projects, for being the project I
         | started over a decade ago, spent a tidy sum on, and am still
         | adamant I will finish one day!
        
           | hasoleju wrote:
           | I can relate a lot. Having the momentum on your side is the
           | main force that pushes such a project forward. Especially if
           | you have family duties that ususally consume most of your
           | free time.
           | 
           | As long as children are smaller even things like scheduling
           | time for your projects don't work very well. Usually because
           | unexpected things happen that need to be handled. Sickness,
           | children waking up early or taking longer to go to sleep,
           | etc..
        
       | H8crilA wrote:
       | There are some very good frameworks out there for doing things
       | like these:                  - ESPHome        - Home Assistant
       | 
       | You can a build a device like this, often without writing one
       | line of code, with ESP32 firmware updates over the air from a web
       | UI.
       | 
       | I wish those ESP chips/mini boards had Zigbee. You can get much
       | better connectivity on a Zigbee network than on wifi in a typical
       | deployment, where every mains powered Zigbee device also acts as
       | a router.
        
         | sodality2 wrote:
         | I have to say, my manual solution was a lot more fun and hands-
         | on and let me learn a lot more skills (C++, Grafana,
         | serverless) than a one-click solution. Though I will be
         | prioritizing maintainability if I decide to go further with it!
        
           | martinsnow wrote:
           | Agree with you fully. It's also a lot more easy to program
           | behavior into an esp32 rather than rely on home assistant pre
           | defined modules.
        
             | H8crilA wrote:
             | It's more about ESPHome rather than Home Assistant.
             | 
             | But yeah it is more fun to do it bare bones :). However,
             | over time I found myself "upgrading" my hand made IoT
             | devices to more standard solutions. Standard solutions
             | usually work better.
        
       | hmottestad wrote:
       | Ultra wide band can be used to detect tiny movements of humans in
       | a room. It can detect a finger twitching from several meters
       | away.
       | 
       | https://www.eenewseurope.com/en/uwb-human-sensor-uses-impuls...
        
         | fotta wrote:
         | Aqara makes a mmWave presence sensor
         | https://www.aqara.com/us/product/presence-sensor-fp2
        
           | haddonist wrote:
           | Make It Work have reviewed 18 different home mmWave sensors,
           | Aqara included:
           | 
           | "Best mmWave Presence Sensors for Home Assistant!"
           | https://www.youtube.com/watch?v=Kt1FpRM8R18
        
           | hmottestad wrote:
           | That's even better!
        
       | linvs wrote:
       | Outstanding write-up! We actually built a business based on this
       | technology (occuspace.io) and have had to address almost all the
       | questions that you brought up. To answer some of them here:
       | 
       | Q: How accurate is BLE beacon count, as a proxy for occupancy?
       | 
       | A: It's very highly correlated but different dining locations,
       | classrooms and library floors have very different correlation
       | factors. Consider using other, perhaps more complex features, as
       | well.
       | 
       | Q: How accurate is BLE beacon availability time, as a proxy for
       | dwell time?
       | 
       | A: We don't believe it is very accurate given that BT MAC's
       | randomize every 8-20 mins depending on manufacturer.
       | 
       | Q: Can we improve the accuracy by setting an RSSI minimum, for
       | which devices weaker than it do not count, to ensure only those
       | who are really nearby get counted?
       | 
       | A: That's a great idea. It's worth noting that different types of
       | spaces and depending on where you installed your sensor, will
       | have different threshold levels.
        
         | georgeashworth wrote:
         | Does this type of occupancy tracking work accurately outdoors?
         | I'm assuming it would be difficult because signals would travel
         | further. (My startup needs occupancy tracking for tennis
         | courts.)
         | 
         | Maybe I could mount a BT device at a corner of the outdoor
         | space and use its signal strength as the threshold value.
        
           | rsaxvc wrote:
           | You could mount one at each corner and use a directional
           | antenna to help.
        
           | rsaxvc wrote:
           | You could use the BLE AoA extension as well to derive an
           | angle: https://www.bluetooth.com/blog/new-aoa-aod-bluetooth-
           | capabil...
        
           | rsaxvc wrote:
           | If you want to get really spicy, you can use two antennas and
           | an SDR to compute angle of arrival using nothing but a BLE
           | advertisement's data . This is different from BLE AoA as it
           | doesn't require a cooperative transmitter.
        
             | alias_neo wrote:
             | If you want to get really, really spicy, use three
             | devices/antennas and you can roughly track in three-
             | dimensional space.
        
               | milkshakes wrote:
               | there are wifi setups that allow you to do this already.
        
               | worthless-trash wrote:
               | I would like to subscribe to this API.
        
               | greggsy wrote:
               | Wouldn't three dimensional tracking need four antennas?
        
           | enva2712 wrote:
           | You the same george ashworth that used to work at d&b?
        
           | linvs wrote:
           | It does but we don't have a battery powered option (yet) so
           | power availability is the bigger issue. Our sensors can only
           | be outlet powered. Each sensor can cover cover roughly 5,000
           | sq.ft. or about two tennis courts.
        
           | sgu999 wrote:
           | Did you consider using BT or UWB as a radar? On a tennis
           | court you won't have any obstacles to worry about, I suspect
           | it's relatively easy to solve.
           | 
           | UWB in particular is accurate enough to get breathing
           | patterns...
        
         | leobg wrote:
         | Can you do fall detection for the elderly?
        
           | hellweaver666 wrote:
           | Seeed Studio (maker of the Xiao) have mmwave sensors that can
           | do fall detection.
        
         | rozab wrote:
         | The differing correlation factors seems like a difficult
         | problem for productizing this. I would expect a STEM college
         | building to have maybe >3 BT devices per person, while other
         | venues may have less than 1.
        
         | bambax wrote:
         | > _How accurate is BLE beacon count, as a proxy for occupancy?_
         | 
         | But how often is an accurate count needed? The initial
         | requirement of the project was just for the OP to "avoid crowds
         | in the dining hall", and for that all one needs is a general
         | idea of highs and lows.
         | 
         | In your business, do customers expect high accuracy? All of
         | them, or some of them?
         | 
         | Also, regarding MAC address, even if they're randomized, isn't
         | there a way to know what kind of device it is? (to count only
         | phones for example, and discard headphones, etc.)
        
       | userbinator wrote:
       | _I am also considering selling it to brick-and-mortar businesses
       | that want to measure occupancy trends._
       | 
       | They've been able to do that long before invasive solutions like
       | this, or computers, even existed --- turnstiles.
        
         | fy20 wrote:
         | How many stores have you visited recently that have turnstiles?
         | You've probably been in more that are using a variation of this
         | tech.
         | 
         | There are a few companies offering it as a commercial product,
         | and yes there are people that want to buy it. The value is not
         | just knowing how many people are in a store, but where they are
         | going, what they are spending their time looking at.
        
       | simonbarker87 wrote:
       | Very impressive. This made me chuckle:
       | 
       | > I asked campus IT to whitelist the MAC address
       | 
       | Back at my uni this would have been a multi week endeavour with a
       | number of forms and likely a denial. This was 15 years ago so
       | hopefully things have changed there now.
        
         | sodality2 wrote:
         | It's actually because our WiFi portal doesn't allow printers,
         | game consoles, etc without browsers to connect, we have to
         | manually add it. Though there is a portal to add it ourselves
         | without IT, I am at the max 5 devices through there so I have
         | to ask for overrides :)
        
       | demondemidi wrote:
       | Really should point out this is for BLE. I thought it was based
       | on heartbeats, heat, ingress/egress, or something more physical.
       | 
       | Also, ESP32 Rx sensitivity is kinda poor: -94 dBm, most devices
       | are pushing -100, -102 and even -104 dBm. Makes a big difference.
        
       | scrose wrote:
       | It's great seeing all the different approaches people can come up
       | with to do similar things!
       | 
       | I recently worked on a similar side project but took a very
       | different approach. It pretty much was just:
       | 
       | 1. A bash script in a Raspberry Pi W that ran `bluetoothctl` once
       | per minute and wrote the raw output for that minute to a file.
       | 
       | 2. A systemd process that would ensure the script would get re-
       | run if the process crashed or machine rebooted
       | 
       | 3. A separate script that parsed the logs and pushed them to a
       | local sqlite DB that tracked the minute the bluetooth id was
       | found.
       | 
       | 4. A daily push to my backup server that stored the sqlite DB
       | 
       | I decided to use systemd and a simple(10loc) bash script since I
       | faced a similar issue to the OP where my pi would crash(although
       | it was purely my fault for messing with a bunch of cables) and I
       | wanted a way to get it running again with the lowest overhead. I
       | also wanted as few dependencies as possible.
       | 
       | I went with SQLite because it made querying for trends and
       | building dashboards simpler and more interesting(and because the
       | real project was the sqlite interface I was building :) )
       | 
       | One thing I learned from running this which also may answer a
       | couple of OPs questions: Apple devices like hiding themselves.
       | The only way I could get my Apple laptops and phones to show up
       | was to keep the bluetooth settings menu open on them. Otherwise,
       | they wouldn't appear to the bluetooth scanner. I can't speak much
       | to whether android devices are 'noisier', but the lack of
       | bluetooth feeds for Apple devices alone cuts out a significant
       | part of the value of bluetooth scanning for crowd sensing, given
       | the market share for Apple devices.
        
       | dekhn wrote:
       | I was initially skeptical, but these days most of my ESP32s just
       | run micropython and I write my "firmware" in Python. This
       | includes (low-performance) callbacks for interrupts, python
       | dicts. Because the ESP32 has nice peripherals, lots of stuff is
       | really just using python or C as a command-and-control to enable
       | part of the chip, like PWM. And porting Python to C++ is usually
       | pretty straightforward.
        
       | ezconnect wrote:
       | He could have just placed a microphone and correlate noise to
       | occupancy level.
        
         | sodality2 wrote:
         | I cannot measure that from a closed dorm room, also plenty of
         | environmental noise in empty rooms or silent, full rooms
         | (during finals!).
        
       | stblack wrote:
       | This kid is alright. He's going to do great. Keep doing what
       | you're doing kid.
        
       | Simon_ORourke wrote:
       | I'm wondering how accurate this might prove measuring automobile
       | traffic on a relatively busy street?
       | 
       | I've been pondering some way to track the number of vehicles that
       | pass my house each day, and would like (but not ideal) to get
       | their average speed too. Was thinking about computer vision, and
       | even audio doppler approaches, but this seems simpler - if only
       | the bluetooth sensing might work on a device moving at 50mph+ at
       | a distance of maybe 20 to 100 yards.
        
         | the-kenny wrote:
         | I have an unorthodox suggestion: get a bicycle rear radar like
         | the Garmin Varia. They communicate via Bluetooth LE incoming
         | cars, their count, and an average speed (in a set of ranges
         | like 0-15km/h, 15-30km/h, ...).
         | 
         | Mount this with a usb cable somewhere, add an ESP32 or anything
         | with Bluetooth somewhere else to accumulate.
        
           | Simon_ORourke wrote:
           | Seems workable - thanks!
        
       | jve wrote:
       | > but some don't carry any devices at all (or keep Bluetooth off
       | on their phone).
       | 
       | Exactly. I always had a habit of turning BT off, hoping to
       | conserve the battery. And then our security department advises it
       | - if you don't use it at the moment, turn it off. For security
       | reasons. Flaws in BT sometimes come up and good if a user can get
       | that update.
        
         | wutwutwat wrote:
         | Turning bt "off" in your settings likely doesn't actually turn
         | off the radios. It probably just disables connections. If you
         | use lost phone features like findmyiphone, the defaults are to
         | participate in the "find my" network, a mesh network of bt low
         | energy devices for airtags, etc. Most often, even turning off
         | your entire phone won't help, as the incentive to participate
         | in the "find my" network is they give you the feature of
         | tracking your phone even when powered off
         | 
         | https://en.wikipedia.org/wiki/Find_My
         | 
         | https://en.wikipedia.org/wiki/IBeacon
        
       | logbiscuitswave wrote:
       | I have a couple of home automation projects that use Raspberry Pi
       | boards and rely on BLE. One thing I've found is the onboard
       | Bluetooth is pretty awful on the Pi 3 and not very good on the Pi
       | 4. Switching to use a Bluetooth dongle made a huge difference
       | with stability and reliability. I no longer had to continually
       | run scripts to try to reset kernel drivers or the entire
       | Bluetooth stack when things got cranky (which seemed to happen
       | many times a day). As a bonus I got a Class 1 adapter that would
       | let me tweak the power output.
        
       | bArray wrote:
       | I'm one of those annoying people that keep BT, data and location
       | services off to save power. WiFi I mostly keep on, so in theory
       | you could collect data this way too.
       | 
       | Regarding the ESP32's reliability, it's likely power supply
       | related. I've seen exactly this before. I run ESP32 devices with
       | a 1A 3v3 linear regulator with a little smoothing, and zero
       | issues.
        
       | hellweaver666 wrote:
       | Just a tip, if you want an ESP32 board that has more memory etc,
       | look at the range from Unexpected Maker!
        
       | yonz wrote:
       | Someone get this kid a job.
       | 
       | Way at the bottom: Think this was cool? Hiring software
       | engineering interns for Summer 2024? Check out my resume here:
       | https://docs.google.com/document/d/1EN2k5ZUOLTvMs_NZtUq8tKVK...
        
       ___________________________________________________________________
       (page generated 2023-11-14 23:02 UTC)