1
0
Fork 0
This repository has been archived on 2023-07-19. You can view files and clone it, but cannot push or open issues or pull requests.
kekskurse.de-old/static/code/ir-wemos-mosfet.txt

134 lines
2.8 KiB
Text
Raw Permalink Normal View History

2020-11-11 14:43:41 +00:00
#include <IRremoteESP8266.h>
#include <Arduino.h>
#include <IRrecv.h>
#include <IRsend.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "";
const char* password = "";
const char* mqtt_server = "";
const char* mqtt_user = "";
const char* mqtt_pass = "";
const char* mqtt_client_id = "edifier-remote";
const char* mqtt_status_topic = "status/edifier-remote";
WiFiClient espClient;
PubSubClient client(espClient);
//IRrecv irrecv(RECV_PIN);
IRsend irsend(0);
//decode_results results;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
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());
}
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(String(topic)=="musik/power") {
irsend.sendNEC(149389439, 32);
Serial.println("EDIFIER: Power");
}
if(String(topic)=="musik/mute") {
irsend.sendNEC(149356799, 32);
Serial.println("EDIFIER: Mute");
}
if(String(topic)=="musik/down") {
irsend.sendNEC(149369039, 32);
Serial.println("EDIFIER: Down");
}
if(String(topic)=="musik/up") {
irsend.sendNEC(149393519, 32);
Serial.println("EDIFIER: UP");
}
if(String(topic)=="musik/bluetooth") {
irsend.sendNEC(149377199, 32);
Serial.println("EDIFIER: Bluetooth");
}
if(String(topic)=="musik/line") {
irsend.sendNEC(149399639, 32);
Serial.println("EDIFIER: Line");
}
}
void reconnect() {
Serial.println("Reconnect!");
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(mqtt_client_id, mqtt_user, mqtt_pass, mqtt_status_topic, 1, true, "0")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish(mqtt_status_topic, "1");
// ... and resubscribe
client.subscribe("musik/#");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
Serial.begin(9600);
irsend.begin();
Serial.println("Start");
Serial.println("Start");
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}