-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathLEDLamp.ino
172 lines (139 loc) · 4.58 KB
/
LEDLamp.ino
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
/**
* Simple server compliant with Mozilla's proposed WoT API
* Originally based on the HelloServer example
* Tested on ESP8266, ESP32, Arduino boards with WINC1500 modules (shields or
* MKR1000)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#define LARGE_JSON_BUFFERS 1
#include <Arduino.h>
#include <Thing.h>
#include <WebThingAdapter.h>
#ifdef ESP32
#include <analogWrite.h>
#endif
const char *ssid = "......";
const char *password = "..........";
#if defined(LED_BUILTIN)
const int lampPin = LED_BUILTIN;
#else
const int lampPin = 13; // manually configure LED pin
#endif
ThingActionObject *action_generator(DynamicJsonDocument *);
WebThingAdapter *adapter;
const char *lampTypes[] = {"OnOffSwitch", "Light", nullptr};
ThingDevice lamp("urn:dev:ops:my-lamp-1234", "My Lamp", lampTypes);
ThingProperty lampOn("on", "Whether the lamp is turned on", BOOLEAN,
"OnOffProperty");
ThingProperty lampLevel("brightness", "The level of light from 0-100", INTEGER,
"BrightnessProperty");
StaticJsonDocument<256> fadeInput;
JsonObject fadeInputObj = fadeInput.to<JsonObject>();
ThingAction fade("fade", "Fade", "Fade the lamp to a given level",
"FadeAction", &fadeInputObj, action_generator);
ThingEvent overheated("overheated",
"The lamp has exceeded its safe operating temperature",
NUMBER, "OverheatedEvent");
bool lastOn = true;
void setup(void) {
pinMode(lampPin, OUTPUT);
digitalWrite(lampPin, HIGH);
Serial.begin(115200);
Serial.println("");
Serial.print("Connecting to \"");
Serial.print(ssid);
Serial.println("\"");
#if defined(ESP8266) || defined(ESP32)
WiFi.mode(WIFI_STA);
#endif
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
adapter = new WebThingAdapter("led-lamp", WiFi.localIP());
lamp.description = "A web connected lamp";
lampOn.title = "On/Off";
lamp.addProperty(&lampOn);
lampLevel.title = "Brightness";
lampLevel.minimum = 0;
lampLevel.maximum = 100;
lampLevel.unit = "percent";
lamp.addProperty(&lampLevel);
fadeInputObj["type"] = "object";
JsonObject fadeInputProperties =
fadeInputObj.createNestedObject("properties");
JsonObject brightnessInput =
fadeInputProperties.createNestedObject("brightness");
brightnessInput["type"] = "integer";
brightnessInput["minimum"] = 0;
brightnessInput["maximum"] = 100;
brightnessInput["unit"] = "percent";
JsonObject durationInput =
fadeInputProperties.createNestedObject("duration");
durationInput["type"] = "integer";
durationInput["minimum"] = 1;
durationInput["unit"] = "milliseconds";
lamp.addAction(&fade);
overheated.unit = "degree celsius";
lamp.addEvent(&overheated);
adapter->addDevice(&lamp);
adapter->begin();
Serial.println("HTTP server started");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.print("/things/");
Serial.println(lamp.id);
#ifdef analogWriteRange
analogWriteRange(255);
#endif
// set initial values
ThingPropertyValue initialOn = {.boolean = true};
lampOn.setValue(initialOn);
(void)lampOn.changedValueOrNull();
ThingPropertyValue initialLevel = {.integer = 50};
lampLevel.setValue(initialLevel);
(void)lampLevel.changedValueOrNull();
analogWrite(lampPin, 128);
randomSeed(analogRead(0));
}
void loop(void) {
adapter->update();
bool on = lampOn.getValue().boolean;
if (on) {
int level = map(lampLevel.getValue().number, 0, 100, 255, 0);
analogWrite(lampPin, level);
} else {
analogWrite(lampPin, 255);
}
if (lastOn != on) {
lastOn = on;
}
}
void do_fade(const JsonVariant &input) {
JsonObject inputObj = input.as<JsonObject>();
long long int duration = inputObj["duration"];
long long int brightness = inputObj["brightness"];
delay(duration);
ThingDataValue value = {.integer = brightness};
lampLevel.setValue(value);
int level = map(brightness, 0, 100, 255, 0);
analogWrite(lampPin, level);
ThingDataValue val;
val.number = 102;
ThingEventObject *ev = new ThingEventObject("overheated", NUMBER, val);
lamp.queueEventObject(ev);
}
ThingActionObject *action_generator(DynamicJsonDocument *input) {
return new ThingActionObject("fade", input, do_fade, nullptr);
}