回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页
best-icon

【IoT套件测评】阿里云IoT 之智能浇花 简单

头像 rzegkly 2019.07.02 4912 7
project-image

一、阿里云IoT 之智能浇花思路:

1、萤火虫 ESP32与物联网建立网络通信连接

2、土壤传感器检测花盆中水分多少,通过网络实时转发,

3、通过第三方钉钉群聊,让信息同步到钉钉聊天机器人,提醒主人为花草浇水。

project-image

二、环境

1、硬件:

阿里云IoT 入门套件x1 FireBeetle Board-ESP32 主板,购买地址:

2、软件:

1、操作系统:基于 Windows 7操作系统,

2、软件:Arduino1.8.0 以上版本下载链接: 
 

3、钉钉下载地址

project-image

三、进入阿里云物联网中进行产品和设备创建,https://iot.aliyun.com

1、创建产品:智能浇花

2、添加设备:Watering

3、功能定义

4、复制设备证书、域名、端口号等连接信息

5、设备的Topic列表

project-image
project-image
project-image
project-image

四、程序编写

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

#define SOIL_SENSOR_PIN  A0

/*配置WIFI名和密码*/
const char * WIFI_SSID     = "wifi;
const char * WIFI_PASSWORD = "wifi";

/*配置设备证书信息*/
String ProductKey = "********";
String ClientId = "12345";
String DeviceName = "********";
String DeviceSecret = "*****";

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

/*需要操作的产品标识符*/
String Identifier = "******";

/*需要上报的TOPIC*/
const char * pubTopic = "/sys/a1tWFIhbL86/Watering/thing/event/property/post";//******post

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

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");
    }else{
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}
void setup(){
  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();
}

uint8_t tempTime = 0;
void loop(){
  if(!client.connected()){
    ConnectAliyun();
  }
  /*一分钟上报两次土壤信息*/
  if(tempTime > 60){
    tempTime = 0;
    client.publish(pubTopic,("{\"id\":"+ClientId+",\"params\":{\""+Identifier+"\":"+analogRead(SOIL_SENSOR_PIN)+"},\"method\":\"thing.event.property.post\"}").c_str());
  }else{
    tempTime++;
    delay(500);
  }
  client.loop();
}

五、数据测试

1、设备管理进入在线调试窗口,获取土壤湿度 ,发送指令{ "SmartWatering": 1127 }

2、查看土壤湿度数据,运行状态,查看数据

project-image
project-image
project-image

六、钉钉机器人相关配置

1、打开钉钉客户端,在本人的钉钉群点开“群设置”里添加群机器人,找到webhook网址,复制粘贴下来;

2、阿里云物联网控制台物联网开发中找到"服务开发",中配置“钉钉机器人”,填入复制的“webhook”

3、启动钉钉机器人时,

当检测到土壤湿度数据大于等于900且小于等于2000,钉钉机器人:“花草健康无需浇水”;

当土壤湿度数据大于2000,钉钉机器人:“嗨,够了,请别再浇水了”;

土壤湿度数据小于900,钉钉机器人:“嗨,我渴了,请给我一点水喝”。

project-image
project-image
project-image
project-image
project-image
project-image
project-image
project-image
project-image
project-image
project-image
project-image

材料清单

  • FireBettle Board-ESP32(焊接排母)x1 X1 链接
  • FireBeetle Covers-Gravity Adapter Board(焊接排针)x1 X1 链接
  • 土壤湿度传感器 X1 链接

七、知识 拓展:

钉钉机器人:

钉钉机器人——DingTalk_Robot 钉钉(DingTalk)是阿里巴巴集团专为中国企业打造的免费沟通和协同的多端平台 ,提供PC版,Web版和手机版。是钉钉群的高级扩展功能。群机器人可以将第三方服务的信息聚合到群聊中,实现自动化的信息同步。例如:通过聚合GitHub,GitLab等源码管理服务,实现源码更新同步;通过聚合Trello,JIRA等项目协调服务,实现项目信息同步。

八、视频回放:

评论

user-avatar
  • 腿毛利小五郎

    腿毛利小五郎2023.06.30

    学习学习

    0
    • 江宇瀚

      江宇瀚2019.07.26

      牛逼

      0
      • rzegkly

        rzegkly2019.07.11

        谢谢,希望多多指导,多多交流,钉钉程序修改一下

        0
        • Fish

          Fish2019.07.04

          很好学习了

          0
          • Fish

            Fish2019.07.04

            棒棒的

            0
            • Fish

              Fish2019.07.04

              棒棒的

              0
              • fpqEu0X2LC7y

                fpqEu0X2LC7y2019.07.04

                好!

                0