Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.33 KB | None | 0 0
  1. *
  2.  * Net LED Project
  3.  * V1.0
  4.  *
  5.  * This Program tries to ping on a IP4 Address every 2 seconds and shows the result via the color of the LED Stripe.
  6.  *
  7.  * State-Color Table:
  8.  * State              | Color
  9.  * -------------------|------------------
  10.  * connection to WLAN | red one moving blue pixel
  11.  * ping failed        | red one moving yellow pixel
  12.  * ping ok            | green one moving magenta pixel
  13.  *
  14.  * Hardware:  
  15.  * - LED Strip with APA102 chip.
  16.  * - Board: ESP8266 D1 Mini.
  17.  *
  18.  * Arduino dev env, board settiungs:
  19.  * - Board: Generic ESP8266 Module
  20.  * - Upload Speed: 921600
  21.  * - CPU Freq: 80MHz
  22.  * - Crystak Freq: 26 MHz
  23.  * - Flash Size: 4M (1M SPIFFS)
  24.  * - Flash Mode: DOUT (comp)
  25.  * - Flash Freq: 40MHz
  26.  * - Reset Methode: ck
  27.  * - Debug port: Dis
  28.  * - Kbug Level: none
  29.  * - IwIP Variant: v1.4 Highter Bandwidth
  30.  * - VTables: Flash
  31.  * - Excepions: Dis
  32.  * - Biltin Led: 1
  33.  * - Erase Flash: only Sktech
  34.  * - Espressif FW: nonos-sdk 2.2.1 legacy
  35.  * - SSL Support: All SSL ciphers
  36.  * - Programmer: ArduinoISP.org
  37.  */
  38.  
  39. #include <ESP8266WiFi.h>
  40. #include <ESP8266Ping.h>
  41. #include <APA102.h>
  42. #include <Ticker.h>  //Ticker Library
  43.  
  44.  
  45. //************************************************************
  46. // Network Setup
  47. //************************************************************
  48. const char* ssid     = "SSID"; //SSID of the WALN
  49. const char* password = "password"; //WAP Password
  50. const IPAddress remote_ip(192, 156, 45, 21); //Ping destination
  51. //************************************************************
  52.  
  53.  
  54. //************************************************************
  55. // LED Setup
  56. //************************************************************
  57. const uint16_t ledCount = 20; //No of LEDs on the strip
  58. const uint8_t brightness = 5; // 0 = off, 31 bright
  59. //************************************************************
  60.  
  61.  
  62. //************************************************************
  63. // Globale Variablen
  64. //************************************************************
  65. const uint8_t dataPin = 5;  //GPIO NO
  66. const uint8_t clockPin = 0;  //GPIO NO
  67.  
  68. APA102<dataPin, clockPin> ledStrip;
  69. rgb_color colors[ledCount];
  70. Ticker oLEDTimer;
  71.  
  72. enum LED_STATE {
  73.   ePingOK,
  74.   ePingNOK,
  75.   eNotCon
  76. } eLedState;
  77.  
  78. #define BUILTIN_LED 2 //GPIO NO
  79.  
  80. //************************************************************
  81. // Functions
  82. //************************************************************
  83. //************************************************************
  84. void UpdateStrip ()
  85. {
  86.   static uint8_t offset= 0;
  87.  
  88.   rgb_color MainColor, RunColor ;
  89.  
  90.  
  91.   switch (eLedState){
  92.     case(ePingOK) : MainColor = rgb_color(  0,255,  0); RunColor = rgb_color(  0,225,0);  break;
  93.     case(ePingNOK): MainColor = rgb_color(225,  0,  0); RunColor = rgb_color(255, 0,  0);  break;
  94.     case(eNotCon) : MainColor = rgb_color(225,  0,  0); RunColor = rgb_color(225,  0, 0);  break;
  95.     default:        MainColor = rgb_color(  0,  0,225); RunColor = rgb_color(  0,  0,225);  break;
  96.   }
  97.  
  98.   offset++;
  99.   if (offset >= ledCount*3) {offset = 0;}
  100.   for(uint16_t i = 0; i < ledCount; i++)
  101.   {
  102.     colors[i] = MainColor;
  103.     if (i == offset )
  104.     {
  105.         colors[i] = RunColor;
  106.     }
  107.   }
  108.  
  109.   ledStrip.write(colors, ledCount, brightness);
  110. }
  111.  
  112. //************************************************************
  113. //************************************************************
  114. void setup() {
  115.  
  116.   WiFi.setmode(WiFi.station)
  117.    
  118.   Serial.begin(115200);
  119.   // Init LED strip timer
  120.   oLEDTimer.attach(0.05, UpdateStrip);
  121.  
  122.   //Connecting to a WiFi network
  123.   WiFi.begin(ssid, password);
  124.  
  125.   pinMode(BUILTIN_LED, OUTPUT);
  126. }
  127. //************************************************************
  128. //************************************************************
  129. void loop() {
  130.  
  131.   if (WiFi.status() == WL_CONNECTED)
  132.   {
  133.      Serial.print("WiFi connected with ip ");  
  134.      Serial.println(WiFi.localIP());
  135.      Serial.print("Pinging ip ");
  136.      Serial.println(remote_ip);
  137.      
  138.      if(Ping.ping(remote_ip))
  139.      {
  140.         eLedState = ePingOK;
  141.         Serial.println("Ping OK!!");
  142.      }
  143.      else
  144.      {
  145.         eLedState = ePingNOK;
  146.         Serial.println("Ping not OK!!");
  147.      }
  148.   }
  149.   else
  150.   {
  151.     eLedState = eNotCon;
  152.     Serial.println("Not Connected!!");
  153.   }
  154.   delay (100);
  155.   digitalWrite(BUILTIN_LED, HIGH);
  156.   delay (1900);
  157.   digitalWrite(BUILTIN_LED, LOW);
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement