Wireless Network Pressure Sensor(s) using ESP 8266-12 programmed in Micropython
![]() |
| Past Times - no WIFI |
This blog is describing a cheap and reliable way to develop a wireless network of (pressure) sensors based on the application of the ESP 8266 -12 and using standard analog pressure sensors with the output in the range of 0.5 V to 4.5 V
In fact the same approach can be taken regarding other sensors or switches working in the analog world or using a typical protocol as I2C for example.
The ESP 8266-12 is basically a wireless microcomputer with 4 Mb of RAM which can be loaded with a basic operating system and programming language.
It is a lot of excitement regarding this Chinese made chip especially considering the performance / price ratio and the availability of the open source software.
Is not the scope of this blog to present the ESP or even the typical applications or open source software used in the development.
![]() |
| Idea recording |
![]() |
| Idea recording |
![]() |
| First prototype Nov. 2016 |
a="""import sys
from time import sleep
from machine import Pin , PWM
pwm14=PWM(Pin(14)) # pin for pressure level
pwm14.duty(0) # put the pin on 0
P13=Pin(13,Pin.IN,Pin.PULL_UP)# pin for stop
P16=Pin(16,Pin.OUT) # pin for transmitting
P16.low()
P12=Pin(12,Pin.IN,Pin.PULL_UP)# pin for idividual sensor
import network
print('P12=',P12.value())
from array import *
import socket
# String to be sent communicated to the site which makes the request
html = '''<!DOCTYPE html>
while P13.value():
avg_a_tst=sum(a_tst)/len(a_tst) # calculate average value
--------------------------------------------------------------------
print(' Sensor1 Master 192.168.6.100 access point')
#===============================================
ap = network.WLAN(network.AP_IF) # create access-point interface
ap.active(True) # activate the interface
ap.ifconfig(('192.168.6.100', '255.255.255.0', '192.168.6.100', '192.168.6.100'))
ap.config(essid='Sensor1',password='1234567890') # set the ESSID of the access point
print(ap.ifconfig())
#===============================================
#wlan = network.WLAN(network.STA_IF) # create station interface
wlan = network.WLAN(network.STA_IF) # create station interface
print('********Master WIFI network sensor mode OFF**********')
wlan.active(False)
#wlan.ifconfig(('192.168.5.110', '255.255.255.0', '192.168.5.100', '192.168.5.100'))
#wlan.connect('Master','1234567890')
wlan.isconnected()
if P12.value()==0:
wlan = network.WLAN(network.STA_IF) # create station interface
print('********Master WIFI network sensor mode ON**********')
wlan.active(True)
wlan.ifconfig(('192.168.5.110', '255.255.255.0', '192.168.5.100', '192.168.5.100'))
wlan.connect('Master','1234567890')
wlan.isconnected()
#===================================================
a_tst=array('f',[])
from machine import ADC
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
adc = ADC(0) # create ADC object on ADC pin
html0 = '''<!DOCTYPE html>
<html>
<head>
<title>Master Sensor Data </title> </head>
<body> <h1><table border="1"> <tr><th>Reads</th><th>BAR</th></tr> %s </table></h1>
</body>
<meta http-equiv="refresh" content="0.25"/>
</html>
'''
<html><body><h0>
<table border="1"> <tr><th>Reading</th><th>Instant Bar</th><th>Numeric</th></tr> %s </table>
</h0></body>
</html>
'''
NM=3
s = socket.socket()
s.bind(addr)
s.listen(1)
if P13.value()==0:
print(' GPIO13=0 ....Exiting for interactive mode')
sys.exit()
print('listening on', addr)
sleep(0.1)
cl, addr = s.accept()
print('client connected from', addr)
P16.high()
cl_file = cl.makefile('rwb', 0)
while True:
line = cl_file.readline()
print(line)
if not line or line == b'\\r\\n':
break
for k in range(NM):
a_tst.append(adc.read()/1.0)
pwm14.duty(int(adc.read()/10))
rows = ['<tr><td>%s</td><td>%6.3f</td></tr>' % (str(len(a_tst)), ((avg_a_tst*300.0)/1024.0))]
response = html0 % '\\n'.join(rows)
cl.send(response)
rows = ['<tr><td>%s</td><td>%s</td><td>%d</td></tr>' % ((k), (a_tst[k]*300.0/1024.0), (a_tst[k]) ) for k in range(NM)]
response = html % '\\n'.join(rows)
cl.send(response)
x=ap.ifconfig()
print('AP=',x)
cl.send('<h3>')
cl.send('AP ADR='+x[0])
cl.send('</h3>')
#cl.send('<br />')
#cl.send('MSK='+x[1])
#cl.send('<br />')
cl.send('GTW='+x[2])
cl.send('<br /> @ C.R. 2016')
cl.send('<br /> ----------------------------------------------<br />')
cl.close()
a_tst=[]
P16.low()
print(' GPIO13=0 ....Exiting for interactive mode')
"""
#___________Loader
f_main=open('main.py','w')
f_main.write(a)
f_main.close()
print(a)
#_____________End of Template



