Skip to content

MicroPython example using a DHT12, AM2320 and a Nokia 5110 with an ESP8266

License

Notifications You must be signed in to change notification settings

mcauser/MicroPython-ESP8266-DHT-Nokia-5110

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MicroPython ESP8266 DHT Nokia 5110

WeMos D1 mini + AM2320 + DHT12 + Nokia 5110 (PCD8544)

DHT Nokia

Parts

Pinouts

The DHT12 and AM2320 sensors have the same pins:

  • 1 VDD
  • 2 SDA
  • 3 GND
  • 4 SCL

(left to right, grill facing you)

Using D1 + D2 for I2C on the WeMos D1 Mini.

  • D1 = GPIO5 = SCL
  • D2 = GPIO4 = SDA

Installation

Start by cloning this repo.

$ git clone git@github.com:mcauser/MicroPython-ESP8266-DHT-Nokia-5110.git

Install MicroPython on your ESP8266 device

I am using a WeMos D1 Mini, but you can use any ESP8266 device.

The WeMos D1 Mini features an ESP-12F with 4MB flash.

If you already have MicroPython v1.8.x installed on your device, you can skip down to installing the scripts.

Install Python

If you are using brew, installing Python 2.7 is a one-liner. Install pip to manage python packages.

$ brew install python
$ pip install --upgrade pip setuptools

You can use pip to install Python packages. eg.

$ pip search SomePackage
$ pip install SomePackage
$ pip install --upgrade SomePackage
$ pip uninstall SomePackage

Install esptool with pip

$ pip install esptool

Download latest MicroPython firmware

Open http://micropython.org/download/#esp8266

Download the latest firmware, currently v1.8.3-61

$ wget http://micropython.org/resources/firmware/esp8266-20160827-v1.8.3-61-g531217a.bin

Flash firmware with esptool

The WeMos D1 mini uses a CH340G USB-TTL driver, which shows up on my MacBook Pro as either /dev/tty.wchusbserial1410 or /dev/tty.wchusbserial1420.

If you are using another device, you may find yours to be /dev/ttyUSB0. Find yours with:

$ ls /dev/tty*

When deploying new firmware, it's best to completely erase all previous versions. The first run executes suspiciously quick, so I run it twice.

$ esptool.py -p /dev/tty.wchusbserial1420 erase_flash
esptool.py v1.1
Connecting...
Erasing flash (this may take a while)...

Upload the new MicroPython firmware.

$ esptool.py -p /dev/tty.wchusbserial1420 write_flash -fm dio -fs 32m 0 esp8266-20160827-v1.8.3-61-g531217a.bin
esptool.py v1.1
Connecting...
Running Cesanta flasher stub...
Flash params set to 0x0240
Writing 532480 @ 0x0... 43008 (8 %)
...
Writing 532480 @ 0x0... 316416 (59 %)
...
Writing 532480 @ 0x0... 532480 (100 %)
Wrote 532480 bytes at 0x0 in 46.0 seconds (92.6 kbit/s)...
Leaving...

More info in the MicroPython docs on flashing the firmware.

Verify firmware

$ screen /dev/tty.wchusbserial1420 115200

Click your hardware Reset button or Control+D in screen.

(lots of funny characters) ets_task(40100390, 3, 3fff6300, 4)
could not open file 'main.py' for reading

MicroPython v1.8.3-61-g531217a on 2016-08-27; ESP module with ESP8266
Type "help()" for more information.
>>>

Check the firmware md5 matches. You should see True. If not, erase_flash and write_flash again, or try different write_flash arguments after reading the esptool readme.

>>> import esp
>>> esp.check_fw()
size: 531032
md5: f54e36598b104f8c5dec883181080aaa
True

To exit screen run: Control+A then Control+\.

Install scripts with WebREPL

I was receiving MemoryErrors when trying to upload the scripts via REPL paste mode.

Using WebREPL Send a file, I'm able to upload the scripts successfully, but with WebREPL enabled, not enough ram to run them.

Reboot with WebREPL disabled and the scripts has enough resources to run.

Configure Access Point

The default settings will give your device the ip 192.168.4.1

>>> import network
>>> ap_if = network.WLAN(network.AP_IF)
>>> ap_if.active(True)

Yep, it's 192.168.4.1. (ip,netmask,gateway,dns)

>>> ap_if.ifconfig()
('192.168.4.1', '255.255.255.0', '192.168.4.1', '208.67.222.222')

Start WebREPL

>>> import webrepl
>>> webrepl.start()
WebREPL daemon started on ws://192.168.4.1:8266
Started webrepl in setup mode

Connect to Access Point

Before leaving your working internet connection, open the WebREPL http://micropython.org/webrepl/ but don't connect yet.

Join the MicroPython-xxxxxx network. Password is micropythoN. Uppercase N is not a typo.

You should see something like this in the REPL:

>>> add 1
aid 1
station: 78:31:c1:bb:cc:dd join, AID = 1

Switch back to WebREPL tab in your browser and click Connect.

It will ask you to set a password.

Welcome to MicroPython!
Welcome to MicroPython WebREPL!

This is the first time you connect to WebREPL, so please set a password
to use for the following WebREPL sessions. Once you enter the password
twice, your board will reboot with WebREPL running in active mode. On
some boards, you may need to press reset button or reconnect power.

New password:

Enter a password twice and it will save the password and reboot.

New password: ********
Confirm password: ********
Password successfully set, restarting...
Disconnected

WebREPL will be disabled when it reboots, so you will need to start it again.

Later, to make it always on, you can add the start command to boot.py. In this case, we do not want it started by default.

Switch back to your terminal and start WebREPL again

>>> import webrepl
>>> webrepl.start()
WebREPL daemon started on ws://192.168.4.1:8266
Started webrepl in normal mode

Notice this time, it says normal mode. FYI - the WebREPL password you entered is saved in port_config.py in the root.

>>> import os
>>> os.listdir()
['boot.py', 'port_config.py']

Click Connect and enter your password.

Welcome to MicroPython!
Password:
WebREPL connected
>>>

Upload files with WebREPL

Under Send a file on the right, choose the file pcd8544.py. It will list the file as pcd8544.py - 9787 bytes.

Click Send to device. At the bottom it should say Sent pcd8544.py, 9787 bytes.

Repeat for the files dht12.py, dht12_nokia.py, am2320.py and am2320_nokia.py.

Click Disconnect. Click your hardware Reset button, or use machine.reset().

>>> import machine
>>> machine.reset()

After rebooting, if you do not need the Access Point anymore, you can disable it with:

>>> import network
>>> ap_if = network.WLAN(network.AP_IF)
>>> ap_if.active(False)
station: 78:31:c1:bb:cc:dd leave, AID = 1
rm 1
bcn 0
del if1
usl
mode : null

Setup and test Nokia 5110 display

Connections:

WeMos D1 Mini (ESP8266) Nokia 5110 PCD8544 LCD Description
D3 (GPIO0) 0 RST Output from ESP to reset display
D4 (GPIO2) 1 CE Output from ESP to chip select/enable display
D8 (GPIO15) 2 DC Output from display data/command to ESP
D7 (GPIO13) 3 Din Output from ESP SPI MOSI to display data input
D5 (GPIO14) 4 Clk Output from ESP SPI clock
3V3 5 Vcc 3.3V from ESP to display
D6 (GPIO12) 6 BL 3.3V to turn backlight on, or PWM
G 7 Gnd Ground

Test the display:

>>> from machine import Pin, SPI
>>> import time
>>> import pcd8544

>>> spi = SPI(1, baudrate=80000000, polarity=0, phase=0)

>>> cs = Pin(2)
>>> dc = Pin(15)
>>> rst = Pin(0)

>>> bl = Pin(12, Pin.OUT, value=1)
>>> lcd = pcd8544.PCD8544(spi, cs, dc, rst)

Switch off the backlight:

>>> bl.value(0)

Switch on the backlight:

>>> bl.value(1)

Use a framebuffer to store the 4032 pixels (84x48):

>>> import framebuf
>>> buffer = bytearray((lcd.height // 8) * lcd.width)
>>> framebuf = framebuf.FrameBuffer1(buffer, lcd.width, lcd.height)

Light every pixel:

>>> framebuf.fill(1)
>>> lcd.data(buffer)

Clear screen:

>>> framebuf.fill(0)
>>> lcd.data(buffer)

Print Hello, World! using the 8x8 font:

>>> framebuf.text("Hello,", 0, 0, 1)
>>> framebuf.text("World!", 0, 9, 1)
>>> lcd.data(buffer)

If all this works, let's try the sensors.

Test

Setup and test DHT12

DHT12

Sensor pinout:

  • 1 VDD
  • 2 SDA
  • 3 GND
  • 4 SCL

Connections:

WeMos D1 Mini (ESP8266) DHT12 Description
3V3 1 VDD 3.3V
D2 (GPIO4) 2 SDA Serial data
D1 (GPIO5) 3 SCL Serial clock
G 4 GND Ground

Connect two pull-up resistors for I2C between 3V3-SDA and 3V3-SCL.

Test the DHT12 sensor:

>>> from machine import I2C, Pin
>>> import dht12

>>> i2c = I2C(scl=Pin(5), sda=Pin(4), freq=20000)
>>> i2c.scan()

You should see sensor at [92]

dht = DHT12(i2c)
dht.measure()
dht.temperature()
dht.humidity()

Display the temperature and humidity on the Nokia 5110 display, updated every 4 seconds:

>>> import dht12_nokia

Setup and test AM2320

AM2320

Sensor pinout:

  • 1 VDD
  • 2 SDA
  • 3 GND
  • 4 SCL

Connections:

WeMos D1 Mini (ESP8266) AM2320 Description
3V3 1 VDD 3.3V
D2 (GPIO4) 2 SDA Serial data
D1 (GPIO5) 3 SCL Serial clock
G 4 GND Ground

Connect two pull-up resistors for I2C between 3V3-SDA and 3V3-SCL.

Test the AM2320 sensor:

>>> from machine import I2C, Pin
>>> import am2320

>>> i2c = I2C(scl=Pin(5), sda=Pin(4), freq=20000)
>>> i2c.scan()

You should see sensor at [92]

>>> dht = AM2320(i2c)
>>> dht.measure()
>>> dht.temperature()
>>> dht.humidity()

Display the temperature and humidity on the Nokia 5110 display, updated every 4 seconds:

>>> import am2320_nokia

Links

License

Licensed under the MIT License.

About

MicroPython example using a DHT12, AM2320 and a Nokia 5110 with an ESP8266

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages