-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLED.ino
186 lines (142 loc) · 4.24 KB
/
LED.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <Arduino.h>
#include <Wire.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <DHT.h>
/* WiFi-Data */
const char *ssid = "IoT-Netz";
const char *password = "CASIOT22";
WiFiMulti wifiMulti;
/* MQTT-Data */
const char *MQTTSERVER = "mq.jreichwald.de";
int MQTTPORT = 1883;
const char *mqttuser = "dbt";
const char *mqttpasswd = "dbt";
const char *mqttdevice = "RJLED"; // Please use a unique name here!
const char *outTopic = "rjLED";
WiFiClient wifiClient;
PubSubClient client(wifiClient);
/* JSON-Document-Size for incoming JSON (object size may be increased for larger JSON files) */
const int capacity = JSON_OBJECT_SIZE(6);
/* Outgoing JSON Documents */
DynamicJsonDocument doc(capacity);
#define MSG_BUFFER_SIZE (256) // Define the message buffer max size
char msg[MSG_BUFFER_SIZE]; // Define the message buffer
#define PIN_RED 23 // GIOP23
#define PIN_GREEN 16 // GIOP16
#define PIN_BLUE 21 // GIOP21
/**
* This function is called when a MQTT-Message arrives.
*/
void mqtt_callback(char* topic, byte* payload, unsigned int length) { //callback includes topic and payload ( from which (topic) the payload is comming)
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("]: ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println("");
// Create a JSON document on call stack
StaticJsonDocument<capacity> doc;
String jsonInput = String((char*)payload);
// try to deserialize JSON
DeserializationError err = deserializeJson(doc, jsonInput);
// if an error occurs, print it out
if (err) {
Serial.print(F("deserializeJson() failed with code "));
Serial.println(err.c_str());
return;
}
// Read out a name from JSON content (assuming JSON doc: {"SensorType" : "SomeSensorType", "value" : 42})
const char* messageLed = doc["messageLed"];
if(String(topic) == "rjOutput") {
Serial.print("Changing output to ");
if (String(messageLed) =="on") {
Serial.println("on");
// color code RED RJ
analogWrite(PIN_RED, 255);
analogWrite(PIN_GREEN, 0);
analogWrite(PIN_BLUE, 0);
}
else if(String(messageLed) =="off") {
Serial.println("off");
// color code RED RJ
analogWrite(PIN_RED, 0);
analogWrite(PIN_GREEN, 255);
analogWrite(PIN_BLUE, 0);
}
else {
Serial.print("Kein fall getroffen");
}
}
}
/**
* This function is called from setup() and establishes a MQTT connection.
*/
void initMqtt() {
client.setServer(MQTTSERVER, MQTTPORT);
// Set the callback-Function when new messages are received.
client.setCallback(mqtt_callback);
client.connect(mqttdevice, mqttuser, mqttpasswd);
while (!client.connected()) {
Serial.print(".");
delay(500);
}
// subscribe to a certain topic
client.subscribe("rjOutput");
}
/**
* This function is called from setup() and establishes a WLAN connection
*/
void initWifi() {
Serial.println("Connecting to WiFi ...");
wifiMulti.addAP(ssid, password);
while (wifiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
/**
* This function is called when the ESP is powered on.
*/
void setup()
{
// Set serial port speed to 115200 Baud
Serial.begin(115200);
// Connect to WLAN
initWifi();
// Connect to MQTT server
initMqtt();
/**
* RGB setup RJ
*/
pinMode(PIN_RED, OUTPUT);
pinMode(PIN_GREEN, OUTPUT);
pinMode(PIN_BLUE, OUTPUT);
// Print to console
Serial.println("Setup completed.");
delay(2000);
}
/**
* This function is the main function and loops forever.
*/
void loop()
{
// loop the mqtt client so it can maintain its connection and send and receive messages
client.loop();
// set measured data to preprared JSON document
// setJSONData(Humidity, Temperature);
// serialize JSON document to a string representation
serializeJsonPretty(doc, msg);
// publish to MQTT broker
client.publish(outTopic, msg);
client.loop();
// wait a second before the next loop.
delay(1000);
}