IOTA

 
 
IOTA is an open-source distributed ledger. It uses a directed acyclic graph (DAG) instead of a blockchain.
IOTA's DAG is referred to as the tangle, and is a generalization of the block chain protocol.

More information about IOTA can be found at:
http://iota.org/







Send DHT11 sensor data using ESP-01S ESP8266 and MQTT to IOTA Tangle using Masked Authenticated Messaging (MAM)



Information
The source codes used in this tutorial can be found at:
https://github.com/robertlie/dht11-esp01s

Operating system used
macOS 10.13 High Sierra

Hardware used
  • ESP-01S ESP8266 WiFi module
    The ESP8266 is a low-cost Wi-Fi chip with full TCP/IP stack and microcontroller capability produced by a Chinese manufacturer, Espressif Systems. The ESP-01S module, using the ESP8266 WiFi chip, is made by a third-party Chinese manufacturer, AI-Thinker. AI-Thinker has produced many different ESP8266 modules, ESP-01, ESP-01S, ESP-02, ESP-03, etc.

    ESP-01S ESP8266

    Top view
    CH_PD = CHip Pull Down, on my ESP-01S it is marked as EN
    VCC = The ESP-01S requires 3.3V input
    GPIO0 = On my ESP-01S it is marked as IO0
    GPIO2 = On my ESP-01S it is marked as IO2

    ESP-01S pin layout

    Bottom view
    ESP-01S bottom view

  • FTDI FT232RL 3.3/5.5V

    The ESP-01S does not have an USB connector. To communicate with the ESP-01S with your computer via an USB port an USB to TTL serial adapter is needed. In this tutorial I will be using the FTDI FT232RL adapter but you can use an Arduino Uno instead, but that method is not described here.

    FTDI FT232RL

    Top view
    FTDI FT232RL top view

    The VCC pin can output 3.3V or 5.0V depending on how you set the jumper.
    However I will NOT use the FTDI VCC pin, the ESP-01S module requires too much current which the FTDI VCC output can not deliver. Instead the ESP-01S module will be powered by an external breadboard power supply module.
    The FTDI adapter itself is powered (5V) by the USB-A -> mini USB-B cable.

    Bottom view
    FTDI FT232RL bottom view

  • Breadboard MB-102

    Solderless breadboard MB-102

  • Breadboard power supply module (2 Channel 3.3V/5V) for MB-102 or half size breadboard
    Voltage input: 7-12 V DC
    Voltage output: 3.3 / 5 V DC
    The breadboard power supply module is used as an external power source for the ESP-01S module.
    The ESP-01S module requires too much current which the FTDI VCC output can not deliver.

    Breadboard power supply module for MB-102 and mini breadboard

  • Mobile phone charger (Output: 5.2 V DC, 2.4 A)
    The mobile phone charger provides power to the breadboard power supply module.

    Mobile phone charger

  • Jumper wires (10x female-male; 20 cm long, 2x female-female, 20 cm long; 2x male-male, 20 cm long)

    Jumper wires female-male

  • USB 2.0 cable, USB-A > Mini USB-B You need 2 cables, one for the mobile phone charger (length: 1.5 m) and the other for the FTDI adapter (length: 80 cm)

    USB 2.0 cable type A/B

  • DHT11 sensor module (DHT = Digital Humidity Temperature)

    Specification:
    Supply voltage: 3.3 ~ 5.5V DC
    Output: single-bus digital signal
    Measuring range: humidity 20-90% RH, temperature 0 ~ 50°C
    Accuracy: humidity ±5% RH, temperature ±2°C
    Resolution: Humidity 1% RH, temperature 1°C
    Long-term stability: < ±1% RH / Year

    DHT11 sensor

  • Push button (pitch 2.54 mm, to be used on breadboard)

    Push button

  • 10 kΩ resistor
    Used as pull up on the DHT11 data line.


    Resistor 10 kOhm

Software prerequisites
none


Procedure
  1. Before you start, please note this tutorial was specifically written for macOS users.

    I also assume you previously have followed the tutorial Send DHT11 sensor data using Raspberry Pi 3 to IOTA Tangle using Masked Authenticated Messaging (MAM).
    In this tutorial I will continue using that Raspberry Pi setup.

  2. Download and install the Arduino IDE on your computer:
    https://www.arduino.cc/en/Main/Software

  3. Install esp8266 packages using the Boards Manager.
    More information about this package see:
    https://github.com/esp8266/Arduino

    • Open the Arduino IDE preferences window.
    • Enter in the Additional Board Manager URLs field:
      http://arduino.esp8266.com/stable/package_esp8266com_index.json

      Additional Board Manager URLs field

    • Select menu: Tools > Board > Board Manager...

      Board manager

    • In the search field, type: esp8266
    • Click the found package.
    • Select the latest version and press the Install button.

      ESP8266 package

    • Select menu: Tools
      You will see additional menu options.

      ESP8266 additional menu options

  4. Connect the FTDI adapter to the ESP-01S using the jumper wires.

    ESP-01S FTDI Breadboard power supply module
    RX TX  
    VCC
    CH_PD (EN)
      3.3 V (DO NOT USE THE 5V OUTPUT!!!)
    RST (connected to a push button)
    GND
    GND GND
    TX RX  
    GPIO0 (Flash mode)   GND
    GPIO0 (Boot mode)    

    Note 1:
    The following pins are not used:
    ESP-01S GPIO2
    FTDI DTR
    FTDI CTS
    FTDI VCC

    Note 2:
    The ESP-01S requires too much current which the FTDI VCC output can not deliver.
    That is why the breadboard power supply module is needed.
    Because of this I will not use the FTDI VCC output, but just in case set the FTDI jumper to 3.3V.
    MAKE SURE YOU USE THE BREADBOARD 3.3 V OUTPUT!

    ESP01S FTDI setup

    The ESP8266 has two modes:

    • Flash mode
      This mode is also known as "UART download mode".
      This mode is used for uploading new firmware or program (eg. Arduino sketch) to the module's flash memory (aka flashing).
      To set the ESP8266 chip in this mode:
      - Connect GPIO0 to ground.
      - Press and release the reset button.

      ESP01S FTDI wiring for flash mode

    • Boot mode
      This mode is also known as "Flash boot mode".
      This mode is used for normal startup and execution of existing firmware or program.
      To set the ESP8266 chip in this mode:
      - Connect GPIO0 to nothing (floating).
      - Press and release the reset button.

      ESP01S FTDI wiring for boot mode

  5. Depending on your FTDI adapter you might need to install a FTDI USB Serial driver.
    My FTDI adapter uses the FT232RL chip.
    • I have installed the FTDIUSBSerialDriver (Virtual COM Port Drivers v2.4.2 for Mac OS X 10.9 and above) which is an implementation of a serial driver for FTDI USB devices on Mac OS X.
      It supports FT8U232AM, FT8U245AM, FT232BM, FT245BM, FT2232, FT232R, FT245R, FT2232H, FT4232H, FT232H and FT X Series devices.
      This driver (FTDIUSBSerialDriver_v2_4_2.dmg) can be found at:
      http://www.ftdichip.com/Drivers/VCP.htm

      But this installed FTDIUSBSerialDriver causes a huge problem!
      It only allows me to upload 3 sketches. The 4th sketch hangs during the upload.
      To fix this problem, I need to restart my computer. This is not good!

      I decided to remove the installed FTDIUSBSerialDriver and use the standard AppleUSBFTDI driver which comes with macOS.

      To remove the previous installed FTDIUSBSerialDriver:
      • Find if the com.FTDI.driver.FTDIUSBSerialDriver is installed.
        Type: pkgutil --pkgs | grep -i ftdi
      • Get list of installed files.
        Type: pkgutil --files com.FTDI.driver.FTDIUSBSerialDriver
      • Delete the files.
        Type: sudo rm -r /Library/Extensions/FTDIUSBSerialDriver.kext
      • Forget the package.
        Type: sudo pkgutil --forget com.FTDI.driver.FTDIUSBSerialDriver

      If you are using macOS 10.13 (High Sierra) or higher DO NOT install the FTDIUSBSerialDriver.

    • Connect the FTDI adapter to your computer.

      Note:
      • To check which port is used.
        Type: ls -al /dev/tty.*
        Output: /dev/tty.usbserial-A9M9DV3R
      • To check which FTDI driver is used.
        Type: /usr/sbin/kextstat | grep FTDI
        Output: com.apple.driver.AppleUSBFTDI

  6. Now lets do some simple tests. First, lets check if I can set the ESP8266 chip in boot mode.
    • Use the Boot mode wiring (see wiring schema above, GPIO0 floating)
    • Power up the breadboard power supply module.
    • Connect the FTDI adapter to your computer.
    • Start Arduino IDE.
    • Select menu: Tools > Board > Generic ESP8266 Module
    • Select the port, select menu: Tools > Port > /dev/cu.usbserial-A9M9DV3R
      Note: Your port name may differ.
    • Select menu: Tools > Serial Monitor.
    • Monitor opens, select: Both NL & CR and select 115200 baud.
    • Press and release the reset (=push) button.
    • You should see:
      Ai-Thinker Technology Co. Ltd.

      ready


      Note:
      If you only see weird characters, lower the baud rate, and press and release the reset button and check if you see the message. If not repeat these steps.
    • After you found the correct baud rate, enter the AT command: AT
      You should see:
      OK

      Note:
      The ESP8266 WiFi module comes pre-programmed with an AT command set firmware.

    • Enter AT command: AT+GMR
      You should see:
      AT+GMR

      AT version:0.21.0.0
      SDK version:0.9.5

      OK


      Note:
      If you have previously uploaded a sketch to the ESP-01S module, the firmware is erased.
      You will not see above mentioned AT command outputs.

      In this tutorial I will not be using the AT commands, I just inform you about its existence.

  7. Download the dht11-esp01s project on your computer:
    Type: git clone https://github.com/robertlie/dht11-esp01s.git

  8. Now lets check if I can set the ESP8266 chip in flash mode by uploading a sketch.
    If you upload a sketch, it will erase the current ESP8266 firmware.
    The AT commands will not work after you upload the sketch but I will demonstrate how to restore this firmware in case you need it in the future.

    Upload the dht11-esp01s/sketches/test/test.ino sketch to the ESP-01S and run this sketch.
    • Use the Flash mode wiring (see wiring schema above, GPIO0 to ground)
    • Power up the breadboard power supply module.
    • Connect the FTDI adapter to your computer.

      FTDI and breadboard power supply connected

    • Start Arduino IDE.
    • Select menu: Tools > Board > Generic ESP8266 Module
    • Select the port, select menu: Tools > Port > /dev/cu.usbserial-A9M9DV3R
      Note: Your port name may differ.
    • Upload the test.ino sketch, select menu: File > Select the test.ino file.
    • Press the verify button to verify the sketch. Make sure there are no errors.
    • Press and release the reset (=push) button.
    • Press the upload button to upload the sketch to the ESP-01S.

      Upload sketch to ESP-01S

    • Open the Serial monitor, select menu: Tools > Serial Monitor.
      Make sure the baud rate is set to 9600 baud (bottom right)

      Sketch output displayed in serial monitor

      The text "Hello World" is displayed every 1 second.
    • Close the serial monitor

    When uploading sketches I sometimes encountered the following "error: espcomm_upload_mem failed".
    Solution: Just press and release the reset button (a few times) and upload the sketch again.

  9. As mentioned earlier, the ESP8266 firmware was erased after you uploaded a sketch.
    If you want to restore this firmware you can follow this procedure.
    Reinstalling the firmware is ONLY needed if you want to play with the AT commands.
    This tutorial is not about using the AT commands!
    If you are not interested in AT commands you can skip this step.

    The latest version of Mac OS X, High Sierra, comes with Python 2.7 out of the box.
    You do not need to install or configure anything else to use Python.
    • Open a terminal and check your installed python version, type:
      python --version

      You should see:
      Python 2.7.10

    • Install the esptool using the Python Package Manager (pip) which should already be installed on a macOS High Sierra.
      Type: sudo pip install esptool

      More information about this tool:
      https://github.com/espressif/esptool

      Note:
      The esptool is a Python-based, open source, platform independent, utility to communicate with the ROM bootloader in Espressif ESP8266 & ESP32 chips.

      The Arduino IDE also uses an esptool to upload the compiled sketch to the ESP-01S module.
      This esptool is located at:
      ~/Library/Arduino15/packages/esp8266/tools/esptool/0.4.13/ To upload the firmware I am using the python esptool and NOT the one used by the Arduino IDE.

    • The ESP8266 firmware can be found at:
      http://wiki.aprbrother.com/en/Firmware_For_ESP8266.html

      I have copied the ai-thinker-0.9.5.2.bin (default baud rate is 115200) firmware in my dht11-esp01s project.

    • Now lets upload this firmware to the ESP-01S module:
      • Use the Flash mode wiring (see wiring schema above, GPIO0 to ground)
      • Power up the breadboard power supply module.
      • Connect the FTDI adapter to your computer.
      • Open a terminal.
      • Type: cd ~
      • Type: cd dht11-esp01s/esp8266_firmware
      • Ensure that the ESP8266 chip is fully erased before flashing:
        Press and release the reset (=push) button.
        Type: esptool.py --port /dev/cu.usbserial-A9M9DV3R erase_flash

        Note:
        To find your port, type: ls -al /dev/tty.*

      • Upload firmware:
        Press and release the reset (=push) button.
        Type: esptool.py --port /dev/cu.usbserial-A9M9DV3R write_flash -fm qio 0x00000 ai-thinker-0.9.5.2.bin

        Note:
        esptool.py --port <serial-port-of-ESP8266> write_flash -fm <mode> 0x00000 <firmware>.bin

        Mode is qio for most ESP8266 ESP-01/07 (512 kByte modules) and dio for most ESP32 and ESP8266 ESP-12 (>=4 MByte modules). ESP8285 requires dout.

        Note:
        Here are some useful esptool commands (will not write anything).
        Before entering each command, first press and release the reset (=push) button.

        Show esptool help: esptool.py -h

        Type: esptool.py --port /dev/cu.usbserial-A9M9DV3R flash_id

        Output:
        esptool.py v2.5.0
        Serial port /dev/cu.usbserial-A9M9DV3R
        Connecting....
        Detecting chip type... ESP8266
        Chip is ESP8266EX
        Features: WiFi
        MAC: xx:xx:xx:xx:xx:xx
        Uploading stub...
        Running stub...
        Stub running...
        Manufacturer: e0
        Device: 4014
        Detected flash size: 1MB
        Hard resetting via RTS pin...


        Type: esptool.py --port /dev/cu.usbserial-A9M9DV3R read_mac

        Output:
        esptool.py v2.5.0
        Serial port /dev/cu.usbserial-A9M9DV3R
        Connecting....
        Detecting chip type... ESP8266
        Chip is ESP8266EX
        Features: WiFi
        MAC: xx:xx:xx:xx:xx:xx
        Uploading stub...
        Running stub...
        Stub running...
        MAC: xx:xx:xx:xx:xx:xx
        Hard resetting via RTS pin..


        Type: esptool.py --port /dev/cu.usbserial-A9M9DV3R chip_id

        Output:
        esptool.py v2.5.0
        Serial port /dev/cu.usbserial-A9M9DV3R
        Connecting....
        Detecting chip type... ESP8266
        Chip is ESP8266EX
        Features: WiFi
        MAC: xx:xx:xx:xx:xx:xx
        Uploading stub...
        Running stub...
        Stub running...
        Chip ID: 0x008e2345
        Hard resetting via RTS pin...


      • The firmware is uploaded.
      • Now you can enter AT commands again.

  10. Connect the DHT11 sensor to ESP-01S using the jumper wires.
    Connect a 10kΩ resistor from DHT11 data pin to 3.3V pin breadboard power supply module.

    DHT11 ESP-01S FTDI Breadboard power supply module
      RX TX  
    VCC VCC
    CH_PD (EN)
      3.3 V (DO NOT USE THE 5V OUTPUT!!!)
    GND RST (connected to a push button)
    GND
    GND GND
      TX RX  
    DATA GPIO2    
    DATA - 10kΩ     3.3 V
      GPIO0 (Flash mode)   GND
      GPIO0 (Boot mode)    

    Note 1:
    The following pins are not used:
    FTDI DTR
    FTDI CTS
    FTDI VCC

    DHT11 ESP01S FTDI setup

    The ESP8266 has two modes:

    • Flash mode
      This mode is also known as "UART download mode".
      This mode is used for uploading new firmware or program (eg. Arduino sketch) to the module's flash memory (aka flashing).
      To set the ESP8266 chip in this mode:
      - Connect GPIO0 to ground.
      - Press and release the reset button.

      DHT11 ESP01S FTDI wiring for flash mode

    • Boot mode
      This mode is also known as "Flash boot mode".
      This mode is used for normal startup and execution of existing firmware or program.
      To set the ESP8266 chip in this mode:
      - Connect GPIO0 to nothing (floating).
      - Press and release the reset button.

      DHT11 ESP01S FTDI wiring for boot mode

  11. In tutorial Send DHT11 sensor data using Arduino Uno to IOTA Tangle using Masked Authenticated Messaging (MAM) I have used Rob Tillaart DHTLib library. However that library does not work when using ESP-01S.

    If you install the "Adafruit DHT Sensor library" and the "Adafruit Unified Sensor Driver", DHT sensor data can be read by ESP8266 or Arduino boards.

    • Install the Adafruit DHT Sensor library in the Arduino IDE:
      • Select menu: Sketch > Include Library > Manage libraries
      • In the Library Manager search for: DHT Sensor library
      • Select this package and install its latest version.
      Note:
      This library can be found at:
      https://github.com/adafruit/Adafruit_Sensor

    • Install the Adafruit Unified Sensor Driver in the Arduino IDE:
      • Select menu: Sketch > Include Library > Manage libraries
      • In the Library Manager search for: Adafruit Unified Sensor
      • Select this package and install its latest version.
      Note:
      This library can be found at:
      https://github.com/adafruit/DHT-sensor-library

    • Restart Arduino IDE. The "Adafruit DHT Sensor library" and the "Adafruit Unified Sensor Driver" are now installed.

      Adafruit DHT Sensor library and the Adafruit Unified Sensor Driver

  12. Lets upload two sketches and see if the ESP-01S module can read the DHT11 temperature and humidity data. WiFi is not used in both cases.
    • Power up the breadboard power supply module.
    • Connect the FTDI adapter to your computer.
    • Start Arduino IDE.
    • Make sure the "Generic 8266 Module" board is selected.
    • Make sure the correct serial port is selected.
    • Set the ESP-01S in flash mode (GPIO0 to ground).
    • Press and release the reset (=push) button.
    • Upload sketch:
      dht11-esp01s/sketches/DHT_Unified_Sensor/DHT_Unified_Sensor.ino
    • Set the ESP-01S in boot mode (GPIO0 floating).
    • Open the Serial Monitor and set the baud rate to 9600
    • Press and release the reset (=push) button.
    • Check if the DHT11 temperature and humidity data are displayed in the Serial Monitor.
    • Repeat the same steps with sketch:
      dht11-esp01s/sketches/DHTtester/DHTtester.ino

  13. Again, lets upload a sketch and see if the ESP-01S module can read the DHT11 temperature and humidity data but now the data is displayed in a browser.
    • Power up the breadboard power supply module.
    • Connect the FTDI adapter to your computer.
    • Start Arduino IDE.
    • Make sure the "Generic 8266 Module" board is selected.
    • Make sure the correct serial port is selected.
    • Set the ESP-01S in flash mode (GPIO0 to ground).
    • Press and release the reset (=push) button.
    • Load sketch:
      dht11-esp01s/sketches/DHTServer/DHTServer.ino
    • Make changes to this sketch according to your situation:
      Your WiFI ssid and password.
    • Upload the sketch to the ESP-01S module.
    • Set the ESP-01S in boot mode (GPIO0 floating).
    • Open the Serial Monitor and set the baud rate to 115200
    • Press and release the reset (=push) button.
    • The ip adress is displayed in the Serial Monitor, for example: 192.168.1.72

      DHT Weather data

    • Open a browser and enter the ip address.
      You should see:
      Hello from the weather esp8266, read from /temp or /humidity

      To see your temperature, type:
      http://192.168.1.72/temp

      DHT11 temperature data

      To see your humidity, type:
      http://192.168.1.72/humidity

      DHT11 humidity data

      Note:
      You need to reload the web page to update the sensor data.

  14. Now lets make the ESP-01S communicate with a mosquitto server. First begin by adding the PubSubClient library to the Arduino IDE. This library is for MQTT messaging and it supports ESP8266.
    • Start Arduino IDE.

    • Select menu: Sketch > Include Library > Manage Libraries

    • Search for: PubSub

    • Select PubSubClient (Nick O'Leary) and install the latest version (eg: v2.6.0)

  15. Next install Mosquitto (MQTT) on the Raspberry Pi 3.
    Mosquitto is an open source message broker that implements the MQTT protocol.
    MQTT stands for Message Queuing Telemetry Transport.
    Mosquitto is lightweight and is suitable for use on all devices from low power 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.

    • Login to your Raspberry Pi and open a terminal.
      Note:
      You can use SSH to access your Raspberry Pi.
      Type: ssh [email protected]

    • Type: cd ~

    • Check what Raspbian version is installed.
      Type: cat /etc/os-release

      You should see:
      PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
      NAME="Raspbian GNU/Linux"
      VERSION_ID="9"
      VERSION="9 (stretch)"
      ID=raspbian
      ID_LIKE=debian
      HOME_URL="http://www.raspbian.org/"
      SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
      BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

    • Download the signing key.
      Type: wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key

    • Add this key to apt-get.
      Type: apt-key add mosquitto-repo.gpg.key

    • Add mosquitto to basic Debian repositories.
      To add the repository, first go to the folder containing the repository lists for apt-get.
      Type: cd /etc/apt/sources.list.d/

    • Download the repository list file for Mosquitto.
      Type: wget http://repo.mosquitto.org/debian/mosquitto-stretch.list

    • Update apt-get's source lists.
      Type: sudo apt-get update

      Note:
      I got the error:
      E: The repository 'http://archive.raspberrypi.org/debian stretch Release' does no longer have a Release file.

      The problem is caused by a bad static DNS IP. To solve this problem:
      Type: sudo nano /etc/dhcpcd.conf
      Add line:
      static domain_name_servers=192.168.1.1 //put your DNS ip address

    • Now install the following:
      • Type: cd ~

      • Type: wget http://security.debian.org/debian-security/pool/
        updates/main/o/openssl/libssl1.0.0_1.0.1t-1+deb8u8_armhf.deb


        Note:
        If the libssl1.0.0_1.0.1t-1+deb8u8_armhf.deb can not be found, then goto http://security.debian.org/debian-security/pool/updates/main/o/openssl/ and check if a newer version is available and use that new file.

      • Type: sudo dpkg -i libssl1.0.0_1.0.1t-1+deb8u8_armhf.deb

        Note:
        As mentioned above if you used a newer file, use that file here.

      • Type: wget http://ftp.nz.debian.org/debian/pool/main/libw/
        libwebsockets/libwebsockets3_1.2.2-1_armhf.deb


      • Type: sudo dpkg -i libwebsockets3_1.2.2-1_armhf.deb

    • Install MQTT broker and mosquito clients.
      Type: sudo apt-get install mosquitto mosquitto-clients

    • Reboot the Raspberry Pi.
      Type: sudo reboot

  16. Login to your Raspberry Pi.

  17. The mosquito server is now up and running. To verify this type:
    mosquito -v

    You should see:
    1532504032: mosquitto version 1.5 starting
    1532504032: Using default config.
    1532504032: Opening ipv4 listen socket on port 1883.
    1532504032: Error: Address already in use

  18. To stop the mosquito server, type:
    sudo /etc/init.d/mosquitto stop

  19. To start the mosquito server, type:
    sudo /etc/init.d/mosquitto start

  20. Test mosquitto, open two terminals:

    In terminal 1 subscribe to a topic, type:
    mosquitto_sub -v -t "demo/topic"

    In terminal 2 publish a message to a topic, type:
    mosquitto_pub -t "demo/topic" -m "Hello"

    In terminal 1, you should see:
    demo/topic Hello

    MQTT subscribe and publish

  21. Lets restrict access to the broker by setting up a username and password.
    Please note the username and password are transmitted in clear text.
    If you want better security, you need to implement mosquito SSL.
    In this tutorial mosquitto SSL will not be implemenented.

    • Stop the mosquito server, type:
      sudo /etc/init.d/mosquitto stop

    • Type: cd /etc/mosquitto

    • Create a password.txt file and add a user and corresponding password.
      Type: sudo mosquitto_passwd -c password.txt robert
      Password: mysecret
      Reenter password: mysecret

    • You can add additional users and passwords to this password.txt file.
      Type: sudo mosquitto_passwd -b password.txt bob mybigsecret

    • To delete a user from this password.txt file.
      Type: sudo mosquitto_passwd -D password.txt bob

    • In this folder you will find the mosquito.conf file.
      Make a backup of this file before making changes to it.
      Type: cp mosquitto.conf mosquitto.conf_original

    • Edit the mosquito.conf file.
      Type: sudo nano mosquitto.conf

      Add the following lines:

      log_dest file /var/log/mosquitto/mosquitto.log

      password_file /etc/mosquitto/password.txt
      allow_anonymous false


      include_dir /etc/mosquitto/conf.d

    • Start the mosquito server, type:
      sudo /etc/init.d/mosquitto start

      Note:
      Mosquitto logging can be found at: /var/log/mosquitto/mosquitto.log

    • Test mosquitto with username and password, open two terminals:

      In terminal 1 subscribe to a topic, type:
      mosquitto_sub -v -t "demo/topic" -u robert -P mysecret

      Note:
      If you try this without username and password, you get this message:
      Connection Refused: not authorised.

      In terminal 2 publish a message to a topic, type:
      mosquitto_pub -t "demo/topic" -m "Hello" -u robert -P mysecret

      Note:
      If you try this without username and password, you get this message:
      Connection Refused: not authorised.
      Error: The connection was refused.


      In terminal 1, you should see:
      demo/topic Hello

  22. Test if the ESP-01S module can publish a simple message to the MQTT broker.
    • Power up the breadboard power supply module.
    • Connect the FTDI adapter to your computer.
    • Start Arduino IDE.
    • Make sure the "Generic 8266 Module" board is selected.
    • Make sure the correct serial port is selected.
    • Set the ESP-01S in flash mode (GPIO0 to ground).
    • Press and release the reset (=push) button.
    • Load sketch:
      dht11-esp01s/sketches/test_mqtt/test_mqtt.ino
    • Make changes to this sketch according to your situation:
      wifi_ssid, wifi_password, mqtt_server, mqtt_port, mqtt_user and mqtt_password.
    • Upload the sketch to the ESP-01S module.
    • Set the ESP-01S in boot mode (GPIO0 floating).
    • Open the Serial Monitor and set the baud rate to 115200.
    • Login to the Raspberry Pi.
    • Open a terminal and subscribe to a topic (defined in the sketch), type:
      mosquitto_sub -v -t "sensor/test" -u robert -P mysecret

    • Each time you press and release the reset (=push) button, the message "Hello World" is published. This message is displayed in the Raspberry Pi terminal.

      test_mqtt.ino output

    • You can also publish a message using the Raspberry Pi terminal and this message is displayed in the Arduino Serial Monitor.
      Open another terminal and publish a message, type:
      mosquitto_pub -t "sensor/test" -m "The sky is blue" -u robert -P mysecret

    • In the Arduino Serial Monitor you will see this message appear.

      test_mqtt.ino output 2

  23. Test if the ESP-01S module can publish DHT11 sensor data to the MQTT broker.
    • Power up the breadboard power supply module.
    • Connect the FTDI adapter to your computer.
    • Start Arduino IDE.
    • Make sure the "Generic 8266 Module" board is selected.
    • Make sure the correct serial port is selected.
    • Set the ESP-01S in flash mode (GPIO0 to ground).
    • Press and release the reset (=push) button.
    • Load sketch:
      dht11-esp01s/sketches/DHT_mqtt/DHT_mqtt.ino
    • Make changes to this sketch according to your situation:
      wifi_ssid, wifi_password, mqtt_server, mqtt_port, mqtt_user and mqtt_password.
    • Upload the sketch to the ESP-01S module
    • Set the ESP-01S in boot mode (GPIO0 floating).
    • Open the Serial Monitor and set the baud rate to 115200
    • Login to the Raspberry Pi
    • The sketch publishes humidity under topic "sensor/humidity", temperature in Celsius under topic "sensor/temperature_celsius" and temperature in Fahrenheit under topic "sensor/temperature_fahrenheit".

      For example, open a terminal and subscribe to the temperature celsius topic, type:
      mosquitto_sub -v -t "sensor/temperature_celsius" -u robert -P mysecret

    • Press and release the reset (=push) button, the DHT11 sensor data are published and the temperature (celsius) data is displayed in the Raspberry Pi terminal.

      dht_mqtt.ino output

  24. Download and install the dht11-esp01s project on the Raspberry Pi:
    • Login to the Raspberry Pi
    • Type: cd ~
    • Type: git clone https://github.com/robertlie/dht11-esp01s.git

      dht11-esp01s git repo cloned

    • Type: cd dht11-esp01s
    • Type: npm install

  25. Test if the ESP-01S module can publish DHT11 sensor data to the MQTT broker and a nodeJS application subscribes to a topic and displays the sensor data.
    • Power up the breadboard power supply module.
    • Connect the FTDI adapter to your computer.
    • Start Arduino IDE.
    • Make sure the "Generic 8266 Module" board is selected.
    • Make sure the correct serial port is selected.
    • Set the ESP-01S in flash mode (GPIO0 to ground).
    • Press and release the reset (=push) button.
    • Load sketch:
      dht11-esp01s/sketches/DHT_mqtt2/DHT_mqtt2.ino

      Note:
      This sketch combines the temperature (Celsius) and humidity to a single message and publish this message to topic "sensor/dht11".

    • Make changes to this sketch according to your situation:
      wifi_ssid, wifi_password, mqtt_server, mqtt_port, mqtt_user and mqtt_password.
    • Upload the sketch to the ESP-01S module.
    • Set the ESP-01S in boot mode (GPIO0 floating).
    • Open the Serial Monitor and set the baud rate to 115200
    • Press and release the reset (=push) button.
      The temperature (Celsius) and humidity are published under topic "sensor/dht11" every 30 seconds (see DHT_mqtt2.ino, MEASUREMENT_TIMEINTERVAL).

      dht_mqtt2.ino output

    • Login to the Raspberry Pi
    • Open a terminal
    • Type: cd ~
    • Type: cd dht11-esp01s
    • Make changes to the dht_mqtt.js according to your situation:
      mqtt_user and mqtt_password.
    • Type: node dht_mqtt.js

      The nodeJS application subscribes to topic "sensor/dht11" and displays the sensor data on the screen.

      dht_mqtt.js output

  26. Test if the ESP-01S module can publish DHT11 sensor data to the MQTT broker and a nodeJS application subscribes to a topic. The sensor data is stored on the Tangle Using Masked Authenticated Messaging.
    • Power up the breadboard power supply module.
    • Connect the FTDI adapter to your computer.
    • Start Arduino IDE.
    • Make sure the "Generic 8266 Module" board is selected.
    • Make sure the correct serial port is selected.
    • Set the ESP-01S in flash mode (GPIO0 to ground).
    • Press and release the reset (=push) button.
    • Load sketch:
      dht11-esp01s/sketches/DHT_mqtt2/DHT_mqtt2.ino

      Note:
      This sketch combines the temperature (Celsius) and humidity to a single message and publish this message to topic "sensor/dht11".

    • Make changes to this sketch according to your situation:
      wifi_ssid, wifi_password, mqtt_server, mqtt_port, mqtt_user and mqtt_password.
    • Upload the sketch to the ESP-01S module.
    • Set the ESP-01S in boot mode (GPIO0 floating).
    • Open the Serial Monitor and set the baud rate to 115200
    • Press and release the reset (=push) button, the temperature (Celsius) and humidity are published under topic "sensor/dht11"

      dht_mqtt2.ino output

    • Login to the Raspberry Pi
    • Open a terminal
    • Type: cd ~
    • Type: cd dht11-esp01s
    • Make changes to the dht_mqtt_mam.js according to your situation:
      mqtt_user and mqtt_password.
    • Type: node dht_mqtt_mam.js

      The nodeJS application subscribes to topic "sensor/dht11" and displays the root, address and sensor data on the screen.

      dht_mqtt_mam.js output

    • Open another terminal and type: node mam_receive.js your_root
      The stored data is displayed.

      mam_receive.js output

  27. For more information, watch this video: