Categories

Follow

ESP8266 and PIR example in Micropython

In this example we look at a PIR example in Micropython for an ESP8266. Once again we use uPyCraft and again we use Wemos shields.

Lets remind ourselves about the PIR

A passive infrared sensor (PIR sensor) is an electronic sensor that measures infrared (IR) light radiating from objects in its field of view. They are most often used in PIR-based motion detectors. A PIR-based motion detector is used to sense movement of people, animals, or other objects. They are commonly used in burglar alarms and automatically-activated lighting systems. They are commonly called simply “PIR”, or sometimes “PID”, for “passive infrared detector”.

If you want to read more basics – https://en.wikipedia.org/wiki/Passive_infrared_sensor

The shield uses D3 so that will be unavailable for any other components or shields you may want to use – The PIR is an AS312

 

Requirements

Lets take a look a the shields and boards that are required for this example

 

Image Summary
The Wemos mini – ESP8266 based board, it comes with various headers. This is the beauty of it you can create stackable projects with the board and pin compatible shields
This is simply a base, you plug the Wemos Mini into one side and you can plug a shield or shields into the other side

 

Parts List

I connect the Wemos Mini to the dual base and then put the DS18B20 shield along side this, you can connect the Wemos DS18B20 shield directly to the Wemos Mini if you want.

Name Link
Wemos Mini D1 mini – Mini NodeMcu 4M bytes Lua WIFI Internet of Things development board based ESP8266 by WeMos
Wemos Base Tripler Base V1.0.0 Shield for WeMos D1 Mini
Wemos PIR shield PIR shield

Code

Create a new file called ldr1.py and import it into uPyCraft

A fairly simple idea here, we read in the state of the pin that the PIR uses

from machine import Pin
import time
 
ldr = Pin(0, Pin.IN)     # create input pin on GPIO2
 
while True:
	print(ldr.value())       # get value, 0 or 1
	time.sleep(1)

and a slight adaptation to create a more readable output

 

from machine import Pin
import time
 
ldr = Pin(0, Pin.IN)     # create input pin on GPIO2
 
while True:
	if ldr.value():
            print('OBJECT DETECTED')
        else:
            print('ALL CLEAR')
	time.sleep(1)

 

Output

You should see something like this

exec(open(‘ldr2.py’).read(),globals())
ALL CLEAR
ALL CLEAR
ALL CLEAR
OBJECT DETECTED
OBJECT DETECTED
OBJECT DETECTED
OBJECT DETECTED
OBJECT DETECTED
ALL CLEAR
ALL CLEAR
ALL CLEAR
OBJECT DETECTED
OBJECT DETECTED
OBJECT DETECTED

 

Links

https://datasheet.lcsc.com/szlcsc/Nanyang-Senba-Optical-Elec-AS312_C90465.pdf

ESP8266 and DS18B20 sensor and MIcropython example

In this example we look at a DS18b20 example in Micropython for an ESP8266. Once again we use uPyCraft and again we use Wemos shields.

Lets remind ourselves about the DS18B20

The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements and has an alarm function with nonvolatile user-programmable upper and lower trigger points. The DS18B20 communicates over a 1-Wire bus that by definition requires only one data line (and ground) for communication with a central microprocessor. In addition, the DS18B20 can derive power directly from the data line (“parasite power”), eliminating the need for an external power supply.

Each DS18B20 has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus. Thus, it is simple to use one microprocessor to control many DS18B20s distributed over a large area. Applications that can benefit from this feature include HVAC environmental controls, temperature monitoring systems inside buildings, equipment, or machinery, and process monitoring and control systems

Requirements

Lets take a look a the shields and boards that are required for this example

 

Image Summary
The Wemos mini – ESP8266 based board, it comes with various headers. This is the beauty of it you can create stackable projects with the board and pin compatible shields
This is not an official Wemos shield – this uses a DS18B20 and is connected to D2. This does mean that you can use another I2C device
This is simply a base, you plug the Wemos Mini into one side and you can plug a shield or shields into the other side

 

Parts List

I connect the Wemos Mini to the dual base and then put the DS18B20 shield along side this, you can connect the Wemos DS18B20 shield directly to the Wemos Mini if you want.

Name Link
Wemos Mini D1 mini – Mini NodeMcu 4M bytes Lua WIFI Internet of Things development board based ESP8266 by WeMos
Wemos Base Tripler Base V1.0.0 Shield for WeMos D1 Mini
Wemos DS18B20 Temperature Sensor Shield DS18B20 Temperature Sensor Shield Wemos D1 Mini D1 Mini Pro ESP NodeMCU

Code

Create a new file called ds.py and import it into uPyCraft

from machine import Pin
import time, ds18x20
import onewire
ow = onewire.OneWire(Pin(4)) # create a OneWire bus on GPIO12
 
ds = ds18x20.DS18X20(ow)
 
roms = ds.scan()
ds.convert_temp()
time.sleep_ms(750)
 
for rom in roms:
    print(ds.read_temp(rom))

 

Output

You should see something like this

Ready to download this file,please wait!
..
download ok
exec(open(‘ds.py’).read(),globals())
26.4375
>>>

Links

 

Wemos Mini and various shields ebook

As you may know looking at this web site we are big fans of the Wemos mini and the various shields that you can buy for this hardware. We have decided to write an ebook and publish it on the Kindle store and of course any money recieved through commision will go towards buying new modules, sensors and shields. We also want to design our own shields which this would help fund.

There will be descriptions of each chapter, code examples (which is available via a github link in the book) and expected output. Here are the contents of the ebook so you can see what we have added

Contents 2
About the ESP8266 4
The Wemos Mini and shields 5
Setting up the Arduino IDE 6
Basic examples 7
Flash an LED example 7
Basic Wifi example 8
I2Scanner example 9
Basic Webserver example 11
Read and write to the eeprom 16
ESP specific APIs 18
A look at SHA-1 20
The ticker library 21
Sampling analog data 22
Control a Wemos using an application on your PC 23
A look at SPIFFS 28
Setup your ESP8266 as an Action point 32
DHT Shield example 33
The 1 button shield 36
WS2812B RGB Shield 37
OLED Shield 40
Barometric Pressure Shield 43
TFT 2.4 Touch Shield 46
BMP180 Shield 55
DS18B20 Shield 57
PIR Shield 60
Micro SD Card Shield 62
IR Controller Shield 64
Ambient light Shield (BH1750) 67
DHT Pro Shield 69
RGB LED Shield 71
SHT30 Shield 74
TFT 1.4 Shield 76
Matrix LED Shield 84
Buzzer Shield 87
Relay Shield 90
Other Shields 91
DHT22 readings on an OLED display simple project 93
OLED shield bitcoin ticker example 96
PIR shield warning example 100
BMP180 versus SHt30 temperature comparison 101
Save sensor values to an SD card 102
Conclusion 105

We receive a percentage of the profits from Amazon which we reinvest into this site with regards domain name renewal, web site hosting and purchasing any parts we use – we don’t get any samples or freebies from any companies (but live in hope).

So please support the site by purchasing the ebook from the following links

 

Site Link
Amazon US Buy here
Amazon UK Buy here
Amazon DE Buy here
Amazon FR Buy here
Amazon ES Buy here
Amazon IT Buy here
Amazon NL Buy here
Amazon JP Buy here
Amazon BR Buy here
Amazon CA Buy here
Amazon MX Buy here
Amazon AU Buy here
Amazon IN Buy here

 

ESP8266 RGB LED examples in Micropython

In this example we look at some WS2812B RGB LEDs example in Micropython for an ESP8266. Once again we use uPyCraft and again we use Wemos shields.

WS2812B is a intelligent control LED light source that the control circuit and RGB chip are integrated in a package of 5050 components. It internal include intelligent digital port data latch and signal reshaping amplification drive circuit. Also include a precision internal oscillator and a 12V voltage programmable constant current control part, effectively ensuring the pixel point light color height consistent.

The data transfer protocol use single NZR communication mode. After the pixel power-on reset, the DIN port receive data from controller, the first pixel collect initial 24bit data then sent to the internal data latch, the other data which reshaping by the internal signal reshaping amplification circuit sent to the next cascade pixel through the DO port. After transmission for each pixel,the signal to reduce 24bit. pixel adopt auto reshaping transmit technology, making the pixel cascade number is not limited the signal transmission, only depend on the speed of signal transmission.

LED with low driving voltage, environmental protection and energy saving, high brightness, scattering angle is large, good consistency, low power, long life and other advantages. The control chip integrated in LED above becoming more simple circuit, small volume, convenient installation

Requirements

Lets take a look a the shields and boards that are required for this example

 

Image Summary
The Wemos mini – ESP8266 based board, it comes with various headers. This is the beauty of it you can create stackable projects with the board and pin compatible shields
WS2812 shield – intelligent control RGB (16777216 colors) LED light source shield.
This is simply a base, you plug the Wemos Mini into one side and you can plug a shield or shields into the other side

 

Parts List

I connect the Wemos Mini to the dual base and then put the RGB LED shield along side this, you can connect the Wemos RGB LED shield directly to the Wemos Mini if you want.

Name Link
Wemos Mini D1 mini – Mini NodeMcu 4M bytes Lua WIFI Internet of Things development board based ESP8266 by WeMos
Wemos Base Tripler Base V1.0.0 Shield for WeMos D1 Mini
Wemos RGB LED shield For WeMos D1 MINI WS2812B RGB Full Color Shield Board

Code

Example 1

from machine import Pin
import neopixel
 
pixels = neopixel.NeoPixel(Pin(4, Pin.OUT), 1)
pixels[0] = (0xff, 0x00, 0x22)
pixels.write()

Example 2

from machine import Pin
import neopixel
import time
 
pixels = neopixel.NeoPixel(Pin(4, Pin.OUT), 1)
 
while True:
	pixels[0] = (0xff, 0x00, 0x00)
	pixels.write()
	time.sleep(1)
	pixels[0] = (0x00, 0xff, 0x00)
	pixels.write()
	time.sleep(1)
	pixels[0] = (0x00, 0x00, 0xff)
	pixels.write()
	time.sleep(1)

Example 3

from machine import Pin
import neopixel
import time
 
pixels = neopixel.NeoPixel(Pin(4, Pin.OUT), 1)
 
while True:
	pixels[0] = (0xff, 0x00, 0x00)
	pixels.write()
	time.sleep(1)
	pixels[0] = (0x00, 0xff, 0x00)
	pixels.write()
	time.sleep(1)
	pixels[0] = (0x00, 0x00, 0xff)
	pixels.write()
	time.sleep(1)
	pixels[0] = (0xff, 0xff, 0x00)
	pixels.write()
	time.sleep(1)
	pixels[0] = (0x00, 0xff, 0xff)
	pixels.write()
	time.sleep(1)
	pixels[0] = (0xff, 0x00, 0xff)
	pixels.write()
	time.sleep(1)
	pixels[0] = (0xff, 0xff, 0xff)
	pixels.write()
	time.sleep(1)

Links