Author Topic: My temperature logging project  (Read 422 times)

ajohnson

  • Newbie
  • *
  • Posts: 16
    • View Profile
My temperature logging project
« on: October 22, 2016, 02:28:50 pm »
I thought I'd share my temperature logging project.  I purchased the dev platform with the idea that I wanted to log the temperatures around my 2 story house throughout the day to see just how bad things were.  An air conditioning guy had told me that the two west facing bedrooms above the garage and little to no insulation in the walls when they were constructed, which has been born out by how hot they get during summer afternoons here in Southern California.

At any rate, I eventually settled on placing several modules at various locations around the house, publishing to an IFTTT maker recipe every 15 minutes.  This recipe actually calls a custom web link on my web site, with the module UID, temperature, and battery voltage in the querystring of the URL.  The web page writes the data to log files for that module, which I read on a couple other pages, one to display the current temperature for the rooms, and another one that graphs the temperature.

Code: [Select]
Class Temp1
    Shared Event GetTemp() RaiseEvent Every 15 Minutes
        Dim mcUIDString As String = Device.mcUID().ToString("X8")
        Dim mcVoltage As Short = Device.BatteryVoltage
        Dim voltS As String = mcVoltage.ToString
        Dim tempC As Float = TempSensor.GetTemp
        Dim tempF As Float = TempSensor.ToFarenheit(tempC)
        Dim tempDstring As String = tempF.ToString
       
        LedGreen = True       
        Lplan.IFTTT("CyWpXpTFO3r0kMeEdJF3W", "Temp1", mcUIDString, voltS, tempDstring)
        Thread.Sleep(50000)
        LedGreen = False
    End Event
   
End Class

I also tried to interface a mcModule to a LaCrosse wind sensor with no success, mostly because the sensor had a single I/O pin and the mcModule seemed to want discreet input or output on a pin, but not both.  I probably missed something there, but while researching stuff, I found a newer LaCrosse ws-2316 kit for $40 with a serial port and some sample code, so I purchased that.  Further research found the open source code "open2300" programs, that are pretty much just ready to go.  I ended up using a Raspberry Pi to run the "log2300" command to dump the weather stats to a file onto my webserver.  the Pi has a mount point mapped into a share on the server to make things easy.  (Using the weather station did cause me to log an inside and outside temperature with that device instead of two mcModule's  :P)

...which brings me to the next thing.  I was never happy with using IFTTT.  Since I'm hosting my webserver locally, it always bothered me that I was having to make a call out to the Internet to update data on my local server.  So I figured out UDP beaconing and wrote some python scripts for the Raspberry Pi  (the same one that's already talking to the weather station).  This has the added bonus of majorly increasing battery life on the mcModules, although I've not yet been able to quantify the increase.

UDP Beaconing:  In a nutshell, the modules beacon every 10 seconds or so to the low power lan (LPLAN), and if the gateway "sees" it, it rebroadcasts the beacon itself to the local subnet, using it's ip address.  Both the gateway's UID and the module's UID are in the broadcast, which the white paper indicates.

First, I changed the IFTTT script on the mcModules to embed the temperature and the battery voltage into the Beacon bytes.  The Beacon is 4 bytes.  The battery voltage was already a short, which is 2 bytes, so that left 2 more bytes.  The temperature was a Float, and I liked a little precision, so I multiplied it by 10 before I converted it.  ex: 76.216 became 762 in the Beacon.  I may play around with that some more.

Code: [Select]
Class BeaconTemp
    Shared Event GetTemp() RaiseEvent Every 15 Minutes
        Dim mcVoltage As Short = Device.BatteryVoltage
        Dim voltS As String = mcVoltage.ToString
        Dim tempC As Float = TempSensor.GetTemp
        Dim tempF As Float = TempSensor.ToFarenheit(tempC) * 10
        Dim TempFSh As Short = tempF.ToShort()
        Dim beaconData As ListOfByte = New ListOfByte()
        beaconData.AddShort(TempFSh)
        beaconData.AddShort(mcVoltage)
        LedGreen = True       
        Lplan.SetBeaconData(beaconData)
        Thread.Sleep(50000)
        LedGreen = False
    End Event
End Class

Anyway, on the Raspberry Pi there are two scripts, which I placed in /home/pi/mcthings. 

mcBeacon.py:  This script starts running at bootup and never ends.  It's only job is to catch beacons and dump them into /home/pi/mcthings/deviceUId.txt
   each file has timestamp,beacon. previous files are overwritten

Code: [Select]
#!/usr/bin/python
import datetime
import socket
def ByteToHex( byteStr ):
    """
    Convert a byte string to it's hex string representation e.g. for output.
    """

    return ''.join( [ "%02X " % ord( x ) for x in byteStr ] ).strip()

UDP_IP = ""
UDP_PORT = 25452

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes

# TEST IF DATA LENGTH MATCHES WHAT WE WANT.  CAN MODIFY THIS TO CHECK FOR BYTE2=22 IF NEEDED
    if len(data) == 17:

# UNCOMMENT PRINT STATEMENTS FOR DEBUGGING
# CURRENT DATE,TIME,BEACON USER BYTES ARE WRITTEN TO mcUID.TXT FILE

#     print 'Receved on address', addr
#     print 'Received %s bytes' %  len(data)
     myhexdata = ByteToHex(data)
     myModule = myhexdata[21:23] + myhexdata[18:20] + myhexdata[15:17] + myhexdata[12:14]
     myBeacon = myhexdata[24:35]
#     print 'Received message from McModule:%s Beacon Bytes: %s' % (myModule, myBeacon)
     myfname = '/home/pi/mcthings/' + myModule + ".txt"
     myfile = open(myfname,"w")
     myfile.write('%s,%s\n' % (datetime.datetime.now(),myBeacon))
     myfile.close()

mcUpdate.py: I run this script every 15 minutes.  It looks for ".txt" files in "/home/pi/mcthings" and pulls the info from them, then writes the data to the files on my server in the same manner that my web page originally did.  It will not process any files that have a timestamp over 15 minutes old.

Code: [Select]
#!/usr/bin/python
import fnmatch
import os
import datetime
import time

def HexToByte( hexStr ):
    """
    Convert a string hex byte values into a byte string. The Hex Byte values may
    or may not be space separated.
    """
    bytes = []

    hexStr = ''.join( hexStr.split(" ") )

    for i in range(0, len(hexStr), 2):
        bytes.append( chr( int (hexStr[i:i+2], 16 ) ) )

    return ''.join( bytes )

# MAIN LOOP - PROCESS ALL .TXT FILES IN CURRENT FOLDER.
# (BE CAREFUL WHERE THIS SCRIPT IS!)

for file in os.listdir('/home/pi/mcthings/'):
    if fnmatch.fnmatch(file, '*.txt'):
       myfile = open("/home/pi/mcthings/" + file)
       mydata = myfile.read()
       myfile.close()

       mydate = mydata[0:19]
       myHexTemp = mydata[30:32] + mydata[27:29]
       myHexVolt = mydata[36:38] + mydata[33:35]
       myTemp = int(myHexTemp,16)/10.0
       myVolt = int(myHexVolt,16)

# UNCOMMENT PRINT FOR SCREEN DEBUG
#       print '%s %s %s %s %.1f %d' %(file,mydate,myHexTemp,myHexVolt,myTemp,myVolt)

       fmt = '%Y-%m-%d %H:%M:%S'
       mydt = datetime.datetime.strptime(mydate, fmt)
       minutes_diff = (datetime.datetime.now() - mydt).total_seconds() / 60.0
#       print '%.1f minutes old' % minutes_diff

# ONLY PROCESS BEACON DATA THAT IS LESS THAN 15 MINUTES OLD
       if minutes_diff < 15:

# FILE AND PATH DEFINITIONS FOR MY WEB SERVER USE
         myfile1 = file[0:8] + '-current.txt'
         myfile2 = file
         path = '/mnt/bothunter7/templogger/'

         fmtdate = mydt.strftime("%m/%d/%Y,%I:%M:%S %p")
         myLine = '%s,%d,%.1f' % (fmtdate,myVolt,myTemp)
         print '%s' % myLine
         f = open(path + myfile1,"w")
         f.write(myLine + '\r\n')
         f.close()
         f = open(path + myfile2,"a")
         f.write(myLine + '\r\n')
         f.close()

If you want to see the current end result, you can check out my web page project at:

http://azj.dyndns.org/templogger/


Note: edited 10/23 to fix typo in mcUpdate.py - wrong version was pasted that wouldn't open the files in /home/pi/mcthings/
« Last Edit: October 23, 2016, 02:54:51 pm by ajohnson »

Social Buttons


kbrooking

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Re: My temperature logging project
« Reply #1 on: November 28, 2016, 07:26:35 pm »
Nicely done. ;)

I would be interested in how difficult it would be to add the water sensors that they show in the video. This could be the basis for a home weather station. ;D

mc-John

  • Global Moderator
  • Full Member
  • *****
  • Posts: 212
    • View Profile
Re: My temperature logging project
« Reply #2 on: November 28, 2016, 09:38:07 pm »
Very nice.

An alternative for IFTTT is MQTT. You can use a free cloud server or run an MQTT broker on a Raspberry Pi. It is faster and you can also receive.


ajohnson

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: My temperature logging project
« Reply #3 on: November 30, 2016, 02:47:51 am »
I do plan to move from just using beacons now to using MQTT, since that does seem to enable Mobile App functionality of some sort.  I'm still not seeing the great increase in battery life I'd hoped to see, but I think that's either because of the possible numeric processor bug, or some generic batteries I was using.  I also added some boot code to cut back the beacon rate and make sure I'm in low power mode, so we'll see if that helps.

As far as the water sensor, the Demo looks like that should do the trick if you want one hooked up, but I've already got one on the Lacrosse weather station that is hooked up to the Raspberry Pi.  I just haven't put the data on the web page.  I'm accumulating it though.  That's actually been in the back of my mind for the last couple weeks because it's been raining off & on here.

Lately I've been too busy printing enclosures for the mcModules with my new 3d printer (birthday present from my wife, yay!) thanks to the post by kristofferis in mc-Product General.  Not a perfect solution, but at least I don't have bare circuit boards strewn about

mc-John

  • Global Moderator
  • Full Member
  • *****
  • Posts: 212
    • View Profile
Re: My temperature logging project
« Reply #4 on: November 30, 2016, 09:22:48 am »
There were problems with the battery life of the mc-Mod120. See this http://mcthings.createaforum.com/support/battery-life/ post.
Specially with beacons the battery life very long

ajohnson

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: My temperature logging project
« Reply #5 on: December 13, 2016, 07:54:25 pm »
well, with the following code currently running

Code: [Select]
Class BeaconTemp
    Shared Event Boot()
        Lplan.SetLowPowerMode(120)
        Lplan.SetBeaconTime(30)
    End Event
   
    Shared Event GetTemp() RaiseEvent Every 15 Minutes
        Dim mcVoltage As Short = Device.BatteryVoltage
        Dim voltS As String = mcVoltage.ToString
        Dim tempC As Float = TempSensor.GetTemp
        Dim tempF As Float = TempSensor.ToFarenheit(tempC) * 10
        Dim TempFSh As Short = tempF.ToShort()
        Dim beaconData As ListOfByte = New ListOfByte()
        beaconData.AddShort(TempFSh)
        beaconData.AddShort(mcVoltage)
        LedGreen = True       
        Lplan.SetBeaconData(beaconData)
        Thread.Sleep(50000)
        LedGreen = False
    End Event
End Class
I seem to hit around 21 days on my Energizer CR2032s.  Am I doing something wrong?

mc-T2

  • Administrator
  • Full Member
  • *****
  • Posts: 246
  • mc-Things! The opportunities are endless!
  • Location: Canada
    • View Profile
Re: My temperature logging project
« Reply #6 on: December 14, 2016, 05:05:45 pm »
Hi ajohnson,

Your code is fine, the issue resides within the firmware of the mcMod120 device. Our engineers have fixed the power consumption issue and we will be releasing new firmware within the next week or so to solve the issue.
Once the firmware is released and you update your modules, you will see the battery life increase significantly. (There was some confusion that this update was already released - it has not been yet)

We appreciate your patience on this and expect to see the new firmware very soon!
Thanks!
Need more mc-Modules, mc-Gateways or other mc-Things? Check out our product page: www.mcthings.com/products. mc-Development kits are available too!
Check out a live Dashboard using mcThings and Losant! Click here: https://goo.gl/ST43hB