Introduction: Milk in Fridge - Internet of Things

I think that one day, all food cupboards & fridges/freezers will be smart. All products will have an RFID or video recognisable label which will update a list of product usage. Your local supermarket will gather this information & replenish your products as required - who knows ??


(After about 3 mins, skip forward to about 30 secs from end)

Anyhow, I wanted to make my own Internet of Things and I wanted it to be useful!

So, I came up with the idea of 'How much milk is in my fridge'. Well, now I can go on the Internet and see ! -very handy when out shopping :-). I use a load cell to weigh the milk & an oled display for status.

There are 4 parts to this project.

1) Connecting to WiFi using the ESP8266

2) Establishing and making a link to Thingspeak

3) Connecting the load cell (strain gauge)

4) Connecting a .96 oled mono display.

In brief:

I have a reed switch and magnet on my fridge door. When the door is opened, an interrupt is triggered. The Load cell value is read & converted to 'pints' and displayed on the Oled screen. There are 8 attempts made to update Thingspeak. If 8 updates have not been successful, after a delay of 30 seconds, retry occurs! Each time the fridge door is opened, the counter is reset to zero, thus initiating another 8 attempts. Why 8 attempts? just to give the load cell time to settle - 5 may well be enough!

Step 1: Step 1: Set Up & Test the Individual Parts.

I set up an Arduino Uno to test each of the 4 sections separately, then, when each was working, I merged the code.

My program is about as tidy as an unmade bed, but it works and is fairly readable.

I used a breadboard power supply & fed it with an old 9v psu - this gave me 5v (for the load cell) & 3.3v for the ESP8266 (which can draw up to 300mA according to the spec sheet). I took the Oled 3.3v from the Arduino board.

Pin connections as follows

// oled SDA #A4
// oled SDC #A5 NOTE Supply is 3.3v

// HX711.DOUT - pin #A1 // HX711.PD_SCK - pin #A0 NOTE Supply is 5v

//ESP8266 TX/RX to arduino RX/TX

//Ext USB to serial monitor(10, 11); // RX, TX //DO NOT FORGET TO Common GND ALL devices, power supplies & comms

// int on D2 normally Gnd - goes high when fridge door opened

Note - I was going to use a light sensor to detect when the fridge door was opened, I needed it on interrupt and was too lazy to fathom out how to use an analogue signal to achieve this - hence in the code you will see a variable 'stilldark'.

Step 2: Step 2 : ESP8266

These seem to come with various firmware, which can add to confusion when trying to talk to it.

I spent hours getting this thing to work, here's hoping this will save you some time.

Once the firmware & speed have been established it is pretty straightforward to use!

I purchased 3 a few months back, all comms were set at 115200baud, a tad quick and not good for communicating with the Arduino using the Software Serial lib.

Establish the speed :

Connect your ESP8266 to TX& RX of a usb - serial convertor (ie TX ESP to RX of USB to Serial).

See here for Pinout http://playground.boxtec.ch/doku.php/wireless/esp8...

Connect ch_pd, reset & vcc to 3.3v & gnd to gnd.

Open up a serial viewer (I use Termite) and power up the ESP. If you get gobbledygook your speed is wrong! Try 115200 & 9600 first!

If you send AT, you should get an OK back !

Here's a great site for the command list : https://room-15.github.io/blog/2015/03/26/esp8266-...

Now you have established speed, you can flash the firmware.

I use - AI-v0.9.5.0 AT Firmware.bin google it :-) , it defaults to 9600 & has an AT command to change the baud rate :- AT+CIOBAUD=9600

The seup WiFi looks like this

boolean connectWiFi(){
//scrupd2(); Serial.println("AT+CWMODE=1");

delay(2000);

String cmd="AT+CWJAP=\""; cmd+=SSID; cmd+="\",\""; cmd+=PASS; cmd+="\"";

sendDebug(cmd);

delay(5000);

if(Serial.find("OK"))

{ monitor.println("RECEIVED: OK");

wifiok=1;

scrupd();

return true;

}else{ monitor.println("RECEIVED: Error");

wifiok=0;

scrupd();

return false;

}

Step 3: Step 3 : Thingspeak

Goto thingspeak.com & sign up for free.

Create a channel & then add a plug-in. I modified a single guage with the following

function displayData(point) {
data.setValue(0, 0, 'Pints of Milk'); data.setValue(0, 1, point); chart.draw(data, options); }

function loadData() { // variable for the data point var p; $.getJSON('https://api.thingspeak.com/channels/4NNNN/field/1/last.json?apiKey=CXMyAPIWriteKey6&callback=?', function(data) { // get the data point p = data.field1; if (p) { //p = Math.round((p / 1023) * 100); displayData(p); } });

I modified the gauge to read from 1 to 4 & altered the colour zones :-

chart = new google.visualization.Gauge(document.getElementById('chart_div'));
options = {width: 220, height: 220, redFrom: 0, redTo: .5,yellowFrom:.5, yellowTo:1, minorTicks: 4,min: 0, max: 4};

On the Thingspeak Data Import / Export tab, there are lines of code which you can copy & paste into a browser to test your gauge.

The Arduino code to communicate with thingspeak is as follows

#include
#define SSID "BTHub5-XXXX"

#define PASS "abcde12345"

#define IP "184.106.153.149" // thingspeak.com

String GET = "GET /update?key=thingspeakwritekey&field1=";


Step 4: Step 4 : the Load Cell / Strain Gauge

I purchased the load cell from eBay for less than a fiver. It requires an A2D so eBay again for about £2.00 for an HX711 chip on a board ( see pic 2 )

I used the HX711 lib example - works a treat!

// HX711.DOUT - pin #A1
// HX711.PD_SCK - pin #A0 NOTE Supply is 5v

I commented out most of the serial output.

On initial power up, it is important that there is no weight on the load cell as part of the init is to Tare (zero) the scale.

Step 5: Step 5 : Oled Display

I use the display to show Internet update status and milk quantity.

The U8glib.h lib is excellent and easy to follow.

I needed to update the screen quite often so I used a simple function
void scrupd()

{ u8g.firstPage();

do { draw();

} while( u8g.nextPage() );

}

Step 6: Step 6 : Summary, Installation & Help Needed

The .ino file will hopefully be of some use whether for IoT, using the ESP8266, HX711 or just getting an OLED display to work! I owe thanks to Rob Elmour who helped out with the ESP / Thingspeak problems I encountered.

Installation :

The load cell was mounted on a metal sheet with a tap washer as a spacer to allow the cell to bend, & another screwed on top for the shelf. I used anti slip matting for top and bottom.

The HX711 board was put inside a plastic SD memory card holder and taped to the base plate.

The Arduino nano, power supply & display are in a small case attached to the fridge with velcro strips.

The reed switch protrudes from the bottom of the case and sits about 0.75mm from the magnet!

Help Please : Re Thingspeak

1) Is it possible to change the size of the iframe in which my gauge appears ?

2) I would like to see a 'text only' frame where I can replace some of the text with variables field1, field2 etc ! - any examples would be much appreciated.