Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Ethernet.h>
- #include <MQTT.h>
- #include <ArduinoJson.h>
- byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
- char device_name[] = "lamp";
- #define PORT_NUMBER_TO_CONTROL LED_BUILTIN
- EthernetClient net;
- MQTTClient client(350);
- void connect() {
- Serial.print("connecting...");
- while (!client.connect("arduino", "try", "try")) {
- Serial.print(".");
- delay(1000);
- }
- Serial.println("\nconnected!");
- client.subscribe("domoticz/out");
- }
- void messageReceived(MQTTClient *client, char topic[], char payload[], int payload_length) {
- StaticJsonDocument<350> doc;
- deserializeJson(doc, payload, payload_length);
- Serial.println(freeMemory());
- char* name = doc["name"].as<char*>();
- int i = 0;
- bool if_name_match = true;
- while (name[i]) {
- if (name[i] != device_name[i]) {
- if_name_match = false;
- break;
- }
- i++;
- }
- if (if_name_match) {
- if (!doc["nvalue"].as<bool>()) {
- digitalWrite(PORT_NUMBER_TO_CONTROL, HIGH);
- Serial.println("Включено");
- }
- else {
- digitalWrite(PORT_NUMBER_TO_CONTROL, LOW);
- Serial.println("Выключено");
- }
- }
- }
- void setup() {
- pinMode(PORT_NUMBER_TO_CONTROL, OUTPUT);
- Serial.begin(115200);
- Ethernet.begin(mac);
- client.begin("192.168.0.120", net);
- client.onMessageAdvanced(messageReceived);
- connect();
- }
- void loop() {
- client.loop();
- if (!client.connected()) {
- connect();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement