From a768cbb781af71775f552cc07c63467338eb45f8 Mon Sep 17 00:00:00 2001 From: dezeyer23 Date: Thu, 11 Jun 2020 23:16:18 +0200 Subject: [PATCH] Wemos D1 with Mosfet support To get my couch led runnung a wemos d1 udp client with support for pwm is created. also made changes to the ws2812b version --- .../settings.example.h | 13 ++ .../strip-wemosd1-udp-mosfet-pwm-2.ino | 123 ++++++++++++ .../strip-wemosd1-udp-ws2812b.ino | 189 +++++++++--------- 3 files changed, 232 insertions(+), 93 deletions(-) create mode 100644 clients/strip-wemosd1-udp-mosfet-pwm-2/settings.example.h create mode 100644 clients/strip-wemosd1-udp-mosfet-pwm-2/strip-wemosd1-udp-mosfet-pwm-2.ino diff --git a/clients/strip-wemosd1-udp-mosfet-pwm-2/settings.example.h b/clients/strip-wemosd1-udp-mosfet-pwm-2/settings.example.h new file mode 100644 index 0000000..50ae556 --- /dev/null +++ b/clients/strip-wemosd1-udp-mosfet-pwm-2/settings.example.h @@ -0,0 +1,13 @@ +const char* ssid = "master"; +const char* password = "master"; + +//RecipientIP is overridden after wl connect, getting localip and set last octet to 255 (Broadcast) +//this will only work for /24 networks. If you want to set an other BC or a sinle IP adress set overriderecipient to true +overriderecipient = false; +RecipientIP = IPAddress(0, 0, 0, 0); +RecipientPort = 8002; + +RGBStrip rgbStrips[] = { + RGBStrip("Couch links", 20, 21, 22), + RGBStrip("Couch rechts", 19, 18, 17), +}; diff --git a/clients/strip-wemosd1-udp-mosfet-pwm-2/strip-wemosd1-udp-mosfet-pwm-2.ino b/clients/strip-wemosd1-udp-mosfet-pwm-2/strip-wemosd1-udp-mosfet-pwm-2.ino new file mode 100644 index 0000000..e224097 --- /dev/null +++ b/clients/strip-wemosd1-udp-mosfet-pwm-2/strip-wemosd1-udp-mosfet-pwm-2.ino @@ -0,0 +1,123 @@ + +//#define FASTLED_ALLOW_INTERRUPTS 0 +//#define FASTLED_INTERRUPT_RETRY_COUNT 0 +//#define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP8266_ASYNC +#define UDP_TX_PACKET_MAX_SIZE = 50*12+1; + +#include +#include +#include + +class RGBStrip { + private: + // buffers for receiving and sending data + char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; + WiFiUDP _udp; + String _name; + int _pin_red; + int _pin_green; + int _pin_blue; + int _col_red = 0; + int _col_green = 0; + int _col_blue = 0; + int _getledvalue(int startpoint){ + return 100 * int(packetBuffer[startpoint] - '0') + 10 * int(packetBuffer[startpoint+1] - '0') + int(packetBuffer[startpoint+2] - '0'); + } + public: + IPAddress _RecipientIP = IPAddress(0, 0, 0, 0); + unsigned int _RecipientPort = 8002; + RGBStrip(String name, int pin_red, int pin_green, int pin_blue){ + _name = name; + _pin_red = pin_red; + _pin_green = pin_green; + _pin_blue = pin_blue; + + pinMode(_pin_red, OUTPUT); + pinMode(_pin_green, OUTPUT); + pinMode(_pin_blue, OUTPUT); + analogWrite(_pin_red,_col_red); + analogWrite(_pin_green,_col_green); + analogWrite(_pin_blue,_col_blue); + _udp.begin(random(5000,5499)); + } + void sendUDP(String text){ + //Serial.println(text); + _udp.beginPacket(_RecipientIP, _RecipientPort); + _udp.print(text); + _udp.endPacket(); + } + void loop(){ + int packetSize = _udp.parsePacket(); + if(packetSize) + { + // read the packet into packetBufffer + _udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE); + if(packetBuffer[0] == 's' && packetBuffer[1] == 'r'){ + sendUDP("r:1:"+_name+":"+String(1)+":0"); + } + if(packetBuffer[0] == 's' && packetBuffer[1] == 'u'){ + analogWrite(_pin_red, _col_red); + analogWrite(_pin_green, _col_green); + analogWrite(_pin_blue, _col_blue); + //Serial.println("SU:"+_name+" "+String(_pin_red)+":"+String(_col_red)+" "+String(_pin_blue)+":"+String(_col_green)+" "+String(_pin_blue)+":"+String(_col_blue)); + } + // d001255255255 + // d LED RED GRE BLU + if(packetBuffer[0] == 'd'){ + _col_red = _getledvalue(4); + _col_green = _getledvalue(7); + _col_blue = _getledvalue(10); + // Serial.println("D:"+_name+":"+String(_col_red)+":"+String(_col_green)+":"+String(_col_blue)); + } + } + } +}; +#include "settings.h" + +WiFiClient client; + + +void setup() { + + analogWriteRange(255); + analogWriteFreq(50); + //Serial.begin(115200); + wdt_enable(WDTO_4S); // Watchdog auf 4 s stellen + WiFi.softAPdisconnect (true); + for (int i = 0; i < (sizeof(rgbStrips) / sizeof(rgbStrips[0])); i++) { + rgbStrips[i]._RecipientPort = RecipientPort; + rgbStrips[i]._RecipientIP = RecipientIP; + } + +} + +void loop() { + EVERY_N_MILLISECONDS( 250 ) { + for (int i = 0; i < (sizeof(rgbStrips) / sizeof(rgbStrips[0])); i++) { + rgbStrips[i].sendUDP(String("s:ping")); + } + } + EVERY_N_MILLISECONDS( 15 ) { + for (int i = 0; i < (sizeof(rgbStrips) / sizeof(rgbStrips[0])); i++) { + rgbStrips[i].loop(); + } + wdt_reset(); + if (WiFi.status() != WL_CONNECTED) { // FIX FOR USING 2.3.0 CORE (only .begin if not connected) + WiFi.begin(ssid, password); // connect to the network + while (WiFi.status() != WL_CONNECTED) { + delay(500); + } + if (WiFi.status() == WL_CONNECTED) { + // Start UDP + //Serial.println("start udp"); + if(!overriderecipient){ + RecipientIP = WiFi.localIP(); + RecipientIP[3] = 255; + for (int i = 0; i < (sizeof(rgbStrips) / sizeof(rgbStrips[0])); i++) { + rgbStrips[i]._RecipientIP = RecipientIP; + } + } + } + } + } +} diff --git a/clients/strip-wemosd1-udp-ws2812b/strip-wemosd1-udp-ws2812b.ino b/clients/strip-wemosd1-udp-ws2812b/strip-wemosd1-udp-ws2812b.ino index a75fca2..99e0b4d 100644 --- a/clients/strip-wemosd1-udp-ws2812b/strip-wemosd1-udp-ws2812b.ino +++ b/clients/strip-wemosd1-udp-ws2812b/strip-wemosd1-udp-ws2812b.ino @@ -1,93 +1,96 @@ - -//#define FASTLED_ALLOW_INTERRUPTS 0 -//#define FASTLED_INTERRUPT_RETRY_COUNT 0 -//#define WEBSOCKETS_NETWORK_TYPE NETWORK_ESP8266_ASYNC -#define UDP_TX_PACKET_MAX_SIZE = 50*12+1; - -#include -#include -#include - -#include "settings.h" - -CRGB leds[NUM_LEDS]; - -WiFiClient client; - -// buffers for receiving and sending data -char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; -WiFiUDP Udp; - -int c = -1; - -void setup() { - Serial.begin(115200); - FastLED.addLeds(leds, NUM_LEDS); // for WS2812 (Neopixel) - //FastLED.addLeds(leds, NUM_LEDS); // for APA102 (Dotstar) - FastLED.setDither(false); - FastLED.setCorrection(TypicalLEDStrip); - FastLED.setBrightness(255); - FastLED.setMaxPowerInVoltsAndMilliamps(5, MILLI_AMPS); - fill_solid(leds, NUM_LEDS, CRGB::Black); - wdt_enable(WDTO_4S); // Watchdog auf 4 s stellen - WiFi.softAPdisconnect (true); - FastLED.show(); - Udp.begin(random(5000,5500)); - -} - -void loop() { - EVERY_N_MILLISECONDS( 250 ) { - sendUDP(String("s:ping")); - } - EVERY_N_MILLISECONDS( 15 ) { - wdt_reset(); - if (WiFi.status() != WL_CONNECTED) { // FIX FOR USING 2.3.0 CORE (only .begin if not connected) - WiFi.begin(ssid, password); // connect to the network - while (WiFi.status() != WL_CONNECTED) { - delay(500); - } - if (WiFi.status() == WL_CONNECTED) { - // Start UDP - Serial.println("start udp"); - if(!overriderecipient){ - RecipientIP = WiFi.localIP(); - RecipientIP[3] = 255; - } - } - } - - } - int packetSize = Udp.parsePacket(); - if(packetSize) - { - // read the packet into packetBufffer - Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE); - if(packetBuffer[0] == 's' && packetBuffer[1] == 'r'){ - sendUDP("r:1:"+StripName+":"+String(NUM_LEDS)+":0"); - } - if(packetBuffer[0] == 's' && packetBuffer[1] == 'u'){ - FastLED.show(); - } - if(packetBuffer[0] == 'd'){ - c=1; - while(c +#include +#include + +#include "settings.h" + +CRGB leds[NUM_LEDS]; + +WiFiClient client; + +// buffers for receiving and sending data +char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; +WiFiUDP Udp; + +int c = -1; + +void setup() { + Serial.begin(115200); + FastLED.addLeds(leds, NUM_LEDS); // for WS2812 (Neopixel) + //FastLED.addLeds(leds, NUM_LEDS); // for APA102 (Dotstar) + FastLED.setDither(false); + FastLED.setCorrection(TypicalLEDStrip); + FastLED.setBrightness(255); + FastLED.setMaxPowerInVoltsAndMilliamps(5, MILLI_AMPS); + fill_solid(leds, NUM_LEDS, CRGB::Black); + wdt_enable(WDTO_4S); // Watchdog auf 4 s stellen + WiFi.softAPdisconnect (true); + FastLED.show(); + Udp.begin(random(5000,5500)); + +} + +void loop() { + EVERY_N_MILLISECONDS( 250 ) { + sendUDP(String("s:ping")); + } + EVERY_N_MILLISECONDS( 15 ) { + wdt_reset(); + if (WiFi.status() != WL_CONNECTED) { // FIX FOR USING 2.3.0 CORE (only .begin if not connected) + WiFi.begin(ssid, password); // connect to the network + while (WiFi.status() != WL_CONNECTED) { + delay(500); + } + if (WiFi.status() == WL_CONNECTED) { + // Start UDP + Serial.println("start udp"); + if(!overriderecipient){ + RecipientIP = WiFi.localIP(); + RecipientIP[3] = 255; + } + } + } + + } + int packetSize = Udp.parsePacket(); + if(packetSize) + { + for( int i = 0; i < sizeof(packetBuffer); ++i ) + packetBuffer[i] = (char)0; + // read the packet into packetBufffer + Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE); + Serial.println(packetBuffer); + if(packetBuffer[0] == 's' && packetBuffer[1] == 'r'){ + sendUDP("r:1:"+StripName+":"+String(NUM_LEDS)+":0"); + } + if(packetBuffer[0] == 's' && packetBuffer[1] == 'u'){ + FastLED.show(); + } + if(packetBuffer[0] == 'd'){ + c=1; + while(c