太极app控制

/**********************************************************************
项目名称/Project          : 零基础入门学用物联网
程序名称/Program name     : ranye_iot_led
团队/Team                : 太极创客团队 / Taichi-Maker (www.taichi-maker.com)
作者/Author              : CYNO朔
日期/Date(YYYYMMDD)     : 2021-10-01
程序目的/Purpose          : 
本程序旨在演示如何使用然也物联(www.ranye-iot.net)社区版实现板上LED控制。

-----------------------------------------------------------------------
本示例程序为太极创客团队制作的《零基础入门学用物联网》中示例程序。
该教程为对物联网开发感兴趣的朋友所设计和制作。如需了解更多该教程的信息,请参考以下网页:
http://www.taichi-maker.com/homepage/esp8266-nodemcu-iot/iot-tuttorial/mqtt-application/
***********************************************************************/
#include 
#include 

// 设置wifi接入信息(请根据您的WiFi信息进行修改)
const char* ssid = "1303_2.4g";
const char* password = "qiuping_123";
const char* mqttServer = "iot.ranye-iot.net";
// 如以上MQTT服务器无法正常连接,请前往以下页面寻找解决方案
// http://www.taichi-maker.com/public-mqtt-broker/

WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
// ****************************************************
// 设置公共变量
// ****************************************************
//计数器
  int count = 0;

// ****************************************************
// 注意!以下需要用户根据然也物联平台信息进行修改!否则无法工作!
// ****************************************************

// const char* subTopic = "imsniper/8266-1/led_kz";        // 订阅主题(需要修改)

// **************D1端口控制
const char* subT_d1_switch = "imsniper/8266-1/d1";  
const char* subT_d1_timer_switch = "imsniper/8266-1/d1/timer";       
const char* subT_d1_offtime = "imsniper/8266-1/d1/offtime"; 
const char* subT_d1_ontime = "imsniper/8266-1/d1/ontime";   
//IO1控制参数
  int count1 =0;
  int timer1 = 1; 
  int switch_1 = 1; 
  int turnOffTime1 = 720; 
  int turnOnTime1 = 200; 

// **************      
const char* pubTopic = "imsniper/8266-1/xinxi";        // 订阅主题(需要修改)
const char* willTopic = "imsniper/8266-1/yizhu";       // 遗嘱主题名称(需要修改)
// ****************************************************

//遗嘱相关信息
const char* willMsg = "esp8266 offline";        // 遗嘱主题信息
const int willQos = 0;                          // 遗嘱QoS
const int willRetain = false;                   // 遗嘱保留

const int subQoS = 1;            // 客户端订阅主题时使用的QoS级别(截止2020-10-07,仅支持QoS = 1,不支持QoS = 2)
const bool cleanSession = false; // 清除会话(如QoS>0必须要设为false)

bool ledStatus = HIGH;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);          // 设置板上LED引脚为输出模式
  digitalWrite(LED_BUILTIN, ledStatus);  // 启动后关闭板上LED
  Serial.begin(9600);                    // 启动串口通讯
  // 设置引脚模式
  pinMode(D0, OUTPUT);   // 初始化NodeMCU控制板载LED引脚为OUTPUT
  pinMode(D1, OUTPUT);   // 初始化NodeMCU控制板载LED引脚为OUTPUT
  pinMode(D2, OUTPUT);   // 初始化NodeMCU控制板载LED引脚为OUTPUT
  pinMode(D3, OUTPUT);   // 初始化NodeMCU控制板载LED引脚为OUTPUT
  pinMode(D4, OUTPUT);   // 初始化NodeMCU控制板载LED引脚为OUTPUT
  pinMode(D5, OUTPUT);   // 初始化NodeMCU控制板载LED引脚为OUTPUT
  pinMode(D6, OUTPUT);   // 初始化NodeMCU控制板载LED引脚为OUTPUT
  pinMode(D7, OUTPUT);   // 初始化NodeMCU控制板载LED引脚为OUTPUT
  pinMode(D8, OUTPUT);   // 初始化NodeMCU控制板载LED引脚为OUTPUT
  //设置ESP8266工作模式为无线终端模式
  WiFi.mode(WIFI_STA);
  
  // 连接WiFi
  connectWifi();
  
  // 设置MQTT服务器和端口号
  mqttClient.setServer(mqttServer, 1883);
  mqttClient.setCallback(receiveCallback);

  // 连接MQTT服务器
  connectMQTTserver();
}

void loop() {
  // 如果开发板未能成功连接服务器,则尝试连接服务器
  if (!mqttClient.connected()) {
    connectMQTTserver();
  }

   // 处理信息以及心跳
   mqttClient.loop();
   
   //循环定时
   count++;
  //循环定时执行
  //GPIO-1
  if (timer1 != 0)
  {
    /* code */
    // Serial.println("turnOffTime1:");Serial.println(turnOffTime1);
    // Serial.println("turnOnTime1:");Serial.println(turnOnTime1);
    // Serial.println("turnOffTime1+turnOnTime1:");Serial.println(turnOffTime1+turnOnTime1);

    if((count-count1) % (turnOffTime1+turnOnTime1) >= turnOffTime1) {
      if(!digitalRead(D1)){
        digitalWrite(D1, HIGH);
        Serial.print(count);
        Serial.println("set [GPIO-D1] in high");
      }
    }else {
      if(digitalRead(D1)){
        digitalWrite(D1, LOW);
        Serial.print(count);
        Serial.println("set [GPIO-D1] in LOW");
      }
    }
  }

     delay(1000);
  if(count == 86400 ){ //按照一天计算
    count = 0;
  }


}

// 连接MQTT服务器并订阅信息
void connectMQTTserver(){
  // 根据ESP8266的MAC地址生成客户端ID(避免与其它ESP8266的客户端ID重名)
  

  /* 连接MQTT服务器
  boolean connect(const char* id, const char* user, 
                  const char* pass, const char* willTopic, 
                  uint8_t willQos, boolean willRetain, 
                  const char* willMessage, boolean cleanSession); 
  若让设备在离线时仍然能够让qos1工作,则connect时的cleanSession需要设置为false                
                  */
  if (mqttClient.connect(clientId, mqttUserName, 
                         mqttPassword, willTopic, 
                         willQos, willRetain, willMsg, cleanSession)) { 
    Serial.print("MQTT Server Connected. ClientId: ");
    Serial.println(clientId);
    Serial.print("MQTT Server: ");
    Serial.println(mqttServer);    
    
    subscribeTopic(); // 订阅指定主题
  } else {
    Serial.print("MQTT Server Connect Failed. Client State:");
    Serial.println(mqttClient.state());
    delay(5000);
  }   
}

// 收到信息后的回调函数
void receiveCallback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message Received [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);

  }
  Serial.println("");
  Serial.print("Message Length(Bytes) ");
  Serial.println(length);

  // if ((char)payload[0] == '1') {     // 如果收到的信息以“1”为开始
  //   ledStatus = LOW;
  //   digitalWrite(BUILTIN_LED, ledStatus);  // 则点亮LED。
  // } else {
  //   ledStatus = HIGH;                           
  //   digitalWrite(BUILTIN_LED, ledStatus); // 否则熄灭LED。
  // }
  
  String top = String(topic);//把char* topic 转换成String,方便判断比较。
  
//************【D1端口处理-开始】****************

  // int timer1 = 1; 
  // int turnOffTime1 = 720; 
  // int turnOnTime1 = 200; 

  if (top == "imsniper/d1")
  {
    
    if ((char)payload[0] == '1') {
      timer1 = 0;
      if (!digitalRead(D1))
           {
            digitalWrite(D1,HIGH);
           }    // 如果收到的信息以“1”为开始
   } else {
      timer1 = 0;
      if (digitalRead(D1))
           {
            digitalWrite(D1,LOW);
           }     
    }
    
  }
  if (top == "imsniper/d1/timer"){
    
    if ((char)payload[0] == '1') {     // 如果收到的信息以“1”为开始
    timer1 = 1;
   } else {
    timer1 = 0;
    }
    
  }else if(top == "imsniper/d1/offtime"){
      
    count1 = count; 
    turnOffTime1 = payloadToInt(payload,length);
    
  }else if (top == "imsniper/d1/ontime"){
    
    count1 = count; 
    turnOnTime1 = payloadToInt(payload,length);
    
   }
    
  
//************【D1端口处理-结束】****************  

}


int payloadToInt(byte* payload,unsigned int length){
  int aa;
      if(length == 1 ){
      aa = int(payload[0]-48);     
      }else if(length == 2){
      aa = int(payload[0]-48)*10+int(payload[1]-48);        
      }else if(length == 3){
      aa = int(payload[0]-48)*100+int(payload[1]-48)*10 + int(payload[2]-48);       
      } 
      return aa;
  }
// 订阅指定主题
void subscribeTopic(){

  
  // 通过串口监视器输出是否成功订阅主题以及订阅的主题名称
  // 请注意subscribe函数第二个参数数字为QoS级别。这里为QoS = 1
  if(mqttClient.subscribe("imsniper/#", subQoS)){
    Serial.print("Subscribed Topic: ");
    Serial.println(subTopic);
  } else {
    Serial.print("Subscribe Fail...");
  }

}

// 发布信息
void pubMQTTmsg(){
  char* pubMessage;
  
  if (ledStatus == LOW){
    pubMessage = "LED ON";
  } else {
    pubMessage = "LED OFF";
  }

  // 实现ESP8266向主题发布信息
  if(mqttClient.publish(pubTopic, pubMessage)){
    Serial.println("Publish Topic:");Serial.println(pubTopic);
    Serial.println("Publish message:");Serial.println(pubMessage);    
  } else {
    Serial.println("Message Publish Failed."); 
  }
}

// ESP8266连接wifi
void connectWifi(){

  WiFi.begin(ssid, password);
 
  //等待WiFi连接,成功连接后输出成功信息
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi Connected!");  
  Serial.println(""); 
}

你可能感兴趣的:(太极app控制)