#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include "DFRobot_Aliyun.h"


/*配置WIFI名和密码*/
const char * WIFI_SSID = "DFRobot-guest";
const char * WIFI_PASSWORD = "dfrobot@2017";

/*配置设备证书信息*/
//根据你的设备设置来更换这些参数
String ProductKey = "your ProductKey";
String ClientId = "your ClientId";
String DeviceName = "your DeviceName";
String DeviceSecret = "your DeviceSecret";

/*配置域名和端口号*/
String ALIYUN_SERVER = "iot-as-mqtt.cn-shanghai.aliyuncs.com";
uint16_t PORT = 1883;

/*需要操作的产品标识符(自产和代理两个标识符)*/
String IdentifierA = "CountA";
String IdentifierB = "CountB";

/*需要上报和订阅的两个TOPIC*/
const char * subTopic = "/sys/a16yECCLMxp/Counter/thing/service/property/set";//****复制你的set到此处
const char * pubTopic = "/sys/a16yECCLMxp/Counter/thing/event/property/post";//******复制你的post到此处

DFRobot_Aliyun myAliyun;
WiFiClient espClient;
PubSubClient client(espClient);


#define BUTTON_PIN1 D4
#define BUTTON_PIN2 D5


int countA=0;
int countB=0;


void connectWiFi(){
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID,WIFI_PASSWORD);
while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.print("IP Adderss: ");
Serial.println(WiFi.localIP());
}

void callback(char * topic, byte * payload, unsigned int len){
Serial.print("Recevice [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < len; i++){
Serial.print((char)payload[i]);
}
Serial.println();
}

void ConnectAliyun(){
while(!client.connected()){
Serial.print("Attempting MQTT connection...");
/*根据自动计算的用户名和密码连接到Alinyun的设备，不需要更改*/
if(client.connect(myAliyun.client_id,myAliyun.username,myAliyun.password)){
Serial.println("connected");
client.subscribe(subTopic);
}else{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup(){

pinMode(BUTTON_PIN1, INPUT);
pinMode(BUTTON_PIN2, INPUT);
Serial.begin(115200);

/*连接WIFI*/
connectWiFi();

/*初始化Alinyun的配置，可自动计算用户名和密码*/
myAliyun.init(ALIYUN_SERVER,ProductKey,ClientId,DeviceName,DeviceSecret);

client.setServer(myAliyun.mqtt_server,PORT);

/*设置回调函数，当收到订阅信息时会执行回调函数*/
client.setCallback(callback);

/*连接到Aliyun*/
ConnectAliyun();
}

int old_valA=LOW;
int old_valB=LOW;

void loop(){
  if(!client.connected()){
    ConnectAliyun();
    
  }


  int valA=digitalRead(BUTTON_PIN1);//读取输入数值并且存储
  int valB=digitalRead(BUTTON_PIN2);//读取输入数值并且存储
  if(valA==HIGH)   //检查按钮的变化情况
  {
   countA++;
   Serial.print("事件A次数：");
   Serial.println(countA);
   client.publish(pubTopic,("{\"id\":"+ClientId+",\"params\":{\""+IdentifierA+"\":"+countA+",\""+IdentifierB+"\":"+countB+"},\"method\":\"thing.event.property.post\"}").c_str());
   delay(300);//防抖
   }
   

  
  if(valB ==HIGH)   //检查按钮的变化情况
  {
   countB++;
   Serial.print("事件B次数：");
   Serial.println(countB);
   client.publish(pubTopic,("{\"id\":"+ClientId+",\"params\":{\""+IdentifierA+"\":"+countA+",\""+IdentifierB+"\":"+countB+"},\"method\":\"thing.event.property.post\"}").c_str()); 
   delay(300);//防抖
   }
   
  
    //在下方更改你的上传的数据内容，如countA和countB
  
  client.loop();
}
