Last active
August 29, 2015 14:05
-
-
Save wendal/3dcc1bba82645cf0abfe to your computer and use it in GitHub Desktop.
Yeelink: W5500模块+mqtt 控制LED灯
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 <SPI.h> | |
#define WIZ550io_WITH_MACADDRESS 1 | |
#include <Ethernet.h> | |
#include "EthernetClient.h" | |
#include "PubSubClient.h" | |
#define WIZ550io_WITH_MACADDRESS 1 | |
#include <JsonParser.h> | |
using namespace ArduinoJson::Parser; | |
byte server[] = { 114,215,121,175 }; // ip for mqtt.yeelink.net | |
void mqtt_callback(char* topic, byte* payload, unsigned int length) { | |
//Serial.print("topic="); | |
//Serial.println(topic); | |
//Serial.print("payload len="); | |
//Serial.println(length); | |
char* p = (char*)malloc(length + 1); | |
memcpy(p,payload,length); | |
p[length] = 0x0; | |
JsonParser<16> parser; | |
JsonObject root = parser.parse(p); | |
if (!root.success()) | |
{ | |
Serial.println("JsonParser.parse() failed"); | |
} else { | |
char* value = root["value"]; | |
Serial.print("value="); | |
Serial.println(value); | |
if (value[0] == '0') { | |
Serial.println("LED Power OFF"); | |
digitalWrite(8, HIGH); // RGB LED, HIGH == OFF | |
} else { | |
Serial.println("LED Power ON"); | |
digitalWrite(8, LOW); // RGB LED, LOW == ON | |
} | |
} | |
free(p); | |
} | |
EthernetClient client; | |
///PubSubClient mqtt("mtqq.yeelink.net", 1883, mqtt_callback, client); | |
PubSubClient mqtt(server, 1883, mqtt_callback, client); | |
void setup() | |
{ | |
pinMode(8, OUTPUT); | |
Serial.begin(9600); | |
Ethernet.begin(); | |
Serial.print("ip="); | |
Serial.println(Ethernet.localIP()); | |
Serial.print("mqtt connect="); | |
Serial.println(mqtt.connect("arduinoClient", "your-username", "your-key")); | |
int re = mqtt.subscribe("/v1.1/device/12825/sensor/21074/datapoints"); | |
Serial.print("subscribe="); | |
Serial.println(re); | |
} | |
void loop() | |
{ | |
mqtt.loop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment