ChipChop Support Forum
Log in
Log out
Join the forum
My Details
REPLIES: 7
VIEWS: 80
BACK
REPLY TO THIS POST
SteveElves
19 Dec 2024
Countdown Timer implementation
I have an D1 Mini ESP8266 controlling temperature in a salvaged food dehydrator that I'm using as a 3D print filament dryer. When started, the code looks at the requested dry time and starts counting down. It monitors the temperature in the dehydrator and turns the heating element on and off to maintain the desired setpoint (temperature based on the filament type). I also monitor for temperature runaway and have a second relay that will kill all power to the dehydrator if the heater relay should malfunction and fail to shut off.

I've implemented the code in Blynk but I'd much rather run it in ChipChop (I don't trust Blynk to keep my cheap plan available to me for much longer!). I have another ChipChop device that prints a line on the serial monitor that looks like this:

"21:54:21.814 -> {"status":"ok","timestamp":1734386058656}"

I understand that the "timestamp" is a UNIX time value. I'd like to be able to use the timestamp as a variable in my dryer sketch, so that I can set a dry time and then simply compare the timestamp that was received right after I started the dryer to the most recent timestamp. A little math would then give me the runtime of the current dry cycle and the time remaining.

My question is this: what code do I use in my Arduino sketch to capture the timestamp? I looked through the various components in the "src/" subfolder, but couldn't come up with anything. There must be some code that prints to the serial monitor - if I knew what it was or where to find it, I could grab the value.

Anyone have any ideas? I'm almost certain that it will be somewhere right under my nose, but I can't seem to find it.
Gizmo
19 Dec 2024

Hiya, I'm back from my trip (absolutely messed up with 4 timezone jet-lag :-)

Ok, so, you have a bunch of different ways you can do this.

I am actually waiting for delivery of a new 3D printer and I'll have to do exactly the same thing myself as this will be my first filament printer, I've been using the SLA for years but need a cheaper alternative right now.

First, to answer your question about the timestamp. The time calculations are built into the ChipChop library so you don't have to do anything special, you just ask it what the time is :-)
The library runs it's internal clock and adjusts it every time it receives the timestamp and keep adjusting it every second, so even if the ESP was to go off-line the clock will still work keep ticking and update itself every 1 second.
Also, the timestamp received is adjusted to the timezone you've specified for that device and will follow any daylight time saving etc...ChipChop as a platform has its own dedicated physical Time Server that recalculates all timezones for I think 3500 cities every minute and informs all API servers when time changes.

I would definitely recommend that use the ChipChop library as the timestamp received is in milliseconds and the ESP8266 can't handle it directly as it wouldn't fit in its max-integer (timestamp = 64bit, esp8266 = 32bit) so there's a lot of work going in the background.

Here's how:

- First ensure that in the setup you specify: ChipChop.useClock(true)
- Optionally if you have a screen and want to display a formatted time you can also specify ChipChop.setDateFormat() ..see below
    
    


    // tell the library to activate the time tracking
    ChipChop.useClock(true);

    // YY - year 2 digits, YYYY - year 4 digits, MM - month number, MMM - month short string, MMMM - month long string, dd - day as number , hh - hour, mm - minute, ss - second

    ChipChop.setDateFormat("dd MMM YY - hh:mm:ss"); // would return a string like: 01 Jan 2025 - 18:45:32





To retrieve the time whenever you want here are the various commands:





    Serial.println(ChipChop.Date.year); // as integer
Serial.println(ChipChop.Date.month); // numeric month - 1, 2, 3, 4...12
Serial.println(ChipChop.Date.monthShort); // Jan, Feb, Mar ....
Serial.println(ChipChop.Date.monthLong); // January, February, March...
Serial.println(ChipChop.Date.day); // Day 1 = Monday and day 7 = Sunday

Serial.println(ChipChop.Date.hour); //in 24 hrs format
Serial.println(ChipChop.Date.minute);
Serial.println(ChipChop.Date.second);

Serial.println(ChipChop.Date.timestamp); // the current UNIX timestamp recalculated in seconds from milliseconds

// automatically formatted date/time string, useful to display on lcd/oled screens
// returns date & time String like: 01 Jan 2025 - 18:45:32
Serial.println(ChipChop.Date.formatted);



There is a gazillion ways you can then use the time retrieved for calculations, one trick I've come up with to for example have something happen at a certain time of the day (i.e. real human time) is to use what I call "hour minutes" or "hour seconds" so 15:46h would be
    
    15 * 60 + 46 = 946 that would be the 946 th minute of the day, quite useful for setting up alarms

Let's say you want to check if something has happened between two times, dunno 8:30AM and 5:15PM




    // you really should use a 24hr clock (AM/PM adds extra complexity) so 5:15PM is 17:15

    int time1 = (8 * 60) + 30; // 8:30am would be the 510th minute of the day
    int time2 = (17 * 60) + 15;
    
    int current_time = (ChipChop.Date.hour * 60) + ChipChop.Date.minute;

    if( current_time >= time1 && current_time <= time2){
            
        // we are within the time range specified

    }

    




Here are some ideas you can use to implement this

Option 1:

- Have a component called "start" of type "ON/OFF"

- If you know by heart the filament drying times then add in the Dev Console another component named "drying_time" of the type "PIN Code" so you can simply enter the time in minutes from the app and send it to the device

On the ESP:

    


int drying_minutes = 0;
int drying_timeout_time = 0;
bool drying = 0;

void ChipChop_onCommandReceived(String component,String value, String source, int command_age){
    
    if(component == "drying_time"){
        drying_minutes = value.toInt();

    }else if(component == "start"){

        int current_time = (ChipChop.Date.hour * 60) + ChipChop.Date.minute;
        drying_timeout_time = current_time + drying_minutes; // this will give you the time when the drying should stop

        drying = 1; // then use the loop to check if the drying should finish
    }

}
    



- if you don't want to remember the times then you can maybe add a "Button Group" component named "drying_time" and add a bunch of button labels i.e. PLA, Nylon, ABS etc...



int drying_minutes = 0;
int drying_timeout_time = 0;
bool drying = 0;
// preset times in minutes for different filaments
int PLA = 35;
int nylon = 86;
int ABS = 124;

void ChipChop_onCommandReceived(String component,String value, String source, int command_age){
    
    if(component == "drying_time"){
        //this can be done more elegantly but to give you an idea
        // the value received in this case would be name of the button label you've set in the button group component
        if(value == "PLA"){
            drying_minutes = PLA;
        }else if(value == "Nylon"){
            drying_minutes = nylon;

        } //... etc

    }else if(component == "start"){

        int current_time = (ChipChop.Date.hour * 60) + ChipChop.Date.minute;
        drying_timeout_time = current_time + drying_minutes; // this will give you the time when the drying should stop

        drying = 1; // then use the loop to check if the drying should finish
    }

}
    


an in the loop:




void loop(){

    if(drying == 1){

        // maybe also include another timer so the check is done every minute
        
        int current_time = (ChipChop.Date.hour * 60) + ChipChop.Date.minute;

        if( current_time >= drying_timeout_time ){
            
            // we've reached the specified time so stop the dryer

            drying = 0; // stop this check

        }
        

    }

}




Option 2:

Instead of using the drying time as a target you would use the internal humidity of the desiccator and use an Action to handle it

- have a humidity component and sensor and keep sending it's value (you just need an AHT10 sensor on the ESP)
- have the "start" button component

- create an action that will monitor if the "start" button component is "ON" (meaning the desiccator is running) and if the humidity goes "below" some threshold you would send some "pause" command to the ESP and also have a reverse action that if the humidity goes high to send some "restart" command

(I've modified my kitchen extractor to do exactly that so it starts automatically when you start cooking and shuts itself down when you after a while when you stop :-)

Option 3.

Combination of the above


As I said I will be making exactly the same thing soon and I was thinking to go the humidity route but it sounds like it's the drying time and the internal temperature that I should be using like you are?




Gizmo
19 Dec 2024

Ah, just realised, the code in the previous post wouldn't be correct as it wouldn't account for day change (starting before midnight and finishing the next day) so the correction I think would be:



    ...blah blah

    }else if(component == "start"){

        int current_time = (ChipChop.Date.hour * 60) + ChipChop.Date.minute;
        drying_timeout_time = current_time + drying_minutes;

        if(drying_timeout_time > 1440) { // there is 1440 minutes in a day

            drying_timeout_time = drying_timeout_time - 1440; // that should give us the target minute time in the next day
        }

        drying = 1;
    }




This only accounts for a period spanning a maximum of two days
SteveElves
19 Dec 2024

Thanks a lot - that looks great! I'll try to implement this over the next few days and let you know.

With regard to controlling the drying of the filament, I gave this some thought. The issue in my mind around using humidity in the dessicator as a guide is as follows:

Humidity is usually represented as %RH, which is "Relative Humidity". This value is temperature-dependent, since the ability of air to hold water increases with temperature. Inside the dessicator the filament will be a source of water vapour. To use measured RH as a reliable indicator of filament dryness without knowing how much water it contained at the beginning of the dry cycle, you'd have to bring the chamber plus the filament up to temperature and then wait for the measured RH to stabilize, fall, and then level off again, indicating that the filament was no longer behaving as a water source. I don't have any information about the amount of water a given volume of filament can hold - certainly it will depend on filament type, but I'd be surprised if even a very "wet" filament changed the measured %RH by a whole lot. The accuracy of home-style RH sensors is not spectacular - I'd be happy if the reading was any better than +/- 5%. The logic for determining when the filament is no longer giving off much H2O would therefore be kinda complex and, I suspect, not particularly useful in the end.

When I have my dryer up and running again, I'm going to do a before/after weight measurement to see if I can approximate a water content value...
Gizmo
19 Dec 2024

Yeah, I was thinking along the same lines with RH as soon as I've posted my reply about using a humidity sensor :-)

I guess the amount of moisture inside the filament is quite minute per weight, I have some experience with line bending polycarbonate and the only reliable solution is to cook it long and slow and then work quickly!

Maybe I'll stick a few pounds of cat litter inside as well, that stuff absorbs moisture like mad and smells nice...works for like magic for sweaty shoes 😂

Crazily my printer arrived few hours ago, I've ordered it yesterday morning at the airport whilst waiting to board the plane! I've got the Bambu Lab X1 Carbon, the specs and general reviews sounded good, do you think it's ok?
Really intrigued to see how these spitty-melty-plastic printers compare to SLA.

I've also got the AMS thingy for it, it's a four spool/color dispenser and I've just had a better look and interestingly that thing has a humidity sensor inside . https://uk.store.bambulab.com/products/ams-multicolor-printing?id=42108955164732

The container is not heated though just a desiccant inside so the sensor is more to give you an idea what's going on at a glance. Or it's just a marketing gimmick, they do boast about using a 32bit MCU, I wander if it's a little ESP32, may have to ChipChop it !!!

Do you keep your printer in some enclosure, mine is going to be in the same area with a CNC and right next to the fibre laser so I don't know if I should extra isolate it from the dust maybe something like a small grow tent?
SteveElves
20 Dec 2024

Nice printer - a buddy of mine has one and he really likes it! You'll be happy with it, I think.

I have a Prusa MK4S that I built from a kit, but I've ordered the next one up (the Prusa Core One), which should be delivered in January. I built an enclosure for my current printer, and it worked very well for stabilizing the temperature. If I had a CNC machine I would worry a bit about airborne particulates - is it for woodworking or metal? I think the Bambu might have filters on the air intake or exhaust, so if they are fine enough it should eliminate any worry.

I occasionally print with ABS or ASA filaments and the odour is noticeable, so I put an activated carbon filter on the exhaust fan I installed on my enclosure. Works reasonably well. You're supposed to vent outdoors, but I don't like the idea of wafting bad smells at my neighbors or blowing expensively-heated air into mother nature. So far the odours haven't percolated upstairs to the point that my wife has noticed - which would be the breaking point, I'm sure!

I had the MMU2 (multi-material unit) on the MK3 iteration of my Prusa printer, and found it was somewhat erratic. I've got the MMU3 upgrade built now, but I'm going to wait to attach it to the new Core One. Nice to have, but I've never found that it's terribly useful for the kind of printing I do. I generally build project cases and small kitchen doo-dads (plastic bag clips, egg separators, funnels, etc.), none of which really benefit from multi-colored filament.

There is also water-soluble filament available for printing support structures. I think if one wanted an absolutely smooth finished surface that might be worthwhile, but since it would require two filament changes (at least!) each layer, it would vastly increase the print time and waste of filament. I think that method really requires a printer with multiple print heads, which is somewhat beyond my budget!
Gizmo
20 Dec 2024

Great info, thank you very much!

The reason why I bought this one is because I will be using it mostly for prototyping. I do occasionally product engineering (mostly health & safety equipment) and the SLA printer can be too slow for doing simple design experiments.

With SLA it takes around 1.6 hours per half inch of height regardless of the horizontal surface area and it's a dirty job with all the cleaning and chemicals. Once I finalise the design then the SLA rules and the detail quality is insane (0.05mm resolution) and for that I don't mind the wait.

The multi-color spool thingy is more to play with multi-materials as there are some filaments that have no equivalent in SLA resins and it was on sale :-) So that will be for making household trinkets!

I rarely cut wood on the CNC, it's mostly acrylic and occasionally metals and I have a dust extraction head on it so very little flies around. The thing that actually makes more extremely fine dust is believe it or not the fibre laser. I had to make a bunch of PCBs recently and as the laser rips through the copper layer some of it gets vaporised but a good part the laser literally breaks the molecular bonds of the metal and you can see it moving on the surface like it was sanded.

I think I'll have to make a dust shield/extractor for the laser and you have a good point about not extracting the warm air from the printer (and pollution!) so I think for the printer although it has a small carbon filter at the exhaust I think I'll just stick a much bigger one

Ah, now just need to find the time!


SteveElves
21 Dec 2024

I built the bog-standard Prusa enclosure based on the Ikea "Lack" tables. Cheap and dirty, but it does the job.



Here's a shot of the extractor fan - 3D printed (of course) with carbon filter behind.



Fan is controlled by a small Z-Wave temperature/smoke monitor I built. Fan comes on when chamber temperature hits 35 deg, and goes off at 30 deg. I also have a high temperature limit and smoke sensor limit coded that will kill the power to the printer if either is exceeded. I'm still researching small automatic fire extinguisher "bombs" that might be useful. I almost never print while I'm away from home, but when I do I want to be as safe as possible!



In a previous life I was one of the principals of a general insurance agency, concurrent with being a member of a fairly active volunteer fire brigade. As a result I'm probably a bit paranoid about fire, but I make no excuses for trying to be safe.


Attached images