加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
bemfa.c 4.42 KB
一键复制 编辑 原始数据 按行查看 历史
刘煜 提交于 2024-05-08 09:36 . 第一次提交
#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;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化