11

members

Easy Raspberry Basics: Project 018a Raspberry PI 3 model B board, ESP8266 ESP-12E module - publishing sen

of Acoptex.com in Raspberry Pi 3

Raspberry Basics: Project 018a

Project name: Raspberry PI 3 model B board, ESP8266 ESP-12E module - publishing sensor readings to SQLite Database

Tags: ESP, ESP8266, WI FI module, ESP-12E, Ai Thinker, AI-Cloud, SOC, GPIO, General Purpose Input Output, System On a Chip, IOT, internet of things, FTDI232, FTDI 232, how to upload sketch, LoLin NODEMCU V3, NODEMCU, V3, Development Board with USB-to-Serial Onboard, NodeMCU DevKit, MQTT, DHT 11 21 22, DHT11, DHT21, DHT22 module, temperature, humidity, Raspberry, Raspberry PI 3 model B board, SQlite, database

Attachments: library1library2library4, rasberrypiscript, main.html, arduinosketch

In this project, you need these parts :

1.Raspberry PI 3 model B 1 pc

2. Micro SD card and SD card adapter 1 pc

3. Micro USB power supply (2.1 A, max 2.5 A) 1 pc

4. USB keyboard 1 pc

5. USB mouse 1 pc

6. TV or PC monitor 1 pc

7. ESP8266 ESP-12E module with micro USB cable 1pc

8.Arduino IDE ( you can download it from here  )

9.Jumper cables F-M, M-M

10. Resistor 1 pc (1pc - 10 KOhm)

11. DHT11 21 22 Humidity and temperature module 1pc

12. Breadboard 1 pc

13. HDMI cable 1 pc

General

We will learn how to create a standalone web server with a Raspberry PI 3 model B  board that displays temperature and humidity readings with a DHT11 sensor that are stored in an SQLite database.

In order to create the web server we will be using a Python microframework called Flask

MQTT Protocol

In this project we are going to establish a communication between a Raspberry PI 3 model B running the Node-RED software and an ESP8266 ESP-12E module using MQTT.

MQTT stands for MQ Telemetry Transport and it is a nice lightweight publish and subscribe system where you can publish and receive messages as a client. It is a simple messaging protocol, designed for constrained devices and with low-bandwidth. 

Mosquitto Broker

The broker is primarily responsible for receiving all messages, filtering the messages, decide who is interested in it and then publishing the message to all subscribed clients in MQTT.

There are several brokers you can use. In this project we’re going to use the Mosquitto Broker which needs to be installed on Raspberry PI 3 model B.

Understanding the Raspberry PI 3 model B

The Raspberry Pi 3 is the third-generation Raspberry Pi. It replaced the Raspberry Pi 2 Model B in February 2016.

Specification:

  • Quad Core 1.2GHz Broadcom BCM2837 64bit CPU
  • 1GB RAM
  • BCM43438 wireless LAN and Bluetooth Low Energy (BLE) on board
  • 40-pin extended GPIO
  • 4 USB 2 ports
  • 4 Pole stereo output and composite video port
  • Full size HDMI
  • CSI camera port for connecting a Raspberry Pi camera
  • DSI display port for connecting a Raspberry Pi touchscreen display
  • Micro SD port for loading your operating system and storing data
  • Upgraded switched Micro USB power source up to 2.5A

Understanding the ESP8266 ESP-12E WI FI module (LoLin NODEMCU V3)

You can read more about it here.

Understanding the DHT 11 21 22 module

You can read more about it here.

Signals and connections of the DHT 11 21 22 module

There are two different versions of the DHT11 you might come across. One type has four pins, and the other type has three pins and is mounted to a small PCB. The PCB mounted version is nice because it includes a surface mounted 10K Ohm pull up resistor for the signal line. Here are the pin outs for both versions:

Signals and connections of the Raspberry PI 3 model B


Signals and connections of the ESP8266 ESP-12E WI FI module (LoLin NODEMCU V3)

TX - transmit pin. GPIO pin

RX  - receive pin.  GPIO pin

3V3 (or 3V or 3.3V) - power supply pin (3-3.6V). 

GND ( or G) - ground pin.

RST - reset pin. Keep it on high (3.3V) for normal operation. Put it on 0V to reset the chip.

EN - Chip enable. Keep it on high (3.3V) for normal operation.

Vin - External power supply 5VDC.

D0-D8 - GPIO (General Purpose Input Output) pins 

D5-D8 - SPI interface

D1-D2– I²C/TWI Interface

SC (or CMD) - (Chip Select) - the pin that the master can use to enable and disable specific devices. GPIO pin

SO (or SDO) - Master In Slave Out (MISO) - SPI communication. The Slave line for sending data to the master. GPIO pin

SK (or CLK) - SCK (Serial Clock) - SPI communication.The clock pulses which synchronize data transmission generated by the master. GPIO pin

S1 (or SD1) - Master Out/Slave In (MOSI). SPI communication. The Master line for sending data to the peripherals. GPIO pin

S2 (or SD2) - GPIO pin

S3 (or SD3) - GPIO pin

VU (or VUSB) - external power 5VDC.

A0 - ADC output.

RSV - reserved

Wiring


Step by Step instruction

1. Preparations

  1. Insert your micro SD card with Raspbian OS to Raspberry Pi 3 board.
  2. Connect the TV or PC monitor Display port (HDMI Port) to your Raspberry PI 3 board HDMI Port (HDMI cable required).
  3. Make sure that your monitor or TV is turned on, and that you have selected the right input (e.g. HDMI 1,  etc).
  4. Plug in your USB mouse and USB keyboard to Raspberry PI 3 board USB ports.
  5. Get connected. Connect an Ethernet cable to 10/100 LAN port of Raspberry Pi 3 or plug in WiFi adapter (see the list of supported here) to USB port of Raspberry Pi 3.
  6. Connect Micro USB power supply to Raspberry PI 3 board micro USB input.
  7. The Raspberry PI desktop will start up.
  8. Install Mosquitto Broker on your Raspberry PI.
  9. Type the command to run Mosquitto Broker on the background in the Terminal window: mosquitto -d

2. Installing Flask

  1. We’re going to use a Python microframework called Flask to turn the Raspberry Pi into web server.
    To install Flask, you’ll need to have pip installed. Run the following commands to update your Pi and install pip:
    We’re going to use a Python microframework called Flask to turn the Raspberry Pi into web server.
    To install Flask, you’ll need to have pip installed. Run the following commands to update your Pi and install pip: 
  2. sudo apt-get update
  3. sudo apt-get upgrade
  4. sudo apt-get install python-pip python-flask git-core

  5. Use pip to install Flask by typing this command: sudo pip install flask

3. Writing Python script

  1. We make a new folder first by typing these commands:
  2. mkdir web-server
  3. cd web-server
  4. We maake a new file called test.py by typing this command: nano test.py
  5. Open rasberrypiscript and copy content to test.py file.
  6. Press Ctrl+X buttons, press Y button and press Enter button to save the file.

4. Install SQLite and create database

  1. You can find how to do it here.
  2. When the SQLite installation done, the SQLite libraries are supplied with an SQLite shell. Use this command to invoke the shell and create a database: sqlite3 sensorvalues.db
  3. The file sensorvalues.db created now.
  4. We will create a simple table with 6 columns that could be used for temperature and humidity logging application in different parts of a house. Use these commands:
  5. BEGIN;
  6. CREATE TABLE dhtreadings(id INTEGER PRIMARY KEY AUTOINCREMENT, temperature NUMERIC, humidity NUMERIC, currentdate DATE, currentime TIME, device TEXT);
  7. COMMIT;
  8. You can see all tables available with the command: .tables
  9. You can see the fullschema of the tables when you enter the command: .fullschema
  10. To quit from the SQLite shell use the .quit command.

5. Installing paho mqtt

  1. Type the command: sudo pip instal paho-mqtt

6. Creating the HTML File

Keeping HTML tags separated from your Python script is how you keep your project organized. Flask uses a template engine called Jinja2 that you can use to send dynamic data from your Python script to your HTML file.

  1. Create a new folder called templates by typing these commands: 
  2. mkdir templates
  3. cd templates
  4. Create a new file called main.html by typing this command: nano main.html
  5. Copy and paste the folowing text - main.html.
  6. Press Ctrl+X buttons, press button and press Enter button to save the file.
  7. Type the command to find your Raspberry Pi IP address: ifconfig

  8. As you can see we have 192.168.0.121. You will need this IP address later in this project.

7. Adding ESP8266 platform to Arduino IDE

The Arduino environment has to be set up to make it compatible with the ESP8266 ESP-12E module. 

We are using PC with Windows 10 OS.

1. Installation of  ESP8266 add-on in Arduino IDE on Windows OS.

  1. Download and install the Arduino IDE version 1.6.13 Windows Installer. You can download it here.
  2. Open Arduino IDE. 
  3. Open the Preferences window from the Arduino IDE. Go to File -> Preferences.
  4. Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field and click the “OK” button. If you already have a URL in there, and want to keep it, you can separate multiple URLs by placing a comma between them. (Arduino 1.6.5 added an expanded text box, separate links in here by line.)
  5. Open Boards manager. Go to Tools -> Board -> Boards Manager…

  6. There should be a couple new entries in addition to the standard Arduino boards. Look for esp8266. or scroll down to the ESP8266 entry (usually at the bottom). Select the ESP8266 entry. When you click it an install option will appear. Select the latest version and click install
  7. The board definitions and tools for the ESP8266 include a whole new set of gcc, g++, and other reasonably large, compiled binaries, so it may take a few minutes to download and install (the archived file is ~110MB). Once the installation has completed, an Arduino-blue “INSTALLED” will appear next to the entry.

8. Uploading sketch to ESP8266 ESP-12E development board

 If you’re using an ESP-12E NodeMCU Kit, uploading the sketch is very simple, since it has built-in programmer. 

Before use ESP8266 ESP-12E WI FI module (LoLin NODEMCU V3), you need to download the manufacture's driver (CH340)  for this chip and install it in your PC. Here is the link. See the description of driver installation package below: CH340 / CH341 USB to serial WINDOWS driver installation package that supports 32/64 bit Windows 10 / 8.1 / 8/7 / VISTA / XP, SERVER 2016/2012/2008/2003, 2000 / ME / 98, through Microsoft digital signature authentication, support USB to 3-wire and 9-wire serial port, with the product release To the end user. Applicable scope: CH340G, CH340C, CH340B, CH340E, CH340T, CH340R, CH341A, CH341T, CH341H chips.

If you have CP2102 chip then  you need to download the manufacture's driver for this chip and install it in your PC. Driver for MacWindowsLinux or more.. 

  1. Do wiring.
  2. Open Arduino IDE.
  3. Plug your ESP8266 ESP-12E module into your PC USB port.
  4. Choose your NodeMCU board. Go to Tools -> Board -> NodeMCU 1.0 (ESP-12E Module)
  5. Select the correct com port.
  6. Open and edit the arduinosketch with your own SSID, password and Raspberry IP address.
  7. Verify and upload it to your ESP8266 ESP-12E module. If everything went as expected, you should see a “Done uploading” message. 
  8. Press RST button on your ESP8266 ESP-12E module to reset it.
  9. Open the Serial Monitor at a baud rate of 115200. You will see the DHT sensor readings.

9. Launching the web server

  1. To launch your Raspberry Pi web server on your Raspberry PI move to the folder that contains the file test.py. We were in templates folder so type the command in Terminal window: cd ..
  2. Then run the following command: sudo python test.py
  3. Your web server will start on port 8181.
  4. Go to any Web browser on your PC.
  5. Locate the address bar at the top and type: http://your_Raspberry_Pi_IP_address:8181 (in our case that is http://192.168.1.98:8181) then press Enter button. This will display the table with DHT sensor readings:
  6. Congrats!!! You project is now completed.

 

Summary

We have learnt how to create a standalone web server with a Raspberry PI 3 model B that displays temperature and humidity readings with a DHT11 sensor that are stored in an SQLite database.

Libraries

  • All libraries attached on the begining of this project description.
  • PubSubClient library included. Download, unzip  and add to libraries in our PC, for example C:\Users\toshiba\Documents\Arduino\libraries. This link you can find in Preferences of Adruino IDE program which installed in your PC. You can read more about it here. Provides a client for doing simple publish/subscribe messaging with a server that supports MQTT (basically allows your ESP8266 ESP-12E module to talk with Node-RED).
  • Adafruit_Sensor library included. Download, unzip  and add to libraries in our PC, for example C:\Users\toshiba\Documents\Arduino\libraries. This link you can find in Preferences of Adruino IDE program which installed in your PC. You can read more about it here.
  • DHT-sensor library included. Download, unzip  and add to libraries in our PC, for example C:\Users\toshiba\Documents\Arduino\libraries. This link you can find in Preferences of Adruino IDE program which installed in your PC.  OR You can install it in Arduino IDE too: select Sketch-> Include library->Manage your libraries->type DHT in Filter your search line and you will see DHT sensor library by Adafruit->More info->select version->install. If it will be problem with compilation - remove 2 files from the library - DHT_U.cpp and DHT_U.h.

Sketch

  • See attachments on the begining of this project


Other projects of Acoptex.com
Medium Basics: Project 083w Sipeed Maixduino board - Using PlatformIO IDE of Acoptex.com in Sipeed Maixduino 08-08-2019
Medium Basics: Project 083e Sipeed Maixduino board - Uploading MaixPy of Acoptex.com in Sipeed Maixduino 04-08-2019
Medium Basics: Project 083f Sipeed Maixduino board - Using MycroPython of Acoptex.com in Sipeed Maixduino 04-08-2019

Published at 21-11-2018
Viewed: 1984 times