Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define MODULE_ID 20
- #define DATAGRAM_PREFIX_SIZE 1
- // Serial debug enabled if DEBUG is defined (comment for production)
- //#define DEBUG
- #define REMOTE_UPDATE
- #define UDP_MAX_PACKET_SIZE 512
- #include <ESP8266WiFi.h>
- #include <WiFiUDP.h>
- IPAddress remote = IPAddress(0, 0, 0, 0);
- #if defined(REMOTE_UPDATE)
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- #include <ESP8266HTTPUpdateServer.h>
- #include <DNSServer.h>
- #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
- #endif
- #if defined(REMOTE_UPDATE)
- String _hostbase = "module-" + String(MODULE_ID);
- const char* host = _hostbase.c_str();
- const char* update_path = "/firmware";
- const char* update_username = "admin";
- const char* update_password = "admin";
- ESP8266WebServer httpServer(80);
- ESP8266HTTPUpdateServer httpUpdater;
- #endif
- int wifiConnected = false;
- unsigned int localPort = 8888;
- WiFiUDP UDP;
- boolean udpConnected = false;
- char packetBuffer[UDP_MAX_PACKET_SIZE]; //buffer to hold incoming packet,
- char ReplyBuffer[5] = "ACKI"; // a string to send back
- #include "wifi_credentials.h"
- #include <NeoPixelBus.h>
- #define NUMLEDS 150
- NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(NUMLEDS);
- void setup() {
- // put your setup code here, to run once:
- pinMode(D0, OUTPUT);
- pinMode(D3, INPUT_PULLUP);
- digitalWrite(D0, HIGH);
- #if defined(DEBUG)
- Serial.begin(115200);
- Serial.println("Initializing...");
- #endif
- strip.Begin();
- for (int i = 0; i < NUMLEDS; i++)
- strip.SetPixelColor(i, RgbColor(0, 0, 0));
- strip.Show();
- wifiConnected = connectWifi();
- if (wifiConnected)
- {
- if (UDP.begin(localPort) == 1)
- {
- for (int i = wifiConnected; i < NUMLEDS; i++)
- {
- strip.SetPixelColor(i, RgbColor(0, 64, 0));
- strip.Show();
- delay(10);
- }
- }
- else
- {
- while (1)
- {
- for (int i = 0; i < NUMLEDS; i++)
- strip.SetPixelColor(i, RgbColor(64, 0, 0));
- strip.Show();
- delay(500);
- for (int i = 0; i < NUMLEDS; i++)
- strip.SetPixelColor(i, RgbColor(0, 0, 0));
- strip.Show();
- delay(500);
- }
- }
- }
- else
- {
- while (1)
- {
- for (int i = 0; i < NUMLEDS; i++)
- strip.SetPixelColor(i, RgbColor(64, 0, 0));
- strip.Show();
- delay(1000);
- for (int i = 0; i < NUMLEDS; i++)
- strip.SetPixelColor(i, RgbColor(0, 0, 0));
- strip.Show();
- delay(1000);
- }
- }
- #if defined(REMOTE_UPDATE)
- MDNS.begin(host);
- httpUpdater.setup(&httpServer, update_path, update_username, update_password);
- httpServer.begin();
- MDNS.addService("http", "tcp", 80);
- #endif
- #if defined(DEBUG)
- Serial.println("Succesfully initialized");
- #endif
- }
- uint32_t ledBlinkTimer = 0;
- bool ledState = 0;
- float fps = 0.0;
- bool serverIsActive = 0;
- uint32_t serverActivityTimer = 0;
- void loop() {
- // put your main code here, to run repeatedly:
- #if defined(REMOTE_UPDATE)
- httpServer.handleClient();
- #endif
- if (digitalRead(D3) == LOW)
- {
- }
- if (millis() - ledBlinkTimer > 1000)
- {
- ledBlinkTimer = millis();
- digitalWrite(D0, ledState);
- ledState = 1 - ledState;
- if (millis() - serverActivityTimer < 500)
- {
- UDP.beginPacket(UDP.remoteIP(), 7777);
- UDP.write(0xF);
- UDP.write((uint8_t)(fps));
- UDP.write(MODULE_ID);
- UDP.endPacket();
- }
- }
- if (millis() - serverActivityTimer > 250)
- {
- fps = 0;
- }
- int packetSize = UDP.parsePacket();
- if (packetSize)
- {
- //get remote IP
- remote = UDP.remoteIP();
- //read the packet into packetBufffer
- UDP.read(packetBuffer, UDP_MAX_PACKET_SIZE);
- serverActivityTimer = millis();
- //Send back IP adress and my ID to main server
- if (packetBuffer[0] == 0xDE) //DEtect
- {
- serverIsActive = true;
- UDP.beginPacket(UDP.remoteIP(), UDP.remotePort());
- UDP.write(0xDE);
- UDP.write(WiFi.localIP()[0]);
- UDP.write(WiFi.localIP()[1]);
- UDP.write(WiFi.localIP()[2]);
- UDP.write(WiFi.localIP()[3]);
- UDP.write(MODULE_ID);
- UDP.endPacket();
- }
- else if (packetBuffer[0] == 0xDA) //DAta
- {
- for (int i = 1; i < NUMLEDS + 1; i++)
- {
- strip.SetPixelColor(i - 1, RgbColor(packetBuffer[3 * i + DATAGRAM_PREFIX_SIZE],
- packetBuffer[3 * i + 1 + DATAGRAM_PREFIX_SIZE],
- packetBuffer[3 * i + 2 + DATAGRAM_PREFIX_SIZE]));
- }
- strip.Show();
- ledState = 1 - ledState;
- digitalWrite(D0, ledState);
- processfps();
- }
- }
- }
- int connectWifi() {
- boolean state = true;
- int i = 0;
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- // Wait for connection
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- strip.SetPixelColor(i++, RgbColor(64, 0, 0));
- strip.Show();
- if (i == NUMLEDS) {
- #if defined(DEBUG)
- Serial.println("Failed to connect to wifi...");
- #endif
- return 0;
- }
- #if defined(DEBUG)
- Serial.print(".");
- #endif
- }
- #if defined(DEBUG)
- Serial.println("Wifi OK");
- Serial.println(WiFi.localIP());
- #endif
- return i;
- }
- uint32_t fpstimer = 0;
- uint32_t frameCount = 0;
- void processfps()
- {
- frameCount++;
- if (frameCount % 10 == 0)
- {
- frameCount = 0;
- //Serial.print("FPS : ");
- fps = float(10000.0 / (millis() - fpstimer));
- //Serial.println(fps);
- fpstimer = millis();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement