· 4 years ago · May 06, 2021, 04:32 PM
1
2/* Modified version off
3*********************************************************************
4*
5* Example sketch WifiTest.ino version 7.01.
6* From Kjell & Company's book "Hur funkar Arduino?" version 7.01.
7* From Kjell & Company's book "Hvordan virker Arduino?" version 7.01.
8*
9* Based on Webserver example by Bportaluri.
10* http://kjll.cm/arduino-wifiesp
11*
12* *****************************************************************
13*
14* and
15* ws2812progress Exemple from Octoprintapi
16*
17* ******************************************************************
18*
19* Use the getPrintJob() function to get the current print job on
20* your 3D printer being managed by OctoPrint. Output the current
21* progress of the job on a strip (or circle!) of 'Neopixel' LEDs
22* such as WS2812B. Uses the FastLED library to drive pixels.
23*
24* You will need the IP or hostname of your OctoPrint server, a
25* port number (will be 80 unless you are reaching it from an
26* external source) and an API key from the OctoPrint
27* installation - http://docs.octoprint.org/en/master/api/general.html#authorization
28* You will also need to enable CORS - http://docs.octoprint.org/en/master/api/general.html#cross-origin-requests
29*
30* OctoprintApi by Stephen Ludgate https://www.youtube.com/channel/UCVEEuAouZ6ua4oetLjjHAuw
31*
32*
33* Example based on original work written by A. Fichtner - https://github.com/anfichtn
34*
35*******************************************************************
36
37 By Mrfalk With Arduino Mega 2560 and Esp8266 wifi module https://nettigo.eu/system/images/2121/original.jpg?1490345692
38*/
39
40// Include library
41
42#include <SoftwareSerial.h>
43#include <WiFiEsp.h>
44#include <WiFiEspClient.h>
45#include <OctoPrintAPI.h> //This is where the magic happens... shazam!
46#include <FastLED.h>
47
48//*********************************************************************************
49
50#define LED_PIN 5 // The pin connected to Data In on the strip
51#define NUM_LEDS 55 // Number of LED's on your strip
52#define BRIGHTNESS 50 // Set Brightness value
53#define LED_TYPE WS2811 // Define led type
54#define MILLI_AMPS 2000 // Maximum current your power source can deliver in mA
55#define COLOR_ORDER GRB // Color Order change if needed
56#define CONNECTION_COLOR 0xFF00FF // Purple WiFi connection status color
57CRGB leds[NUM_LEDS];
58
59// Create WiFi module object on GPIO pin 6 (RX) and 7 (TX)
60SoftwareSerial WifiSerial(10, 11);
61
62// Declare and initialise global arrays for WiFi settings
63const char ssid[] = "SSID";
64const char pass[] = "PASSWORD";
65WiFiEspClient Client;
66
67// Declare and initialise variable for radio status
68int status = WL_IDLE_STATUS;
69
70// You only need to set one of the of follwowing:
71//IPAddress ip(192, 1, 2, 3); // Your IP address of your OctoPrint server (internal or external)
72char *octoprint_host = "http://octopi.local"; // Or your hostname. Comment out one or the other.
73int 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
74String octoprint_apikey = "APIKEY"; //See top of file or GIT Readme about getting API key
75
76unsigned long api_mtbs = 10000; //mean time between api requests (10 seconds)
77unsigned long api_lasttime = 0; //last time api request has been done
78byte connection_retry = 0;
79byte point = 0;
80
81long printed_timeout = 3600000; //60 mins in milliseconds - timeout after printing completed, to clear strip
82long printed_timeout_timer = printed_timeout;
83
84// Use one of the following:
85//OctoprintApi api(Client, ip, octoprint_httpPort, octoprint_apikey); //If using IP address
86OctoprintApi api ( Client, octoprint_host, octoprint_httpPort, octoprint_apikey); //If using hostname. Comment out one or the other.
87
88void setup() {
89// Initialize serial for debugging
90 Serial.begin(115200);
91
92// Initialize serial for ESP module
93 WifiSerial.begin(9600);
94
95// Initialize ESP module
96 WiFi.init(&WifiSerial);
97
98// Check for the presence of the shield
99 if (WiFi.status() == WL_NO_SHIELD) {
100 Serial.println("WiFi shield not present");
101
102// Don't continue
103 while (true);
104 }
105
106// Attempt to connect to WiFi network
107 while (status != WL_CONNECTED) {
108 Serial.print("Attempting to connect to SSID: ");
109 Serial.println(ssid);
110
111// Connect to WPA/WPA2 network
112 status = WiFi.begin(ssid, pass);
113 }
114
115 Serial.println("You're connected to the network");
116 printWifiStatus();
117
118//Led startup/connection config ********************************************
119
120 FastLED.addLeds<WS2811, LED_PIN, GRB>(leds, NUM_LEDS);
121 FastLED.setDither(false);
122 FastLED.setCorrection(TypicalLEDStrip);
123 FastLED.setBrightness(BRIGHTNESS);
124 FastLED.setMaxPowerInVoltsAndMilliamps(5, MILLI_AMPS);
125 fill_solid(leds, NUM_LEDS, CRGB::Black);
126 delay(100);
127 FastLED.show();
128 fill_solid(leds, NUM_LEDS, CRGB::Red);
129 delay(2000);
130 FastLED.show();
131 fill_solid(leds, NUM_LEDS, CRGB::Yellow);
132 delay(2000);
133 FastLED.show();
134 fill_solid(leds, NUM_LEDS, CRGB::Green);
135 delay(2000);
136 FastLED.show();
137
138 while (WiFi.status() != WL_CONNECTED) {
139 delay(400);
140 Serial.print(".");
141 fill_solid(leds, NUM_LEDS, CRGB::Black);
142 leds[connection_retry] = CONNECTION_COLOR;
143 delay(100);
144 FastLED.show();
145 if (connection_retry == NUM_LEDS) {
146 connection_retry = 0;
147 }
148 else {
149 connection_retry++;
150 }
151 }
152//if you get here you have connected to the WiFi
153 fill_solid(leds, NUM_LEDS, CRGB::Black);
154 FastLED.show();
155}
156void loop()
157{
158// put your main code here, to run repeatedly:
159 if (millis() - api_lasttime > api_mtbs || api_lasttime == 0)
160 { //Check if time has expired to go check OctoPrint
161 if (WiFi.status() == WL_CONNECTED)
162 { //Check WiFi connection status
163 if (api.getPrintJob())
164 { //Get the print job API endpoint
165 if ((api.printJob.printerState == "Printing"))
166 {
167 FastLED.clear();
168 // we are printing something....
169 Serial.print("Progress:\t");
170 Serial.print(api.printJob.progressCompletion);
171 Serial.println(" %");
172
173 int progress = int((NUM_LEDS * int(api.printJob.progressCompletion)) / 100);
174
175 //Set at least one pixel on when the job starts, as the strip may look 'off' for some time.
176 if (progress < 1)
177 {
178 progress = 1;
179 }
180
181 fill_solid(leds, progress, CRGB::Green);
182 delay(100);
183 FastLED.show();
184
185 printed_timeout_timer = 0;
186 }
187 else if (api.printJob.progressCompletion == 100 && api.printJob.printerState == "Operational" && printed_timeout_timer < printed_timeout)
188 {
189 // 100% complete is no longer "Printing" but "Operational"
190 FastLED.clear();
191 Serial.print("Progress:\t");
192 Serial.print(api.printJob.progressCompletion);
193 Serial.println(" %");
194
195 int progress = int(NUM_LEDS); //FULL LEDs
196
197 fill_solid(leds, progress, CRGB::Green);
198 delay(100);
199 FastLED.show();
200
201 printed_timeout_timer += api_mtbs;
202 }
203 else if (api.printJob.printerState == "Offline" || api.printJob.printerState == "Operational" && printed_timeout_timer >= printed_timeout)
204 {
205 // we are without working printer.... or the printer is no longer printing.... lights off
206 fill_solid(leds, NUM_LEDS, CRGB::Black);
207 delay(100);
208 FastLED.show();
209 }
210 }
211 api_lasttime = millis(); //Set api_lasttime to current milliseconds run
212 }
213 }
214}
215void printWifiStatus() {
216// Print the SSID of the network
217 Serial.print("SSID: ");
218 Serial.println(WiFi.SSID());
219// Print the IP address
220 IPAddress ip = WiFi.localIP();
221 Serial.print("IP Address: ");
222 Serial.println(ip);
223}