Here is code from my latest project using a NodeMcu ESP28226 and a WS28112 LED strip to indicate the Ping speed from a website. The LED strip is connected to the D4 pin on the NodeMcu board. The LEds powered from a 5V supply and the NodeMcu has a Step-Down Power Supply Buck Module to feed it 3.7V.
#include <Adafruit_NeoPixel.h>
#include <ESP8266WiFi.h>
#include <ESP8266Ping.h>
#define PIN 2
#define NUM_PIXELS 23
// Put the WIF information on the next lines
const char* ssid = "Nhat Binh";
const char* password = "binh12345";
Adafruit_NeoPixel strip = Adafruit_NeoPixel(23, PIN, NEO_GRB + NEO_KHZ800);
//The next line is the ip address to ping..note that the numbers are seperated by commas and
// not a dot!
const IPAddress remote_ip(149,210,160,107);
// 149,210,160,107
// 72,52,4,122 slow 171ms slow
// 8,8,8,8 fast
// 212,58,244,23 no 8,8,8,1 route
void setup()
{
strip.begin();
strip.show(); // Initialize all pixels to 'off
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println();
Serial.print("WiFi connected with ip ");
Serial.println(WiFi.localIP());
Serial.print("Pinging ip ");
Serial.println(remote_ip);
}
void loop() {
bool ret = Ping.ping(remote_ip);
int avg_time_ms = Ping.averageTime();
Serial.println(avg_time_ms);
Serial.println(" ms ");
if (avg_time_ms >=1 && avg_time_ms <= 50){
knightRider(1, 32, 4, 0x00FF00); // Cycles, Speed, Width, RGB Color (green)
}
else if ( avg_time_ms >=50 && avg_time_ms <= 350){
knightRider(1, 64, 8, 0xFFFF00); // Cycles, Speed, Width, RGB Color (yellow)
}
else if ( avg_time_ms = 0){
knightRider(1, 150, 16, 0xFF0000); // Cycles, Speed, Width, RGB Color (red)
}
clearStrip();
delay(10);
}
// Cycles - one cycle is scanning through all pixels left then right (or right then left)
// Speed - how fast one cycle is (32 with 16 pixels is default KnightRider speed)
// Width - how wide the trail effect is on the fading out LEDs. The original display used
// light bulbs, so they have a persistance when turning off. This creates a trail.
// Effective range is 2 - 8, 4 is default for 16 pixels. Play with this.
// Color - 32-bit packed RGB color value. All pixels will be this color.
// knightRider(cycles, speed, width, color);
void knightRider(uint16_t cycles, uint16_t speed, uint8_t width, uint32_t color) {
uint32_t old_val[NUM_PIXELS]; // up to 256 lights!
// Larson time baby!
for(int i = 0; i < cycles; i++){
for (int count = 1; count<NUM_PIXELS; count++) {
strip.setPixelColor(count, color);
old_val[count] = color;
for(int x = count; x>0; x--) {
old_val[x-1] = dimColor(old_val[x-1], width);
strip.setPixelColor(x-1, old_val[x-1]);
}
strip.show();
delay(speed);
}
for (int count = NUM_PIXELS-1; count>=0; count--) {
strip.setPixelColor(count, color);
old_val[count] = color;
for(int x = count; x<=NUM_PIXELS ;x++) {
old_val[x-1] = dimColor(old_val[x-1], width);
strip.setPixelColor(x+1, old_val[x+1]);
}
strip.show();
delay(speed);
}
}
}
void clearStrip() {
for( int i = 0; i<NUM_PIXELS; i++){
strip.setPixelColor(i, 0x000000); strip.show();
}
}
uint32_t dimColor(uint32_t color, uint8_t width) {
return (((color&0xFF0000)/width)&0xFF0000) + (((color&0x00FF00)/width)&0x00FF00) + (((color&0x0000FF)/width)&0x0000FF);
}