The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.

Introduction to NodeMCU ESP32 & Micro Python Programming

Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by charles, 2023-01-03 02:54:25

Introduction to NodeMCU ESP32 & Micro Python Programming

Introduction to NodeMCU ESP32 & Micro Python Programming

Keywords: ESP32,micropython

Chapter 3 – NodeMCU ESP32 using HTTP Protocol 45 server_socket = None def get_connection(): """return a working WLAN(STA_IF) instance or None""" # First check if there already is any connection: if wlan_sta.isconnected(): return wlan_sta connected = False try: # ESP connecting to WiFi takes time, wait a bit and try again: time.sleep(3) if wlan_sta.isconnected(): return wlan_sta # Read known network profiles from file profiles = read_profiles() # Search WiFis in range wlan_sta.active(True) networks = wlan_sta.scan() AUTHMODE = {0: "open", 1: "WEP", 2: "WPA-PSK", 3: "WPA2-PSK", 4: "WPA/WPA2-PSK"} for ssid, bssid, channel, rssi, authmode, hidden in sorted(networks, key=lambda x: x[3], reverse=True): ssid = ssid.decode('utf-8') encrypted = authmode > 0 print("ssid: %s chan: %d rssi: %d authmode: %s" % (ssid, channel, rssi, AUTHMODE.get(authmode, '?'))) if encrypted: if ssid in profiles: password = profiles[ssid] connected = do_connect(ssid, password) else: print("skipping unknown encrypted network") else: # open connected = do_connect(ssid, None) if connected: break except OSError as e: print("exception", str(e)) # start web server for connection manager: if not connected: connected = start() return wlan_sta if connected else None def read_profiles(): with open(NETWORK_PROFILES) as f:


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 46 lines = f.readlines() profiles = {} for line in lines: ssid, password = line.strip("\n").split(";") profiles[ssid] = password return profiles def write_profiles(profiles): lines = [] for ssid, password in profiles.items(): lines.append("%s;%s\n" % (ssid, password)) with open(NETWORK_PROFILES, "w") as f: f.write(''.join(lines)) def do_connect(ssid, password): wlan_sta.active(True) if wlan_sta.isconnected(): return None print('Trying to connect to %s...' % ssid) wlan_sta.connect(ssid, password) for retry in range(100): connected = wlan_sta.isconnected() if connected: break time.sleep(0.1) print('.', end='') if connected: print('\nConnected. Network config: ', wlan_sta.ifconfig()) else: print('\nFailed. Not Connected to: ' + ssid) return connected def send_header(client, status_code=200, content_length=None ): client.sendall("HTTP/1.0 {} OK\r\n".format(status_code)) client.sendall("Content-Type: text/html\r\n") if content_length is not None: client.sendall("Content-Length: {}\r\n".format(content_length)) client.sendall("\r\n") def send_response(client, payload, status_code=200): content_length = len(payload) send_header(client, status_code, content_length) if content_length > 0: client.sendall(payload) client.close() def handle_root(client): wlan_sta.active(True) ssids = sorted(ssid.decode('utf-8') for ssid, *_ in wlan_sta.scan())


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 47 send_header(client) client.sendall("""\ <html> <h1 style="color: #5e9ca0; text-align: center;"> <span style="color: #ff0000;"> Wi-Fi Client Setup </span> </h1> <form action="configure" method="post"> <table style="margin-left: auto; margin-right: auto;"> <tbody> """) while len(ssids): ssid = ssids.pop(0) client.sendall("""\ <tr> <td colspan="2"> <input type="radio" name="ssid" value="{0}" />{0} </td> </tr> """.format(ssid)) client.sendall("""\ <tr> <td>Password:</td> <td><input name="password" type="password" /></td> </tr> </tbody> </table> <p style="text-align: center;"> <input type="submit" value="Submit" /> </p> </form> <p>&nbsp;</p> <hr /> <h5> <span style="color: #ff0000;"> Your ssid and password information will be saved into the "%(filename)s" file in your ESP module for future usage. Be careful about security! </span> </h5> <hr /> <h2 style="color: #2e6c80;"> Some useful infos: </h2> <ul> <li> Original code from <a href="https://github.com/cpopp/MicroPythonSamples" target="_blank" rel="noopener">cpopp/MicroPythonSamples</a>.


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 48 </li> <li> This code available at <a href="https://github.com/tayfunulu/WiFiManager" target="_blank" rel="noopener">tayfunulu/WiFiManager</a>. </li> </ul> </html> """ % dict(filename=NETWORK_PROFILES)) client.close() def handle_configure(client, request): match = ure.search("ssid=([^&]*)&password=(.*)", request) if match is None: send_response(client, "Parameters not found", status_code=400) return False # version 1.9 compatibility try: ssid = match.group(1).decode("utf-8").replace("%3F", "?").replace("%21", "!") password = match.group(2).decode("utf-8").replace("%3F", "?").replace("%21", "!") except Exception: ssid = match.group(1).replace("%3F", "?").replace("%21", "!") password = match.group(2).replace("%3F", "?").replace("%21", "!") if len(ssid) == 0: send_response(client, "SSID must be provided", status_code=400) return False if do_connect(ssid, password): response = """\ <html> <center> <br><br> <h1 style="color: #5e9ca0; text-align: center;"> <span style="color: #ff0000;"> ESP successfully connected to WiFi network %(ssid)s. </span> </h1> <br><br> </center> </html> """ % dict(ssid=ssid) send_response(client, response) try: profiles = read_profiles() except OSError: profiles = {}


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 49 profiles[ssid] = password write_profiles(profiles) time.sleep(5) return True else: response = """\ <html> <center> <h1 style="color: #5e9ca0; text-align: center;"> <span style="color: #ff0000;"> ESP could not connect to WiFi network %(ssid)s. </span> </h1> <br><br> <form> <input type="button" value="Go back!" onclick="history.back()"></input> </form> </center> </html> """ % dict(ssid=ssid) send_response(client, response) return False def handle_not_found(client, url): send_response(client, "Path not found: {}".format(url), status_code=404) def stop(): global server_socket if server_socket: server_socket.close() server_socket = None def start(port=80): global server_socket addr = socket.getaddrinfo('0.0.0.0', port)[0][-1] stop() wlan_sta.active(True) wlan_ap.active(True) wlan_ap.config(essid=ap_ssid, password=ap_password, authmode=ap_authmode) server_socket = socket.socket() server_socket.bind(addr)


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 50 server_socket.listen(1) print('Connect to WiFi ssid ' + ap_ssid + ', default password: ' + ap_password) print('and access the ESP via your favorite web browser at 192.168.4.1.') print('Listening on:', addr) while True: if wlan_sta.isconnected(): return True client, addr = server_socket.accept() print('client connected from', addr) try: client.settimeout(5.0) request = b"" try: while "\r\n\r\n" not in request: request += client.recv(512) except OSError: pass print("Request is: {}".format(request)) if "HTTP" not in request: # skip invalid requests continue # version 1.9 compatibility try: url = ure.search("(?:GET|POST) /(.*?)(?:\\?.*?)? HTTP", request).group(1).decode("utf-8").rstrip("/") except Exception: url = ure.search("(?:GET|POST) /(.*?)(?:\\?.*?)? HTTP", request).group(1).rstrip("/") print("URL is {}".format(url)) if url == "": handle_root(client) elif url == "configure": handle_configure(client, request) else: handle_not_found(client, url) finally: client.close() 2. Save the scripts in the “This PC” named wifimgr.py. 3. Save copy… this file named wifimgr.py to MicroPython Device. 4. Create a new file and copy the following scripts. import wifimgr from time import sleep


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 51 import machine try: import usocket as socket except: import socket led2 = machine.Pin(2, machine.Pin.OUT) wlan = wifimgr.get_connection() if wlan is None: print("Could not initialize the network connection.") while True: pass # you shall not pass :D # Main Code goes here, wlan is a working network.WLAN(STA_IF) instance. print("ESP OK") def web_page(): if led2.value() == 1: gpio2_state="ON" else: gpio2_state="OFF" html = """<html><head> <title>ESP Web Server</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" href="data:,"> <style>html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;} h1{color: #0F3376; padding: 2vh;}p{font-size: 1.5rem;}.button{display: inline-block; background-color: #e7bd3b; border: none; border-radius: 4px; color: white; padding: 16px 40px; textdecoration: none; font-size: 30px; margin: 2px; cursor: pointer;} .button2{background-color: #4286f4;}</style></head><body> <h1>ESP Web Server</h1> <p>GPIO state: <strong>""" + gpio2_state + """</strong></p><p><a href="/?led2=on"><button class="button">ON</button></a></p> <p><a href="/?led2=off"><button class="button button2">OFF</button></a></p></body></html>""" return html try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(('', 80)) s.listen(5) except OSError as e: machine.reset() while True: try: if gc.mem_free() < 102000: gc.collect() conn, addr = s.accept()


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 52 conn.settimeout(3.0) print('Got a connection from %s' % str(addr)) request = conn.recv(1024) conn.settimeout(None) request = str(request) print('Content = %s' % request) led2_on = request.find('/?led2=on') led2_off = request.find('/?led2=off') if led2_on == 6: print('LED2 ON') led2.value(1) if led2_off == 6: print('LED2 OFF') led2.value(0) response = web_page() conn.send('HTTP/1.1 200 OK\n') conn.send('Content-Type: text/html\n') conn.send('Connection: close\n\n') conn.sendall(response) conn.close() except OSError as e: conn.close() print('Connection closed') 5. Save all the files and RUN the scripts. B. Web server demonstration 6. Observe the Shell after RUN the scripts. Figure 3.15: Shell shows the SSID and Password that you need. 7. Connect your PC to this WiFi SSID: WifiManager and password tayfunulu. After connecting, the next figure will appear to select your router WiFi SSID and your WiFi password to connect to the internet.


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 53 Figure 3.16: Display the webpage after being connected to SSID WifiManager. 8. After connecting to your WiFi router, the IP address will appear in Shell like the next figure. Copy that IP address, paste and run it into your internet browser (Chrome, Firefox, or Microsoft Edge) Figure 3.17: Connection to your Wi-Fi has been successful.


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 54 Figure 3.18: Show the IP Address. Figure 3.19: Display of the webpage after access via IP address. 9. Click the button ‘ON’ 10. Observe the Shell part in Thonny IDE, the response from the web server will appear as in the next figure.


Chapter 3 – NodeMCU ESP32 using HTTP Protocol 55 Figure 3.20: Display on Shell after clicking the button ‘ON’. 11. Response will same when clicking on the button ‘OFF’ 12. Please run on your own and record all your observation. Problem Based Learning 1. Write the scripts based on DHT22 Web Server 2. If the temperature is > 28, the LED is on. 3. If the temperature is < 30, the LED is off. Summary In this chapter, we control switching and monitoring sensor reading via a web page using the HTTP protocol. Even though using the HTTP protocol, the connection is still in WLAN, and cannot access the internet. But all this guide will lead to connecting to the IoT cloud or virtual PC in the cloud. So, we still can control switching and monitoring sensors through the internet. The rest next topic will discuss another internet communication protocol, especially for IoT. It is MQTT protocol. This MQTT has more advantages in IoT technology.


Chapter 4 – Set up and Configure MQTT Protocol 56 4 Set up and Configure MQTT Protocol MQTT is one of the most used protocols in IoT projects. It stands for Message Queuing Telemetry Transport. In addition, it is designed as a lightweight messaging protocol that uses publish/subscribe operations to exchange data between clients and the server. Furthermore, its small size, low power usage, minimized data packets, and ease of implementation make the protocol ideal for the “machine-to-machine” or “Internet of Things” world. Figure 4.1: Show the IoT system using the MQTT communication protocol. How do these devices communicate with each other? Certainly, web browsers use the HTTP protocol to communicate with the servers on the internet. Likewise, IoT devices use specific protocols to communicate (send and receive data) over the internet to each other. Here are some of the most known IoT protocols: • CoAP (Constrained Application Protocol) • AMQP (Advanced Message Queuing Protocol) • DDS (Data Distribution Service) • STOMP (Simple Text Oriented Messaging Protocol) • MQTT (Message Queue Telemetry Transport) The last one is the most used protocol in IoT projects. That’s due to its fast, lightweight, and easy to implement.


Chapter 4 – Set up and Configure MQTT Protocol 57 Why MQTT? MQTT has unique features you can hardly find in other protocols, like: • It’s a lightweight protocol. So, it’s easy to implement in software and fast in data transmission. • It’s based on a messaging technique. Of course, you know how fast your messenger/WhatsApp message delivery is. Likewise, the MQTT protocol. • Minimized data packets. Hence, low network usage. • Low power usage. As a result, it saves the connected device’s battery. • It’s real-time! That’s specifically what makes it perfect for IoT applications. How MQTT works Like any other internet protocol, MQTT is based on clients and a server. Likewise, the server is the guy who is responsible for handling the client’s requests of receiving or sending data between each other. The MQTT server is called a broker and the clients are simply the connected devices. So: • When a device (a client) wants to send data to the broker, we call this operation a “publish”. • When a device (a client) wants to receive data from the broker, we call this operation a “subscribe”. Figure 4.2: MQTT Protocol System In addition, these clients are publishing and subscribe to topics. So, the broker here is the one that handles the publishing/subscribing actions to the target topics. Example: Let’s say there is a device that has a temperature sensor. Certainly, it wants to send his readings to the broker. On the other side, a phone/desktop application wants to receive this temperature value. Therefore, 2 things will happen: • The device defines the topic it wants to publish on, ex: “temp”. Then, it publishes the message “temperature value”. • The phone/desktop application subscribes to the topic “temp”. Then, it receives the message that the device has published, which is the temperature value.


Chapter 4 – Set up and Configure MQTT Protocol 58 Again, the broker’s role here is to take the message “temperature value” and deliver it to the phone/desktop application. Figure 4.4: Example publish data and subscribe data in MQTT Protocol. MQTT Components: That takes us to the MQTT components, which are 5 as follows: • Broker, which is the server that handles the data transmission between the clients. • A topic, which is the place a device wants to put or retrieve a message to/from. • The message, which is the data that a device receives “when subscribing” from a topic or sends “when publishing” to a topic. • Publish, is the process a device does to send its message to the broker. • Subscribe, where a device does to retrieve a message from the broker. Figure 4.5: Relation between Publisher, Topic, Broker Message, and Subscriber. How many devices you can connect to a broker? The number of connected devices “clients” to the broker depends on the broker service provider. In fact, it can reach a massive number of clients who are publishing and subscribing all the time.


Chapter 4 – Set up and Configure MQTT Protocol 59 But the amazing part of this isn’t only the huge number of these connected devices but also any the fact that any device can get any other device’s data at any time. As a result, the applications based on these quickly shared data are limitless. Why not HTTP HTTP is a slower, more overhead, and more power-consuming protocol than MQTT. So, let’s get into each one separately: • Slower: because it uses bigger data packets to communicate with the server. • Overhead: HTTP request opens and closes the connection at each request, while MQTT stays online to make the channel always open between the broker “server” and clients. • Power consuming: since it takes a longer time and more data packets, therefore it uses much power. Additional notes The difference to HTTP is that a client doesn’t have to pull the information it needs, but the broker pushes the information to the client, in case there is something new. Therefore each MQTT client has a permanently open TCP connection to the broker. If this connection is interrupted by any circumstances, the MQTT broker can buffer all messages and send them to the client when it is back online. As mentioned before the central concept in MQTT to dispatch messages is topics. A topic is a simple string that can have more hierarchy levels, which are separated by a slash. A sample topic for sending temperature data of the living room could be house/living room/temperature. On one hand, the client can subscribe to the exact topic or on the other hand use a wildcard. The subscription to house/+/temperature would result in all messages sent to the previously mentioned topic house/living-room/temperature as well as any topic with an arbitrary value in the place of living room, for example, house/kitchen/temperature. The plus sign is a single-level wild card and only allows arbitrary values for one hierarchy. If you need to subscribe to more than one level, for example to the entire subtree, there is also a multilevel wildcard (#). It allows subscribing to all underlying hierarchy levels. For example, house/# is subscribing to all topics beginning with the house. Which broker to use? There are many brokers that implement the MQTT protocol. • Bevywise MQTTRoute • Mosquito • HiveMQ • Blynk • Arduino Cloud PRE-Installation NODE-RED Node-RED is a programming tool for wiring together hardware devices, APIs, and online services in new and interesting ways. It provides a browser-based editor that makes it easy to wire together flows using the wide range of nodes in the palette that can be deployed to its runtime in a single click.


Chapter 4 – Set up and Configure MQTT Protocol 60 Node-RED provides a browser-based flow editor that makes it easy to wire together flows using the wide range of nodes in the palette. Flows can be then deployed to the runtime in a single click. JavaScript functions can be created within the editor using a rich text editor. A built-in library allows you to save useful functions, templates or flows for reuse. The lightweight runtime Node-RED is built on Node.js, taking full advantage of its event-driven, non-blocking model. This makes it ideal to run at the edge of the network on lowcost hardware such as the Raspberry Pi as well as in the cloud. With over 225,000 modules in Node's package repository, it is easy to extend the range of palette nodes to add new capabilities. The flows created in Node-RED are stored using JSON which can be easily imported and exported for sharing with others. An online flow library allows you to share your best flows with the world. Node-RED can install and run locally in Windows, Raspberry OS, Linux, and in the cloud. Follow these guidelines to set up and install Node-RED. 1. Enter to nodejs website and download the nodejs installer for windows, https://nodejs.org/en/ Figure 4.6: Nodejs website display


Chapter 4 – Set up and Configure MQTT Protocol 61 Figure 4.7: Nodejs windows installation. 2. Then run the application. Figure 4.8: Open Nodejs after installation. 3. After successfully installing Nodejs, then Node-RED can be installed. 4. Open this link and read carefully https://nodered.org/docs/getting-started/windows 5. Installing Node-RED as a global module adds the command node-red to your system path. Execute the following at the command prompt:


Chapter 4 – Set up and Configure MQTT Protocol 62 Figure 4.9: Open Command Prompt to install Node-RED 6. Type this npm install -g --unsafe-perm node-red as shown below: Figure 4.10: Node-RED installation is in progress. 7. After installation has been finished, Type this node-red Figure 4.11: Display on command prompt after typing node-red 8. Then click Allow Access


Chapter 4 – Set up and Configure MQTT Protocol 63 Figure 4.12: Click Allow access to proceed. 9. On your browser type: localhost:1880. The same goes to type your device IP address followed by:1880. Figure 4.13: Type localhost:1880 to the address bar in your browser. 10. The interface of your browser now will look like this Figure 4.14: Node-RED display after successfully installation.


Chapter 4 – Set up and Configure MQTT Protocol 64 11. Then click the setting at the right of the panel (number 1) and choose manage palette (number 2) as shown below: Figure 4.15: Location of Panels bar and Manage palette. 12. At the install panel, type dashboard, then install node red dashboard as shown below. 1 2


Chapter 4 – Set up and Configure MQTT Protocol 65 Figure 4.16: Location of Install tab and find dashboard. 13. Click CLOSE and now your dashboard is ready as shown below. Figure 4.17: Dashboard has been installed. Install this


Chapter 4 – Set up and Configure MQTT Protocol 66 MOSQUITTO MQTT BROKER Installation At this point, Node-RED ready for programming, but we need another program as a BROKER to manage all the data that need to pass up to from Publisher to Subscriber in MQTT protocol. So, in this book, we are using Eclipse Mosquitto as an MQTT Broker. Eclipse Mosquitto is an open-source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 5.0, 3.1.1, and 3.1. Mosquitto is lightweight and is suitable for use on all devices from low-powered single-board computers to full servers. The MQTT protocol provides a lightweight method of carrying out messaging using a publish/subscribe model. This makes it suitable for Internet of Things messaging such as with low-power sensors or mobile devices such as phones, embedded computers, or microcontrollers. The Mosquitto project also provides a C library for implementing MQTT clients, and the very popular mosquitto_pub and mosquitto_sub command line MQTT clients. Follow this guide to the installation of Eclipse Mosquitto. 1. Open the Mosquitto website to download https://mosquitto.org/download/ Figure 4.18: Mosquitto website to download the Eclipse Mosquitto. 2. After download, double click the .EXE file to install. In this case, the installation was made in Windows OS. 3. After the ‘Finish’ installation, open Task Manager on Windows. Find ‘mosquitto’ in the list of services. Right-click the mouse on it and click ‘STOP’. Close the Task Manager window.


Chapter 4 – Set up and Configure MQTT Protocol 67 Figure 4.19: Mosquitto in Windows Task Manager list. 4. Now open Command Prompt as Administrator on Windows, and type the following: a. Navigate to: “C:\Program Files\mosquitto\” b. Open the mosquitto folder by this command: C:\Program Files\mosquitto>dir c. Start Mosquitto Service by this command: “net start mosquitto” d. Stop Mosquitto Service by this command: “net stop mosquitto” e. Change Port for Mosquitto Broker by this command: “mosquitto -p 1883”. MQTT port must be 1883 or 8883 only. 5. Try checking the activity of Mosquitto Broker by this command: “mosquitto -v” If an Error appears like in the next figure, proceed to step 6. Figure 4.20: Error appears because port 1883 is used by another program in Windows. 6. Open a new window CMD as Administrator and type the following: a. “netstat -ano | findstr 1883”.


Chapter 4 – Set up and Configure MQTT Protocol 68 Figure 4.21: Show which program used port 1883. b. “taskkill /F /PID 3964” (depend on your PC, what the number appear like in a red circle. c. On the first window CMD, try “mosquitto -v” again. Check if ERRORS still appear. 7. If no more errors appear, try sending a message from the first CMD window by typing this command: “mosquitto_pub -h localhost -t topic/pub -m "message from p1" -r -i client -d” 8. In the second CMD window, try to get the message by typing this command: “mosquitto_sub -h localhost -t topic/# -d” Figure 4.22: Installation of Eclipse Mosquitto was successful.


Chapter 4 – Set up and Configure MQTT Protocol 69 MQTTBox installation MQTTBox is a cross-platform application that makes it simple to create MQTT clients, virtual device networks, and load test MQTT devices and brokers. The MQTTBox app enables you to create MQTT clients to publish or subscript topics, create MQTT virtual device networks, load test MQTT devices or brokers, and much more. MQTTBox is a cross-platform application available on Linux, MAC, and Windows. So, install MQTTBox to load test MQTT data by following this guide. 1. Click Microsoft Store and search MQTTBox. Install it and open it. Figure 4.23: MQTTBox successfully installed. 2. Then the MQTTBox is ready to be used:


Chapter 4 – Set up and Configure MQTT Protocol 70 Figure 4.24: MQTTBox is ready to be used. START YOUR FIRST FLOW To testing and to understand the visual programming in Node-RED, we need to experience it. Follow this guide to help you understand the Node-RED. 1. Open node-red and node-red dashboard. Figure 4.25: Find mqtt in in network tab. 2. Choose “mqtt in” as shown above. 3. Drag the “mqtt in” to the flow1 plane as shown below 4. Double-click the “mqtt in” node and change the setting as shown below: Figure 4.26: mqtt in placed in Flow 1.


Chapter 4 – Set up and Configure MQTT Protocol 71 5. Change the properties below and click “update”. Figure 4.27: Properties are shown after double clicking mqtt into edit mqtt server. 6. Then, change the topic and name below: Figure 4.28: Properties are shown after double clicking mqtt in. Use your own name or tittle Use your own topic and remember this is case sensitive


Chapter 4 – Set up and Configure MQTT Protocol 72 7. Drag “Gauge” Figure 4.29: Find gauge in dashboard tab. 8. Edit the gauge properties. Figure 4.30: Double click the gauge to edit properties. 9. Join the 2 nodes together. You can group your gauge etc using this property Name your gauge here. The range can be set


Chapter 4 – Set up and Configure MQTT Protocol 73 10. Then click “deploy”. 11. Then after its successfully deployed. Click the open dashboard icon as shown below: 12. The node-red dashboard will be shown below: Figure 4.31: Node-red Dashboard display. Open dashboard icon


Chapter 4 – Set up and Configure MQTT Protocol 74 Figure 4.32: Website to get the mqtt Host or server. 13. Open MQTTbox. 14. Click “create MQTT client” and change MQTT client name, Protocol and Host as shown below. Then click Save. Figure 4.33: Focus on three red rectangles to edit the properties.


Chapter 4 – Set up and Configure MQTT Protocol 75 15. Change the Topic to publish the same as “topic” in your node-red. 16. Then click publish button. 17. If success the display as below will have appeared.


Chapter 4 – Set up and Configure MQTT Protocol 76 18. And the node-red dashboard gauge will be shown as below: 19. Try change the published value:


Chapter 4 – Set up and Configure MQTT Protocol 77 Using Android Apps to Connect to MQTT Broker In this part, 1. Go the AppStore and Install “IOT MQTT Panel”. 2. Click the icon “+” at the below on the right-side panel to set the new connection. Name the connection name (use any name you prefer). Type “broker.hivemq.com” at Broker web. Port number:1883 • Network protocol: TCP


Chapter 4 – Set up and Configure MQTT Protocol 78 • Create device list and name the device. • Then click “Create” button. Click to create device list


Chapter 4 – Set up and Configure MQTT Protocol 79 I name my device as “Meter” here. Click “ADD PANEL” button.


Chapter 4 – Set up and Configure MQTT Protocol 80 • Click “Gauge”


Chapter 4 – Set up and Configure MQTT Protocol 81 • Then, set the “panel name”, “topic”, Unit, QoS


Chapter 4 – Set up and Configure MQTT Protocol 82 • Then ADD PANEL and choose TEXT INPUT. • Then, click “TEXT INPUT” panel and set panel name and topic


Chapter 4 – Set up and Configure MQTT Protocol 83 • Now, your panel will be shown like this. • Try to send the value in “text input” and the gauge will be changed accordingly based on the value in text input box.


Chapter 4 – Set up and Configure MQTT Protocol 84 Problem Based Learning Based on all the steps above, use your understanding to get the output in your node-red dashboard. Results: Summary In this chapter, we install and configure our own IOT Broker in the PC, access and communicate in LAN and WLAN. Protocol communication between IoT sensor devices and IoT Broker using MQTT protocol with publish and subscribe method. We create IoT Broker using Node-Red as platform and Mosquitto as a IoT Broker. To display mobility all the data, we use node-red dashboard and IoT MQTT Panel in Android App. Now, IoT Broker has been set up and is ready to receive data from IoT sensor devices. The next chapter will show how to publish data from IoT sensors devices to monitor and subscribe data to trigger any switch that we want to control.


Chapter 5 – NodeMCU ESP32 Using MQTT Protocol 85 5 NodeMCU ESP32 using MQTT Protocol NodeMCU is an open-source networking platform. It is programmed by using the LUA scripting language. The platform is based on eLUA open-source projects. NodeMCU-32S is based on the ESP-32S module. The platform uses a lot of open-source projects, such as luacjson, and spiffs. NodeMCU-32S contains firmware that can run on ESP32 Wi-Fi SoC chips and hardware based on ESP-32S modules. The core of this module is the ESP32 chip, which is scalable and adaptive. Two CPU cores can be individually controlled. The clock frequency is adjustable from 80 MHz to 240 MHz and supports RTOS. It is a general-purpose Wi-Fi, BT, BLE, MCU module ESP-WROOM-32s. In this chapter, we will set up the connection to send sensor data from NodeMCU ESP32 to node-red to program some machine learning. NodeMCU as an IoT device will publish sensor data and will subscribe to data commands from IoT Broker. To accomplish this task, just follow the simple procedure. Configuring the MQTT Publish and Subscribe Nodes in Node-Red Node-Red provides both an MQTT subscribe (input) and publish (output) node. The configuration for these nodes is almost Identical as the main part of the configuration concerns the actual client connection. Because of this, it is useful to think of the publish and subscribe nodes as consisting of two components as shown in the schematic below. Before we look at the actual configuration, we will look at MQTT client connections in general.


Chapter 5 – NodeMCU ESP32 Using MQTT Protocol 86 Connecting to an MQTT Broker or Server • To connect to an MQTT broker or server you need to know the Broker IP address or name, and the port that it is using (default 1883). • In addition, a client needs to provide a client name to identify itself. Each client connection requires a unique client name. • A single client connection can be used to both publish and subscribe. • An MQTT broker can enforce encryption and username/password authentication in which case the connecting client must comply. Turn ON and OFF LED NodeMCU ESP32 via Node-Red Dashboard 1. Run Node-js application. 2. Open cmd-prompt and type node-red. 3. On your browser type: localhost:1880


Chapter 5 – NodeMCU ESP32 Using MQTT Protocol 87 4. Join the node as shown below. 5. Set the MQTT in objects numbered 1 and 2 as shown below.


Chapter 5 – NodeMCU ESP32 Using MQTT Protocol 88 6. Set the debug object number 3 as shown below. 7. Set the ON button as shown below.


Chapter 5 – NodeMCU ESP32 Using MQTT Protocol 89 8. Set the OFF button as shown below. 9. Then, DEPLOY the node. 10. Launch the node-red dashboard, and the interface will be shown below. 11. Open your Thonny IDE and create a new file. Write the given first script. try: import usocket as socket except: import socket import ustruct as struct from ubinascii import hexlify class MQTTException(Exception):


Chapter 5 – NodeMCU ESP32 Using MQTT Protocol 90 pass class MQTTClient: def __init__(self, client_id, server, port=0, user=None, password=None, keepalive=0, ssl=False, ssl_params={}): if port == 0: port = 8883 if ssl else 1883 self.client_id = client_id self.sock = None self.server = server self.port = port self.ssl = ssl self.ssl_params = ssl_params self.pid = 0 self.cb = None self.user = user self.pswd = password self.keepalive = keepalive self.lw_topic = None self.lw_msg = None self.lw_qos = 0 self.lw_retain = False def _send_str(self, s): self.sock.write(struct.pack("!H", len(s))) self.sock.write(s) def _recv_len(self): n = 0 sh = 0 while 1: b = self.sock.read(1)[0] n |= (b & 0x7f) << sh if not b & 0x80: return n sh += 7 def set_callback(self, f): self.cb = f def set_last_will(self, topic, msg, retain=False, qos=0): assert 0 <= qos <= 2 assert topic self.lw_topic = topic self.lw_msg = msg self.lw_qos = qos self.lw_retain = retain def connect(self, clean_session=True): self.sock = socket.socket() addr = socket.getaddrinfo(self.server, self.port)[0][-1] self.sock.connect(addr) if self.ssl:


Chapter 5 – NodeMCU ESP32 Using MQTT Protocol 91 import ussl self.sock = ussl.wrap_socket(self.sock, **self.ssl_params) premsg = bytearray(b"\x10\0\0\0\0\0") msg = bytearray(b"\x04MQTT\x04\x02\0\0") sz = 10 + 2 + len(self.client_id) msg[6] = clean_session << 1 if self.user is not None: sz += 2 + len(self.user) + 2 + len(self.pswd) msg[6] |= 0xC0 if self.keepalive: assert self.keepalive < 65536 msg[7] |= self.keepalive >> 8 msg[8] |= self.keepalive & 0x00FF if self.lw_topic: sz += 2 + len(self.lw_topic) + 2 + len(self.lw_msg) msg[6] |= 0x4 | (self.lw_qos & 0x1) << 3 | (self.lw_qos & 0x2) << 3 msg[6] |= self.lw_retain << 5 i = 1 while sz > 0x7f: premsg[i] = (sz & 0x7f) | 0x80 sz >>= 7 i += 1 premsg[i] = sz self.sock.write(premsg, i + 2) self.sock.write(msg) #print(hex(len(msg)), hexlify(msg, ":")) self._send_str(self.client_id) if self.lw_topic: self._send_str(self.lw_topic) self._send_str(self.lw_msg) if self.user is not None: self._send_str(self.user) self._send_str(self.pswd) resp = self.sock.read(4) assert resp[0] == 0x20 and resp[1] == 0x02 if resp[3] != 0: raise MQTTException(resp[3]) return resp[2] & 1 def disconnect(self): self.sock.write(b"\xe0\0") self.sock.close() def ping(self): self.sock.write(b"\xc0\0") def publish(self, topic, msg, retain=False, qos=0): pkt = bytearray(b"\x30\0\0\0") pkt[0] |= qos << 1 | retain sz = 2 + len(topic) + len(msg) if qos > 0:


Chapter 5 – NodeMCU ESP32 Using MQTT Protocol 92 sz += 2 assert sz < 2097152 i = 1 while sz > 0x7f: pkt[i] = (sz & 0x7f) | 0x80 sz >>= 7 i += 1 pkt[i] = sz #print(hex(len(pkt)), hexlify(pkt, ":")) self.sock.write(pkt, i + 1) self._send_str(topic) if qos > 0: self.pid += 1 pid = self.pid struct.pack_into("!H", pkt, 0, pid) self.sock.write(pkt, 2) self.sock.write(msg) if qos == 1: while 1: op = self.wait_msg() if op == 0x40: sz = self.sock.read(1) assert sz == b"\x02" rcv_pid = self.sock.read(2) rcv_pid = rcv_pid[0] << 8 | rcv_pid[1] if pid == rcv_pid: return elif qos == 2: assert 0 def subscribe(self, topic, qos=0): assert self.cb is not None, "Subscribe callback is not set" pkt = bytearray(b"\x82\0\0\0") self.pid += 1 struct.pack_into("!BH", pkt, 1, 2 + 2 + len(topic) + 1, self.pid) #print(hex(len(pkt)), hexlify(pkt, ":")) self.sock.write(pkt) self._send_str(topic) self.sock.write(qos.to_bytes(1, "little")) while 1: op = self.wait_msg() if op == 0x90: resp = self.sock.read(4) #print(resp) assert resp[1] == pkt[2] and resp[2] == pkt[3] if resp[3] == 0x80: raise MQTTException(resp[3]) return # Wait for a single incoming MQTT message and process it. # Subscribed messages are delivered to a callback previously # set by .set_callback() method. Other (internal) MQTT # messages processed internally. def wait_msg(self):


Chapter 5 – NodeMCU ESP32 Using MQTT Protocol 93 res = self.sock.read(1) self.sock.setblocking(True) if res is None: return None if res == b"": raise OSError(-1) if res == b"\xd0": # PINGRESP sz = self.sock.read(1)[0] assert sz == 0 return None op = res[0] if op & 0xf0 != 0x30: return op sz = self._recv_len() topic_len = self.sock.read(2) topic_len = (topic_len[0] << 8) | topic_len[1] topic = self.sock.read(topic_len) sz -= topic_len + 2 if op & 6: pid = self.sock.read(2) pid = pid[0] << 8 | pid[1] sz -= 2 msg = self.sock.read(sz) self.cb(topic, msg) if op & 6 == 2: pkt = bytearray(b"\x40\x02\0\0") struct.pack_into("!H", pkt, 2, pid) self.sock.write(pkt) elif op & 6 == 4: assert 0 # Checks whether a pending message from server is available. # If not, returns immediately with None. Otherwise, does # the same processing as wait_msg. def check_msg(self): self.sock.setblocking(False) return self.wait_msg() 12. Save this script named as umqttsimple.py to “This Computer” and save a copy to “Micropython Device”. 13. Write the given second script: import time from umqttsimple import MQTTClient import ubinascii import machine import micropython import network import esp esp.osdebug(None) import gc gc.collect() # Setup a GPIO Pin for output


Chapter 5 – NodeMCU ESP32 Using MQTT Protocol 94 led = machine.Pin(26, machine.Pin.OUT) pin = machine.Pin(2, machine.Pin.OUT) ssid = 'REPLACE_WITH_YOUR_SSID' password = 'REPLACE_WITH_YOUR_PASSWORD' mqtt_server = 'broker.hivemq.com' #EXAMPLE IP ADDRESS #mqtt_server = '192.168.1.144' client_id = ubinascii.hexlify(machine.unique_id()) topic_sub = b'test' topic_pub = b'data' last_message = 0 message_interval = 5 counter = 0 station = network.WLAN(network.STA_IF) station.active(True) station.connect(ssid, password) while station.isconnected() == False: pass print('Connection successful') print(station.ifconfig()) def sub_cb(topic, msg): print((topic, msg)) if msg == b"on": led.on() elif msg == b"off": led.off() def connect_and_subscribe(): global client_id, mqtt_server, topic_sub client = MQTTClient(client_id, mqtt_server) client.set_callback(sub_cb) client.connect() pin.on() client.subscribe(topic_sub) print('Connected to %s MQTT broker, subscribed to %s topic' % (mqtt_server, topic_sub)) return client def restart_and_reconnect(): print('Failed to connect to MQTT broker. Reconnecting...') time.sleep(10) pin.off() machine.reset() try: client = connect_and_subscribe() except OSError as e: restart_and_reconnect()


Click to View FlipBook Version