Temperature and Humidity sensor and control using ESP8266-03

Temperature and humidity sensor and control

Temperature and humidity sensor and control

This module is available for preorder at Tindie click here

This project implements a temperature and humidity sensor using the ESP8266 WIFI module and a DHT11 temp/hum sensor.

The board can be configured via web in a similar way as previous projects. Connect to the  ESP8266 station (ESP8266-XXXXXXX) and on a web browser point to IP 192.168.4.1, then you are prompted to enter wifi network SSID and password. Within a few seconds, the IP address of the ESP8266 module will appear on the web page.

Any TCP message sent to the module IP address will return a JSON feed  with the current temperature and humidity:

{“dth11”: [{ “Temperature:”: “24”, “Humidity”: “39”,}]}

Now the interesting part. By sending a TCP special string, I can set a minimum and maximum temperature and humidity. When the minimum temperature is reached, then a relay is triggered; similarly, when the maximum temperature that has been set is reached, another relay is triggered and the other one is reset. The same can be done for humidity. Now we have a sensor that reacts.

The control string is of the form

LT=010-HT=020-LH=030-HH=060

and can be sent at any time to set the min/max limits, in return you always get the current temp and hum as

{“dth11”: [{ “Temperature:”: “24”, “Humidity”: “39”,}]}

A video of the project can be accessed here

Advertisements

About hbouzas

Born in Buenos Aires, Argentina. Studied Physics at the University of Buenos Aires. Joined Schlumberger in February 1985 in Houston, Texas, and worked in several technical and managerial positions until 2000. From 2000 until 2008 held several management positions in Abingdon, UK; Calgary, Alberta and London, UK. Worked in the areas of Geophysical Exploration, Geological Modeling, Structural Modeling, Reservoir Modeling and Petroleum Economics and holds several patents. He is currently the Norway Technology Center Manager for Schlumberger Information Solutions and is based in Oslo and Stavanger. Main interest are software, technology, innovation, 3D visualization, design, human computer interaction, energy, environment.
This entry was posted in Uncategorized. Bookmark the permalink.

20 Responses to Temperature and Humidity sensor and control using ESP8266-03

  1. Rich says:

    Do you have any sample code or messages that will correctly return the temperature and humidity? I’m new to TCP messaging and am connected via a LabWindows CVI terminal

    • hbouzas says:

      Here is a python example on how to send TCP messages:

      #!/usr/bin/env python

      import socket

      import time

      TCP_IP = “192.168.1.118”

      TCP_PORT = 9999

      BUFFER_SIZE = 256

      MESSAGE = “THL032100020090”

      print (“Reading module at IP: “, TCP_IP)

      print (“Press Control-C to exit”)

      while True:

      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

      s.settimeout(30)

      s.connect((TCP_IP, TCP_PORT))

      s.send(MESSAGE.encode())

      data = s.recv(BUFFER_SIZE).decode()

      s.close()

      print (time.strftime(“%H%M%S”), data)

      time.sleep (5)

  2. Rich says:

    back to working on this and still having trouble. I can’t get a proper response from it it keeps closing the connection.
    (‘Reading module at IP: ‘, ‘192.168.2.14’)
    Press Control-C to exit

    Traceback (most recent call last):
    File “C:/Python27/tcp temp4.py”, line 27, in
    data = s.recv(BUFFER_SIZE)
    error: [Errno 10054] An existing connection was forcibly closed by the remote host

  3. Rich says:

    I can connect, I get a blue LED blink, but no response, then timeout.

  4. Rich says:

    I’m basically running your Python code. The compiler wants an indent after the “While” statement. Which lines do you have indented in the while? It looks like an issue with the encode and decode attributes. I was getting the blue LED without them included, but when I put them back I get receive or timeout errors.

    #!/usr/bin/env python

    import socket

    import time

    TCP_IP = ‘192.168.2.14’
    TCP_PORT = 9999
    BUFFER_SIZE = 256
    MESSAGE = ‘THL032100020090’

    print (‘Reading module at IP: ‘, TCP_IP)

    print (‘Press Control-C to exit’)

    while True:

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    s.settimeout(30)

    s.connect((TCP_IP, TCP_PORT))

    s.send(MESSAGE.encode())

    data = s.recv(BUFFER_SIZE).decode()

    s.close()

    print (time.strftime(‘%H%M%S’), data)

    time.sleep (5)

  5. Rich says:

    below is the error i’m getting

    Traceback (most recent call last):
    File “C:\Python27\tcp temp3.py”, line 27, in
    data = s.recv(BUFFER_SIZE)
    timeout: timed out
    >>>

  6. Rich says:

    Sorry that was the other version without the encode and decode. the correct error is below (still a receive error)
    Traceback (most recent call last):
    File “C:\Python27\tcp temp2.py”, line 27, in
    data = s.recv(BUFFER_SIZE).decode()
    error: [Errno 10054] An existing connection was forcibly closed by the remote host
    >>>

    • hbouzas says:

      Check this code, you need to handle exemptions.

      #
      #TCP_20160201.py
      #29 November 2015

      import socket
      import datetime
      import time
      import urllib2
      from socket import error as SocketError
      import errno
      import numpy as np

      TCP_IP = ‘192.168.1.81’
      TCP_PORT = 9999
      BUFFER_SIZE = 16
      MESSAGE = “GCT”

      while True:

      try:
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      s.connect ((TCP_IP, TCP_PORT))
      s.setsockopt( socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
      s.send (MESSAGE)

      data = s.recv(BUFFER_SIZE)

      s.close

      parts = data.split(“: “)
      print parts
      if parts[0] != ‘ESP8266 WIFI mod’:
      temp_F = (float(parts[1])/10)* 1.8 +32
      ts=datetime.datetime.now().strftime(“%Y-%m-%d %H:%M:%S”)
      print ” “*3, ts, (“%.1f” %temp_F),(chr(0x00F8)),’F’

      except SocketError as e:
      print “Error detected”

      time.sleep(2)

  7. Rich says:

    I’m now able to get an output from the device most of the time, I does respond to the GCT command with b” . What format is this in I it doesn’t seem like hex, or ASCI representation of decimal, and it doesn’t change with temperature. Do you know of a site that deals with programming these devices? Where did you get your code from? Thanks – Rich

  8. Rich says:

    The sensor looks like good solder joints, it was installed when I received it.

  9. Rich says:

    Yes, I got it from you thru Ebay, but not much documentation. Is there a list of all applicable commands it accepts?

  10. Rich says:

    I was looking for a command that returns the temperature, the THL command returns an error, and the GCT command returns b”

  11. Rich says:

    Will it report temperature from the serial port? or is it just for setup and programming?

  12. hbouzas says:

    from the serial port you can inquire variable values, which requires knowing the variable names.
    In any case, the functionality as described in the documentation must work, if not, lets investigate and if there is a problem with the board, you’ll get a replacement

  13. Rich says:

    Been out of town, but wanted to let you know that I got it working. your earlier post to this string had the MESSAGE = “THL032100020090” which threw me off, it was missing 2 more digits in the command string.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s