3D printing
Filament Health Monitor (Part 4 – Software)

Filament Health Monitor (Part 4 – Software)

Quick Links

Preamble

In this section we will be covering how to setup and program the D1 Mini. For this tutorial I will still be using the Arduino IDE, however for actual development I use Visual Studio Code. It takes a little setup but brings you code completion and suggestions. It is really great and I have documented how to set it up here: Arduino Development in Visual Studio Code

The code I ended up with can be found over on GitHub at: https://github.com/tshilling/FilamentHealthMonitor

Arduino IDE D1 Mini

The Arduino IDE does not natively work with the ESP8266 family of microcontrollers. However, others have plowed this path for us already and making it compatible is now very easy.

1.) First, if you don’t already have it, install the most recent version of the Arduino IDE.  When written that was 1.8.7 and can be found here: Arduino website.

NOTE: Skip the Microsoft store app version and the webapp version. For this to work you need to use the versions available from the link above.

2.) Once installed, open the IDE and proceed to File-Preferences:

3.) In the resulting dialog box, find the “Additional Boards Manager URLs:” input and paste the following link into it: https://arduino.esp8266.com/stable/package_esp8266com_index.json

NOTE: If you already have links in this textbox or need to add multiple links, you can separate URLs via commas.

4.) Close the Properties dialog and proceed to Tools-Board-Board Manager. 

5.) In the search box, enter ESP.  You will be presented with several options, choose the esp8266 by esp8266 Community one and install.

6.) Once installation is complete, we must select the proper board from the now updated options.  Proceed to Tools-Board and select from the list “WeMos D1 R1” which is the none generic name for the boards we are using from Amazon (The list of boards is pretty long now and you may have to scroll to find it.)

You should now be good to go. If you have problems, head over to the home of the good people who made this library: https://github.com/esp8266/Arduino

Installing the Libraries

We are using several different components and each needs to be interfaced with in a particular way. To achieve these interfaces and make life as easy as possible, we will be using the following libraries:

ComponentLibrary Used
HX711 (Scale)HX711 by Bogdan
DHT-11 (Humidity / Temp)Adafruit DHT
SSD1306 (LCD Display)Adafruit GFX
Adafruit SSD1306
TCS34725 (Color Sensor)Adafruit TCS34725

To add each of these libraries, in the Arduino IDE, go to Sketch-Include Library-Manage Libraries. From that dialog, search for each of the above Libraries. Double check that you have the right one installed and install each. Once the libraries are installed, you should be able to finally open the .ino file downloaded earlier.

Preparing the Code

If you haven’t done so already, download the code from the link above. I would recommend you then go to “Documents-Arduino” or wherever else your Arduino sketches are normally stored on your computer. Create a folder called “FilamentMonitor”. Then, within this folder you just created paste the following files:

  • FilamentMonitor.ino
  • index.h
  • index.html

The first file is the sketch itself, it contains the main program. The next file, the .h file is how we are getting of html content into the program memory of the ESP8266. In the *.ino file we have the line “import “index.h” which will import the .h files contents. The final file is the working file for the html. I edit the page that the esp will deliver in the .html file and then copy its content to the .h file when I am happy with it. Next, you will want to configure the wifi network information. find the following lines of code and enter your specific information inside the quotes:

#ifndef STASSID
    #define STASSID "name"        // Network Name
    #define STAPSK  "password"     // Network Passcode
#endif

Write the code to the D1 Mini

Once you have the *.ino file how you want it and the libraries installed writing the program to the D1 Mini is performed exactly the same way as most other Arduinos. Press the “Upload” button and the IDE will go from there. However, what you see in the console readout and how long it takes will be different than normal. First you will see a lot more lines of console output. Secondly it will take up to 30sec to burn.

Testing and Calibrating

If you have built without errors the filament monitor should boot up and the screen in the front should display some information. Before the ESP8266 connects to your WIFI, the IP shown at the top center of the screen will be “000.000.000.000”. Once it has connected, it will show its new IP on your network. Open a browser and type that IP address into your navigation bar and press enter. If everything is going well, you should see this page:

You’re Weight is currently probably something ridiculously large or small. Right now the value you are seeing is the raw 24bit value from the HX711. With the Monitor sitting flat on a surface and with nothing on it, Tare it just like any other scale. To get the “tare” option, just hover over the readout for the Weight and a dropdown will appear:

Taring will set the zero point for the scale. Next we need to calibrate another point on the scale. To do this it is best to have a full roll of filament and if able, weight it with another scale. If you don’t have a scale, my full rolls of OVERTURE / Amazon Basic PETG weight an average of 1266g. Enter this weight into the Weight box and press “Weight”:

The final option under the Weight drop down is the Offset Option. Right now, you scale should read “1266” or whatever weight you entered in the last step. If you want the scale to report how much filament is left, we need to provide an offset for the weight of the empty spool. This is usually around 250g but based on my number above and assuming that when I buy 1kg of filament I get 1kg, the spool should weight 266g. Enter -266 (Or your own number if better known) into the offset and press the Offset button.

NOTE: Offset is negative because you want the scale to read 266 less than the actual weight.

Your scale should now be calibrated. These calibration settings are stored in EEPROM and will persist even after power cycle and after future program uploads.

Next we need to calibrate the color sensor. Lets start with expectation management. I was never able to get the color detection to render the exact color on my html page. I got close enough that I could tell given the fidelity and available tones of filament a color well enough. To calibrate well you will need a roll of black and a roll of white filament. First, take your white filament and set it on the scale. Then hover over the color square on the html page to see the color dropdown:

Press the “Cal White” Button. Then, grab your black filament, place it on the scale and press the “Cal Black” Button. Now your Color readout should be close to what is actually on the scale. Test it out with a few other colors to see if it satisfies you. In daylight, my Orange turns out pretty good. In darker rooms, it looks more Salmon. If anyone has ideas on how to improve this, please share.

That’s all there is. Go forth and explore, improve and report back. Happy Making.