Skip to content

Commit 8df5703

Browse files
committed
ESP8266 MQTT NeoPixel
0 parents  commit 8df5703

File tree

3 files changed

+307
-0
lines changed

3 files changed

+307
-0
lines changed

humiditytemperature.ino

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
void currTempHumidity() {
2+
3+
4+
// start working...
5+
Serial.println("=================================");
6+
Serial.println("Sample DHT11...");
7+
8+
// read without samples.
9+
byte temperature = 0;
10+
byte humidity = 0;
11+
if (dht11.read(pinDHT11, &temperature, &humidity, NULL)) {
12+
Serial.print("Read DHT11 failed.");
13+
return;
14+
}
15+
16+
Serial.print("Sample OK:");
17+
Serial.print((int)temperature); Serial.print(" *C, ");
18+
Serial.print((int)humidity); Serial.println(" %");
19+
//
20+
String temp = String(temperature);
21+
//
22+
String humi = String(humidity);
23+
24+
if (!client.connected()) {
25+
Serial.print("trying to publish after connecting...");
26+
// Attempt to connect
27+
if (client.connect("ESP8266Client")) {
28+
Serial.println("connected");
29+
// Once connected, publish an announcement...
30+
client.publish("esp8266", "hello world");
31+
}
32+
} else {
33+
Serial.print("already connected and trying to publish ...");
34+
client.publish("temperature", temp);
35+
client.publish("humidity", humi);
36+
37+
}
38+
39+
// client.publish("temperature", (char *)temperature);
40+
// client.publish("humidity", (char *)humidity);
41+
42+
// DHT11 sampling rate is 1HZ.
43+
//delay(3000);
44+
//webSocket.broadcastTXT("ConnectionAlive");
45+
//webSocket.broadcastTXT("Temperature " + (String)temperature + "Humidity " + (String)humidity);
46+
47+
}

led.ino

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
2+
String myh;
3+
String myb;
4+
String mys;
5+
float h;
6+
float b;
7+
float s;
8+
uint32_t currbri;
9+
uint32_t r;
10+
uint32_t g;
11+
uint32_t bl;
12+
13+
void setPixelColor( uint16_t n, uint16_t brightness) {
14+
pixels.setPixelColor(n, (brightness * r / 255) , (brightness * g / 255), (brightness * bl / 255));
15+
}
16+
17+
uint32_t HSVColor(float h, float s, float v) {
18+
19+
h = constrain(h, 0, 360);
20+
s = constrain(s, 0, 1);
21+
v = constrain(v, 0, 1);
22+
23+
int i, b, p, q, t;
24+
float f;
25+
26+
h /= 60.0; // sector 0 to 5
27+
i = floor( h );
28+
f = h - i; // factorial part of h
29+
30+
b = v * 255;
31+
p = v * ( 1 - s ) * 255;
32+
q = v * ( 1 - s * f ) * 255;
33+
t = v * ( 1 - s * ( 1 - f ) ) * 255;
34+
switch ( i ) {
35+
case 0:
36+
r = b; g = t; bl = p;
37+
return pixels.Color(b, t, p);
38+
case 1:
39+
r = q; g = b; bl = p;
40+
return pixels.Color(q, b, p);
41+
case 2:
42+
r = p; g = b; bl = t;
43+
return pixels.Color(p, b, t);
44+
case 3:
45+
r = p; g = q; bl = b;
46+
return pixels.Color(p, q, b);
47+
case 4:
48+
r = t; g = p; bl = b;
49+
return pixels.Color(t, p, b);
50+
default:
51+
r = b; g = p; bl = q;
52+
return pixels.Color(b, p, q);
53+
}
54+
}
55+
void CallBrightness(uint32_t i) {
56+
HSVColor(h, s, b);
57+
for (int j = 0; j < NUMPIXELS; j++) {
58+
setPixelColor(j, i * 2.5);
59+
}
60+
pixels.show();
61+
/*
62+
pixels.setBrightness(i);
63+
for (int i = 0; i < NUMPIXELS; i++) {
64+
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
65+
pixels.setPixelColor(i, pixels.Color(150, 0, 0)); // Moderately bright green color.
66+
}
67+
pixels.show(); // This sends the updated pixel color to the hardware.
68+
*/
69+
}
70+
71+
void hueSet() {
72+
for (int i = 0; i < NUMPIXELS; i++) {
73+
pixels.setPixelColor(i, HSVColor(h, s, b));
74+
}
75+
pixels.show();
76+
}
77+
78+
79+
void extractLEDDetails(String payload) {
80+
81+
//payload[length] = '\0';
82+
//String input = (char*)payload;
83+
String input = payload;
84+
Serial.println(input);
85+
if (input.indexOf("LIGHTON") > -1) {
86+
b = currbri;
87+
}
88+
if (input.indexOf("LIGHTOFF") > -1) {
89+
currbri = b;
90+
b = 0;
91+
}
92+
93+
int count = input.length();
94+
if (input.indexOf("brightness") > -1) {
95+
myb = input.substring(10, count);
96+
b = myb.toInt();
97+
}
98+
if (input.indexOf("hue") > -1) {
99+
myh = input.substring(3, count);
100+
h = myh.toInt();
101+
102+
}
103+
if (input.indexOf("saturation") > -1) {
104+
mys = input.substring(10, count);
105+
s = mys.toInt();
106+
}
107+
108+
//hueSet();
109+
Serial.println("Hue: " + myh);
110+
Serial.println("Brightness: " + myb);
111+
Serial.println("Sat: " + mys);
112+
113+
CallBrightness(b);
114+
115+
}
116+

mqtt_esp8266.ino

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
2+
#include <ESP8266WiFi.h>
3+
#include <PubSubClient.h>
4+
#include <Adafruit_NeoPixel.h>
5+
#include <ArduinoOTA.h>
6+
#include <SimpleDHT.h>
7+
#ifdef __AVR__
8+
#include <avr/power.h>
9+
#endif
10+
#include <Hash.h>
11+
#define USE_SERIAL Serial
12+
13+
14+
15+
16+
// How many NeoPixels are attached to the Arduino?
17+
#define NUMPIXELS 60
18+
#define PIN 12
19+
20+
21+
int pinDHT11 = 14;
22+
SimpleDHT11 dht11;
23+
24+
25+
26+
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
27+
// Update these with values suitable for your network.
28+
29+
const char* ssid = "RMR";
30+
const char* password = "rahulmr123";
31+
const char* mqtt_server = "192.168.1.99";
32+
33+
WiFiClient wclient;
34+
PubSubClient client(wclient, mqtt_server);
35+
36+
long lastMsg = 0;
37+
char msg[50];
38+
int value = 0;
39+
#define BUFFER_SIZE 100
40+
41+
void setup() {
42+
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
43+
Serial.begin(115200);
44+
pixels.begin();
45+
setup_wifi();
46+
client.set_callback(callback);
47+
}
48+
49+
void setup_wifi() {
50+
51+
delay(10);
52+
// We start by connecting to a WiFi network
53+
Serial.println();
54+
Serial.println("Booting");
55+
WiFi.mode(WIFI_STA);
56+
WiFi.begin(ssid, password);
57+
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
58+
Serial.println("Connection Failed! Rebooting...");
59+
delay(5000);
60+
ESP.restart();
61+
}
62+
ArduinoOTA.onStart([]() {
63+
Serial.println("Start");
64+
});
65+
ArduinoOTA.onEnd([]() {
66+
Serial.println("\nEnd");
67+
});
68+
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
69+
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
70+
});
71+
ArduinoOTA.onError([](ota_error_t error) {
72+
Serial.printf("Error[%u]: ", error);
73+
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
74+
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
75+
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
76+
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
77+
else if (error == OTA_END_ERROR) Serial.println("End Failed");
78+
});
79+
ArduinoOTA.begin();
80+
Serial.println("Ready");
81+
Serial.print("IP address: ");
82+
Serial.println(WiFi.localIP());
83+
}
84+
85+
void callback(const MQTT::Publish& pub) {
86+
Serial.print("Message arrived [");
87+
Serial.print(pub.topic());
88+
Serial.print("] ");
89+
90+
// for (int i = 0; i < length; i++) {
91+
// Serial.print((char)payload[i]);
92+
// }
93+
94+
Serial.println(pub.payload_string());
95+
96+
extractLEDDetails(pub.payload_string());
97+
98+
Serial.println();
99+
100+
}
101+
102+
void reconnect() {
103+
// Loop until we're reconnected
104+
while (!client.connected()) {
105+
106+
Serial.print("Attempting MQTT connection...");
107+
// Attempt to connect
108+
109+
if (client.connect("ESP8266Client")) {
110+
Serial.println("connected");
111+
// Once connected, publish an announcement...
112+
client.publish("esp8266", "hello world");
113+
// ... and resubscribe
114+
client.subscribe("brightness");
115+
client.subscribe("saturation");
116+
client.subscribe("hue");
117+
client.subscribe("LIGHTON");
118+
client.subscribe("LIGHTOFF");
119+
} else {
120+
Serial.print("failed, rc=");
121+
//Serial.print(client.state());
122+
Serial.println(" try again in 5 seconds");
123+
// Wait 5 seconds before retrying
124+
delay(5004);
125+
}
126+
}
127+
}
128+
129+
130+
unsigned long interval;
131+
void loop() {
132+
133+
if (!client.connected()) {
134+
reconnect();
135+
}
136+
client.loop();
137+
138+
ArduinoOTA.handle();
139+
//600000
140+
if (millis() - interval > 600000) {
141+
interval = millis();
142+
currTempHumidity();
143+
}
144+
}

0 commit comments

Comments
 (0)