Hey!
So I wanted to make an esp32 react to my printers, and was very surprised to find that although there was a library for this with octoprint, there wasn’t really a moonraker equivalent! after spending a little while making my own standing on the shoulders of this project here:
https://makerworld.com/en/models/1439324-klipper-status-monitor
I have paired my project down to the file I wish I had stumbled across when I started the project! what it does is ping the printer's status and saves it to a bunch of variables!
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
//Your SSID here!
const char* ssid = "WIFINAME";
//Your WiFi Password Here!
const char* password = "PASSWORD";
//pop your printer's IP address here!
const char* printerURL = "http://YOUR-IP-HERE:7125/printer/objects/query?led%20cavity_led&print_stats&display_status&extruder&extruder1&extruder2&extruder3&heater_bed&pause_resume&fan_generic%20exhaust_fan&fan_generic%20cavity_fan&fan_generic%20circulation_fan&fan&heater_fan%20e0_nozzle_fan&temperature_sensor%20cavity";
//---------------------------------------------------------------- Here are all the variables I have pulled,
//----------- File info
float progress;
int progressPercent;
const char* state;
//----------- Nozzle Temps and Targets
float nozzleTemp;
float nozzleTarget;
float nozzlePower;
float nozzleTemp1;
float nozzleTarget1;
float nozzlePower1;
float nozzleTemp2;
float nozzleTarget2;
float nozzlePower2;
float nozzleTemp3;
float nozzleTarget3;
float nozzlePower3;
//------------ Bed Temps and Targets
float bedTemp;
float bedTarget;
//------------ Chamber Temp
float chamberTemp;
//-------- Fans!
float exhaustFan;
float cavityFan;
float circulationFan;
float partFan;
//------- LED
float LED;
//------- Layers
int currentLayer;
int totalLayer;
//------- printerStats
const char* Filename;
int totalDuration;
int printDuration;
int filamentUsed;
const char* State;
const char* Exception;
const char* Message;
bool pausedState;
//---------------------------------------------------------------- Function to setup the printer's wifi connection
void setupPrinter() {
WiFi.begin(ssid, password);
int retry = 0;
while (WiFi.status() != WL_CONNECTED && retry < 20) {
delay(500);
Serial.print(".");
retry++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("WiFi OK");
} else {
Serial.println("WiFi Fail");
return;
}
delay(200);
}
//---------------------------------------------------------------- Function to get and seperate out all of the printers info!
void checkPrinterStatus() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(printerURL);
int httpCode = http.GET();
if (httpCode == 200) {
String payload = http.getString();
StaticJsonDocument<4096> doc;
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.println("JSON Error");
} else {
//----------- Printer Progress
progress = doc["result"]["status"]["display_status"]["progress"] | 0.0;
progressPercent = int(progress * 100.0);
state = doc["result"]["status"]["print_stats"]["state"] | "unknown";
//----------- Nozzle Temps and Targets
nozzleTemp = doc["result"]["status"]["extruder"]["temperature"] | 0.0;
nozzleTarget = doc["result"]["status"]["extruder"]["target"] | 0.0;
nozzlePower = doc["result"]["status"]["extruder"]["power"] | 0.0;
nozzleTemp1 = doc["result"]["status"]["extruder1"]["temperature"] | 0.0;
nozzleTarget1 = doc["result"]["status"]["extruder1"]["target"] | 0.0;
nozzlePower1 = doc["result"]["status"]["extruder1"]["power"] | 0.0;
nozzleTemp2 = doc["result"]["status"]["extruder2"]["temperature"] | 0.0;
nozzleTarget2 = doc["result"]["status"]["extruder2"]["target"] | 0.0;
nozzlePower2 = doc["result"]["status"]["extruder2"]["power"] | 0.0;
nozzleTemp3 = doc["result"]["status"]["extruder3"]["temperature"] | 0.0;
nozzleTarget3 = doc["result"]["status"]["extruder3"]["target"] | 0.0;
nozzlePower3 = doc["result"]["status"]["extruder3"]["power"] | 0.0;
//------------ Bed Temps and Targets
bedTemp = doc["result"]["status"]["heater_bed"]["temperature"] | 0.0;
bedTarget = doc["result"]["status"]["heater_bed"]["target"] | 0.0;
//------------ Chamber Temp
chamberTemp = doc["result"]["status"]["temperature_sensor cavity"]["temperature"] | 0.0;
//-------- Fans!
exhaustFan = doc["result"]["status"]["fan_generic exhaust_fan"]["speed"] | 0.0;
cavityFan = doc["result"]["status"]["fan_generic cavity_fan"]["speed"] | 0.0;
circulationFan = doc["result"]["status"]["fan_generic circulation_fan"]["speed"] | 0.0;
partFan = doc["result"]["status"]["fan"]["speed"] | 0.0;
//------- LED
for (int i = 0; i <= 3500; i++) {
String colourSubstringtemp = payload.substring(i, (i + 36));
if (colourSubstringtemp.startsWith("color_data")) {
String LEDString = colourSubstringtemp.substring(30, 38);
LED = LEDString.toFloat();
i = 99999;
}
}
//------- Layers
currentLayer = doc["result"]["status"]["print_stats"]["info"]["current_layer"] | 0;
totalLayer = doc["result"]["status"]["print_stats"]["info"]["total_layer"] | 0;
//------- printerStats
Filename = doc["result"]["status"]["print_stats"]["filename"] | "unknown";
totalDuration = doc["result"]["status"]["print_stats"]["total_duration"] | 0.0;
printDuration = doc["result"]["status"]["print_stats"]["print_duration"] | 0.0;
filamentUsed = doc["result"]["status"]["print_stats"]["filament_used"] | 0.0;
State = doc["result"]["status"]["print_stats"]["state"] | "unknown";
Exception = doc["result"]["status"]["print_stats"]["exception"] | "unknown";
Message = doc["result"]["status"]["print_stats"]["message"] | "unknown";
pausedState = doc["result"]["status"]["pause_resume"]["is_paused"] | false;
}
} else {
Serial.println("Klipper Inactive");
}
http.end();
} else {
Serial.println("WiFi Lost");
}
}
//---------------------------------------------------------------- Normal functions!
void setup() {
Serial.begin(115200);
delay(1000);
setupPrinter();
}
void loop() {
checkPrinterStatus();
Serial.println(LED);
}
If you copy and paste this into your IDE (I used the Arduino one even though I know I should have moved onto platformIO a long time ago!) and add your wifi credentials and printer's ip address, - Then it should give you any of the outputs you might want to monitor for your project! I kept all of the outputs up at the top of the file so you can see what they are and what they're monitoring!
At the moment it prints the LEDs brightness to the serial monitor so you can see it's working!
I am not claiming that this code is particularly groundbreaking or even well done. In fact it is pretty slow and inefficient (especially 136-141, I think that may actually be some of the worst code I have ever written!)
YMMV But it works fine for me! I can now have my cabinet react to the printers lighting and status. and ping updates and notifications via espnow to the shitty lilygo esp32 watch I bought like 5 years ago!