ESP 8266 Voltage Sensor – (Part Six)

So it has been a while since I’ve blogged about anything related to the boat, and the ESP 8266 Voltage Sensor and fuel Sensor. Regardless, what I have done since the last time was to get a tank sensor working. I got frustrated and decided that I was just going to buy a tank sensor from Yacht Devices. Primarily. because I wanted my analog gauges to work as a backup. Furthermore, this gave me my fuel tank levels relatively easily. In the future, it will also provide levels of my water and black water tank.

Additionally, I’ve got all the parts for the autopilot, but also have not done that installation either. So let’s continue with the long-delayed task at hand and use what we foundationally built last time. Except we are just going to read battery voltages. Starting with the Generator Room Battery.

Generator Battery Monitoring!

So, To recap, in the fifth part of this series. We finished the build of a Sensor. This will be used as an ESP 8266 Voltage Sensor. Secondly, we will use the ADS1115 and a voltage divider. What I will use is the following Hardware from last time:

So you may ask what’s the rationale behind building an ESP 8266 Voltage Sensor? Let me first start by saying that I’ve had some electrical gremlins. What I tracked it down to was a bad alternator. It wasn’t so evident until I had the boat off-shore power and anchored somewhere. That got me thinking If I have five batteries, why am I not looking at them all the time? So let’s get to building and deploying our Arduino Voltage Sensor for Generator Battery Monitoring!

Generator Battery Sensor

Let’s start by using the layout we had from last time. But instead of using A0, we are going to read the voltage of the ADS1115 (Green Arrow) for the Arduino Voltage Sensor

ESP8266-Modular-1
ESP8266-Modular-1

In the picture above. I’ll point out what the devices are that are denoted by the colored arrows:

So the sensor pictured above is where I will end up eventually after Breadboarding. This is a bunch of wiring and Soldering. I’m not doing this for volume so one-off customs will do.

ESP 8266 Voltage Sensor / ESP-12F Node MCU Pinning

Firstly, we are going to start by assembling/Pinning the ESP 8266 Voltage Sensor by adding the header pins. We need to Solder the header pins to the ESP8266. In my case, I used a male and female header on the ESP8266 on my prototype board. I keyed the ESP so I can’t orient it incorrectly. If you are not concerned about that just use the short end of the eight-pin male headers that came with the ESP8266 kit. For breadboarding just use the male pins. The Female pins will not extend into the breadboard.

ESP8266
ESP8266

So, to start, there are two rows of eight pins. Above is a Picture of the ESP8266 before I soldered it. Additionally, it can be tricky to get this soldered, especially with the keyed approach (Male & Female). For the keyed approach, use the male and female to keep the ESP8266 level while soldering. For the male pinning just put them in the breadboard and solder them. I use a fine tip and adjustable soldering iron in my soldering station. You don’t need all that, I just happen to have that device for my Sea-Doo digital instrument repair.

Once you have the ESP-12F/ESP 8266 header pins soldered, it’s time to move on to Pinning the ADS 1115. So, basically, it’s the same process. Just a bit more difficult because there is just one row of pins.

ADS 1115 Pinning

So as with the ESP-12F/ESP 8266, you will need to solder the header pins to the ADS 1115 board. This will be the short end of the ten-pin male header pin. This will be tricky likewise, but I’ll try and explain how I accomplish this. solder just one of the pins on one end or the other of the ADS 1115. Additionally, you just need to get one pin though. Then heat it back up and get the correct alignment by using the other side of the header where there won’t be hot.

ADS1115-Base
ADS1115 Arduino Sensor

First, keep in mind that the ADS 1115 uses the I2C bus/ Protocol. You can have quite a few I2C devices. Each device has its unique address and must be set on the devices if there are multiple devices like the ADS 1115. In my case, I will have two.

Once you have the ADS 1115 header pins soldered, it’s time to move on to Pinning and configuring buck converter. This is how we will provide power to our ESP-12F Node MCU and a related devices.

Buck Converter Pinning & Configuration

So the buck converter is either adjustable or hardwired to the desired output voltage. We are going to hardwire it! The ESP-12F/ESP 8266 can accept either 5V or 3.3V. Anything above ten percent will burn up the ESP-12F/ESP 8266. I did this once because of a bad connection on the buck converter. Rendering it useless. We are going to go with 3.3V.

Below are the two changes to the buck converter that are needed to set this to 3.3V. First, the red arrow is where you will cut the trace. It’s labeled ADJ. This sets it to a fixed voltage.

Secondly, the yellow arrow is where you will solder the two pads to create a bridge. This one is labeled 3.3V. After that is done solder the short end four-pin male header. Additionally, below you can see all three devices installed into the breadboard. This is not the final layout, but you get the general idea of the layout.

.

Programming Dependencies

Firstly, I will need to download install some software to configure the Sensor. Keep in mind I’m using a Windows 10 laptop. So, in my case, I will pull the ESP-12F Node MCU off the breadboard and program it via the USB port. Secondly, to perform the programming of the ESP-12F Node MCU. You will need the following pieces of software:

So to start, first, download and install Visual Studio Code. Second, once you have “Microsoft Visual Studio” installed you will then add the “Platform IO Plugin”. Next, select “Extensions” from the left toolbar. Search for “platformio”, select the first result, and click “Install”.Thirdly, install “Windows GIT”. Additionally, I installed the “Arduino IDE”. There are some nuances that “Microsoft Visual Studio” and “Platform IO Plugin” are not so easy to deal with that the “Arduino IDE” Handles like clearing the flash. Additionally, there are a lot more programming examples for Arduino vs SensESP. I’m not all that savvy at coding or programming.

Programming

Once you have PlatformIO installed, its home screen should open automatically when you start VSCode. On the PIO Home, select “New Project”. Enter a name (something like SensESP_Test for your first project) and then select “NodeMCU 1.0 (ESP-12E)” in the board dropdown. (This assumes you’re using a Node MCU ESP-12E/F based development board. If you’re using a different ESP, select that in the board dropdown.) The Arduino framework should become automatically selected. Complete the New Project dialog, then open the project you created.

Platform.ini

Once you have your new project open, open the automatically generated platformio.ini file that’s in your project’s directory (NOT the one that you find if you go down into the .pio/libdeps/… folders). Save it as platformio.ini.auto so you can refer back to it.

Create a new file, and copy/paste the entire contents of this example platformio.ini file into it. Save it as platformio.ini.

If the [platformio] section of this file contains your ESP board, make sure your board is not commented out, and that all other boards are commented out, and save it. This should be a good platformio.ini file for SensESP. If your board is NOT included, you need to add it (see how it’s represented in the platformio.ini.auto that you saved). And you need to merge the other lines in platformio.ini.auto into your platformio.ini file. (Detailed instructions for merging these two files are available in the Wiki.)

[platformio]
;set default_envs to whichever board(s) you use. Build/Run/etc processes those envs
default_envs = 
   ;esp32dev
   d1_mini
;   esp-wrover-kit

So the Node MCU ESP-12F is actually a d1_mini. So we are good to go from a platfrom.ini perspective.

Main.cpp

Now, open src/main.cpp. The default template is for the Arduino IDE, but a SensESP main.cpp file will look very different. Replace the contents of main.cpp with the contents of one of the SensESP examples in the examples subdirectory here on GitHub. (This is a good one to start with: https://github.com/SignalK/SensESP/blob/master/examples/analog_input.cpp .) Below is an example of what works for me. This will read the analog pin ADC1115. Additionally, Do not apply voltage to the input pin at this time. You Will need to use the buck converter for this.

// ADC Battery Voltage (V1) RKH
#include <Arduino.h>
// Signalk Specific 

#include "sensesp_app.h"
#include "sensesp_app_builder.h"
#define USE_LIB_WEBSOCKET true
#include "signalk/signalk_output.h"
#include "transforms/linear.h"
#include "sensors/analog_input.h"
#include "sensors/ads1x15.h"
#include "transforms/voltage_multiplier.h"
#include "Wire.h"

// SensESP builds upon the ReactESP framework. Every ReactESP application
// defines an "app" object vs defining a "main()" method.
ReactESP app([]() {

// Some initialization boilerplate when in debug mode...
#ifndef SERIAL_DEBUG_DISABLED
  SetupSerialDebug(115200);
#endif

SystemStatusLed* led = new SystemStatusLed(LED_PIN);
 
 //Create a builder object
 
  SensESPAppBuilder builder;

  // Create the global SensESPApp() object
  sensesp_app = builder.set_hostname("Generator_Room")
                    ->set_sk_server("Signalk_server_name_or IP", 80)
                    ->set_wifi("wifi_network_name", "PASSWORD")
                    ->set_standard_sensors()
                    ->set_system_status_led(led)
                    ->get_app();

 // Create an instance of a ADS1115 Analog-to-Digital Converter (ADC).
  // You could use an ADS1015 (the 12-bit version of the chip) instead.

  adsGain_t gain = GAIN_TWOTHIRDS;
  ADS1115* ads1115_0x48 = new ADS1115(0x48, gain);
 
  // Create an instance of ADS1115Voltage to read a specific channel of the ADC,
  // (the channel on the physical chip that the input is connected to), and
  // convert it back into the voltage that was read by the ADC.
  
  // Generator Room Battery - ADS0 Channel 0
  ADS1x15Channel_t channel_battery_0 = channel_0;
  int read_delay_battery_0 = 1000;
  auto* battery_0_voltage = new ADS1115Voltage(ads1115_0x48, channel_battery_0, read_delay_battery_0,
                                             "/Battery_0/read delay");

  battery_0_voltage->connect_to(new VoltageMultiplier(
                                  30000, 7500,                // Measured ohm values of R1 and
                                  "/Battery_0/VoltMultiplier")) // R2 in the physical voltage divider
  ->connect_to(new SKOutputNumber("electrical.batteries.0.voltage",
                                      "/Battery_0/skPath"));
  // Start the SensESP application running
  sensesp_app->enable();
});  

Check that the settings (pin numbers, etc.) match your hardware. Then click on the checkmark icon on the blue status bar along the bottom of your screen. (That’s the “Build” shortcut.) If the build succeeds, you can plug in your ESP board and press “Upload and Monitor”.Hopefully, the project compiles and uploads, your ESP will be running the example code. (If you get errors about missing libraries, look at this Troubleshooting guide in the Wiki).

Since the first thing the sensor needs to do is connect to a Wi-Fi network, and it doesn’t know what network to connect to, it will broadcast a Wi-Fi SSID for you to connect to so you can configure it. Connect your computer or phone Wi-Fi to the “Configure SensESP” network. A captive portal may pop up, but if it doesn’t, open a browser and go to 192.168.4.1. Enter your Wi-Fi credentials to allow the device to access the network that your Signal K Server is on. Also enter a suitable name for the ESP, for example, Port_Sensors. (No more than 16 characters, no spaces.) Save the configuration with the button on the bottom of the page, and the ESP will restart and try to connect to your Wi-Fi network.

Connecting the Voltage Sensor to the SignalK Server

Once on the network, SensESP should automatically find your Signal K server, assuming it has mDNS enabled (see instructions below). So, first, bring up the SingnalK Server web page (http://localhost or http://remoteip). Next, login as Admin into the Dashboard via a web browser, select Server – Settings from the left side menu and make sure the “mdns” option is ON.

If your server has security enabled (it does by default), you should see an access request for your ESP in the Signal K Dashboard, under Security – Access Requests. (You must be logged into the Signal K Server to see the Security sub-menu.) Set the “Authentication Timeout” field to “NEVER”, set the Permission to “Read / Write”, then Approve it. You should start getting data on the Signal K Instrument Panel. (Dashboard – Webapps – Instrument Panel) You can see lots of activity in the Visual Studio Code Serial Monitor, including the connection to the Signal K Server, the request for and approval of the security token, and the flow of data.

If you have any problems with configuring the WiFi credentials, or with SensESP finding your Signal K server, you can hard-code those settings. See Hard-coding Certain Program Attributes.

Once you’ve added it, you’ll need to restart the Signal K server (upper right). You have to do this whenever you make a configuration change to the server or plugin, add a plugin, etc.

Signal K Data Ingestion

So, if everything is working correctly. Now you will see the Arduino Voltage Sensor sending data to the SignalK server. You will seethe data under the data browser. Secondly, you can see in the “Signalk-Dashboard-Data”, screenshot that I’m collecting an additional two inputs (Fuel.1.Tank and Water.1.Tank). This is a sensor that has the ADS1115 Connected. Additionally, you should data being logged. Lastly. I am seeing voltage because the sensor is not grounded.

  • Signalk-Dashboard
  • Signalk-Dashboard-Data

You will also see the IP address of the ESP 8266 Voltage Sensor in the window above, the “Signalk-Dashboard-Data” will list the IP address of the Sensors. In this case, we only have one. You can log into the Sensor and see how it can be configured. Below are screenshots of the data and the device.

Wrapping things up in this Installment!

What we have up to this point is a functioning installation of the Hardware, Software Components of the SignalK Server, and a Sensor sending data to the SignalK server. All of this is running on the bench and working

ESP 8266 Voltage Sensor – Part Six Conclusion

  • Node-MCU-1
  • Node-MCU-2
  • Node-MCU-3
  • Generator Voltage Sensor

So what we have up to this point is a functional Voltage sensor sending data to the SignalK server and logging data. We still have yet to install InfuxDB and Grafana.

Additionally, in the next Part of this series. We will configure the ADS1115, add the four additional batteries, to the ESP 8266 Voltage Sensor, and do some testing before I install this on “Nauti”. When we are done we will read the more than the Battery voltage of the generator battery. We will monitor the Battery, current, charge rate, voltage, and discharge rate and display it on the RayMarine Axiom RV 9. So until the next installment, Hopefully this inspires you to build your first sensor and to start working on transforming your boat into a modern connected boat!

Leave a Reply

Your email address will not be published. Required fields are marked *