|
#include <WiFiEsp.h>
|
|
#include <Countdown.h>
|
|
#include <IPStack.h>
|
|
#include <MQTTClient.h>
|
|
|
|
WiFiEspClient c;
|
|
|
|
IPStack ipstack(c);
|
|
MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack);
|
|
|
|
void CallBackMessageArrived(MQTT::MessageData& md)
|
|
{
|
|
Serial.print("======>");
|
|
Serial.println(md.topicName.lenstring.data);
|
|
}
|
|
|
|
char prefix[13]="\x08\x12\x08\x0c\x00\x1e\x12\x04\x02\x13\x04\x15";
|
|
char groupe[15]="\x05\x10\x0d\x17\x12\x07\x1d\x0e\x03\x04\x04\x0d\x0c\x16";
|
|
char periph[9]="\x0f\x06\x07\x6c\x0f\x06\x07\x72";
|
|
|
|
char topic[128];
|
|
char tmp1[13];
|
|
char tmp2[15];
|
|
char tmp3[9];
|
|
|
|
void BrokerConnect()
|
|
{
|
|
ipstack.connect("192.168.1.136", 10800);
|
|
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
|
|
data.MQTTVersion = 3;
|
|
data.clientID.cstring = (char*)"User";
|
|
data.username.cstring=(char*)"user";
|
|
data.password.cstring=(char*)"test";
|
|
data.cleansession=true;
|
|
client.connect(data);
|
|
|
|
unXor(prefix,tmp1,'A',12);
|
|
unXor(groupe,tmp2,'B',14);
|
|
unXor(periph,tmp3,'C',9);
|
|
|
|
sprintf(topic,"/%s/%s/#",tmp1,tmp2);
|
|
client.subscribe(topic, MQTT::QOS0, CallBackMessageArrived);
|
|
}
|
|
|
|
void WifiConnect(){
|
|
Serial1.begin(9600);
|
|
while(!Serial1);
|
|
Serial.begin(9600);
|
|
while(!Serial);
|
|
WiFi.init(&Serial1);
|
|
WiFi.begin((char*)"ZZ_HSH","WIFI_ZZ_F5");
|
|
}
|
|
void setup() {
|
|
WifiConnect();
|
|
BrokerConnect();
|
|
}
|
|
|
|
void unXor(char *in,char *out,unsigned char key,int len){
|
|
for(int i=0;i<len;i++){
|
|
*out++=(*in++)^key;
|
|
}
|
|
*out='\0';
|
|
}
|
|
|
|
void loop() {
|
|
if(!client.isConnected())
|
|
BrokerConnect();
|
|
|
|
sprintf(topic,"/%s/%s/%s",tmp1,tmp2,tmp3);
|
|
Serial.println(topic);
|
|
client.publish(topic,(void *)"ON",2);
|
|
|
|
client.yield(10000);
|
|
|
|
}
|
|
|