execonmcode mailgun script



  • I threw this together as a poor man's notification system until either we have time to build something better or someone else does (wink wink).

    If you don't know, Mailgun is an email service which allows you to send emails automatically through their API. I believe you do have to have your own domain to get it working. It's relatively cheap (1000 emails costs $.80 US) and we use it for our mailing list, etc.. so expanding it for this was a no-brainer.

    As I stated in the title, this requires execonmcode. It also uses M-JPEG streamer for the attached snapshot.

    I chose M5545 for my custom code. Here is the service file:

    [Unit]
    Description=Duet API listener for Mailgun
    After=duetcontrolserver.service
    Requires=duetcontrolserver.service
    
    [Service]
    ExecStart=/usr/local/bin/execonmcode -mCode 5545 -command "./scripts/mailgun.sh %%C %%Z %%P %%L"
    Restart=always
    RestartSec=10
    
    [Install]
    WantedBy=multi-user.target
    

    And the script:

    #!/bin/sh
    # Mailgun Mailer
    # Usage: ./mailgun.sh Current_Layer Current_Z_Height Current_Percentage Total_Layers
    # Example: ./mailgun.sh "20" "4.0" "55.5" "36"
    # gCode: M5545 C"20" Z"4.0" P"55.5" L"36"
    #  C=Current_Layer
    #  Z=Current_Z_Height
    #  P=Current_Percentage
    #  L=Total_Layers
    
    sudo wget http://localhost:8080/?action=snapshot -O snapshot.jpg
    
    curl -s --user 'api:<your-mailgun-api>' \
      https://api.mailgun.net/v3/<your-domain>/messages \
      -F from='<your-alert-email-alias>' \
      -F to='<your-actual-email-address>' \
      -F subject='Status update: '$3'%' \
      -F text='The current job is on layer '$1' of '$4', at z-height '$2', and is '$3'% complete.' \
      -F attachment=@snapshot.jpg
    
    rm snapshot.jpg
    

    I am using Simplify3D as my Slicer. In my layer change script, I have the following:

    {IF "[current_layer]%10==0"}M5545 L"[current_layer]" Z"[current_Z_position]" P"[progress]" T"[total_layer_count]"
    

    This tells Simplify3D to put the M5545 command in the code every 10 layers. Change the "10" to whatever works best for you..

    A sample mCode is:

    M5545 C"20" Z"4.0" P"55.5" L"36"
    

    This produces an email containing the following with attached snapshot.

    • Subject: Status update: 55.5%
    • Body: The current job is on layer 20 of 36, at z-height 4.0, and is 55.5% complete.

Log in to reply