FastLED 3.9.7
Loading...
Searching...
No Matches
net.h
1#pragma once
2
3#include <Arduino.h>
4#include <FastLED.h>
5
6#define OTA_SUPPORTED 0
7#define WIFI_SUPPORTED 0
8
9#if OTA_SUPPORTED && !WIFI_SUPPORTED
10#error "You can't have OTA without WiFi, dummy"
11#endif
12
13#if WIFI_SUPPORTED
14#include <ArduinoOSC.h>
15#include <ESPmDNS.h>
16#include <WiFi.h>
17#include <WiFiUdp.h>
18const char *ssid = "YourMom";
19const char *password = "is a nice lady";
20// WiFi stuff - CHANGE FOR YOUR OWN NETWORK!
21const IPAddress ip(4, 20, 6, 9); // IP address that THIS DEVICE should request
22const IPAddress gateway(192, 168, 1, 1); // Your router
23const IPAddress
24 subnet(255, 255, 254,
25 0); // Your subnet mask (find it from your router's admin panel)
26const int recv_port = 42069; // Port that OSC data should be sent to (pick one,
27 // put same one in EmotiBit's OSC Config XML file)
28#endif
29
30#if OTA_SUPPORTED
31#include <ArduinoOTA.h>
32#endif
33
34void net_init();
35void net_loop();
36
37void net_init() {
38#if WIFI_SUPPORTED
39 WiFi.mode(WIFI_STA);
40 WiFi.begin(ssid, password);
41 WiFi.config(ip, gateway, subnet);
42 while (WiFi.waitForConnectResult() != WL_CONNECTED) {
43 Serial.println("Connection Failed! Rebooting...");
44 delay(5000);
45 ESP.restart();
46 }
47
48 Serial.print("WiFi connected, IP = ");
49 Serial.println(WiFi.localIP());
50
51 // Subscribe to OSC transmissions for important data
52 OscWiFi.subscribe(
53 recv_port, "/EmotiBit/0/EDA",
54 [](const OscMessage &m) { // This weird syntax is a lambda expression
55 // (anonymous nameless function)
56 lastKnownTemperature = m.arg<float>(0);
57 });
58
59 OscWiFi.subscribe(recv_port, "/EmotiBit/0/GYRO:X", [](const OscMessage &m) {
60 gyroX = m.arg<float>(0) * gyroAlpha + gyroX * (1 - gyroAlpha);
61 });
62
63 OscWiFi.subscribe(recv_port, "/EmotiBit/0/GYRO:Y", [](const OscMessage &m) {
64 gyroY = m.arg<float>(0) * gyroAlpha + gyroY * (1 - gyroAlpha);
65 });
66
67 OscWiFi.subscribe(recv_port, "/EmotiBit/0/GYRO:Z", [](const OscMessage &m) {
68 gyroZ = m.arg<float>(0) * gyroAlpha + gyroZ * (1 - gyroAlpha);
69 });
70
71 // Heartbeat detection and visualization happens here
72 OscWiFi.subscribe(recv_port, "/EmotiBit/0/PPG:IR", [](const OscMessage &m) {
73 float reading = m.arg<float>(0);
74 Serial.println(reading);
75
76 int hue = 0;
77
78 // Ignore heartbeat when finger is wiggling around - it's not accurate
79 float gyroTotal = abs(gyroX) + abs(gyroY) + abs(gyroZ);
80
81 if (gyroTotal < gyroThreshold && lastIrReading >= reading) {
82 // Our hand is sitting still and the reading dropped - let's pulse!
83 Serial.print("> ");
84 Serial.println(highestIrReading - reading);
85 if (highestIrReading - reading >= heartbeatDelta) {
86 if (millis() - lastHeartbeat >= heartbeatLockout) {
87 hue = fmap(lastKnownTemperature, lowTemperature,
88 highTemperature, 0xFFFF, 0);
89 for (int i = 0; i < 6; i++) {
90 if (nodeConnections[15][i] > 0) {
91 bool firedRipple = false;
92 // Find a dead ripple to reuse it
93 for (int j = 0; j < 30; j++) {
94 if (!firedRipple && ripples[j].state == dead) {
95 ripples[j].start(
96 15, i, strip0.ColorHSV(hue, 255, 255),
97 float(random(100)) / 100.0 * .2 + .8,
98 500, 2);
99
100 firedRipple = true;
101 }
102 }
103 }
104 }
105 }
106
107 lastHeartbeat = millis();
108 }
109 } else {
110 highestIrReading = 0;
111 }
112
113 lastIrReading = reading;
114 if (reading > highestIrReading)
115 highestIrReading = reading;
116 });
117#endif // WIFI_SUPPORTED
118
119#if OTA_SUPPORTED
120 // Wireless OTA updating? On an ARDUINO?! It's more likely than you think!
121 ArduinoOTA
122 .onStart([]() {
123 String type;
124 if (ArduinoOTA.getCommand() == U_FLASH)
125 type = "sketch";
126 else // U_SPIFFS
127 type = "filesystem";
128
129 // NOTE: if updating SPIFFS this would be the place to unmount
130 // SPIFFS using SPIFFS.end()
131 Serial.println("Start updating " + type);
132 })
133 .onEnd([]() { Serial.println("\nEnd"); })
134 .onProgress([](unsigned int progress, unsigned int total) {
135 Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
136 })
137 .onError([](ota_error_t error) {
138 Serial.printf("Error[%u]: ", error);
139 if (error == OTA_AUTH_ERROR)
140 Serial.println("Auth Failed");
141 else if (error == OTA_BEGIN_ERROR)
142 Serial.println("Begin Failed");
143 else if (error == OTA_CONNECT_ERROR)
144 Serial.println("Connect Failed");
145 else if (error == OTA_RECEIVE_ERROR)
146 Serial.println("Receive Failed");
147 else if (error == OTA_END_ERROR)
148 Serial.println("End Failed");
149 });
150
151 ArduinoOTA.begin();
152
153 Serial.println("Ready for WiFi OTA updates");
154 Serial.print("IP address: ");
155 Serial.println(WiFi.localIP());
156#endif
157}
158
159void net_loop() {
160#if WIFI_SUPPORTED
161 OscWiFi.parse();
162#endif
163
164#if OTA_SUPPORTED
165 ArduinoOTA.handle();
166#endif
167
168}
central include file for FastLED, defines the CFastLED class/object