Search

Node.js, MQTT setup

run.sh

1. LEO_node.js server

#!/bin/bash curl -fsSL https://deb.nodesource.com/setup_12.x | sudo -E bash - sudo apt-get install -y nodejs npm install async request compression express
Bash

2. LEO MQTT setup

Step 1. Install Mosquitto Broker

sudo apt install -y mosquitto mosquitto-clients
Bash

Step 2. Subscribe/Publish TEST

# 듣는애(* -t : topic name) mosquitto_sub -t "${topic-name}" # 말하는애(* -m : message) mosquitto_pub -m "message test0101" -t "${topic-name}"
Bash

Step3. 101

# 비번생성(* -c : 비번 create) mosquitto_passwd -c /etc/mosquitto/passwd ${id} # 계정생성 sudo mosquitto_passwd -c /etc/mosquitto/passwd ${id} Password: alooh1234 # 계정파일 생성 sudo vi /etc/mosquitto/conf.d/default.conf ./ allow_anonymous false # 아무나 접근못하게 막는거 password_file /etc/mosquitto/passwd # mqtt serviece restart sudo systemctl restart mosquitto
Bash

Step4. TEST2

# 보내는애 mosquitto_pub -t "${topic-name}" -u "${id}" -P "${password}" -m "alooh~~" # 받는애 mosquitto_sub -t "${topic-name}" -u "${id}" -P "${password}" alooh~~
Bash

3. LEO-AGX setup

From. LEO
git clone http://bitbucket.cresprit.co.kr/scm/ed/mosquittodocker.git cd mosquittodocker vi docker-compose.yml # ADD line # volume: # - ./mosquitto.conf:/mosquitto/conf/mosquitto.conf
Bash
(From. Jetson AGX)
sudo apt install mosquitto-clients
Bash

4. LEO-AGX link web/display test

Step 1. setup

(From. LEO)
git clone http://bitbucket.cresprit.co.kr/scm/ed/mosquittodocker.git cd mosquittodocker $ vi docker-compose.yml # 아래 볼륨 추가 volume: - $PWD/mosquitto.conf:/mosquitto/config/mosquitto.conf $ docker-compose up -d # mqtt 1883(default) 포트로 수신중
Bash
(From. Jetson AGX)
$ sudo apt install mosquitto-clients
Bash

Step 2. Simple test

(From. LEO)
$ git clone http://bitbucket.cresprit.co.kr/scm/ed/safetyfence_mobile_event.git $ cd ./safetyfence_mobile_event $ git checkout -t origin/B1-64-leo-board $ git branch # host ip를 LEO 내부주소로 수정 $ vi ./display/indenx.html var host = "192.168.10.29" # app.js 에 모듈추가 $ vi app.js app.use('/display', express.static('display')); $ node app.js Running node.js Server port: 6002
Bash
(From. AGX)
# LEO 내부IP = 192.168.10.29 $ mosquitto_sub -t "test_event" -h 192.168.10.29 -m "${message}" message : { "cameraId":"CAM0001", "eventId":"CAM0001", "time":20211005144700 }
Bash
(Python publisher)
python3 -m pip install --upgrade pip python3 -m pip install paho-mqtt
Bash
import paho.mqtt.client as mqtt import json import argparse, sys def set_argument(): parser = argparse.ArgumentParser(prog=f"python3 {sys.argv[0]}") parser.add_argument("--port", "-p", default='1883', type=int, help='host_port') parser.add_argument("--host", default='192.168.10.29', type=str, help='host_address') parser.add_argument("--topic", "-t", default='test', type=str, help='topic_name') #parser.add_argument("--message", "-m", nargs='*', help='message_field ['cameraId', 'eventId', 'time']') args = parser.parse_args() return args ARGS = set_argument() def publishEvent(event): #Client(client_id="", clean_session=True, userdata=None, protocol=MQTTv311, transport="tcp") client_id = "subscripborNo1" # connect(host="", port=1883, keepalive=60, bind_address="") broker_address = ARGS.host topic = ARGS.topic client = mqtt.Client(client_id) try: print("try") client.connect(host=broker_address, port=ARGS.port) client.publish(topic=topic, payload=event) print("topic " + topic + " published") except Exception as e: print("error" + str(e)) fields = { "cameraId":"CAM0001", "eventId":"CAM0001", "time":20211005144700 } if __name__ == "__main__": testEvent = json.dumps(fields) publishEvent(testEvent)
Python
같은 네트워크에서 (cresprit10),
해당 형식의 message 를 수신하면 이벤트 화면으로 바뀐다.