In the previous post I used the LM75 temperature sensing IC, but I wasn’t happy with the resolution or accuracy of the device (+- 2C). Therefore I started searching for alternative ICs and came across the TMP112. However, that was FAR TOO SMALL and I couldn’t even solder it onto a breakout board and couldn’t find a breakout board small enough to solder it on to. I then found the DS1631 chip, which is a uSOP package which I have no issue soldering on to breakout boards.

The DS1631 also works off I2C, but has an accuracy of about 0.5C and a resolution of 0.0625C which is much better than the LM75. It cost about £3.50 so it’s not too expensive either.

I’m still using Openhab and the ESP8266 wifi board, but the code is slightly different for each, though I’ll explain the differences later.

Hardware

The circuit for the board is very similar to the previous blog, but I’ve now created a small EagleCAD circuit diagram to make things a bit easier to follow (and potentially create boards from).

Below if the circuit diagram for the boards. Hopefully I’ve not make some silly mistake this time, but please let me know if I have! I’ve left out the USB-serial board, but it should be easy enough to connect RX to TX and TX to RX between the ESP board and the serial board.

DS1613 ESP8266 Circuit Diagram

DS1613 ESP8266 Circuit Diagram

I’ve yet to create a PCB or proto-board of the circuit, so at the moment the circuit is on a breadboard. I hope to create a simple proto-board of the circuit soon.

Software

At the moment I simply have one board in my living room, but I hope to have one in each room so I can – hopefully – control the temperature in each room. The software should be easy enough to adjust for multiple sensors and I hope to do that soon.

I’ve created a github project where all the code and circuit diagrams can be found here. I intend to add thermostat control to the project soon, hence the name of the github project.

ESP code

Again, the code for the ESP board is pretty similar to the previous entry. However, because I’m using the DS1631 IC rather than the LM75 IC, there’s a few small changes to the blink_cb function (I really need to change the name of that function as I just stole it from the blink example and forgot to change it’s name!) as shown below.


LOCAL void ICACHE_FLASH_ATTR blink_cb(uint32_t *args)
{
	MQTT_Client* client = (MQTT_Client*)args;
	INFO("Publishing\r\n");


	uint8_t ack = 0;

	i2c_start();
	i2c_writeByte(ADDRESS_W); //write address 0x40
	ack = i2c_check_ack();
	if (!ack){
		INFO("\r\nNot acknowledging 3\r\n");
		return;
	}
	i2c_writeByte(0xAA);
	ack = i2c_check_ack();
	if (!ack){
		INFO("\r\nSecond Ack failed\r\n");
		return;
	}

	i2c_start();
	i2c_writeByte(ADDRESS);
	ack = i2c_check_ack();
	if (!ack){
		INFO("Didn't get temp\r\n");
		return;
	}
	uint8_t T = i2c_readByte();
	i2c_send_ack(1);
	uint8_t T2 = i2c_readByte();
	i2c_stop();

	char buf[10];
	uint16_t temp = (uint16_t)((T << 8) + T2); 	temp >>= 4;
	uint8_t size = itoa(temp, buf, 10);

	if (temp < 1600)
		MQTT_Publish(client, "/LivingRoom/Temp/1", buf, size, 0, 1);
	INFO("%d %d\r\n", T, T2);
}

Also, because the DS1631 needs to have temperature sensing initiated I’ve added a small function called initI2C which is called at the start of the program.


void initI2C(){
	i2c_init();
	INFO("\r\nStarting I2C\r\n");
	i2c_start();
	i2c_writeByte(ADDRESS_W);
	uint8_t ack = 0;
	ack = i2c_check_ack();
	if (!ack){
		INFO("\r\nNot acknowledging 1\r\n");
		return;
	}
	i2c_writeByte(0x51);
	if (!ack){
		INFO("\r\nNot acknowledging 2\r\n");
		return;
	}
	i2c_stop();
}

The full code can be found at the above mentioned github project page under the folder ESP-TemperatureSensor. Although I’ve not done any extensive testing, the code does seem to work well and I’ve yet to have any stability issues. However, I will try and do more testing soon.

OpenHab

The Openhab code is very similar to the previous one, with two major exceptions:

  • I’m using rrd4j persistence instead of sql
  • I have to do a small calculation when the temperature changes, because the temperature the ESP board sends over isn’t actually correct

I’m using rrd4j rather than sql to try and keep the overheads on the Raspbery Pi to a minimum. I might change over to mysql in the near future, but for now the rrd4j seems to be fine for what I need.

I have to do the calculation in Openhab because number sent over by the ESP board hasn’t been divided by 0.0625 because I’ve yet to find a suitable function to convert a float to a string that works on the ESP board. But this is a simple enough route, so I’m not too bothered by it.

All the Openhab files can be found in the github project folder Openhab.

Temperature Chart

As everyone likes graphs and charts, here a screenshot showing the temperature of my living room since it booted up (which was less than 24 hours, but it gives you an idea). As you can see, my house doesn’t really get that cold at night despite it being bloody freezing outside 😀

Openhab temperature graph

Openhab temperature graph

Coming Up

For my next update, I hope to have a wireless temperature sensing board in each room of my house to see how the temperature varies in each.

Advertisements