代码拉取完成,页面将自动刷新
同步操作将从 刘煜/FSMP1A开发板示例代码 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include <stdio.h>
#include <string.h> //strlen
#include <unistd.h> //sleep
#include <errno.h>
#include <mosquitto.h>
char* uid = "4e78c607dbbc9f4d6b5c16ffa5622fb7";
//写文件
//参数1:文件路径
//参数2:写入的字符串
//返回值:成功返回0,失败返回错误码
int write_file(char* path, char* data)
{
FILE* fp = fopen(path, "w");
if (fp == NULL)
{
perror("fopen");
return errno;
}
fprintf(fp, data);
fclose(fp);
return 0;
}
//读文件
//参数1:文件路径
//参数2:保存文件中数字的变量地址
//返回值:成功返回0,失败返回错误码
int read_file(char* path, float* data)
{
FILE* fp = fopen(path, "r");
if (fp == NULL)
{
perror(path);
return errno;
}
fscanf(fp, "%f", data);
fclose(fp);
return 0;
}
float read_temp(void)
{
float raw = 0;
float offset = 0;
float scale = 0;
read_file("/sys/bus/iio/devices/iio:device0/in_temp_raw", &raw);
read_file("/sys/bus/iio/devices/iio:device0/in_temp_offset", &offset);
read_file("/sys/bus/iio/devices/iio:device0/in_temp_scale", &scale);
return (raw + offset) * scale /1000;
}
float read_humi(void)
{
float raw = 0;
float offset = 0;
float scale = 0;
read_file("/sys/bus/iio/devices/iio:device0/in_humidityrelative_raw", &raw);
read_file("/sys/bus/iio/devices/iio:device0/in_humidityrelative_offset", &offset);
read_file("/sys/bus/iio/devices/iio:device0/in_humidityrelative_scale", &scale);
return (raw + offset) * scale /1000;
}
void on_message(struct mosquitto * client, void * data, const struct mosquitto_message * msg)
{
printf("从%s主题收到消息: %*s\n", msg->topic, msg->payloadlen, msg->payload);
if (strncmp(msg->topic, "test002", strlen("test002")) == 0)
{
//处理开关灯命令
if (strncmp(msg->payload, "on", 2) == 0)
{
//开灯
write_file("/sys/class/leds/led1/brightness", "1");
printf("开灯\n");
}
if (strncmp(msg->payload, "off", 3) == 0)
{
//关灯
write_file("/sys/class/leds/led1/brightness", "0");
printf("关灯\n");
}
}
if (strncmp(msg->topic, "test003", strlen("test003")) == 0)
{
//处理开关灯命令
if (strncmp(msg->payload, "on", 2) == 0)
{
//开风扇
write_file("/sys/class/hwmon/hwmon1/pwm1", "255");
}
if (strncmp(msg->payload, "off", 3) == 0)
{
//关风扇
write_file("/sys/class/hwmon/hwmon1/pwm1", "0");
}
}
}
int main()
{
//初始化mosquitto函数库
mosquitto_lib_init();
//创建MQTT客户端
struct mosquitto* client = mosquitto_new(uid, true, NULL);
if (client == NULL)
{
perror("mosquitto_new");
mosquitto_lib_cleanup();
return 1;
}
//连接MQTT服务器
int error = mosquitto_connect(client, "bemfa.com", 9501, 60);
if (error != MOSQ_ERR_SUCCESS)
{
printf("MQTT服务器连接失败: %s\n", mosquitto_strerror(error));
mosquitto_destroy(client);
mosquitto_lib_cleanup();
return 1;
}
printf("MQTT服务器连接成功\n");
mosquitto_message_callback_set(client, on_message);
//订阅主题
error = mosquitto_subscribe(client, NULL, "test002", 0);
if (error != MOSQ_ERR_SUCCESS)
{
printf("主题订阅失败: %s\n", mosquitto_strerror(error));
}
error = mosquitto_subscribe(client, NULL, "test003", 0);
if (error != MOSQ_ERR_SUCCESS)
{
printf("主题订阅失败: %s\n", mosquitto_strerror(error));
}
error = mosquitto_subscribe(client, NULL, "test004", 0);
if (error != MOSQ_ERR_SUCCESS)
{
printf("主题订阅失败: %s\n", mosquitto_strerror(error));
}
printf("主题订阅成功\n");
//启动任务接收消息
mosquitto_loop_start(client);
//主循环,读取传感器信息,上报到云平台
for (;;)
{
char msg[20];
sprintf(msg, "#%.1f#%.1f####", read_temp(), read_humi());
//发送消息
mosquitto_publish(client, NULL, "test004/set", strlen(msg), msg, 0, false);
printf("消息发送成功: %s\n", msg);
sleep(60);
}
//销毁客户端,释放资源
mosquitto_destroy(client);
//清理函数库占用的资源
mosquitto_lib_cleanup();
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。