-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduinoPrinterHost.ino
More file actions
302 lines (269 loc) · 12.4 KB
/
Copy pathArduinoPrinterHost.ino
File metadata and controls
302 lines (269 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
This sketch will receive data from octoprint, write it to thingspeak channels, and display the data on a neopixel ring.
*/
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <String.h>
#include <OctoPrintAPI.h>
#include <Adafruit_NeoPixel.h>
#include <WiFiClient.h>
unsigned long api_mtbs = 60000; //mean time between api requests
unsigned long api_lasttime = 0; //last time api request has been done
unsigned long api2_mtbs = 60000; //mean time between api requests
unsigned long api2_lasttime = 0; //last time api request has been done
#define ringLength 16
#define PIN 12
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
uint32_t off = strip.Color(0,0,0);
WiFiClient client;
//------------------------------------------
// User Data
//------------------------------------------
const char* ssid = "----------"; //your SSID(name) of WIFI
const char* password = "----------"; // password of Wifi
String OctoPrintAPIKey = "----------"; //API key from OctoPrint
String PrinterTSAPIKey = "----------"; //thingspeak PrinterHost.Printer write Key
String JobTSAPIKey = "----------"; //thingspeak PrinterHost.Job API Key
IPAddress ip(--, -, -, ---); // IP adress of Raspberry Pi. 11.1.1.111 = (11, 1, 1, 111)
const int octoprint_httpPort = 80; //If you are connecting through a router this will work, but you need a random port forwarded to the OctoPrint server from your router. Enter that port here if you are external
//------------------------------------------
// Color Setttings
//------------------------------------------
int background = 1; //Would you like yout Printer Host to show a color even when not printing? 1 = yes 0 = no
int printingBackgound = 0; //Would you like the backround to be displayed even when the printer is prining? 1 = yes 0 = no (disregard if you do not want a background)
int brightness = 150; //How bright would you like your Neopixel Display? 1-255
uint32_t standby = strip.Color(0, 0, 255); //What color would you like to show as the background (disregard if you do not want a background)
uint32_t error = strip.Color(255, 0, 0); //What color would you like to indicate that the printer is disconnected or experiencing an error
uint32_t paused = strip.Color(255, 255, 0); //What color would you like to indicate that the printer is paused
uint32_t printing = strip.Color(0, 255, 0); //What color would you like to indicate that the printer is printing
//All Colors Are In RGB Format
//------------------------------------------
//------------------------------------------
String operational;
String status;
String fileName;
String percentComplete;
int percent;
int timePrinted;
int timePrintedRemaining;
String timePrintedHours;
String timePrintedMinutes;
int timeLeft;
int timeLeftRemaining;
String timeLeftHours;
String timeLeftMinutes;
int estimatedTime;
int estimatedTimeRemainder;
String estimatedTimeHours;
String estimatedTimeMinutes;
const char* ESP_HOST_NAME = "esp-" + ESP.getFlashChipId();
const char* host = "api.thingspeak.com";
//WiFiClient client;
OctoprintApi api(client, ip, octoprint_httpPort, OctoPrintAPIKey);
void wifiConnect(){
WiFi.begin(ssid, password);
Serial.print("ssid");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected!");
Serial.println(WiFi.localIP());
Serial.println();
}
void setup() {
Serial.begin(115200);
delay(10);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
strip.setBrightness(brightness);
// start by connecting to WiFi
wifiConnect();
}
void loop() {
delay(100);
//-----------------------------------------------------------------------------
// Fetch data from Octoprint
//-----------------------------------------------------------------------------
if (millis() - api_lasttime > api_mtbs || api_lasttime==0) { //only do this after a certain period of time has passed
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
if(api.getOctoprintVersion()){
Serial.println("---------Version---------");
Serial.print("Octoprint API: ");
Serial.println(api.octoprintVer.octoprintApi);
Serial.print("Octoprint Server: ");
Serial.println(api.octoprintVer.octoprintServer);
Serial.println("------------------------");
}
Serial.println();
if(api.getPrinterStatistics()){
Serial.println("---------Stats---------");
Serial.print("Printer Current State: ");
Serial.println(api.printerStats.printerState);
Serial.print("Printer State - closedOrError: ");
Serial.println(api.printerStats.printerStateclosedOrError);
Serial.print("Printer State - error: ");
Serial.println(api.printerStats.printerStateerror);
Serial.print("Printer State - operational: ");
Serial.println(api.printerStats.printerStateoperational);
Serial.print("Printer State - paused: ");
Serial.println(api.printerStats.printerStatepaused);
Serial.print("Printer State - printing: ");
Serial.println(api.printerStats.printerStatePrinting);
Serial.print("Printer State - ready: ");
Serial.println(api.printerStats.printerStateready);
Serial.print("Printer State - sdReady: ");
Serial.println(api.printerStats.printerStatesdReady);
Serial.println("------------------------");
}
Serial.println();
if(api.getPrintJob()){ //Get the print job API endpoint
Serial.println("---------------------------");
Serial.print("Printer current state string:\t");
Serial.println(api.printJob.printerState);
Serial.println();
Serial.println("----------Job (File information)--------");
Serial.print("jobFileDate (Epoch) long:\t");
Serial.println(api.printJob.jobFileDate);
Serial.print("jobFileName String:\t\t");
Serial.println(api.printJob.jobFileName);
Serial.print("jobFileOrigin String:\t\t");
Serial.println(api.printJob.jobFileOrigin);
Serial.print("jobFileSize (bytes) long:\t");
Serial.println(api.printJob.jobFileSize);
Serial.print("estimatedPrintTime (sec) long:\t");
Serial.println(api.printJob.estimatedPrintTime);
Serial.println("----------------------------------------");
Serial.println();
Serial.println("-------Job (Progress information)-------");
Serial.print("progressCompletion (%) float:\t\t");
Serial.println(api.printJob.progressCompletion);
Serial.print("progressFilepos (bytes) long:\t\t");
Serial.println(api.printJob.progressFilepos);
Serial.print("progressPrintTime (sec) long:\t\t");
Serial.println(api.printJob.progressPrintTime);
Serial.print("progressPrintTimeLeft (sec) long:\t");
Serial.println(api.printJob.progressPrintTimeLeft);
Serial.println("----------------------------------------");
Serial.println();
}
//Map Values to Variables
operational = api.printerStats.printerStateoperational;
if(api.printerStats.printerStatePrinting == 1){
status = "1"; //if printing, set status to printing
}else if(api.printerStats.printerStatepaused == 1){
status = "2";
}else if(api.printerStats.printerStateready == 1){
status = "3";
}else{
status = "4";
}
//map all received data to variables and convert times to hh:mm format
fileName = api.printJob.jobFileName;
estimatedTime = api.printJob.estimatedPrintTime;
percentComplete = api.printJob.progressCompletion;
percent = percentComplete.toFloat();
timePrinted = api.printJob.progressPrintTime;
timeLeft = api.printJob.progressPrintTimeLeft;
timePrintedHours = timePrinted / 3600;
timePrintedRemaining = timePrinted % 3600;
timePrintedMinutes = timePrintedRemaining /60;
timeLeftHours = timeLeft / 3600;
timeLeftRemaining = timeLeft % 3600;
timeLeftMinutes = timeLeftRemaining /60;
estimatedTimeHours = estimatedTime / 3600;
estimatedTimeRemainder = estimatedTime % 3600;
estimatedTimeMinutes = estimatedTimeRemainder /60;
}
api_lasttime = millis();
}
//-----------------------------------------------------------------------------
// Update thingspeak with new data from octoprint
//-----------------------------------------------------------------------------
//connect to thingspeak
if (millis() - api2_lasttime > api2_mtbs || api2_lasttime==0) {
// Serial.print("connecting to ");
//Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
//update data
//updates values in printer thingspeak channel
client.print(String("GET ") + "/update?api_key="+PrinterTSAPIKey+"&field1="+operational+"&field2="+status+" HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n"); //updates values in printer thingspeak channel
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
//updates values in job thingspeak channel
client.print(String("GET ") + "/update?api_key="+JobTSAPIKey+"&field1="+fileName+"&field2="+estimatedTimeHours+"&field3="+estimatedTimeMinutes+"&field4="+percentComplete+"&field5="+timePrintedHours+"&field6="+timePrintedMinutes+"&field7="+timeLeftHours+"&field8="+timeLeftMinutes+" HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n"); //updates values in job thingspeak channel
//serial print data: for debugging
Serial.println("Thingspeak Updated");
delay(10);
Serial.println("operational = " + operational);
delay(10);
Serial.println("status = " + status);
delay(10);
Serial.println("file name = " + fileName);
delay(10);
Serial.println("estimated print time = " + estimatedTimeMinutes);
delay(10);
Serial.println("percent complete = " + percentComplete);
delay(10);
Serial.println("hours printed = " + timePrintedHours);
delay(10);
Serial.println("minutes printed = " + timePrintedMinutes);
delay(10);
Serial.println("Hours remaining = " + timeLeftHours);
delay(10);
Serial.println("mnutes remaining = " + timeLeftMinutes);
api2_lasttime = millis();
}
//-----------------------------------------------------------------------------
// Display data using neopixels
//-----------------------------------------------------------------------------
if(printingBackgound == 0){
for(int i = 0; i<= ringLength; i++){ // clear the pixels between displays
strip.setPixelColor(i, off);
}
}
if (operational == "0"){
for(int i = 0; i<= ringLength; i++){ //display error color if experiencing an error
strip.setPixelColor(i, error);
}
}else{
if(status == "1"){
drawRing(printing, percent); //display printing color on number of pixels representing print completion percentage
}else if(status == "2"){
drawRing(paused, percent); //display paused color on number of pixels representing print completion percentage
}else{
if(background = 1){
for(int i = 0; i<= ringLength; i++){
strip.setPixelColor(i, standby); //display background color if a backround is wanted
}
}else{
for(int i = 0; i<= ringLength; i++){
strip.setPixelColor(i, off); //turn all pixels off if no background is wanted
}
}
}
}
strip.show();//show the pixels in their written state
}
void drawRing(uint32_t color, float percent){
int pixelPercent = 100/ringLength; // the percentage needed to fill one pixel
int fullPixels = percent/(pixelPercent); //the number of pixels that should be lit up
int i = 0;
while (i < fullPixels){ //color all pixels up to fullPixels
strip.setPixelColor(i, color);
i++;
}
}