139
правок
Строка 572: | Строка 572: | ||
==Финальный код скетча== | ==Финальный код скетча== | ||
#include <Arduino.h> | #include <Arduino.h> | ||
#include <FastLED.h> | #include <FastLED.h> | ||
Строка 582: | Строка 581: | ||
#include <ESPAsyncWebServer.h> | #include <ESPAsyncWebServer.h> | ||
#include <AsyncElegantOTA.h> | #include <AsyncElegantOTA.h> | ||
Строка 605: | Строка 594: | ||
server.begin(); | server.begin(); | ||
Serial.println("HTTP server started"); | Serial.println("HTTP server started"); | ||
} | |||
#define WIFI_SSID "ssid" | |||
#define WIFI_PASSWORD "xxx" | |||
void setupWifi() { | |||
WiFi.mode(WIFI_STA); | |||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |||
char emptyMsg[1] = ""; | |||
char pointMsg[2] = "."; | |||
Serial.println(emptyMsg); | |||
// Wait for connection | |||
while (WiFi.status() != WL_CONNECTED) { | |||
delay(500); | |||
Serial.println(pointMsg); | |||
} | |||
Serial.println(emptyMsg); | |||
char connectedToMsg[14] = "Connected to "; | |||
Serial.println(connectedToMsg); | |||
Serial.println(WIFI_SSID); | |||
char ipAddrMsg[30]; | |||
sprintf(ipAddrMsg, "IP address: %s", WiFi.localIP().toString().c_str()); | |||
Serial.println(ipAddrMsg); | |||
} | } | ||
Строка 625: | Строка 637: | ||
// Some notes on the more abstract 'theory and practice' of | // Some notes on the more abstract 'theory and practice' of | ||
// FastLED compact palettes are at the bottom of this file. | // FastLED compact palettes are at the bottom of this file. | ||
#define DIN_PIN 27 | |||
#define NUM_LEDS 5 | |||
#define LED_TYPE WS2811_400 | |||
#define COLOR_ORDER GRB | |||
CRGB leds[NUM_LEDS]; | |||
#define UPDATES_PER_SECOND 50 | |||
uint8_t BRIGHTNESS = 50; | |||
bool hueLoop = true; | |||
CRGBPalette16 currentPalette; | CRGBPalette16 currentPalette; | ||
TBlendType currentBlending; | TBlendType currentBlending; | ||
Строка 634: | Строка 656: | ||
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM; | extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM; | ||
void setupFastLED(){ | void setupFastLED(){ | ||
delay( 3000 ); // power-up safety delay | delay( 3000 ); // power-up safety delay | ||
FastLED.addLeds<LED_TYPE, | FastLED.addLeds<LED_TYPE, DIN_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); | ||
FastLED.setBrightness( BRIGHTNESS ); | FastLED.setBrightness( BRIGHTNESS ); | ||
Строка 667: | Строка 666: | ||
#define MODBUS_SERVER " | #define MODBUS_SERVER "10.50.0.253" | ||
#define MODBUS_PORT 1883 | #define MODBUS_PORT 1883 | ||
WiFiClient wifiClient; | WiFiClient wifiClient; | ||
Строка 732: | Строка 731: | ||
} | } | ||
void ChangePalettePeriodically(); | void ChangePalettePeriodically(); | ||
void FillLEDsFromPaletteColors( uint8_t colorIndex); | void FillLEDsFromPaletteColors( uint8_t colorIndex); | ||
void FillLEDsFromPaletteColors( uint8_t colorIndex) | void FillLEDsFromPaletteColors( uint8_t colorIndex) | ||
Строка 891: | Строка 868: | ||
// Green, followed by a smooth gradient from green-to-blue, and then Blue. | // Green, followed by a smooth gradient from green-to-blue, and then Blue. | ||
#define LED_PIN 2 | |||
int led_status = HIGH; | |||
void ledUpdate(int v) { | |||
digitalWrite(LED_PIN, v); | |||
} | |||
void ledBlink( void * parameter ) { | |||
const TickType_t xDelay = 1000 / portTICK_PERIOD_MS; | |||
for(;;) { | |||
switch (led_status) | |||
{ | |||
case HIGH: | |||
led_status = LOW; | |||
break; | |||
default: | |||
led_status = HIGH; | |||
break; | |||
} | |||
ledUpdate(led_status); | |||
vTaskDelay(xDelay); | |||
Serial.printf("blink %d\n", led_status); | |||
} | |||
} | |||
void setupLedBlink() { | |||
pinMode(LED_PIN, OUTPUT); | |||
xTaskCreate( | |||
ledBlink, /* Task function. */ | |||
"ledBlink", /* String with name of task. */ | |||
10000, /* Stack size in bytes. */ | |||
NULL, /* Parameter passed as input of the task */ | |||
1, /* Priority of the task. */ | |||
NULL); /* Task handle. */ | |||
} | |||
void setup() { | |||
Serial.begin(9600); | |||
setupLedBlink(); | |||
setupWifi(); | |||
setupFastLED(); | |||
setupModbus(); | |||
setupOTA(); | |||
} | |||
void loop() | |||
{ | |||
modbusLoop(); | |||
if(!hueLoop) { | |||
return; | |||
} | |||
ChangePalettePeriodically(); | |||
static uint8_t startIndex = 0; | |||
startIndex = startIndex + 1; /* motion speed */ | |||
FillLEDsFromPaletteColors( startIndex); | |||
FastLED.show(); | |||
FastLED.delay(1000 / UPDATES_PER_SECOND); | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> |
правок