In the last article, we covered how to authenticate and connect the W5100S-EVB-Pico to Azure IoT Central using an X.509 certificate.
The above process can be briefly summarized as follows.
- W5100S-EVB-Pico development environment installation
- Create and configure IoT Central application
- Generating X.509 Certificates (Using OpenSSL)
- Download and build the project
- Execution and monitoring
In this article, I would like to summarize how to send a C2D (Cloud to Device) message to the connected W5100-EVB-Pico and use it to control other devices.
There are several ways to use C2D messages in Azure IoT Central, and we will use commands among them.
Commands enable remote management, such as controlling devices or changing settings. Alternatively, state data may be collected from the device.
More details can be found in the document below.
Based on the W5100S-EVB-Pico device, we will summarize the contents of adding Azure IoT functions for using related functions and simply controlling an external device (Raspberry Pi 3) using GPIO.
Since it is based on the previous project, it is assumed that steps 1 to 3 have been preceded.
ComponentsThe structure used in this article is as follows.
- W5100S-EVB-Pico
- Raspberry Pi 3 (to be controlled, possibly other devices)
- Jumper cable
- Ethernet cable
In addition to the existing hardware configuration, I needed a control target, and I had a Raspberry Pi playing on my desk, so I used it. If you can control the GPIO and run python, you can use any Pi.
I selected one GPIO pin and connected it to W5100S-EVB-Pico using a jumper cable.
Download Project
In the last article, I used the provisioning example as an example for interworking with Azure IoT Central.
If you have a downloaded project, you can use it as is, otherwise, download it using git clone.
git clone https://github.com/Wiznet/RP2040-HAT-AZURE-C
W5100S-EVB-Pico codemain.c
Set the example to be used. We used the X.509 Provisioning example.
// The application you wish to use should be uncommented
//
//#define APP_TELEMETRY
//#define APP_C2D
//#define APP_CLI_X509
#define APP_PROV_X509
sample_certs.c
Add the previously created certificate as a variable value.
prov_dev_client_ll_sample.c
In the sample source code, add and register a callback function for calling IoT Central Commands.
Related examples can be found at the link below.
The contents of the code below are as follows.
- Callback function call when sending commands
- Method name comparison (Used Name: powerReset)
- Perform setting action and send a response
In W5100S-EVB-Pico, GPIO15 was used. (GP15)
The pin-out can be referred to at the following link.
#define RESET_GPIO 15
#define LED_PIN 25
As an option, a user LED control part is temporarily added to check whether the function works.
static int deviceMethodCallback(const char* method_name, const unsigned char* payload, size_t size, unsigned char** response, size_t* response_size, void* userContextCallback)
{
(void)userContextCallback;
(void)payload;
(void)size;
int result;
// User led on
gpio_put(LED_PIN, 1);
printf("Device method %s arrived...\n", method_name);
if (strcmp("powerReset", method_name) == 0) {
printf("\nReceived device powerReset request.\n");
const char deviceMethodResponse[] = "{ \"Response\": \"powerReset OK\" }";
*response_size = sizeof(deviceMethodResponse)-1;
*response = malloc(*response_size);
(void)memcpy(*response, deviceMethodResponse, *response_size);
sleep_ms(500);
gpio_put(RESET_GPIO, 1);
sleep_ms(100);
gpio_put(RESET_GPIO, 0);
result = 200;
} else {
// All other entries are ignored.
const char deviceMethodResponse[] = "{ }";
*response_size = sizeof(deviceMethodResponse)-1;
*response = malloc(*response_size);
(void)memcpy(*response, deviceMethodResponse, *response_size);
result = -1;
}
// User led off
gpio_put(LED_PIN, 0);
return result;
}
Finally, register the device method callback to the existing device client handle.
(void)IoTHubDeviceClient_LL_SetMessageCallback(device_ll_handle, receive_msg_callback, &iothub_info);
// added line
(void)IoTHubDeviceClient_LL_SetDeviceMethodCallback(device_ll_handle, deviceMethodCallback, NULL);
Raspberry Pi 3 Python codeOn the Raspberry Pi 3 to be controlled, I wrote Python code to monitor the GPIO status and perform actions accordingly.
To write the code, you can connect to the Raspberry Pi and write it with an editor such as Vim, or you can write it on the host PC and copy the code with Samba or scp.
Here, after setting up Samba, the code was loaded from VS Code, edited and tested.
The contents are as follows.
- Use BCM mod
- Selectable between Board mode and BCM mode
- Use of BCM mode based on GPIO Pin
- See Pinmap: Raspberry Pi Pinmap
- GPIO Falling event detection
- Performs the set action when an event occurs (eg reboot)
gpio_test.py
import RPi.GPIO as gpio
import time
import os
channel = 23
# Set numbering system
gpio.setmode(gpio.BCM)
# gpio.setmode(gpio.BOARD)
# BCM23 (pin 16)
gpio.setup(channel, gpio.IN, pull_up_down=gpio.PUD_UP)
def restart():
print('System will be restart...')
time.sleep(2)
os.system("sudo reboot")
def on_falling(channel):
print(f'gpio falling event detected! {channel}')
restart()
try:
# Add event detect and callback function
gpio.add_event_detect(channel, gpio.FALLING, callback=on_falling, bouncetime=200)
# Wait for event
while True:
time.sleep(1)
except KeyboardInterrupt:
gpio.cleanup()
finally:
gpio.cleanup()
Azure IoT Central application setupAdd a Command component
In the existing template configuration, added a Command type capability with the name powerReset
.
If you have never created a template, add a value after creating it.
If you have never created a template, add a value after creating it.
The name value is the name of the method actually called from device and is case-sensitive.
After saving, the changes are applied when publish is completed.
When publish is complete, the Publish button is disabled.
Now, once go to the device detail page, you can see the Command
tab and there is a component added previously.
Commands can be sent to the device through this tab.
Build, Run and MonitorBuild and upload firmware to W5100S-EVB-Pico
Build the modified code and upload it to the device.
You can refer to the previous article on how to build and upload.
Additionally, I used Tera Term to monitor real-time.
Execute python code on Raspberry Pi 3
After connecting to Raspberry Pi 3 via ssh, run the python code.
python gpio_test.py
Send Command and Monitoring
Go to the Commands tab of the device page and click the Run
button of powerReset component to send the command.
A command is sent to the connected device, and when a response is received, it is treated as a successful command.
You can check command execution logs such as execution time and response through Command History.
For the device monitoring, I set up the windows as follows the image.
The left terminal monitors W5100S-EVB-Pico through the COM port, and the right terminal is the screen connected to Raspberry Pi through SSH.
- Sending commands from IoT Central
- Receive powerReset command from W5100-EVB-Pico
- Send a signal to RPI3 via GPIO
- RPI3 do Reboot by receiving a signal from W5100S-EVB-Pico
You can see that the command is received normally and the raspberry pi reboots and the SSH connection is disconnected.
In this article, only a simple reboot operation is applied, but if you apply it, you will be able to control and manage various components.
Comments
Please log in or sign up to comment.