element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • Experts & Guidance
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • 'Choose another store...'
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Personal Blogs
  • Members
  • More
Personal Blogs
Legacy Personal Blogs Home temperature monitoring using openHAB on RaspberryPi, ESP8266 and HDC1000.
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Former Member
  • Date Created: 24 Apr 2015 9:35 AM Date Created
  • Views 392 views
  • Likes 1 like
  • Comments 1 comment
  • openhab
  • hdc1000
  • esp8266
  • iot
Related
Recommended

Home temperature monitoring using openHAB on RaspberryPi, ESP8266 and HDC1000.

Former Member
Former Member
24 Apr 2015

It's been awhile since I've wanted to start making my home "smart".

 

I figured I could put some of my past projects together to build something nice, so I installed openHAB, a very known and powerful home automation software, on my raspberryPi.

Installing OpenHAB

 

There are plenty of guides on how to do it, so I'm just telling you the basic commands:

 

mkdir /opt/openhab/
cd /opt/openhab/


#remember to get latest versions from openhab.org
sudo wget https://github.com/openhab/openhab/releases/download/v1.6.2/distribution-1.6.2-runtime.zip https://github.com/openhab/openhab/releases/download/v1.6.2/distribution-1.6.2-addons.zip
sudo unzip distribution-1.6.2-runtime.zip
sudo unzip distribution-1.6.2-demo-configuration.zip
cd addons_temp
sudo unzip /opt/openhab/distribution-1.6.2-addons.zip
cd ..

#we are moving only the addons we need
sudo mv addons_temp/org.openhab.binding.mqtt-1.6.2.jar addons/org.openhab.binding.mqtt-1.6.2.jar

Installing an MQTT broker

 

Install the latest mosquitto binaries:

 

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list #you may need to use 'jessie' instead of 'wheezy' depending on your OS version
sudo apt-get update && sudo apt-get install mosquitto

Setting up OpenHAB

 

(code adapted from here) Let's copy the default config file:

 

cd /opt/openhab/configurations
sudo cp openhab_default.cfg openhab.cfg

 

Open it in your favorite text editor, scroll down to the MQTT transport section and add this line:


mqtt:mosquitto.url=tcp://localhost:1883

 

Create a sitemap file:

 

sudo nano /opt/openhab/configurations/sitemaps/default.sitemap

 

sitemap default label="Main Menu"
{
        Frame label="HDC1000" {
                Text item = Temp
                Text item = Humi
        }
}

 

Now the items file:


sudo nano /opt/openhab/configurations/items/default.items

 

Group All
Group gAttic (All)


Group Balcony "Living Room" <video> (gAttic)


Number Temp "Temperature [%.2f F]" <temperature> (Balcony) {mqtt="<[mosquitto:home/temperature:state:default]"}
Number Humi "Humidity: [%.2f]%" <humidity> (Balcony) {mqtt="<[mosquitto:home/humidity:state:default]"}

 

  OpenHAB is now configured to read the two values from the MQTT broker and is subscribed to the "home/temperature" and "home/humidity" topics, which have been assigned to the two items. The "<humidity>" icon does not exist but I googled one and uploaded it to "/opt/openhab/www/webapps/images/". Neat! Start openHAB by typing:

 

cd /opt/openhub
sudo ./start.sh

 

This will take a couple of minutes but this is how the interface looks like:

 

The openHAB interface.

 

 

NodeMCU Code

 

The NodeMCU MQTT code is bugged in 0.9.5 so you most likely need to update your ESP8266 with a newer release (click!). I flashed nodemcu_float_0.9.6-dev_20150331.bin because the latest version also had problems connecting to the MQTT server. Just remember to get the float version. Let me make this clear, coding for NodeMCU is a pain in the ass. You are going to get LOTS of "out of memory" errors and watchdog timeouts as soon as you implement a program which is bigger than an example. However, after a lot of trial-and-error, I've been able to make a working program. To do this, we need to split the program into different files and compile them separately, then call the compiled files from the main program, in order to reduce memory usage. (I really hope I've been doing this wrong and there is a better way. If you know any, please tell me.)

    1. Upload my NodeMCU module (named "HDC1000.lua"). Instructions here.
    2. Create a file called "main.lua" with the following content:

      HDC1000 = require("HDC1000")
      sda = 1
      scl = 2
      drdyn = 3
      
      
      HDC1000.init(sda, scl, drdyn)
      HDC1000.config()
      
      
      dofile("connect.lc")
      dofile("loop.lc")

    3. Now a "connect.lua" file, which handles the WiFi connection:

       

      SSID = "yourssid"
      password = "yourpassword"
      
      
      wifi.setmode(wifi.STATION)
      wifi.sta.config(SSID, password)
      cfg = {
          ip="192.168.1.100",
          netmask="255.255.255.0",
          gateway="192.168.1.1"
      }
      wifi.sta.setip(cfg)
      wifi.sta.connect()

       

    4. And last but not least, the main loop of our program, "loop.lua"

       

      ip = "192.168.1.3"
      port = 1883
      seconds = 60
      
      tmr.alarm(0, 1000 * seconds, 1, function()
        m = mqtt.Client("1", 60)
             m:connect(ip, port, 3000, function()
                  tmr.wdclr()
                  m:publish("home/humidity", HDC1000.getHumi(), 0, 0, function()
                       tmr.wdclr()
                       m:publish("home/temperature", HDC1000.getTemp(), 0, 0, function()
                            tmr.wdclr()
                            m:close()
                       end
                       )
                  end
                  )
             end
             )
        end
      )

      This sends MQTT messages to "home/humidity" and "home/temperature" every 60 seconds.

       

    5. Now we need to compile all those files. Run these commands (press the "Send to ESP" button  instead of the "Save to ESP"):

      node.compile("HDC1000.lua")
      node.compile("connect.lua")
      node.compile("loop.lua")

       

    6. You can then test your program by doing:

      dofile("main.lua")

 

 

And that's it. If you want your program to start at boot, you need to rename your "main.lua" file to "init.lua". You should do this so that the program starts again if it crashes, but be sure your code works as you might get stuck into infinite loops. You can monitor MQTT messages by running

 

mosquitto_sub -v -t 'yourtopic'

 

on your RaspberryPi.

 

Yay, it works!

 

Now open your openHAB page: http://raspberrypi:8080/openhab.app

 

image

 

Finally, everything works! I loved building this from the ground up: from the breakout board, to the NodeMCU library, to a fully working system! Tomorrow I will place my ESP8266 and HDC1000 sensor outside and find a way power them (USB charger? Solar panel + LiPo?) I hope to add more sensors and features (light and curtains control would be awesome) in the following weeks. Note: I'm now learning how to program the ESP8266 in C. It may not be very straightforward or "Arduino-like" but will definitely save me some headaches and will allow me to take advantage of the full power of the chip. Let me know what you think and if you have any questions or feedback!

 

(note: this post is also published on my website.)

  • Sign in to reply
  • DAB
    DAB over 8 years ago

    Nice post.

     

    You have a lot of good information here.

     

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube