ÉèÖÃLinuxϵͳÒÔÖ§³ÖÖÇÄÜÖÆÔìºÍ¹¤ÒµÎïÁªÍø¿ª·¢
ÉèÖÃlinuxϵͳÒÔÖ§³ÖÖÇÄÜÖÆÔìºÍ¹¤ÒµÎïÁªÍø¿ª·¢
ÖÇÄÜÖÆÔìºÍ¹¤ÒµÎïÁªÍøÊǵ±½ñ¹¤ÒµÁìÓòÖеÄÖ÷ÒªÉú³¤Æ«Ïò£¬ÔÚÕâЩÁìÓòÖУ¬Linuxϵͳ±»ÆÕ±éÓ¦ÓÃÓÚÖÖÖÖ¹¤Òµ×°±¸¡¢»úеÈË¡¢´«¸ÐÆ÷µÈ×°±¸ÉÏ¡£ÎªÁËÄܹ»³ä·ÖÑéÕ¹LinuxϵͳµÄÓÅÊƲ¢Ö§³ÖÖÇÄÜÖÆÔìºÍ¹¤ÒµÎïÁªÍø¿ª·¢£¬ÎÒÃÇÐèÒª¾ÙÐÐһЩÉèÖúÍ×°ÖÃÊÂÇé¡£
Ò»¡¢×°ÖÃLinuxϵͳ
Òª×îÏÈÉèÖÃlinuxϵͳÒÔÖ§³ÖÖÇÄÜÖÆÔìºÍ¹¤ÒµÎïÁªÍø¿ª·¢£¬Ê×ÏÈÐèҪװÖúÏÊʵÄLinux¿¯Ðа档³£¼ûµÄLinux¿¯ÐаæÈçUbuntu¡¢CentOSµÈ¶¼¿ÉÒÔÑ¡Ôñ£¬ÎÒÃÇ¿ÉÒÔƾ֤×Ô¼ºµÄÐèÇóºÍÊìϤˮƽѡÔñºÏÊʵĿ¯Ðа档ÔÚ×°ÖÃÀú³ÌÖУ¬ÎÒÃÇÐèҪѡÔñ×°ÖÃЧÀÍÆ÷°æ±¾£¬ÒÔ±ã¾ÙÐкóÐøµÄÉèÖá£
¶þ¡¢×°Öûù´¡¿âºÍ¿ª·¢¹¤¾ß
ÖÇÄÜÖÆÔìºÍ¹¤ÒµÎïÁªÍø¿ª·¢Í¨³£ÐèҪʹÓõ½Ò»Ð©Ìض¨µÄ¿âºÍ¿ª·¢¹¤¾ß¡£ÔÚ×îÏÈÕæÕýµÄ¿ª·¢Ö®Ç°£¬ÎÒÃÇÐèҪװÖÃÕâЩ»ù´¡¿âºÍ¿ª·¢¹¤¾ß¡£ÒÔUbuntuϵͳΪÀý£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁî×°ÖÃһЩ³£ÓõĿâºÍ¿ª·¢¹¤¾ß£º
sudo apt-get update sudo apt-get install build-essential sudo apt-get install git sudo apt-get install cmake sudo apt-get install libssl-dev sudo apt-get install libboost-all-dev
µÇ¼ºó¸´ÖÆ
ÕâЩÏÂÁî»á×°ÖÃһЩÐëÒªµÄ¿âºÍ¿ª·¢¹¤¾ß£¬ÓÃÓÚºóÐøµÄ¿ª·¢ÊÂÇé¡£
Èý¡¢ÉèÖÃÍøÂçͨѶ
¹¤ÒµÎïÁªÍø¿ª·¢ÐèҪװ±¸Ö®¼äÄܹ»¾ÙÐÐÍøÂçͨѶ£¬ÎÒÃÇÐèÒªÉèÖÃÍøÂçÉèÖÃÒÔÈ·±£×°±¸Äܹ»Ï໥ͨѶ¡£ÎÒÃÇ¿ÉÒÔʹÓÃÍøÂçÉèÖù¤¾ßÈçifconfig»òÕßNetworkManager¾ÙÐÐÍøÂçÉèÖá£
ÒÔÉèÖþ²Ì¬IPµØµãΪÀý£¬ÎÒÃÇ¿ÉÒÔ±à¼ÍøÂçÉèÖÃÎļþ£¬ºÃ±È/etc/network/interfaces£¬Ìí¼ÓÒÔÏÂÉèÖãº
auto eth0 iface eth0 inet static address 192.168.0.100 netmask 255.255.255.0 gateway 192.168.0.1
µÇ¼ºó¸´ÖÆ
ÉèÖÃÍê³Éºó£¬ÉúÑIJ¢Ó¦ÓÃÍøÂçÉèÖá£ÕâÑù£¬ÎÒÃǾͿÉÒÔͨ¹ý×°±¸µÄIPµØµã¾ÙÐÐͨѶÁË¡£
ËÄ¡¢×°ÖÃÎïÁªÍøÐÒéÕ»
ÖÇÄÜÖÆÔìºÍ¹¤ÒµÎïÁªÍø¿ª·¢¾³£ÐèҪʹÓõ½ÎïÁªÍøÐÒéÕ»£¬ºÃ±ÈMQTT¡¢CoAPµÈ¡£ÎÒÃÇ¿ÉÒÔÑ¡Ôñ×°ÖÃÏìÓ¦µÄÎïÁªÍøÐÒéÕ»À´Ö§³Ö¿ª·¢¡£
ÒÔ×°ÖÃMQTTÐÒéջΪÀý£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº
git clone https://github.com/eclipse/mosquitto.git cd mosquitto make sudo make install
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬ÎÒÃǾÍÀÖ³É×°ÖÃÁËMQTTÐÒéÕ»£¬¿ÉÒÔÔÚLinuxϵͳÖÐʹÓÃMQTT¾ÙÐÐÎïÁªÍøͨѶÁË¡£
Îå¡¢ÉèÖÃÊý¾Ý¿â
¹¤ÒµÎïÁªÍø¿ª·¢Öо³£ÐèҪʹÓÃÊý¾Ý¿âÀ´´æ´¢ºÍ´¦ÀíÊý¾Ý£¬ÎÒÃÇ¿ÉÒÔÑ¡Ôñ×°ÖÃSQLite¡¢MySQLµÈÊý¾Ý¿â¡£ÒÔ×°ÖÃSQLiteΪÀý£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº
sudo apt-get install sqlite3
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃSQLiteÏÂÁîÐй¤¾ß»òÕßÔÚ´úÂëÖÐʹÓÃSQLite APIÀ´¾ÙÐÐÊý¾Ý¿â²Ù×÷¡£
Áù¡¢Ê¾Àý´úÂë
ÏÂÃæÊÇÒ»¸öʹÓÃCÓïÑÔ±àдµÄ¼òÆÓʾÀý´úÂ룬ÓÃÓÚ¶©ÔÄMQTTÐÂÎŲ¢½«ÐÂÎÅ´æ´¢µ½SQLiteÊý¾Ý¿âÖУº
#include <stdio.h> #include <stdlib.h> #include <sqlite3.h> #include <mosquitto.h> void message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message) { sqlite3 *db; char *errmsg; int rc; rc = sqlite3_open("data.db", &db); if (rc != SQLITE_OK) { printf("Can't open database: %s ", sqlite3_errmsg(db)); return; } char *sql = "INSERT INTO messages (topic, payload) VALUES (?, ?)"; rc = sqlite3_exec(db, sql, NULL, NULL, &errmsg); if (rc != SQLITE_OK) { printf("SQL error: %s ", errmsg); sqlite3_free(errmsg); } sqlite3_close(db); } int main() { struct mosquitto *mosq = NULL; mosquitto_lib_init(); mosq = mosquitto_new(NULL, true, NULL); mosquitto_connect(mosq, "localhost", 1883, 60); mosquitto_message_callback_set(mosq, message_callback); mosquitto_subscribe(mosq, NULL, "topic", 0); mosquitto_loop_start(mosq); while (1) { // ¼á³Ö³ÌÐòÔËÐÐ } mosquitto_loop_stop(mosq, true); mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return 0; }
µÇ¼ºó¸´ÖÆ
Õâ¶Î´úÂëʹÓÃÁËmosquitto¿âÀ´¶©ÔÄMQTTÐÂÎÅ£¬²¢½«ÐÂÎÅ´æ´¢µ½SQLiteÊý¾Ý¿âÖС£ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃÇ¿ÉÒÔƾ֤×Ô¼ºµÄÐèÇó¾ÙÐÐÐ޸ĺÍÀ©Õ¹¡£
ͨ¹ý¶ÔLinuxϵͳ¾ÙÐÐÉèÖúÍ×°ÖÃÊÂÇ飬ÎÒÃÇ¿ÉÒÔ³ä·ÖÑéÕ¹LinuxϵͳµÄÓÅÊÆ£¬²¢Ö§³ÖÖÇÄÜÖÆÔìºÍ¹¤ÒµÎïÁªÍø¿ª·¢¡£Ï£ÍûÒÔÉϵÄÉèÖúÍʾÀý´úÂëÄܹ»¶Ô¿ª·¢ÕßÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇÉèÖÃLinuxϵͳÒÔÖ§³ÖÖÇÄÜÖÆÔìºÍ¹¤ÒµÎïÁªÍø¿ª·¢µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡