Archive for the ‘ IoT ’ Category

ESP8266 Temperature logger for Nagios

Previously I had a post about ESP8266 microcontroller and I had some brief review of this lovely hardware here.
Now I’m going to post a new schematics and a Nagios module to use it as a real time temperature logger for Nagios server in Data-centres.

Briefly what it does and how it works:

– ESP8266 module reads the temperature sensor in every 10 seconds and sends data via UDP
– Nagios server processes the received UDP data  from the ESP module and compares with the settings in Nagios

If Nagios server picks value that triggers the alarm, than it will send warning or alarm to the Nagios admin.
So here we have the electronics schematics and the related program codes for the ESP and the Nagios server.

Connect the ESP and the Dallas sensor as on this picture below. This is the easiest way to wire up them. (1-Wire )
I’m not going to go into details of the ESP module programing, there are dozens articles on the net regarding to this.
Myself I use the LuaLoader, which I think is the easiest one to use. If you just need the related code files, then jump to the end of this article, there you can download all lua files.
You must correct the Nagios server’s address, which is this: ” cu:connect(7,”10.0.4.252″) ” and also your SSID and Password to connect to your access point.

esp8266-ds18b20-2_bb

For power supply I used an old USB cable to power up the ESP module from a server in the Data-centre. After all this is to check the racks and server’s temperature in the DC. 🙂
The USB has 5V as we know, so you would need to lower this up to 3.3V. You can use an AMS1117 5V to 3V stabilizer, please check the link below about this.
Temperature Sensor = Dallas
ESP module = ESP
AMS1117-3.3 = AMS

And from here the codes for the ESP8266 module and for the Nagios server as well.
Two files need to be uploaded to ESP8266:

Github links for source code: https://github.com/7layerorg/ESP8266/tree/master/Temperature_Logger

first file init.lua:

#####

function startup()
if abort == true then
print(‘startup aborted’)
return
end
print(‘Starting xmitTemp’)
dofile(‘xmitTemp.lua’)
end
abort = false
print(‘Startup in 5 seconds’)
tmr.alarm(0,15000,0,startup)
#####

Second file xmitTemp.lua:

#####

function getTemp()
local addr = nil
local count = 0
local data = nil
local pin = 3 — pin connected to DS18B20
local s = ”
— setup gpio pin for oneWire access
ow.setup(pin)
— do search until addr is returned
repeat
count = count + 1
addr = ow.reset_search(pin)
addr = ow.search(pin)
tmr.wdclr()
until((addr ~= nil) or (count > 100))
— if addr was never returned, abort
if (addr == nil) then
print(‘DS18B20 not found’)
return -999999
end
s=string.format(“Addr:%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X”,
addr:byte(1),addr:byte(2),addr:byte(3),addr:byte(4),
addr:byte(5),addr:byte(6),addr:byte(7),addr:byte(8))
–print(s)
— validate addr checksum
crc = ow.crc8(string.sub(addr,1,7))
if (crc ~= addr:byte(8)) then
print(‘DS18B20 Addr CRC failed’);
return -999999
end
if not((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
print(‘DS18B20 not found’)
return -999999
end
ow.reset(pin) — reset onewire interface
ow.select(pin, addr) — select DS18B20
ow.write(pin, 0x44, 1) — store temp in scratchpad
tmr.delay(1000000) — wait 1 sec
present = ow.reset(pin) — returns 1 if dev present
if present ~= 1 then
print(‘DS18B20 not present’)
return -999999
end
ow.select(pin, addr) — select DS18B20 again
ow.write(pin,0xBE,1) — read scratchpad
— rx data from DS18B20
data = nil
data = string.char(ow.read(pin))
for i = 1, 8 do
data = data .. string.char(ow.read(pin))
end
s=string.format(“Data:%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X”,
data:byte(1),data:byte(2),data:byte(3), data:byte(4),
data:byte(5),data:byte(6), data:byte(7),data:byte(8))
–print(s)
— validate data checksum
crc = ow.crc8(string.sub(data,1,8))
if (crc ~= data:byte(9)) then
print(‘DS18B20 data CRC failed’)
return -9999
end
— compute and return temp as 99V9999 (V is implied decimal-a little COBOL there)
return (data:byte(1) + data:byte(2) * 256) * 625
end — getTemp
function xmitTemp()
local temp = 0
temp = getTemp()
if temp == -999999 then
return
end
cu:send(tostring(temp))
end — xmitTemp
function initUDP()
— setup UDP port
cu=net.createConnection(net.UDP)
cu:connect(7,”10.0.4.252″)
— cu:connect(7,”10.0.4.252″)
end — initUDP
function initWIFI()
print(“Setting up WIFI…”)
wifi.setmode(wifi.STATION)
wifi.sta.config(“Your SSID”,”SSID Password”)
wifi.sta.connect()
tmr.alarm(1, 1000, 1,
function()
if wifi.sta.getip()== nil then
print(“IP unavailable, Waiting…”)
else
tmr.stop(1)
print(“Config done, IP is “..wifi.sta.getip())
end
end — function
)
end — initWIFI
initWIFI()
initUDP()
tmr.alarm(0, 10000, 1, xmitTem

Here follows the Nagios server modules:

Add to your localhost.cfg the following configuration.
This is usually at /usr/local/nagios/etc/objects/

define service{
use                             local-service         ; Name of service template to use
host_name                       Telehouse
service_description             Telehouse_Temperature
check_command                   check_temp
#check_interval                 0.5
#retry_interval                 1
#max_check_attempts             5
notification_interval           1
check_interval          1
retry_check_interval    1
max_check_attempts      5
}

#####

Create a file called check_temp in the libexec directory and make it executable.  (check_temp at /usr/local/nagios/libexec)

#!/bin/bash
DIRS=”/var/log /tmp”

temp1=`/usr/bin/cut -c 1-2 /home/nagios/current_temp.txt`
temp2=`/usr/bin/cut -c 3-4 /home/nagios/current_temp.txt`

op1=2200
op2=2500

count=$(/usr/bin/tail -n 1 /home/temp/current_temp.txt)

count2=$count

if [[ “$count2” < “$op1” ]] ; then

status=0
statustxt=OK

elif [[ “$count2” < “$op2” ]] ; then

status=1
statustxt=WARNING
else

status=2
statustxt=CRITICAL
fi

echo “$status Temperature:$temp1.$temp2; Triggers: 22.00;25.00;0; $statustxt – $count2”
exit $status

######

Add a new crontab to run tshark which will check the UDP echo messages from the ESP module.
If you don’t have tshark/wireshark installed, then make it available for your box.
CentOS: yum install wireshark
Debian: apt-get install wireshark

nano /etc/crontab

01 * * * * root cd /home/temp && /usr/bin/tshark -a duration:3600 -i eth0 src 10.0.4.30 -T fields -e data -w temp2.pcap & > /dev/null
* * * * * root /home/temp/temp.sh

######

Create a new directory in /home as temp

mkdir /home/temp

Create a file called temp.sh

#!/bin/bash

cat /home/temp/temp2.pcap | tr -dc ‘[:alnum:]\n\r’ | cut -c 2-5 | awk ‘length($0) > 2’ | tail -n 1 -c 5 > /home/temp/current_temp.txt

To check ESP8266 sending the correct UDP packet run this command:

tcpdump -i eth0 udp

You need to see similar UDP packets from the ESP module every 10 seconds:

18:10:40.248116 IP 10.0.4.30.45908 > nagiosnew.echo: UDP, length 6

And also in the Nagios you will hopefully see this:

telehouse

References:

https://bigdanzblog.wordpress.com/2015/04/29/snmp-environmental-monitoring-using-esp8266-based-sensors/
http://www.instructables.com/id/Low-cost-WIFI-temperature-data-logger-based-on-ESP/?ALLSTEPS
http://benlo.com/esp8266/
https://github.com/nodemcu/nodemcu-firmware/tree/master/


https://www.7layer.org/downloads/init.lua

https://www.7layer.org/downloads/xmitTemp.lua
https://www.7layer.org/downloads/esp8266_flasher.exe
https://www.7layer.org/downloads/v0.9.2.2 AT Firmware.bin

 

IoT Temperature logger with ESP8266 and DS18B20 sensor

Room temperature:

I will post the circuit schematics and coding shortly, in the meantime this is the module that I used.
Also I’m posting the firmware flasher and the firmware that I used for this project.
There are many available on the net and you could get confused easily, so there you go follow this links and check what I bought and used.

ESP8266 used for this project: ESP-01: http://www.esp8266.com/wiki/doku.php?id=esp8266-module-family#esp-01

esp8266-pinout

ESP8266 on ebay: http://www.ebay.co.uk/sch/items/?_nkw=esp8266&_sacat=&_ex_kw=&_mPrRngCbx=1&_udlo=&_udhi=&_sop=12&_fpos=&_fspt=1&_sadis=&LH_CAds=&rmvSB=true

DS18B20 sensor on ebay: http://www.ebay.co.uk/sch/i.html?_fspt=1&_mPrRngCbx=1&_from=R40&_sacat=0&_nkw=DS18B20+sensor&_sop=15

Nodemcu Firmware: https://github.com/nodemcu/nodemcu-firmware/tree/master/pre_build/latest

esp8266_flasher

Programming and testing with Lualoader: http://benlo.com/esp8266/
download: http://benlo.com/esp8266/LuaLoader.zip

Programming and testing with ESPlorer: http://esp8266.ru/esplorer/
download: http://esp8266.ru/esplorer-latest/?f=ESPlorer.zip  

 

 

 

 
Show Buttons
Hide Buttons