2017年5月24日 星期三

NodeMcu (1) - Mqtt

By using Lua, it is surprisingly simple to program the NodeMcu (i.e. ESP8266) to connect to a MQTT broker. Here I write a simple lua on NodeMcu, it will
1. connect to a home wifi router
2. connect to a mqtt broker server
3. subscribe to a topic
4. when a message is received, parse the message then turn on/off a LED.

Wifi router

  About the wifi router,  I just use WPA2, an 2.4GHz wifi router.

Mqtt broker

  For the Mqtt broker, you may use "test.mosquitto.org", of course you can setup your own(see my previous post).

Hardware Connection

  The NodeMCU that I use is just a popular one (using CH385 as the USB-to-Uart bridge), make sure you have install the USB-UART driver. Also install the popular nodcmcu development tools ESPlorer.
  I connect a LED to to GPIO0 (marked as D0 on PCB), a 100ohm resistor is used to limit the current draw from GPIO pin. According to the ESP8266 datasheet, the maximum GPIO output current is only 12mA, if no resistor is used, just connect a LED to GPIO and ground directly, I'm afraid it may damage the GPIO, I haven't try, I also don't suggest your try...
   Anyway, see the connection picture below, quite simple.


















Source code

  The code is quite strange forward, it will do the following
  1. Setup the wifi as station mode and connect the the router
  2. Repeat to get IP until a valid IP is obtained.
  3. Connect to the Mqtt broker (test.mosquitto.org)
  4. Once the Mqtt broker is connected, subscribed to the topic "wing"
  5. Every time a message is received, then parse the message. If the first character of the message is "0", then set the GPIO0 to low, otherwise set to high. (so as to turn on/off a LED)

wifi.setmode(wifi.STATION)
wifi.sta.config("YOUR_SSID","YOUR_PASSWORD")
wifi.sta.connect()

tmr.alarm(0, 1000, 1, function()
if wifi.sta.getip() == nil then
    print("Connecting")
else 
    print('ip:',wifi.sta.getip())
    tmr.stop(0)
end
end)

print ("start mqtt")
m = mqtt.Client("wingtest",120,"","",1)
m:connect("test.mosquitto.org", "1883", 0)

m:on("connect", function(con) print ("connected") 
    m:subscribe("wing",0)
end)

m:on("offline", function(con) print ("offline") end)

m:on("message", function(conn, topic, data)
    print (topic .. ":")
    print("message=>", data)
    print (string.byte(data,1))
--    print (string.byte(data,2))

    pin=0
    gpio.mode(pin, gpio.OUTPUT)

    if string.byte(data,1) == 48 then
        print ("LOW")
        gpio.write(pin, gpio.LOW)
    else    
        print ("HIGH")
        gpio.write(pin, gpio.HIGH)
    end
end)
 

2017年5月2日 星期二

Node-Red (2) - MQTT testing

What I really want to do is connecting different MCU to the node-red, before I connect a real hardware, I have to test mqtt on node-red first. Without a real hardware, I can just publish a message from the command prompt (using mosquitto_pub) , and see the result at the node-red debug panel.
In this experiment, I use the public Mosquitto MQTT test server, but you can also follow my previous post to setup your own MQTT server.

Run node-red !

Run node-red , then you can see the following UI in your browser


Drag "mqtt" node from the Left-Hand-Side (under input category) and drop to the Flow 1 area.

Similarly, drag and drop the "debug" node to the Flow 1 area.


While you moving your mouse inside the Flow 1 area, it is actually in the "wire mode", you can just move the  "cross" (i.e. mouse cursor) to connect a wire from mqtt node to the debug node. (you click on the right square of the mqtt node and drag a line to the left square of the debug node.)

 Then we have to configure the nodes, in this simple flow, only mqtt node is need to be configure. Just double click at mqtt node, and you will see

Then click the pencil at the end of "server",  and you will see the sever input diaglog

You only need to need to enter the server name  "test.mosquitto.org", then click Add on the top. It will then go back to the previous dialog. Now you can enter the topic "wing" (you can change to any other string). Finally press Done button on the top.

Now the Flow is finish creating, we can just press "Deploy" (on the top right) to start the flow.
You can see the mqtt node show status "connected"

You can also check the command prompt (where you run the node-red), it will show some debug information message, such as connected to test.mosquitto.org


To test the Flow, we can publish a message to the topic "wing" at the test.mosquitto.org server.  You can open a command prompt at Windows, and run the following command



-h is the server name
-t is the topic
-m is the message you are going to publish

Then at the node-red UI, you should see a received message at the debug area (right hand side).

Node-Red (1) - Installation

We heard the term "IoT" for years, when people talking about IoT, MQTT and Node-red are usually mentioned. It seem Node-red is a user-friendly tools, so I start to evaluate. The first step is, of course, install the node-red.

 

Installation of node-red for windows

1.  Download node.js windows 64bit version from here . The latest version that I used is v6.10.2 LTS. The Windows that I used is Win 7 64-bit, CPU is i3.

2. Double click to run the file node-v6.10.2-x64.msi , just press next for each screen to finish the installation.

3. After node.js is install successfully, there will be a new item "Node.js" in the start menu. Click the "Node.js command prompt" inside the "Node.js", and you will see a cmd prompt like this

4. In the "Node.js command prompt", enter the following command, then it will install node-red.



5. After the installation finish, type "node-red" to run.

6. Then in browser, enter the link "127.0.0.1:1880" and you can see the node-red UI.



Install MQTT for Linux - using docker

Docker is very popular nowaday, so instead of installing MQTT broker directly on Linux, I try to find if there are any MQTT docker image. It's great that there is an official Mosquitto implementation of MQTT broker by Eclipse. Here are the procedure to install and test these Mosquitto-MQTT broker for Linux.

Linux server

    I'm running and Ubuntu 16.04.2 - 64 bit virtual machine on Digital Ocean, but I think the setup procedure should be the same on a real Linux machine.  I originally run a 32bit version ubuntu VM, but docker only support 64bit version, so I destroy the previous one and create a new 64bit version.

Install Docker

  The first step is , of course, to install the Docker. I use "curl" to install, so install curl first,



Then install docker



You also need to add your user to docker group



The following command will download the eclipse-mosquitto image then create a container (that also mean running the mosquitto services)
web sites for the eclipse-mosquitto https://hub.docker.com/_/eclipse-mosquitto/


see the result