As I finally got my Arduino reading the temperature sensor, and sending the values to this page consistently I noticed that the values were quite ‘jittery’ – the issue being that I was basically grabbing a single read from the sensor every 60 seconds, and then firing it on. Since this was a single read any issues affecting the sensor at the instant of the read could throw off the value significantly.
As you can see in the above chart we can have massive changes in the read temperature without any major changes (that I could tell) in the the actual physical environment – anything between 5 and 20 degrees between samples.
I decided that a better approach would be to average the sensor values over a time and use that as the value sent to the server. The code below shows how I’ve done that:
float readTempAvg(){ static float buff[12]; static int index = 0; static float sum = 0; static int count = 0; sum -= buff[index]; buff[index] = (analogRead(A0) / 9.31); sum += buff[index]; index = ( (index + 1) % 12 ); if( count < 12 ){ count++; } return( sum / count); }
The sum-=buff[index] has the value that is about to be overwritten in the buffer removed from the sum. This means that sum is always kept up to date. Small improvement to this code would be to define a buffer size at the start and replace all the ‘magic’ const 12 that is in the code.
Following this change I’m getting much smoother temperature readings – generally +/-1 degree or less every 60 seconds.
Next step is to try and sort out the Ethernet Shield crashes.







