Lesson 9 - Radio, WiFi, Bluetooth (IoT)


About


Home



Introduction

Today we were challenged to use the Internet of Things to control a microcontroller remotely. By the end of this lesson we should be familiar with Radio and WiFi communication with Arduino, as well as a fundamental understanding of various objects in the IoT.

I have always really enjoyed working with APIs. I am a massive proponent of the open-source movement, and free APIs are definitely a driving force for this movement. Besides working with an API, I also want to work toward the completion of my final project by sending Serial data between two Arduinos. To solve this, I decided on using radio communication.

Components

This project required 2 components: radio communication between two Arduinos, and Arduino communication with an API.

Part 1 - Radio Communication

For my final project, I began working on a way for my input and output devices to communicate. This will need to happen wirelessly, so I settled on using radio communication. My current output device takes a coordinate pair as a Serial input.

To begin sending this data over radio, I first found the mac addresses of my two boards using this code.

Code:

Next, I examined this sample code that uses ESP-NOW.

Code:

Importantly, this code defines a datatype called "struct_message".

Code:

Because I need to send two doubles, I changed this defintion to the following.

Code:

I then added the mac address of my recieving Arduino. Additionally, I ensured that data would only be sent if there was something in the Serial. This gave me my final program.

Code:

A key part of this program is that it parses all of the information that is inputted. I could have done this on the receiving end, but opted to do it here to more equally distribute computation.

Next, I took a look at the tutorial's sample code for receiving information.

Code:

I then integrated the tutorial's reciever code into my existing code for my output device (the full output device's code will be included in the final project documentation).

Code:

As you can see, I replaced the portion of the code that determines the coordinate to move to with the information that was sent from the Arduino (already parsed).

I then tested the radio communication out and it worked perfectly!


Fig 1. Final Arduino communication

Part 2 - API Communication

To begin, I took a look at a list of free APIs and settled on one called "Agify". As you can see, this is a simple API with a simple functionality: guessing one's age based on their name.

Final sketch
Fig 2. Agify documentation

I started experimenting with the API and got some very interesting results.

Final sketch
Fig 3. API request used
Final sketch
Fig 4. Data from Fig 3 request

I then read the APIs documentation and learned that I could use a country code to narrow down the results. I tried this as well.

Final sketch
Fig 5. API request used
Final sketch
Fig 6. Data from Fig 5 request

I took a look at some example code for reading information from an API.

Code:

Next, I switched out the endpoint for an endpoint from Agify.

Code:

I tested this program and got the correct results.

Now that I had a way to guess the age of a given name, I had to create a way for the GET request to change (if two different named people want to play). For this, I used more String methods. By starting with a root endpoint and concatinating it with a name query, I was able to get results for multiple names.

Now I have to sort through the JSON data I am getting in order to get the specific information I want. To do this, I used the ArduinoJson library.

Final sketch
Fig 7. Library used

Finally, I used the various pieces of information that I got to make a fun message display in the Serial. This is what the final code looked like.

And this is how it worked!

Final sketch
Fig 8. Final collected data and message

Concluding Remarks

This project was extremely difficult to document as the majority of it was nonphysical. However, I really enjoyed showing my programming process as I move from something that I know to work to something experimental or new. This was an absolutely massive step toward the completion of my final project, and I am really happy with how I integrated radio communication into it.

Files Mentioned in this Document

Get Mac Address (.INO)

Send Data (.INO)

Receive Data (.INO)

Read from API (.INO)