Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directus and IoT: Sensor Data with an ESP32 #159

Closed
wants to merge 12 commits into from

Conversation

vicradon
Copy link

@vicradon vicradon commented May 2, 2024

Completed the first draft of the article on Directus and IoT: Sensor Data with an ESP32.

Closes #119

@phazonoverload phazonoverload changed the title First Draft - Directus and IoT: Sensor Data with an ESP32 Directus and IoT: Sensor Data with an ESP32 May 7, 2024
Copy link
Contributor

@phazonoverload phazonoverload left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good post. You'll notice most of my suggestions are removing a lot of content.

You don't need to teach people how to use Directus - for this novel project it's ok to assume they hold that knowledge, or link out to documents like the quickstart when they don't.

You'll also notice that I removed your thoughts about how to host Directus - it's better to stay generic and link to a quickstart. This isn't the place for that editorial voice.

But very good - take a look through suggestions / requests / comments and re-request a review once you've made changes.

directus-and-iot-sensor-data-with-an-esp32/index.md Outdated Show resolved Hide resolved
directus-and-iot-sensor-data-with-an-esp32/index.md Outdated Show resolved Hide resolved
directus-and-iot-sensor-data-with-an-esp32/index.md Outdated Show resolved Hide resolved
directus-and-iot-sensor-data-with-an-esp32/index.md Outdated Show resolved Hide resolved
Comment on lines 112 to 130
## Creating dummy temperature and humidity values

You can immediately populate your temperature_and_humidity table by making a POST request. This POST request will have an Authorization header with the token inputed as a Bearer token. The snippet below shows an example post request using cURL. To run this request, replace the `<YOUR_TOKEN>` with the token generated in the previous section and then run it in a UNIX shell or Powershell.

```
curl --location 'http://localhost:8055/items/temperature_and_humidity' \
--header 'Authorization: Bearer Vi8m1wdXTEv0rhQtteUWaZKfHTqCOwDx' \
--header 'Content-Type: application/json' \
--data '{"temperature": 33.34,"humidity": 80.42}'
```

After running the curl command above, you will see a new value in the collection on your Directus dashboard (/admin/content/temperature_and_humidity).

You can run the cURL command with different values of temperature and humidity to see more data in your collection, but first, take a moment to look at the URL used in the cURL command above. It is your Directus root URL at localhost:8055 with a path /items/temperature_and_humidity. Directus gives a straightforward way to interact with collections via a REST API by appending /items to the root URL and then the collection name. You can perform API operations using the standard REST principles such as:

- GET /items/temperature_and_humidity
- POST /items/temperature_and_humidity
- PATCH /items/temperature_and_humidity
- DELETE /items/temperature_and_humidity
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this - it doesn't add any direct value other than a validation that the permissions are correct. They're fairly straightforward so it's safe to assume that a user can debug them by reading the instructions closer if required.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

directus-and-iot-sensor-data-with-an-esp32/index.md Outdated Show resolved Hide resolved
Comment on lines 183 to 192
## Sending the temperature and humidity data to Directus

At this point, you have your ESP32 logging data to the Serial monitor. But you actually want to send this data to Directus. You have to introduce the HTTP and WiFi libraries to achieve this. The WiFi library connects your ESP32 to the internet while the HTTP library turns your ESP32 into an HTTP agent that can make GET and POST requests among others. The script below is the complete code for logging data to Directus. Here are the things you must change for the script to work:

1. Your WiFI SSID, i.e. the name of your WiFi network, as the value of the `ssid` variable on line 6.
2. Your WiFi password on line 7.
3. Your Directus esp32-board user token that you used with cURL at the `Creating dummy temperature and humidity values` section of this article. You can still [regenerate the token if you lost it](./regenerate_lost_token.png). If you have the token, set it as the <TOKEN> placeholder value on line 8.
4. Your WiFi gateway address is defined on line 9 as `directusEndpoint`. It won't start with localhost because the ESP32 is running as a separate system. So you must check the gateway address of your local network. This address will either start with 10, 172, or 192. Follow [this blog post](https://nordvpn.com/blog/find-router-ip-address/) for instructions for checking your gateway address on different operating systems. Note that your ESP32 and your computer running Directus must be connected to the same WiFi network for this to work.

With all the changes made, you can upload your script to your ESP32 and observe it log temperature and humidity data on your Directus `temperature_and_humidity` collection.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Sending the temperature and humidity data to Directus
At this point, you have your ESP32 logging data to the Serial monitor. But you actually want to send this data to Directus. You have to introduce the HTTP and WiFi libraries to achieve this. The WiFi library connects your ESP32 to the internet while the HTTP library turns your ESP32 into an HTTP agent that can make GET and POST requests among others. The script below is the complete code for logging data to Directus. Here are the things you must change for the script to work:
1. Your WiFI SSID, i.e. the name of your WiFi network, as the value of the `ssid` variable on line 6.
2. Your WiFi password on line 7.
3. Your Directus esp32-board user token that you used with cURL at the `Creating dummy temperature and humidity values` section of this article. You can still [regenerate the token if you lost it](./regenerate_lost_token.png). If you have the token, set it as the <TOKEN> placeholder value on line 8.
4. Your WiFi gateway address is defined on line 9 as `directusEndpoint`. It won't start with localhost because the ESP32 is running as a separate system. So you must check the gateway address of your local network. This address will either start with 10, 172, or 192. Follow [this blog post](https://nordvpn.com/blog/find-router-ip-address/) for instructions for checking your gateway address on different operating systems. Note that your ESP32 and your computer running Directus must be connected to the same WiFi network for this to work.
With all the changes made, you can upload your script to your ESP32 and observe it log temperature and humidity data on your Directus `temperature_and_humidity` collection.
## Sending Data to Directus
At this point, you have your ESP32 logging data to the Serial monitor. To send these to Directus, you have to introduce the HTTP and WiFi libraries to your project.
The WiFi library connects your ESP32 to the internet while the HTTP library turns your ESP32 into an HTTP agent that can make HTTP requests. The script below is the complete code for logging data to Directus - add it to the `xyz` file:

Replace XYZ. Move the 'things to change' and 'how to validate it works' below the snippet. Can you please also add a breakdown of what's it's doing? This is the key part of education in this post so give it some time.

It may even be worthwhile to split it out into a few subsections - installing libraries, connecting to internet, making request.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

directus-and-iot-sensor-data-with-an-esp32/index.md Outdated Show resolved Hide resolved
directus-and-iot-sensor-data-with-an-esp32/index.md Outdated Show resolved Hide resolved
@phazonoverload
Copy link
Contributor

Hey @vicradon how are you getting on? I'm away this week but would love to be able to review changes next Monday if possible. :)

vicradon and others added 3 commits May 29, 2024 00:32
@vicradon
Copy link
Author

Hi @phazonoverload
Thank you for reviewing my article. I have effected the changes you requested.

@phazonoverload
Copy link
Contributor

Thank you for writing this, and we are happy to consider it done in it's current state. My team will schedule it in to be published.

This repo is just for authoring/reviewing - the actual piece will be published via a Directus project, so we'll take it from here. I'll close this issue to keep the main branch clean, but this is a acceptance of your work.

Could you email me at devrel@directus.io and we'll share what we need to get you paid. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Directus and IoT: Sensor Data with an ESP32
2 participants