Skip to content

Instantly share code, notes, and snippets.

@perigalacticon
Created October 29, 2019 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perigalacticon/6d2b8c76a0c1fb5514ec4f4d67f367b8 to your computer and use it in GitHub Desktop.
Save perigalacticon/6d2b8c76a0c1fb5514ec4f4d67f367b8 to your computer and use it in GitHub Desktop.
Receive_UDP_Right_Eye.ino
// Receive_UDP_Right_Eye.ino
// ESP8266
#include <FastLED.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <NeoPixelBus.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#define LED_DT 2
#define NUM_LEDS 80
#define COLOR_ORDER GRB
#define CHIPSET WS2812B // Outdoor strip is RGB + WS2811
#define BRIGHTNESS 255
#define DEBUG false
//=============================================================================================================================
const char* host = "Receive_UDP_Right_Eye";
const char* ssid = "WIFIXF";
const char* password = "Kugel08642!";
byte packetBufferData[255];
unsigned int localPortData = 5000;
int reconnect;
uint8_t ledData[NUM_LEDS * 3];
const char* serverHost = "192.168.0.8";
const int serverHostPort = 81;
//=============================================================================================================================
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;
CRGBArray<NUM_LEDS> leds;
WiFiUDP udp; // A UDP instance to let us send and receive packets over UDP
WiFiUDP udpData; // A UDP instance to let us send and receive packets over UDP
NeoPixelBus<NeoGrbFeature, NeoEsp8266Uart0800KbpsMethod> NPBStrip(NUM_LEDS, LED_DT);
WiFiClient client;
//=============================================================================================================================
void setup()
{
Serial.begin(115200);
delay(1000);
// Serial.println("Receive_UDP_Right_Eye.ino"); // 0,1,2,3,4
// Serial.println("Booting");
//WiFi.mode(WIFI_STA);
IPAddress ip(10, 0, 0, 221); //
IPAddress gateway(10, 0, 0, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(8, 8, 8, 8);
WiFi.config(ip, dns, gateway, subnet);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
// Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
// Serial.println("Starting UDPs");
// udp.begin(localPortTime);
udpData.begin(localPortData);
delay(1000);
// Serial.print("Data Local port: ");
// Serial.println(udpData.localPort());
MDNS.begin(host);
httpUpdater.setup(&httpServer);
httpServer.begin();
MDNS.addService("http", "tcp", 80);
// Serial.printf("HTTPUpdateServer ready! Open http://%s.local/update in your browser \n", host);
LEDS.addLeds<CHIPSET, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
NPBStrip.Begin();
fill_solid(leds, NUM_LEDS, CHSV(0, 0, 0));
TransferToNeoPixelBus();
NPBStrip.Show();
// Serial.println("setup complete");
}
//=============================================================================================================================
void loop()
{
EVERY_N_MILLISECONDS(500)
{
httpServer.handleClient();
// Serial.println("Receive_UDP_Right_Eye");
}
EVERY_N_MILLISECONDS(5000)
{
//connect wifi if not connected
if (WiFi.status() != WL_CONNECTED)
{
reconnect++;
Serial.println("wifi reconnect count = ");
Serial.println(reconnect);
delay(1);
startWIFI();
return;
}
}
CheckUDP3(); // for UDP.
//CheckClient(); // for TCP.
//SendLedData();
delay(1);
}
//=============================================================================================================================
void SendLedDataHSV1(void)
{
float H = ledData[0] / 255.0;
float S = ledData[1] / 255.0;
float V = ledData[2] / 255.0;
for (uint16_t led = 0; led < NUM_LEDS; led++)
{
NPBStrip.SetPixelColor(led, HsbColor(H, S, V));
}
NPBStrip.Show();
}
//=============================================================================================================================
void CheckUDP3(void) // get the udp packet:
{
int packetSize = udpData.parsePacket();
if (packetSize)
{
//Serial.print("packetSize = ");
//Serial.println(packetSize);
// read udp packet data into buffer:
int len = udpData.read(ledData, NUM_LEDS * 3);
//Serial.print("len = ");
//Serial.println(len);
//for (int i = 0; i < NUM_LEDS * 3; i++)
//{
// Serial.print("array val ");
// Serial.print(i);
// Serial.print(" = ");
// Serial.println(ledData[i]);
//}
//SendLedData();
SendLedDataHSV1();
}
}
//=============================================================================================================================
void TransferToNeoPixelBus(void)
{
RgbColor NPBPixel;
for (int i = 0; i < NUM_LEDS; i++)
{
NPBPixel = RgbColor(leds[i].r, leds[i].g, leds[i].b);
NPBStrip.SetPixelColor(i, NPBPixel);
}
}
//=======================================================================================
void startWIFI(void)
{
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi reconnected");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment