[DigiKey Smart Manufacturing Happy Creativity Contest] STM32MP157D Remote Pet Bird Feeder One MQTT GPIO Control
[Copy link]
STM32MP157D-DK runs on Linux. To achieve remote bird feeding, the previous article has introduced the transplantation of MQTT, which realizes the remote sending of data to STM32MP157D through MQTT. On this basis, GPIO needs to be controlled.
To control GPIO in Linux, you need to know the pin number of each GPIO. Starting from Linux 4.8, the sysf interface is no longer recommended to control GPIO. Instead, the libgpio library is used to control GPIO using character devices in user mode. At the same time, some tools are also provided to help users debug GPIO, such as gpiodetect to list all gpiochips on the system, gpiofind to find the corresponding gpiochip and the offset within the line by name, gpioget to read the value of the specified gpio line, gpioinfo to list all gpiochip lines and their names, users and direction activity status and other information, gpiomon to wait for events on the specified gpio line or specified monitored events, and gpioset to set the value of the specified gpio line.
The GPIOA14 of the STM32MP157D-DK1 development board is connected to a yellow LED indicator light. The high and low outputs of GPIO14 can be controlled through the gpioset command. For example, gpioset gpiochip0 14=0 or gpioset gpiochip0 14=0 controls the GPIO output to be high level to light up the yellow LED light.
Through this, a GPIOA14 control interface function can be encapsulated.
void gpioA14_ctrl(unsigned char u8level)
{
unsigned char cmdbuf[255];
memset(cmdbuf,0,sizeof(cmdbuf));
snprintf(cmdbuf,sizeof(255),"gpioset gpiochip0 14=%d",u8level);
printf("cmdbuf %s\r\n",cmdbuf);
system(cmdbuf);
}
Define a thread to process MQTT data to achieve control
void * mqtt_process(void *parg)
{
unsigned u8cmd=0;
//parse json data
switch(u8cmd){
case LED_ON:
gpioA14_ctrl(0);
break;
case LED_OFF:
gpioA14_ctrl(1);
break;
default:
;
}
}
|