Close

Taking temperature readings in the attic

A project log for Remote environmental logging

simple way to log temperature, humidity, etc. with the help of one or more ESP8266 boards

mcunerdmcu_nerd 07/22/2020 at 13:440 Comments

With this current heat wave I was curious to find out just how hot my attic gets.  I had some DS18B20 1-wire temp sensors on hand so I set out to create a cable for it. and connect it to an ESP8266 D1 board  I started off using some speaker cable and made up a three conductor cable.  I was tempted to try going with parasitic power which would have only required 2 conductors (and thus save me the trouble of having the split a 2 conductor speaker cable to get that third conductor) but I've read it's not reliable for any runs of considerable length.  I used some heat-shrink tubing at intervals to help keep things somewhat neat. For end connectors, I simply cannibalized some jumper wires.

For the pull-up resistor between the VCC and data pin I tried to place a thru-hole resistor on the sensor leads but it was simply too big to fit nicely.  I ended up soldering an 0805 4.7K resistor on the sensor leads that fitted rather nicely.

An in all the cable was around 4M in length. I've read that cable length can be up to 20M, although the pull-up resistor value may need to be lowered.

So why did I go to the effort to make a cable of considerable length?  I could have simply stuck everything up in the attic.  I didn't want to subject the ESP8266 board and it's wall-wart to the the rather harsh conditions in the attic. While according to the ESP8266 datasheet it can easily withstand it, I'm doubtful other components on the board are as quite as robust.  I'm sure the wall wart would like it much either.  So I figured why subject all of those things to the harsh attic environment, if I don't have to?

On to the coding side of things. I first created the python file for the ESP8266:

def go():
	import time
	import machine
	import urequests
	import network
	import onewire, ds18x20
	sta_if = network.WLAN(network.STA_IF)

	
	dat = machine.Pin(12)
	ds = ds18x20.DS18x20(onewire.OneWire((dat))
	roms = ds.scan()

	time.sleep(2)

	

	def send_data():
		url = 'http://[webserver ip address here]/sensorlogging/php/rsensor_ds18b20.php'
		headerd = {'Content-Type': 'application/x-www-form-urlencoded'}
		jsons = "data=" + str(ds18b20temp_f)
		if sta_if.isconnected() == True:	
			try:
				r = urequests.post(url, data=jsons, headers= headerd)
				r.close()
			except OSError:
				pass
	while True:
		ds.convert_temp()
		time.sleep(15)
		ds18b20temp_c=ds.read_temp(roms[1])
		ds18b20temp_f=ds18b20temp_c * 9.0/5.0 +32
		send_data()

	

Then I made up the php file on the server side(rsensor_ds18b20.php):

<?php
if($_POST){


$statefile = fopen("../logfiles/rsensor_ds18b20.txt", "r");
if (flock($statefile,LOCK_SH)){
	$edstatus = fgets($statefile);
	flock($statefile,LOCK_UN);
}
fclose($statefile);
if($edstatus =="enable"){

//date_default_timezone_set("UTC");
date_default_timezone_set("America/New_York");
$data = $_POST['data'];
$datfile = fopen("../logfiles/rsensor_ds18b20.csv", "a+");
if (flock($datfile,LOCK_EX)){
	//fwrite($datfile, $data . "," . $data2 . "," . date("H:i:s") ."\n");
	//fwrite($datfile, $data . "," . $data2 . "," . date("H:i:s") . "," . date("Y-m-d") ."\n");
	fwrite($datfile, date("Y-m-d") . "," . date("H:i:s") . "," . $data  . "\n");
	flock($datfile,LOCK_UN);
	}
fclose($datfile);
}
$statusfile = '../logfiles/rsensor_ds18b20.csv';
$filesize = filesize($statusfile);
if($filesize >= 2097152){
rename($statusfile,'../logfiles/rsensor_ds18b20_' . date("Y-m-d-H-i-s") . '.csv');


}


}

?>

The enable file (rsensor_ds18b20.txt):

enable

 I ran the commands to run the python file on:

import esp8266_1_wire_ds
esp8266_1_wire_ds.go()

 Everything worked fine after correcting a few stupid mistakes.  I haven't created/modified a php page for viewing it on a browser yet.

Discussions