Issue link: https://resources.mouser.com/i/1442841
11 / 11. Check to see if at least 1000ms has elapsed since the last message has been sent to the MQTT broker. If so, transmit a new message to the broker with the latest reading from the ambient light sensor. Handle Any Messages Received from the MQTT Broker 12. Print the received message onto the serial port. 13. Convert the payload to a string. 14. Determine if the string is requesting to toggle the manual control on or off; or if it is an integer that should be assigned as the manual brightness set point. A quick note on the map function used in the project. The function definition for the map is the following: map(value, fromLow, fromHigh, toLow, toHigh) The map function is very useful for re-mapping one range of numbers to another range of numbers. For example, the ADC has a resolution of 1024, whereas the analog Write function only accepts values of 0 to 255. The map function takes five arguments including: 1. The number to be scaled. 2. The low-end of the range the number is being scaled from. 3. The high-end of the range the number is being scaled from. 4. The low-end of the range the number is being scaled to. 5. The high-end of the range the number is being scaled to. Figure 1: The raw data stream from the microcontroller and smartphone application. (Source: Mouser Electronics) The map function returns an integer that has been re-mapped to the new scale. For example: X = map(50, 1, 100, 1, 200); In this case, x would be set to 100 because 50 is halfway between 1 and 100 on the old scale and 100 is in the middle of the new scale. Project Files The following files can be found in this project's GitHub repository. • MicrochipHorticultureLighting.ino: The project-specific code for this effort is stored in this file. It is based upon an example that is provided by Medium One to demonstrate how embedded devices can interact with the IoT platform backend. • secrets.h: When working on a project that is made publicly available, there is always a risk of exposing sensitive data such as passwords or API keys. Instead of hardcoding such information directly into the firmware and having to remember to alter the code prior to each Git commit, we can create an unpublished header file to store this information. Libraries The preprocessor directive #include lets us add libraries into our projects. This promotes code re-use. Unless you have very specific needs, re-inventing the wheel is not necessary. This project uses the following libraries (Figure 1): • ArduinoMqttClient.h: This library provides a convenient interface to MQTT-based services. • WiFi101.h: This library provides the interface to the ATWINC1500 chip to simplify interacting with Wi-Fi ® networks.