MQTT

  1. MQTT
  2. Mosquitto 설치 (Ubuntu)
  3. Paho MQTT Python Client
  4. MQTT Client, ESP8266 (nodeMCU)
    1. Mbed
  5. References

MQTT

MQTT(Message Queuing Telemetry Transport)는 ISO 표준(ISO/IEC PRF 20922) 발행/구독 기반의 메시징 프로토콜이며, TCP/IP 프로토콜 위에서 동작. 발행/구독 메시징 패턴은 메시지브로커가 필요 wiki

Mosquitto 설치 (Ubuntu)

  • Repositories Update and Install

    $ sudo apt-get install python-software-properies
    $ sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
    $ sudo apt-get update
    $ sudo apt-cache search mosquitto
    $ sudo apt-get install mosquitto
    $ netstat -nap | grep mosquitto
    $ mosquitto
    
  • Test

    $ sudo apt-get install mosquitto-clients # install client for test
    $ mosquitto_sub -d -t hello/world # subscribe
    $ mosquitto_pub -d -t hello/world -m "Hi, there?" # publish
    $ mosquitto_sub -u popup-iot -P 1234567890 -d -t hello/world # with authentication
    $ mosquitto_pub -u popup-iot -P 1234567890 -d -t hello/world -m "Hi, there?" # with authentication
    
  • Mosquitto Username and Password Authentication

    $ mosquitto_passwd -c /etc/mosquitto/password popup-iot #create initial file
    $ mosquitto_passwd -b /etc/mosquitto/password john secret # add new user and password
    $ mosquitto_passwd -D /etc/mosquitto/password john # delete user
    $ vi /etc/mosquitto/mosquitto.conf
    allow_anonymous false
    password_file /etc/mosquitto/password
    
    $ pkill mosquitto
    $ mosquitto &
    $ tail -f /var/log/mosquitto/mosquitto.log # log
    

Paho MQTT Python Client

  • Paho MQTT Install

    $ pip install paho-mqtt
    
  • Example Python scripts

    import paho.mqtt.client as mqtt
    broker_address="127.0.0.1" 
    client = mqtt.Client()
    client.connect(broker_address)
    client.publish("hello/world","Hi, there?")
    #or 
    client.subscribe("hello/world")
    

MQTT Client, ESP8266 (nodeMCU)

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid        = "";
const char* password    = "";
const char* mqtt_server = "";

WiFiClient wifi;
PubSubClient client(wifi);

void setup_wifi() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
}

void loop() {
  while (!client.connected()) {
    if (!client.connect("esp8266")) {
        delay(1000); Serial.print(".");
        continue;
    }
    Serial.println("");
  }
  client.loop();
  client.publish("hello/world", "Hi, there?");
  delay(1000);
}

Mbed

https://os.mbed.com/teams/ST/code/pelion-example-disco-iot01/

$ mbed import http://os.mbed.com/teams/ST/code/pelion-example-disco-iot01/
$ cd pelion-example-disco-iot01
$ mbed config -G CLOUD_SDK_API_KEY <PELION_DM_API_KEY>
$ mbed dm init -d "https://api.us-east-1.mbedcloud.com" --model-name "DISCO_L475VG_IOT01A" -q --force
$ mbed add http://os.mbed.com/teams/sandbox/code/mbed-http/
$ mbed compile -t GCC_ARM -m DISCO_L475VG_IOT01A

References

  1. MQTT, MQTT-Client 설치하기
  2. Mosquitto Username and Password Authentication -Configuration and Testing\
  3. Beginners Guide To The Paho MQTT Python Client

참고사이트

  1. 펌웨어 다운로드 (Getting the firmware)

MicroPython download page

wind+x 키를 눌러서 포트(COM & LPT)의 COM포트번호를 확인

  1. ESP8266 보드에 설치하기
> pip install esptool
> esptool.py --port COMxx erase_flash
> esptool.py --port COMxx write_flash 0 xxx.bin
esptool.py v2.6
Serial port COM13
Connecting....
...

Leaving...
Hard resetting via RTS pin...
import esp
esp.check_fw()
from machine import Pin
from time import sleep

led = Pin(2, Pin.OUT)

while True:
  led.value(not led.value())
  sleep(0.5)

© 2018. All rights reserved.

Powered by Hydejack v8.4.0