Skip to main content

ESP-IDF Basics - Assign. 2.3 (Optional)

·1 min·
Table of Contents

This assignment is optional and it should be done if there’s some time left before the break.

Add another route to the HTTP server from the previous assignments:

  • POST /led/flash → accepts JSON {"periods": [int], "duty_cycles": [int]} and for each element, calculates the on-time and off-time and drives the LED accordingly.

You need to first check that both periods and duty_cycles have the same length and contain positive number only. duty_cycles should contain numbers between 0 and 100.

Then, you can traverse the two arrays and calculate for each element at index i the LED on_time and off_time as follows:

on_time[i] = duty_cycle[i]/100 * periods[i]
off_time[i] = periods[i]-on_time[i]

Now you can drive the LED according to the sequence:

ON: on_time[1]
OFF: off_time[1]
ON: on_time[2]
OFF: off_time[2]
...

Conclusion
#

If you managed to reach this point, it means you have good understanding of a basic REST API implementation. You can now move to the third lecture, detailing the management of external libraries and the use of the components found on the component registry.

Next step
#

Next lecture → Lecture 3

Related

ESP-IDF Basics - Assign. 1.1
3 mins
ESP-IDF Basics - Assign. 1.2
1 min
ESP-IDF Basics - Assign. 2.1
7 mins