Tuesday, December 27, 2016

ESP8266 Environmental Station

ESP8266 Environmental Station


Description:
  Using ESP 8266 12 broadcast environmental data packed in a html self refreshing format to a computer or a mobile device.
  The environmental station will run in an independent mode or being part of a larger network. Is not intended to be used as I.O.T. device for obvious reasons.
Basic configuration
  The main functions which will be available :
  • Temperature data (DTH-11)
  • Humidity Data (DTH-11)  
  • Atmospheric pressure data (BP180 -I2C)
  • I.M.U data (MPU6050-I2C)
  • Temperature Data (LM75-I2C)
  • Light level (?)
  The DHT sensors family:



DHT11 ( http://adafru.it/386 )

  • Ultra low cost
  • 3 to 5V power and I/O
  • 2.5 mA max current use during conversion (while requesting data)
  • Good for 20-80% humidity readings with 5% accuracy
  • Good for 0-50°C temperature readings ±2°C accuracy
  • No more than 1 Hz sampling rate (once every second)
  • Body size 15.5 mm x 12 mm x 5.5 mm
  • 4 pins with 0.1" spacing


DHT22 ( http://adafru.it/385 )

  • Low cost
  • 3 to 5V power and I/O
  • 2.5 mA max current use during conversion (while requesting data)
  • Good for 0-100% humidity readings with 2-5% accuracy
  • Good for -40 to 125°C temperature readings ±0.5°C accuracy
  • No more than 0.5 Hz sampling rate (once every 2 seconds)
  • Body size 15.1 mm x 25 mm x 7.7 mm
  • 4 pins with 0.1" spacing


----------------Code---DHT 11 + LM75 I2C -----------------
a="""
gc.enable()
from os import *
from time import sleep
from machine import Pin, I2C
import socket
import network
import dht
d = dht.DHT11(Pin(13))

#======Init Section

i2c = I2C(scl=Pin(4),sda=Pin(5) , freq=100000)
address=0X48
P12=Pin(12,Pin.IN,Pin.PULL_UP)
#P13=Pin(12,Pin.IN,Pin.PULL_UP)
P16=Pin(16,Pin.OUT)
P16.high()

print('Weather acceesss point 192.168.50.100')

ap = network.WLAN(network.AP_IF)
ap.active(True) 
ap.ifconfig(('192.168.50.100', '255.255.255.0', '192.168.50.100', '8.8.8.8'))
#ap.config(essid='Basemsnt_192.168.50.100',password='1234567890')
ap.config(authmode=0)
ap.config(essid='Basement_192.168.50.100')
print(ap.ifconfig())

#Functions I2C=========

def read_word(adr):
    tst=True
     
    while tst:
      try:
        sleep(0.1)
        high =i2c.readfrom_mem(address,adr,1)
        h=str(high)
        ih=int('0X'+h[4:6])
        tst=False
      except:
        tst=True
      
    tst=True
    
    while tst:
      try:
        sleep(0.1)
        low = i2c.readfrom_mem(address,adr+1,1)
        l=str(low)
        il=int('0X'+l[4:6])
        tst=False
      except:
        tst=True           
    return ih,il 
#Read Temp==========================
def TC ():
    T=read_word(0)
    TT=(T[0]+T[1]*256)&0xFFFF
    TT = ((TT << 8) & 0xFF00) + (TT >> 8)
    return (TT / 32.0) / 8.0
#==================================
print('******** WIFI network  sensor mode ON**********')
wlan = network.WLAN(network.STA_IF)
wlan.active(False)
#wlan.ifconfig(('192.168.1.200', '255.255.255.0', '192.168.1.1', '192.168.1.1'))
#wlan.connect('C_CA','1234567890') 
#wlan.isconnected()
#==========
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
#===========
html0 = '''<!DOCTYPE html> 
<html>
    <head>    
     <title> Room Data </title> 
     <meta http-equiv="refresh" content="1"/> 
     </head>
    <body> <h1> Room Data Stream
    <table border="1"> <tr><th>T-C </th><th>Humidity</th><th>Alt T-C</th></tr> %s </table></h1>
'''
#==========
s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)

while P12.value()!=0:

    d.measure()
    #sleep(0.01)
    cl, addr = s.accept()
    print('client connected from', addr)
    P16.low()
    cl_file = cl.makefile('rwb', 0)
    while True:
        line = cl_file.readline()
        #print(line)
        if not line or line == b'\\r\\n':#*********
            break
    rows = ['<tr><td>%s</td><td>%s</td><td>%s</td></tr>'% (str(d.temperature()), (d.humidity()),(TC()))]
    response = html0 % '\\n'.join(rows)#***************
    cl.send(response)
    x=ap.ifconfig()
    cl.send('ADR Independent ='+x[0])
    cl.send('<hr/>')
    cl.send('GTW Independent='+x[2])
    x=wlan.ifconfig()
    cl.send('<hr/>')   
    cl.send('ADR Network ='+x[0])
    cl.send('<hr/>')
    cl.send('GTW Network='+x[2])
    cl.send('<hr />********@ 22081-2016 @********<hr />')
    cl.send('Your address='+addr[0]+'<hr/>')
    cl.send('</body></html>')
    cl.close()
    P16.high()
    gc.collect()
print(' GPIO13=0 ....Exiting for interactive mode')
P16.high()
ap.active(False)
wlan.active(False)
gc.collect()
"""
#___________Loader
from machine import reset
f_main=open('main.py','w')
f_main.write(a)
f_main.close()
print(a)
reset()
#_____________End of Template

--Screen format-------------------------------------------

-----------------------------------------------------------