Networked Temperature Sensor

Oct 13, 2024 |  Author: Nic La |  Tags:  project   code   3D print   microcontroller  

More than just a temperature sensor, the Networked Temperature Sensor (NTS) integrates into your Home Assistant to regularly report temperature data and alert you of temperature events. I recently switched my home automation hub to Home Assistant and it is the one home automation hub to rule them all. It owes allegiances to no brand and yet accommodates every brand. Home Assistant's accommodating nature had me questioning if I could integrate my own home automation devices. The NTS is a resounding yes!

Step 1: Materials + Tools

Materials:

Tools:

Step 2: 3D Print Parts

Start by downloading the enclosure CAD files from Thingiverse. Print using your preferred 3d printer. I used a Prusa MK3S+. Recommended printer settings in the link.

I need more filament

Once the parts are finished printing, do whatever post processing you believe is necessary. There shouldn't be much as the parts are designed to be printed without any supports. Use a soldering iron and needle nose pliers to secure the threaded inserts in the enclosure base (Part_Studio_1_-_Part_1.stl). The Pico mounting gets x4 of the M3 H8. The lid mounting gets x4 of the M4 H10.

Step 3: Program

While the parts are printing, follow Installing CircuitPython to setup your Pico with the latest CircuitPython. With CircuitPython configured, add the following libraries to the lib folder on the Pico's CIRCUITPY drive. Libraries can be found in the Library Bundle.

Download the project code from the GitHub Repo. Configure your settings.toml by replacing your_wifi_ssid_here and your_wifi_password_here with your wifi info. For now, visit Webhook.site for a "Your unique URL". Leave the page open for later testing. Replace your_webhook_endpoint_url_here with this URL. We'll revisit settings.toml with a Home Assistant webhook endpoint URL once we configure Home Assistant. Copy code.py and settings.toml to the Pico's CIRCUITPY drive.

Step 4: Assemble

Mount the Pico in the PiCowbell being sure to orient the Pico such that the USB and STEMMA ports are on the same side. Prewire the Pico with USB cable, temperature sensor and rotary encoder according to the below schematic. Wire the rotary encoder by first soldering individual wires to each terminal. Heatshrink as necessary. Install the Prewired Pico in the enclosure base.

A Decent Schematic

Some Assembly Required

Prepare the enclosure lid. Reason I designed a separate TPU spacer for the OLED display is because I cracked the first OLED display when I went to install it. This is what a cracked OLED display looks like.

Sad Face

WARNING: Don't crack your OLED display. A cracked OLED display is not very useful.

Solder individual wires to the green push button terminals. On the negative terminal, solder the 470 Ohm resister. Heatshrink as necessary. Install the TPU spacer, OLED display and green push button in the enclosure lid.

Happy Face

Bring it all together. Mount the temperature sensor in the enclosure base front opening. Use the STEMMA cable to wire the sensor to the OLED display. Land the green push button on the PiCowbell according to the schematic. Install the rotary encoder in the enclosure lid. Lastly install the USB panel mount in the enclosure base and connect the USB cable.

Together At Last

Sensor

At this point, there's probably not a lot of room left. Carefully tuck the cables inside and close the lid. Screw the lid down until snug. Don't forget to add the little rubber bumper feet.

Batten Down The Hatches

Step 6: Test

Test that your NTS is functioning as intended.

  1. On power up, the OLED display should report the actual temperature at all times. Place your hand against the sensor. The temperature should increase...unless you're in a very warm environment for some reason.

  2. Press the green button. The LED should turn on and the temperature data should be reported to your instance of Webhook.site from earlier. Data example below. Press the green button again to turn off the LED and temperature reporting.

  3. Rotate the knob. The Upper Alert value should change depending on the direction the knob is rotated. Press the knob button and rotate. Pressing the knob button should toggle between the Upper and Lower Alert setpoints. Change the Upper or Lower Alert value such that the Temp value falls outside this range. Return to Webhook.site. Again, you should see data similar to the below. Continue to monitor Webhook.site. New data should arrive every 10 minutes.

    Data Example:
    {
        "temperature_f": 74.9,
        "critical_alert": True,
    }
    

If all looks well, continue on to Step 7. Otherwise, rinse and repeat until clean, i.e. step through 1 through 6 again to make sure you didn't miss anything.

Step 7: Setup Home Assistant

You've finished making a device that periodically reports the temperature via webhooks. Now, let's use that webhook data to display live temperature on Home Assistant and generate temperature alert notifications on your phone.

  1. We're using Home Assistant because it has a built-in webhook server. This allows us to use the existing dashboard to display our temperature data, saving us from having to build a server and dashboard from scratch. Follow Home Assistant's guide DIY with Raspberry Pi to get started with Home Assistant OS. Other installation methods will work. I'll just judge you.

  2. Follow Remote UI to setup Remote Access. Remote Access is needed to access your Home Assistant instance remotely from your phone. I used Nabu Casa because it's convenient.

  3. Download the Home Assistant Mobile App for you respective mobile device. Setup the mobile app and connect to your Home Assistant instance.

  4. Create a Sensor entity. From the Home Assistant OS > Settings > Devices & Services > Helpers > Create Helper > Number.

    Pico Temp Sensor

    Note the Entity Name "Pico Temp Sensor".

  5. Assign temperature data to the Sensor entity using Webhooks configuration. From the Home Assistant OS > Settings > Automations & scenes > Create Automation > Create new automation.
    • When: Add Trigger > Webhook. Note the Webhook ID. From the cogwheel, deselect "Only accessible from the local network" to make your webhook accessible from the cloud.
    • Then do: Add Action > Input number: Set. Switch to the YAML editor (three dots > Edit in YAML). Copy and paste the following into the YAML editor and save the automation.

    service: input_number.set_value
    metadata: {}
    data:
    entity_id: input_number.pico_temp_sensor
    value: "{{ trigger.json.temperature_f }}"
  6. Update the webhook endpoint on the NTS to the newly created webhook endpoint URL on Home Assistant. From the Home Assistant OS > Settings > Home Assistant Cloud > Manage (next to your automation's webhook). Note the Public address. From your settings.toml on your Pico, replace your_webhook_endpoint_url_here with the Public address URL. Reboot the NTS and press the green button to trigger the webhook. From the Home Assistant OS > Settings > Automations & scenes, you should now see a value under Last triggered column indicating that your Automation is receiving webhook data from the NTS.

  7. Display Sensor temperature data in a Sensor Card. Go to the default dashboard or follow Creating a new dashboard to create a dashboard of you own. I'm partial to the Tile layout. From the dashboard, edit and add a Sensor Card. For Entity, enter "Pico Temp Sensor". For Units, enter "°F". Save. Wait and watch the temperature data flow in. Look at all that data.

    That's Nice


  8. Setup mobile notifications for when the temperature reporting is first triggered. Earlier, when you setup the Home Assistant mobile app, you also add a new mobile integration to your Home Assistant OS. You can now use that integration in Automations. From Home Assistant OS > Settings > Automations & scenes > Create Automation > Create new automation.
    • When: Add Trigger > Webhook. Replace the generated Webhook ID with the Webhook ID noted from the previous Automation. This way, the same webhook data is parsed by both Automations.
    • And if: Add Condition > Template. Enter the below in Value template*. This parses the webhook data for the alert flag.

      {{ trigger.json.critical_alert == True }}
    • Then do: Add Action > Notifications: Send a notification via your mobile app integration. Enter a descriptive message. Something derivative like "Temperature Alert!".

    Reboot the NTS, adjust the Alert thresholds, or press the green button to trigger a temperature notification on your phone.

    Temperature Alert!

Step 8: Enjoy

Enjoy. Place the NTS on your prototype project to monitor temperature over long periods of time. Place on your 3d printer if you're worried about thermal run away events and filling your apartment with smoke. Place on your bonsai to know when it's too hot outside and time to bring the plant inside. Go wild.

Happy Bonsai


Leave a comment: