-
-
Notifications
You must be signed in to change notification settings - Fork 39
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
Conversation
There was a problem hiding this 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.
## 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
## 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
## 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
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. :) |
include content cuts Co-authored-by: Kevin Lewis <kvn@lws.io>
Hi @phazonoverload |
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! |
Completed the first draft of the article on Directus and IoT: Sensor Data with an ESP32.
Closes #119