今天非常榮幸向各位小伙伴詳細展示一個由共創社成員完成的MQTT遠程溫濕度監控系統項目。該項目借助ELF 1開發板作為核心技術支撐,成功實現了對各類環境空間中溫濕度數據的實時、遠程、穩定監測。該系統不僅集成了先進的數據采集模塊,用于精確感知現場環境變化,同時利用MQTT協議的輕量級特性,確保了數據在復雜網絡環境下的可靠傳輸。在此接下來,就為各位小伙伴詳盡展示這一項目的相關細節。
1、Linux開發板開發環境搭建
(1)開發板動態分配ip地址
(開發板與家用路由器連接,路由器支持DHCP自動IP地址分配)
root@ELF1:~# udhcpc -i eth0
(2)將nfs服務器掛載到開發板的/mnt目錄
(其中“192.168.1.10”是Ubuntu的ens36的ip)
root@ELF1:~# mount -t nfs -o nolock,vers=3 192.168.1.10:/home/book/nfs_rootfs /mnt
可以看到開發板的/mnt目錄已經有了文件。
2、Ubuntu編譯環境搭建:
(1)將paho mqtt的官方庫克隆到Ubuntu的“~/nfs_rootfs”路徑
book@100ask:~/nfs_rootfs$ git clone https://github.com/eclipse/paho.mqtt.c.git
(2)修改“~/nfs_rootfs/paho_mqtt/paho.mqtt.c”路徑下的Makefile文件
修改prefix所代表的工具鏈路徑:
修改編譯器:
(3)編譯后得到鏈接庫
book@100ask:~/nfs_rootfs/paho_mqtt/paho.mqtt.c$ make
arm-gcc編譯生成的.so庫文件,保存在paho.mqtt.c/build/output里面。
將.so庫文件安裝到本地PC:
book@100ask:~/nfs_rootfs/paho_mqtt/paho.mqtt.c$ sudo make install
(4)將.so庫文件安裝到開發板的“/lib”路徑,開發板才能運行paho mqtt編譯后的可執行文件
root@ELF1:~# install /mnt/paho_mqtt/paho.mqtt.c/build/output/libpaho-mqtt3* /lib
以上開發環境搭建完成!
3、工程文件的建立
(1)將“~/nfs_rootfs/paho_mqtt/paho.mqtt.c”
路徑中的src文件夾拷貝到“~/nfs_rootfs/mqtt_iot”路徑下
book@100ask:~$ cp -r ~/nfs_rootfs/paho_mqtt/paho.mqtt.c ~/nfs_rootfs/mqtt_iot
(2)在“~/nfs_rootfs/mqtt_iot”路徑下添加文件:
4、阿里云服務器設置:
(1)服務器的注冊及產品、設備設置可以參照ElfBoard官方文檔:《01-1 ELF1、ELF1S開發板_軟件教程_V1》第5章-5.4小節。
(2)工程文件中所需要的服務器參數查找
查看服務器地址
查看DeviceName、DeviceSecret
查看MQTT連接參數
5.工程文件的編譯執行
(1)編譯工程文件,得到可在Linux開發板上執行的Main二進制文件
book@100ask:~/nfs_rootfs/mqtt_iot$ arm-buildroot-linux-gnueabihf-gcc main.c mqtt_iot.c -o main -lpaho-mqtt3c -lpthread
MQTT的異步通信收發,依賴的庫是libpaho-mqtt3a,MQTT的同步通信收發,依賴的庫就是libpaho-mqtt3c。此外工程編譯的時候需要鏈接線程的庫pthread,所以編譯的時候要加上-lpthread。
(2)開發板運行程序
root@ELF1:/mnt/mqtt_iot# ./main
(3)實驗結果
Linux開發板將采集到的溫度、濕度數據每5s上傳一次阿里服務器,串口窗口顯示數據發送成功字符。此外通過阿里服務器日志服務可以看到濕度、溫度數據。
通過阿里服務器調試窗口給開發板發送LED1、LED2控制指令。
6、下面將貼出工程文件的代碼,并介紹其思路
(1)main.c文件
//main.c //定義線程句柄 pthread_t discon_t; pthread_t thread_AT20Read_t; pthread_t thread_ledctrl_t; static int isConnected = 0;//表明客戶端和服務器是斷開還是連接狀態(1-連接狀態,-1斷開狀態) static void *thread_AT20Read(void *paramater) { int fd = -1; unsigned int databuf[2]; int c1,t1; float hum,temp; int ret = 0; msgbuf pubMsg = {2, 0}; while(fd < 0){ fd = open(AHT20_DEV, O_RDWR); if(fd < 0){ printf("can't open file %s\r\n", AHT20_DEV); sleep(1); }else{ printf("open file %s successfully\r\n", AHT20_DEV); } } while(1){ ret = read(fd, databuf, sizeof(databuf)); if(ret == 0){ c1 = databuf[0]*1000/1024/1024; t1 = databuf[1] *200*10/1024/1024-500; hum = (float)c1/10.0; temp = (float)t1/10.0; //printf("hum = %0.2f temp = %0.2f \r\n",hum,temp); pubMsg.mtext[0] = (unsigned int)(hum*100); pubMsg.mtext[1] = (unsigned int)(temp*100); int ret1 = msgsnd(pubmsg_d, &pubMsg.mtype, sizeof(pubMsg.mtext), IPC_NOWAIT); // 非阻塞發送 if(ret1 != 0) { printf("Failed to send message.\r\n"); } } sleep(5); } } static void *thread_ledctrl(void *paramater) { int on=1; int led; int fd; msgbuf subMsg = {1, 0}; fd = open(LED_BRIGHTNESS, O_WRONLY); if(fd < 0) { perror("open device leds"); exit(1); } system(LED1_OFF); system(LED2_OFF); while(1) { int res = msgrcv(submsg_d, &subMsg, sizeof(subMsg.mtext), 0, 0);//阻塞 if(res < 0) continue; else{ if((subMsg.mtext[0] & 0x01)== 1){ system(LED1_ON); }else{ system(LED1_OFF); } if((subMsg.mtext[0] & 0x02)== 0x02){ system(LED2_ON); }else{ system(LED2_OFF); } } } } //斷開和mqtt服務器連接的線程入口函數 static void *mqtt_disconnect_t(void* argv) { int retval; while(1) { char ch; ch = getchar(); if(ch=='Q' || ch=='q') { printf("Try to exit mqtt task\n"); if(mqtt_disconnect() == EXIT_SUCCESS) break; } } isConnected = -1; pthread_exit(&retval); // 退出線程 return NULL; } int main(void) { //初始化mqtt成功建立客戶端和服務器的連接后,將主動斷開服務器的任務放到一個線程里面去 //成功建立客戶端和服務器的連接且訂閱主題后才創建斷開連接的線程 if(mqtt_iot() == 0) { isConnected = 1; pthread_create(&discon_t, 0, mqtt_disconnect_t, NULL); } //AT20 read thread int ret = pthread_create(&thread_AT20Read_t, NULL, thread_AT20Read, NULL); if(ret != 0) { printf("Failed to create AT20Read thread.\n"); return -1; } //led control thread ret = pthread_create(&thread_ledctrl_t, NULL, thread_ledctrl, NULL); if(ret != 0) { printf("Failed to create ledctrl thread.\n"); return -1; } while(1) { //printf("isConnected state:%d\n",isConnected); sleep(5); } return 0; }
(2)mqtt_iot.c文件
//mqtt_iot.c volatile MQTTClient_deliveryToken deliveredtoken; pthread_t threads[2]; sem_t discon_sem;//信號量 int pubmsg_d = -1; int submsg_d = -1; msgbuf subMsg = {1, 0}; msgbuf pubMsg = {2, 0}; pthread_t thread_mqtt_publish_t; MQTTClient client; //定義一個MQTT客戶端client MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; //傳遞給MQTTClient_setCallbacks的回調函數,消息發送成功后,調用此回調函數 void delivered(void *context, MQTTClient_deliveryToken dt) { printf("Message with token value %d delivery confirmed\n", dt); deliveredtoken = dt; } //傳遞給MQTT-Client_setCallbacks的回調函數 消息到達后,調用此回調函數 int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message) { printf("---------------------------------------------------------------\n"); printf("Message arrived\n"); printf(" topic: %s\n", topicName); printf(" message: %.*s\n", message->payloadlen, (char*)message->payload); printf("---------------------------------------------------------------\n"); subMsg.mtext[0] = 0; unsigned short len = message->payloadlen; char *buf = (char*)message->payload; for(unsigned short i=0; i='0') subMsg.mtext[0] = subMsg.mtext[0]*10 + buf[i] - '0'; } int ret = msgsnd(submsg_d, &subMsg.mtype, sizeof(subMsg.mtext), IPC_NOWAIT); // 非阻塞發送 if(ret != 0) { printf("Failed to send message.\r\n"); } MQTTClient_freeMessage(&message); // 釋放消息 MQTTClient_free(topicName); // 釋放主題名 return 1; } //傳遞給MQTTClient_setCallbacks的回調函數 連接異常斷開后調用此回調函數 void connlost(void *context, char *cause) { printf("\nConnection lost\n"); printf(" cause: %s\n", cause); } //實現MQTT的發布 void *mqtt_publish(void *argv) { MQTTClient_message pubmsg = MQTTClient_message_initializer; MQTTClient_deliveryToken token; char data[9]; int rc; pubmsg.qos = QOS; pubmsg.retained = 0; while(1) { //接收消息(消息隊列的ID,存放消息的指針,指定接收消息的大小,0-讀取消息隊列中第一個數據,阻塞) int res = msgrcv(pubmsg_d, &pubMsg, sizeof(pubMsg.mtext), 0, 0); if(res < 0) continue; { //printf("Publish_hum: %d\n", pubMsg.mtext[0]); //printf("Publish_temp: %d\n", pubMsg.mtext[1]); sprintf(data, "%d,%d", pubMsg.mtext[0],pubMsg.mtext[1]); pubmsg.payload = data; pubmsg.payloadlen = sizeof(data); if((rc = MQTTClient_publishMessage(client, PUB_TOPIC, &pubmsg, &token)) != MQTTCLIENT_SUCCESS) { printf("Failed to publish message, return code %d\n", rc); break; } rc = MQTTClient_waitForCompletion(client, token, TIMEOUT); printf("Message with delivery token %d delivered\n", token); } } if((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS) //斷開和服務器的連接 { printf("Failed to disconnect, return code %d\n", rc); } pthread_exit(&threads[PubThread]); return NULL; } //封裝主動斷開連接服務器的函數 int mqtt_disconnect(void) { int rc = EXIT_SUCCESS; //兩個參數:MQTT客戶端和斷開連接超時時間 if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS) //斷開和服務器的連接 { printf("Failed to disconnect, return code %d\n", rc); rc = EXIT_FAILURE; } else { printf("MQTT disconnect success\n"); MQTTClient_destroy(&client); } return rc; } // mqtt建立客戶端、連接服務器、訂閱主題的封裝入口函數 int mqtt_iot(void) { int rc = EXIT_SUCCESS; //創建客戶端 if ((rc = MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS) { printf("Failed to create client, return code %d\n", rc); goto exit; } //設置回調函數(連接丟失處理回調函數,處理訂閱消息的回調函數,成功發布消息后的回調函數) if((rc = MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered)) != MQTTCLIENT_SUCCESS) { printf("Failed to set callbacks, return code %d\n", rc); goto destroy_exit; } conn_opts.username = USERNAME; conn_opts.password = PASSWORD; conn_opts.keepAliveInterval = 60;//保活周期,客戶端向服務器發送心跳包的周期,單位秒 conn_opts.cleansession = 1; //連接服務器 if((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) { printf("Failed to connect, return code %d\n", rc); goto destroy_exit; } //訂閱主題(傳入客戶端句柄、訂閱的主題以及消息質量) if ((rc = MQTTClient_subscribe(client, SUB_TOPIC, QOS)) != MQTTCLIENT_SUCCESS) { printf("Failed to subscribe, return code %d\n", rc); goto destroy_exit; } //初始化信號量 if(sem_init(&discon_sem, 1, 0) != 0) { printf("Failed to init semaphore\n"); return -1; } //創建隊列 pubmsg_d = msgget(0x1234, IPC_CREAT); submsg_d = msgget(0x5678, IPC_CREAT); if(pubmsg_d == -1 || submsg_d==-1) //返回錯誤碼-1 { printf("Failed to create a mqtt message, pubid:%d, subid:%d\n", pubmsg_d, submsg_d); return -1; } else { printf("Publish message id: %d\n", pubmsg_d); printf("Subscribe message id: %d\n", submsg_d); } int ret = pthread_create(&thread_mqtt_publish_t, NULL, mqtt_publish, NULL); if(ret != 0) { printf("Failed to create mqtt_publish thread.\n"); return -1; } printf("MQTT connect success, press 'Q' or 'q' to disconnect mqtt server\n"); return 0; destroy_exit: MQTTClient_destroy(&client); //釋放客戶端的資源, 參數-同步客戶端的句柄 return -1; exit: return -1; }
至此,就完成了關于ELF 1開發板研發的MQTT遠程溫濕度監測系統介紹。希望這套實踐案例能夠成為各位小伙伴的寶貴參考,啟迪創新思維,推進各位嵌入式愛好者在學習的道路上不斷前進。
-
嵌入式
+關注
關注
5046文章
18823瀏覽量
298714 -
監測系統
+關注
關注
8文章
2615瀏覽量
81092 -
開發板
+關注
關注
25文章
4771瀏覽量
96199
發布評論請先 登錄
相關推薦
評論