-
-
Save Baael/1ba70000db7c539571cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <espduino.h> | |
#include <mqtt.h> | |
#include <SoftwareSerial.h> | |
#include <IRremote.h> | |
// SSID sieci wifi | |
#define SSID "ssid" | |
// Haslo sieci wifi | |
#define PASS "pass" | |
// Nalezy podac adres brokera MQTT | |
#define BROKER "192.168.0.1" | |
// nazwa pszczoly w naszym roju | |
char* bee_name = "hifi"; | |
IRsend irsend; | |
MQTT mqtt(&esp); | |
SoftwareSerial espPort(11, 10); // TX, RX | |
ESP esp(&espPort, &Serial, 9); | |
// callback stanu polaczenia z siecia wifi | |
void wifiCb(void* response) | |
{ | |
uint32_t status; | |
RESPONSE res(response); | |
if (res.getArgc() == 1) { | |
res.popArgs((uint8_t*)&status, 4); | |
if (status == STATION_GOT_IP) { mqtt.connect(BROKER, 1883); } | |
} | |
} | |
// callback polaczenia z brokerem MQTT | |
void mqttConnected(void* response) | |
{ | |
// subskrybcja kanalu | |
mqtt.subscribe("bee/hifi/+"); | |
// zameldowanie sie pszczoly w ulu ;) Lubie takie nazewnictwo | |
// mqtt.publish("swarm/bee/online", bee_name); | |
} | |
void mqttDisconnected(void* response){} | |
void mqttPublished(void* response){} | |
// callback otrzymanych danych z MQTT | |
void mqttData(void* response){ | |
// przetwarzamy otrzymane informacje, na temat i payload | |
RESPONSE res(response); | |
String topic = res.popString(); | |
String payload = res.popString(); | |
// w zaleznosci od tematu wiadomosci | |
// uruchamiamy odpowiednia funkcje z payloadem jako argumentem | |
if (topic == "bee/hifi/volume") { setVolume(payload); } | |
if (topic == "bee/hifi/mode") { setMode(payload); } | |
if (topic == "bee/hifi/power") { triggerPower(); } | |
} | |
void setVolume(void* mode) { | |
if (mode == "down"){ irsend.sendSony(0xc81, 12); } | |
if (mode == "up") { irsend.sendSony(0x481, 12); } | |
} | |
void setMode(void* mode) { | |
if (mode == "radio") { irsend.sendSony(0x181, 12); } | |
if (mode == "dvd") { irsend.sendSony(0xbe1, 12); } | |
if (mode == "aux") { irsend.sendSony(0x761, 12); } | |
} | |
// przycisk power moze tylko przelaczac aktualny stan urzadzenia | |
// trzeba jakos domyslic sie czy jest wlaczone czy wylaczone | |
void triggerPower() { | |
irsend.sendSony(0xa81, 12); | |
} | |
// inicjalizujemy modul esp8266 | |
void setupESP8266() { | |
esp.enable(); | |
delay(200); | |
esp.reset(); | |
delay(200); | |
// jesli zawiesi sie w tym miejscu to warto zewrzec na chwile RST modulu z GND | |
while(!esp.ready()); | |
// podpinamy callback sieci wifi | |
esp.wifiCb.attach(&wifiCb); | |
// podlaczamy sie do istniejacej sieci wifi | |
esp.wifiConnect(SSID,PASS); | |
} | |
// inicjalizacja MQTT | |
void setupMQTT() { | |
mqtt.begin(bee_name, "admin", "Isb_C4OGD4c3", 120, 1); | |
mqtt.connectedCb.attach(&mqttConnected); | |
mqtt.disconnectedCb.attach(&mqttDisconnected); | |
mqtt.publishedCb.attach(&mqttPublished); | |
mqtt.dataCb.attach(&mqttData); | |
} | |
void setup() { | |
espPort.begin(19200); | |
setupESP8266(); | |
setupMQTT(); | |
} | |
void loop() { | |
esp.process(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment