mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
167 lines
3.5 KiB
C++
167 lines
3.5 KiB
C++
#include <IRremoteESP8266.h>
|
|
#include <ESP8266WiFi.h>
|
|
#include <PubSubClient.h>
|
|
|
|
IRsend irsend(4); //an IR led is connected to GPIO pin 0
|
|
|
|
/*const char* ssid = "bigChief2";
|
|
const char* password = "987654321";*/
|
|
const char* ssid = "ESP8266 - TEST";
|
|
const char* password = "Coconuts09";
|
|
|
|
const char* mqtt_server = "m21.cloudmqtt.com";
|
|
|
|
const int led = 5;
|
|
|
|
WiFiClient espClient;
|
|
PubSubClient client(espClient);
|
|
|
|
void setup()
|
|
{
|
|
irsend.begin();
|
|
pinMode(led, OUTPUT);
|
|
|
|
Serial.begin(115200);
|
|
WiFi.softAPdisconnect(true);
|
|
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());
|
|
digitalWrite(led, HIGH);
|
|
|
|
client.setServer(mqtt_server, 18932);
|
|
client.setCallback(callback);
|
|
}
|
|
|
|
void callback(char* topic, byte* payload, unsigned int length) {
|
|
Serial.print("Message arrived [");
|
|
Serial.print(topic);
|
|
Serial.print("] ");
|
|
for (int i = 0; i < length; i++) {
|
|
Serial.print((char)payload[i]);
|
|
}
|
|
Serial.println();
|
|
|
|
if ((char)payload[0] == '0') {
|
|
for (int i = 0; i < 3; i++) {
|
|
irsend.sendNEC(0xfff807, 32);
|
|
delay(40);
|
|
}
|
|
}
|
|
|
|
if ((char)payload[0] == '1') {
|
|
for (int i = 0; i < 3; i++) {
|
|
irsend.sendNEC(0xffb04f, 32);
|
|
delay(40);
|
|
}
|
|
for (int i = 0; i < 3; i++) {
|
|
irsend.sendNEC(0xff8877, 32);
|
|
delay(40);
|
|
}
|
|
}
|
|
|
|
if ((char)payload[0] == '2') {
|
|
for (int i = 0; i < 3; i++) {
|
|
irsend.sendNEC(0xffb04f, 32);
|
|
delay(40);
|
|
}
|
|
for (int i = 0; i < 3; i++) {
|
|
irsend.sendNEC(0xff38c7, 32);
|
|
delay(40);
|
|
}
|
|
}
|
|
|
|
if ((char)payload[0] == '3') {
|
|
for (int i = 0; i < 3; i++) {
|
|
irsend.sendNEC(0xffb04f, 32);
|
|
delay(40);
|
|
}
|
|
for (int i = 0; i < 3; i++) {
|
|
irsend.sendNEC(0xffa857, 32);
|
|
delay(40);
|
|
}
|
|
}
|
|
|
|
if ((char)payload[0] == '4') {
|
|
for (int i = 0; i < 3; i++) {
|
|
irsend.sendNEC(0xffb04f, 32);
|
|
delay(40);
|
|
}
|
|
for (int i = 0; i < 3; i++) {
|
|
irsend.sendNEC(0xffe817, 32);
|
|
delay(40);
|
|
}
|
|
}
|
|
|
|
if ((char)payload[0] == '5') {
|
|
for (int i = 0; i < 3; i++) {
|
|
irsend.sendNEC(0xffb04f, 32);
|
|
delay(40);
|
|
}
|
|
for (int i = 0; i < 3; i++) {
|
|
irsend.sendNEC(0xff20df, 32);
|
|
delay(40);
|
|
}
|
|
}
|
|
|
|
if ((char)payload[0] == '6') {
|
|
for (int i = 0; i < 3; i++) {
|
|
irsend.sendNEC(0xffb04f, 32);
|
|
delay(40);
|
|
}
|
|
for (int i = 0; i < 3; i++) {
|
|
irsend.sendNEC(0xff8877, 32);
|
|
delay(40);
|
|
}
|
|
}
|
|
}
|
|
|
|
void reconnect() {
|
|
// Loop until we're reconnected
|
|
while (!client.connected()) {
|
|
Serial.print("Attempting MQTT connection...");
|
|
// Attempt to connect
|
|
if (client.connect("ESP8266Client-LedWeatherSensor", "oilkfgjy", "lEyZb90q49Rf")) {
|
|
Serial.println("connected");
|
|
client.subscribe("temp");
|
|
} else {
|
|
Serial.print("failed, rc=");
|
|
Serial.print(client.state());
|
|
Serial.println(" try again in 5 seconds");
|
|
// Wait 5 seconds before retrying
|
|
delay(5000);
|
|
}
|
|
}
|
|
}
|
|
|
|
void loop(void) {
|
|
if(WiFi.status() != WL_CONNECTED){
|
|
digitalWrite(led, LOW);
|
|
Serial.println("Trying to reconnect !");
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("");
|
|
Serial.println("Connected !");
|
|
digitalWrite(led, HIGH);
|
|
}
|
|
|
|
if (!client.connected()) {
|
|
digitalWrite(led, LOW);
|
|
reconnect();
|
|
}else{
|
|
digitalWrite(led, HIGH);
|
|
}
|
|
client.loop();
|
|
}
|