Guest User

fastledissue

a guest
Feb 2nd, 2023
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. /*******************************************************************
  2.  
  3. * Use the getPrintJob() function to get the current print job on
  4.  
  5. * your 3D printer being managed by OctoPrint. Output the current
  6.  
  7. * progress of the job on a strip (or circle!) of 'Neopixel' LEDs
  8.  
  9. * such as WS2812B. Uses the FastLED library to drive pixels.
  10.  
  11. *
  12.  
  13. * You will need the IP or hostname of your OctoPrint server, a
  14.  
  15. * port number (will be 80 unless you are reaching it from an
  16.  
  17. * external source) and an API key from the OctoPrint
  18.  
  19. * installation - http://docs.octoprint.org/en/master/api/general.html#authorization
  20.  
  21. * You will also need to enable CORS - http://docs.octoprint.org/en/master/api/general.html#cross-origin-requests
  22.  
  23. *
  24.  
  25. * OctoprintApi by Stephen Ludgate https://www.youtube.com/channel/UCVEEuAouZ6ua4oetLjjHAuw
  26.  
  27. *
  28.  
  29. * Example based on original work written by A. Fichtner - https://github.com/anfichtn
  30.  
  31. *******************************************************************/
  32.  
  33. #include <OctoPrintAPI.h> //This is where the magic happens... shazam!
  34.  
  35. #include <ESP8266WiFi.h>
  36.  
  37. #include <WiFiClient.h>
  38.  
  39.  
  40.  
  41. #define FASTLED_ESP8266_RAW_PIN_ORDER
  42.  
  43. #define FASTLED_INTERRUPT_RETRY_COUNT 0
  44.  
  45. #include <FastLED.h> // Uses FastLED Library to drive WS2812B 'Neopixel' LEDs
  46.  
  47.  
  48.  
  49. #define LED_PIN D4 // The pin connected to Data In on the strip
  50.  
  51. #define NUM_LEDS 5 // Number of LED's on your strip
  52.  
  53. #define LED_BRIGHTNESS 80 // set Brightness value
  54.  
  55. #define MILLI_AMPS 4000 // Maximum current your power source can deliver in mA
  56.  
  57. #define CONNECTION_COLOR 0xFF4500 // Purple WiFi connection status color
  58.  
  59. CRGB leds[NUM_LEDS];
  60.  
  61.  
  62.  
  63. const char* ssid = ""; // your network SSID (name)
  64.  
  65. const char* password = ""; // your network password
  66.  
  67. WiFiClient client;
  68.  
  69.  
  70.  
  71. // You only need to set one of the of follwowing:
  72.  
  73. // IPAddress ip(192, 168, 1, 174); // Your IP address of your OctoPrint server (inernal or external)
  74.  
  75. char* octoprint_host = "octopi.local"; // Or your hostname. Comment out one or the other.
  76.  
  77. 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
  78.  
  79. String octoprint_apikey = ""; //See top of file or GIT Readme about getting API key
  80.  
  81.  
  82.  
  83. unsigned long api_mtbs = 10000; //mean time between api requests (10 seconds)
  84.  
  85. unsigned long api_lasttime = 0; //last time api request has been done
  86.  
  87. byte connection_retry = 0;
  88.  
  89. byte point = 0;
  90.  
  91.  
  92.  
  93. long printed_timeout = 3600000; //60 mins in milliseconds - timeout after printing completed, to clear strip
  94.  
  95. long printed_timeout_timer = printed_timeout;
  96.  
  97.  
  98.  
  99. // Use one of the following:
  100.  
  101. //OctoprintApi api; //Be sure to call init in setup.
  102.  
  103. //OctoprintApi api(client, ip, octoprint_httpPort, octoprint_apikey); //If using IP address
  104.  
  105. OctoprintApi api(client, octoprint_host, octoprint_httpPort, octoprint_apikey); //If using hostname. Comment out one or the other.
  106.  
  107.  
  108.  
  109. void setup() {
  110.  
  111. Serial.begin(115200);
  112.  
  113. FastLED.addLeds<WS2811, LED_PIN, GRB>(leds, NUM_LEDS);
  114.  
  115. FastLED.setDither(false);
  116.  
  117. FastLED.setCorrection(TypicalLEDStrip);
  118.  
  119. FastLED.setBrightness(LED_BRIGHTNESS);
  120.  
  121. FastLED.setMaxPowerInVoltsAndMilliamps(5, MILLI_AMPS);
  122.  
  123. fill_solid(leds, NUM_LEDS, CRGB::Black);
  124.  
  125. delay(100);
  126.  
  127. FastLED.show();
  128.  
  129. fill_solid(leds, NUM_LEDS, CRGB::Red);
  130.  
  131. delay(1000);
  132.  
  133. FastLED.show();
  134.  
  135. fill_solid(leds, NUM_LEDS, CRGB::Yellow);
  136.  
  137. delay(1000);
  138.  
  139. FastLED.show();
  140.  
  141. fill_solid(leds, NUM_LEDS, CRGB::Green);
  142.  
  143. delay(1000);
  144.  
  145. FastLED.show();
  146.  
  147.  
  148.  
  149. /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
  150.  
  151. would try to act as both a client and an access-point and could cause
  152.  
  153. network-issues with your other WiFi-devices on your WiFi-network. */
  154.  
  155. WiFi.mode(WIFI_STA);
  156.  
  157. // disconnect if previously connected
  158.  
  159. WiFi.disconnect();
  160.  
  161. delay(100);
  162.  
  163.  
  164.  
  165. // We start by connecting to a WiFi network
  166.  
  167. Serial.println();
  168.  
  169. Serial.println();
  170.  
  171. Serial.print("Connecting to ");
  172.  
  173. Serial.println(ssid);
  174.  
  175. WiFi.begin(ssid, password);
  176.  
  177.  
  178.  
  179. while (WiFi.status() != WL_CONNECTED) {
  180.  
  181. delay(400);
  182.  
  183. Serial.print(".");
  184.  
  185. fill_solid(leds, NUM_LEDS, CRGB::Black);
  186.  
  187. leds[connection_retry] = CONNECTION_COLOR;
  188.  
  189. delay(100);
  190.  
  191. FastLED.show();
  192.  
  193. if (connection_retry == NUM_LEDS) {
  194.  
  195. connection_retry = 0;
  196.  
  197. } else {
  198.  
  199. connection_retry++;
  200.  
  201. }
  202.  
  203. }
  204.  
  205. //Use one of these Only if you have not initialized the api object above with parameters"
  206.  
  207. //api.init(client, ip, octoprint_httpPort, octoprint_apikey); //If using IP address
  208.  
  209. //api.init(client, octoprint_host, octoprint_httpPort, octoprint_apikey);//If using hostname. Comment out one or the other.
  210.  
  211. //if you get here you have connected to the WiFi
  212.  
  213. fill_solid(leds, NUM_LEDS, CRGB::Black);
  214.  
  215. FastLED.show();
  216.  
  217. Serial.println("");
  218.  
  219. Serial.println("WiFi connected");
  220.  
  221. Serial.println("IP address: ");
  222.  
  223. Serial.println(WiFi.localIP());
  224.  
  225. }
  226.  
  227.  
  228.  
  229. void loop() {
  230.  
  231. Serial.println("black");
  232.  
  233. fill_solid(leds, NUM_LEDS, CRGB::Black);
  234.  
  235. FastLED.show();
  236.  
  237. delay(2000);
  238.  
  239. Serial.println("Red");
  240.  
  241. fill_solid(leds, NUM_LEDS, CRGB::Red);
  242.  
  243. FastLED.show();
  244.  
  245. delay(2000);
  246.  
  247. Serial.println("Yellow");
  248.  
  249. fill_solid(leds, NUM_LEDS, CRGB::Yellow);
  250.  
  251. FastLED.show();
  252.  
  253. delay(2000);
  254.  
  255. Serial.println("Greenck");
  256.  
  257. fill_solid(leds, NUM_LEDS, CRGB::Green);
  258.  
  259. FastLED.show();
  260.  
  261. delay(2000);
  262.  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment