The Wifi Caravan Monitor

 

So after looking around the local Anaconda on the weekend I came across a Bluetooth battery monitor the BM Pro which I thought was a great idea. Then I glanced at the price tag… $250.99 AUD and thought there’s no way I couldn’t make a similar system myself.  I’ve already got a microcontroller to play with (Arduino Uno) that I haven’t used in anything.  Surely I’d just need to measure the voltage – Process is somehow and send it out over Bluetooth to a connected device.  Then the part I don’t know how to do – Program an android app to display that info.

From that simple seed of inspiration, I had another eureka moment.  I’ve already got the van router so why would I both using Bluetooth.  Let’s send information around the van from various sensors via wifi and have the router do the processing.  Then all I’d need is a nice little web page that I could go to on any connected device with a web browser.

From there as with all my projects scope started becoming a problem as my brain kicks into gear and thinks “So we’ll check the voltage this way… ohh squirrel … I can do this, and I can also do that”.  So let define what we’re playing with its capabilities and then what the actual monitor can do for us.

Arduino Uno Specs:

Microcontroller ATmega328P
Operating Voltage 5V
Input Voltage (recommended) 7-12V
Input Voltage (limit) 6-20V
Digital I/O Pins 14 (of which 6 provide PWM output)
PWM Digital I/O Pins 6
Analog Input Pins 6
DC Current per I/O Pin 20 mA
DC Current for 3.3V Pin 50 mA
Flash Memory 32 KB (ATmega328P)
of which 0.5 KB used by bootloader
SRAM 2 KB (ATmega328P)
EEPROM 1 KB (ATmega328P)
Clock Speed 16 MHz
LED_BUILTIN 13
Length 68.6 mm
Width 53.4 mm
Weight 25 g

So from that, we can see 14 digital pins and 6 analogue pins.  Essentially if we think of the digital pins as a way to turn circuits on and off (1 or 0) and the analogue pins as a way to read in values such as battery voltage we can monitor 6 devices which in my case (at this stage of thinking) was enough.

The caravan monitor would need to be able to do the following:

  • Run from 12volts
  • Measure the battery voltage
  • Measure the power use (In amps) at any given time
  • Give an approximate state of charge level (percentage charged) of the battery bank
  • Measure two water tank levels
  • Measure gas bottle levels
  • Communicate with the van router over wifi

Now that we’ve defined what it will do.  How will we achieve this… simple really.

Figuring I’d start with the most basic thing on the list of wants – BATTERY VOLTAGE.

The Arduino uses an analogue to digital converter with a max input of 5V so we need a way to translate the possible max of say 15V down to 5V. We do that with the aid of a resistive voltage divider.  Not going into the details i settled on some resistor values based on what i had laying around and got to work on the code.

Essentially the code was this:

int VoltageReadPin = A0; // arduino pin our RVD sense pin is on
int sum = 0; // sum of samples taken
unsigned char sample_count = 0; // current sample number
float voltage = 0.00; // initilise voltage variable
// Setup Voltage Divider Resistors
int resistor1 = 5000;
int resistor2 = 1000;
float arduinovoltage = 4.95;

void setup()
{
 pinMode(VoltageReadPin, INPUT);
}

void loop()
{
 while (sample_count < 3) {
 sum += analogRead(VoltageReadPin);
 sample_count++;
 }

 // calculate the voltage
 // use 5.0 for a 5.0V ADC reference voltage
 // 5.015V is the calibrated reference voltage
 voltage = ((float)sum / (float)3 * arduinovoltage) / 1024.0;
 // Divide calculated voltage by RVD factor - Vin/Vout = 6.96V in our example
 voltage = (voltage * 6.77);


 sum = 0;
 sample_count = 0;

 Serial.print(F("Voltage: ")); Serial.println(String(voltage).c_str());
}

Forgive me if the code no longer works I had to do it from memory as the project has long since progressed to something bigger.

So that gets us the voltage and prints it out to the computer via USB/serial – Now we need to send it to the router/network.  After a few ideas of using a web page etc I stumbled on an article relating to MQTT from hiveMQ.

Essentially MQTT allows for very simple comms over a network and there already exists iPhone and Android apps to work with it…. YIPEE I still don’t have to learn to program for phones.

Now we have two problems – getting the Arduino onto wifi and sending MQTT data

I’d seen a lot of press about a small cheap wifi chip named the esp8266 for less than $10 you could add wifi to your projects hell you could do away with the Arduino all together and just use the esp if you wanted (I needed more than 1 analogue pin). I jumped on eBay and ordered 3 of them.  Only need one for the project but come on – for $6 i can wifi enable anything i like.  lol

Chip arrives – I wire it up, it springs to life – Using a USB to serial converter i can send it AT type commands and make it connect to the van router and ping things etc.  OK.  now how to get the Arduino to talk to my wifi network using this thing.

OK now how to get it work with the Arduino – Turns out really easily.  There’s a library wifiESP that takes care of the legwork for us.  We make a few config lines in our Arduino sketch to tell it what pins the esp is on and it takes care of the rest.  One problem down one to go.

MQTT is just as simple – There’s a library called PubSubClient which we can utilise to publish and subscribe to topics.

TO BE CONTINUED…..