-
Notifications
You must be signed in to change notification settings - Fork 0
/
noDisplay.cpp
201 lines (176 loc) · 7.11 KB
/
noDisplay.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*Wifi Clock Lamp designed by Derrick Huynh
UCLA EE Student
Designed from 12/26/2019 to 1/20/2020
NOTES: THIS IS THE NON TFT VERSION. THIS IMPLEMENTS ONLY A ON/OFF FUNCTION OF LAMP, INCLUDING PAIR MODE.
I am using a SparkFun Capactive Touch sensor, but first designed using the CapacitiveSensor library by Paul Badger and Paul Stoffregen
Note: the CapactiveSensor library was incompatible with the Arduino IDE for me.
The on message is sent automatically by the program, but the custom color must be manually done using a MQTT Websocket or
MQTT phone app AND MUST BE IN HTML COLOR CODE - any other code (RGB etc.) WILL NOT WORK.
A custom mobile app will probably not be developed :p but who knows
This was programmed in PlatformIO IDE running on VS Code.
*/
#include <Arduino.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <NeoPixelBus.h>
/*||*//////////TODO: Set up pins, wifi info, mqtt server//////////*||*/
/*||*/ #define neoPin 13 // Neopixel data pin /*||*/
/*||*/ #define numPixels 12 //number of Neopixels /*||*/
/*||*/ #define touchPin 25 // Touch sensor /*||*/
/*||*/ /*||*/
/*||*/ const String myClientId = ""; /*||*/
/*||*/ const String otherClientId = ""; /*||*/
/*||*/ /*||*/
/*||*/ const char* ssid = ""; /*||*/
/*||*/ const char* password = ""; /*||*/
/*||*/ const char* mqttServer = ""; /*||*/
/*||*/ const int mqttPort = 0; /*||*/
/*||*/ const char* mqttUser = ""; /*||*/
/*||*/ const char* mqttPassword = ""; /*||*/
/*||*/////////////////////////////////////////////////////////////*||*/
//Initalize library objects
WiFiClient wifiClient;
PubSubClient client(wifiClient);
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> neopixels(numPixels, neoPin);
//custom vars core to the lamp
bool touch = false; //bool to determine if led is on
const int smoothness = 2000; //smoothness determines the speed at which colors change or when lamp turns on/off
unsigned long lastMillis; //var to store when it last published its on/off flag to MQTT server
//define some commonly used colors
const RgbColor blueGray(51, 119, 255);
const RgbColor black(0,0,0);
const RgbColor pink(255,20,147);
//colors to store current and previous colors for restoration
RgbColor currColor;
RgbColor prevColor = black;
//function begins wifi connection
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
//function to connect to MQTT server
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(myClientId.c_str(),mqttUser,mqttPassword)) {
Serial.println("connected");
}
else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
// Wait 2 seconds before retrying
delay(2000);
}
}
}
//function to set the color of LEDs to input RgbColor
void setColor(RgbColor newColor) {
prevColor = currColor;
for(int i = 0; i < smoothness; i++) {
RgbColor tempColor = RgbColor::LinearBlend(prevColor, newColor, (float(i)/ float(smoothness))); //slowly transition to the current Color
for(int j = 0; j < neopixels.PixelCount(); j++) { //for each pixel, set it to the new color
neopixels.SetPixelColor(j, tempColor);
}
if(neopixels.IsDirty()) {neopixels.Show(); } //TFT that tempcolor
}
currColor = newColor;
touch = true;
}
//turns off the LEDs
void turnOff() {
prevColor = currColor;
for(int i = smoothness; i > 0; i--) {
RgbColor tempColor = RgbColor::LinearBlend(black, currColor, (float(i)/ float(smoothness))); //slowly transition to the black from current Color
for(int j = 0; j < neopixels.PixelCount(); j++) { //for each pixel, set it to the new color
neopixels.SetPixelColor(j, tempColor);
}
if(neopixels.IsDirty()) {neopixels.Show(); } //display that tempcolor
}
currColor = black;
touch = false;
}
//MQTT Callback function that runs when a message is received from the subscribed topic.
void callback(char* topic, byte* payload, unsigned int length) {
char *msg = (char *) payload;
msg[length] = '\0';
Serial.println("Message Received!: ");
Serial.println(msg);
Serial.println("On Topic: ");
Serial.println(topic);
if((String) topic == (otherClientId+"/on")) { //if the topic is the other lamp's on/off flag
if(touch && (*msg == 'Y')) { //if my lamp and her lamp are both on, set color to pink
setColor(pink);
}
if(touch && (*msg == 'N')) { //if her lamp turned off, set color to the prev color
//fix bug where it would turn off, or revert to pink if 'N' message was sent twice
//color falls back to standard blueGray if wack stuff occurs
if(prevColor == black || prevColor == pink) {
prevColor = blueGray;
}
setColor(prevColor);
}
}
else if((String) topic == (myClientId+"/color")) { //receive HTML color code form websocket and update the color
//converts the 0xABCDEF string to a double, casts to uint32_t and makes a HTMLColor out of that
uint32_t htmlColor = String((msg)).toDouble();
setColor(RgbColor(HtmlColor(htmlColor)));
}
}
void setup() {
neopixels.Begin();
neopixels.Show(); // initialize all pixels to "off"
pinMode(touchPin, INPUT);
Serial.begin(115200);
Serial.setTimeout(500);// Set time out
setup_wifi();
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
reconnect();
//subscribe to other lamp's topics
client.subscribe((otherClientId + "/on").c_str());
client.subscribe((myClientId + "/color").c_str());
//when first turned on, turn on lamp to blueGray default
setColor(blueGray);
currColor = blueGray;
lastMillis = millis();
}
void loop() {
//if the touch sensor is activated, alternate
if (digitalRead(touchPin)){
if(touch == false) {
setColor(blueGray);
client.publish((myClientId+"/on").c_str(), "Y");
}
else if(touch == true) {
turnOff();
client.publish((myClientId+"/on").c_str(), "N");
}
}
//if 30 secs elapsed since last update to MQTT server
if(millis() - lastMillis == 30*1000)
{
if(touch) {
client.publish((myClientId+"/on").c_str(), "Y");
}
else {
client.publish((myClientId+"/on").c_str(), "N");
}
}
client.loop();
reconnect();
}