基于Arduino与机智云的家居环境远程监控系统

电子说

1.3w人已加入

描述

基于Arduino与机智云的家居环境远程监控系统具备协议简单、成本低、功耗低、组网容易等优势。在智慧家居实现的同时,可以根据其它控制领域的需要,进行需求的移植,具有很强的延伸性和可移植性,提高开发效率。同时当前环境也存在这样的需求也有硬件基础保障。通过该项目的制作,也让我了解到远程家电控制并不是什么神奇的东西的,只要每一个人肯去尝试,肯去面对不同的困难,并通过自己的思考和向他人请求帮助,每一个过程都是很奇妙的,往往在这个过程中,你会获得许多意想不到的收获。

 

本项目分成三部分——工程实施、云平台部署、APP开发,本次分享云平台部署部分。

 

一、创建项目

1.1 登陆机智云开发者中心dev.gizwits.com,创建新产品

Arduino

1.2 进入项目,查看产品的基本信息

Arduino

二、添加数据点

2.1 根据产品功能定义数据点

 

Arduino

 

三、进行虚拟设备调试

3.1 创建好数据点后,进行虚拟设备调试

 

Arduino

 

3.2 在机智云APP能够观察机智云平台模拟的数据

根据系统的需要添加或修改数据点信息

Arduino

 

四、下载MCU代码包

4.1 在确定数据点无误后,点击MCU开发

Arduino

 

4.2 添加库文件,进行代码移植

生成代码包后,把代码包导入Arduino IDE开发环境的libraries文件中,libraries是Arduino的库文件,包含自带的库文件和用户添加的外部库文件

Arduino

把networkConfig.ino的配网代码移植到simpleTry.ino,把串口打印的代码注释起来,可参考“史上最详细的Arduino uno R3接入机智云教程”

https://club.gizwits.com/thread-6728-1-1.html

然后根据要求修改代码,以下仅供参考:


#include #include #include #include int con_ledPin = 13;    //成功连接网络,13引脚低电平int smokePin = 2;int movePin = 3;int alarmPin = 4;int ledPin = 5;int relayPin1 = 8;int relayPin2 = 9;int firePin = A0;int lightPin = A1;int beepPin = 10;int temhumPin = 11;bool alarmflag = 0;dht myDHT_11;SoftwareSerial mySerial(A2, A3); // A2 -> RX, A3 -> TXGizwits myGizwits;#define   KEY1              6#define   KEY2              7#define   KEY1_SHORT_PRESS  1#define   KEY1_LONG_PRESS   2#define   KEY2_SHORT_PRESS  4#define   KEY2_LONG_PRESS   8#define   NO_KEY            0#define   KEY_LONG_TIMER    3#define   ALARM_SHORT_PRESS 16#define   ALARM_LONG_PRESS  32unsigned long Last_KeyTime = 0;
unsigned long gokit_time_s(void){  return millis() / 1000;}char gokit_key1down(void){  unsigned long keep_time = 0;  if (digitalRead(KEY1) == LOW)  {    delay(100);    if (digitalRead(KEY1) == LOW)    {      keep_time = gokit_time_s();      while (digitalRead(KEY1) == LOW)      {        if ((gokit_time_s() - keep_time) > KEY_LONG_TIMER)        {          Last_KeyTime = gokit_time_s();          return KEY1_LONG_PRESS;        }      } //until open the key      if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER)      {        return KEY1_SHORT_PRESS;      }      return 0;    }    return 0;  }  return 0;}char gokit_key2down(void){  unsigned long keep_time = 0;  if (digitalRead(KEY2) == LOW)  {    delay(100);    if (digitalRead(KEY2) == LOW)    {    delay(100);    if (digitalRead(KEY2) == LOW)    {      keep_time = gokit_time_s();      while (digitalRead(KEY2) == LOW) //until open the key      {        if ((gokit_time_s() - keep_time) > KEY_LONG_TIMER)        {          Last_KeyTime = gokit_time_s();          return KEY2_LONG_PRESS;        }      }      if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER)      {        return KEY2_SHORT_PRESS;      }      return 0;    }    return 0;  }  return 0;}//室内报警监测char gokit_alarmdown(void){  unsigned long keep_time = 0;  if (digitalRead(alarmPin) == LOW)  {    delay(100);    if (digitalRead(alarmPin) == LOW)    {      keep_time = gokit_time_s();      while (digitalRead(alarmPin) == LOW) //until open the key      {        if ((gokit_time_s() - keep_time) > KEY_LONG_TIMER)        {          Last_KeyTime = gokit_time_s();          return ALARM_LONG_PRESS;        }      }      if ((gokit_time_s() - Last_KeyTime) > KEY_LONG_TIMER)      {        return ALARM_SHORT_PRESS;      }      return 0;    }    return 0;  }  return 0;}char gokit_keydown(void){  char ret = 0;  ret |= gokit_alarmdown();  ret |= gokit_key2down();  ret |= gokit_key1down();  return ret;}void KEY_Handle(void){  /*  Press for over than 3 second is Long Press  */  switch (gokit_keydown())  {    case KEY1_SHORT_PRESS:      myGizwits.setBindMode(WIFI_PRODUCTION_TEST);      break;    case KEY1_LONG_PRESS:      myGizwits.setBindMode(WIFI_RESET_MODE);      break;    case KEY2_SHORT_PRESS:      myGizwits.setBindMode(WIFI_SOFTAP_MODE);      //Soft AP mode      break;    case KEY2_LONG_PRESS:      myGizwits.setBindMode(WIFI_AIRLINK_MODE);      //AirLink mode      break;    case ALARM_SHORT_PRESS:      alarmflag = ((alarmflag == 0) ? 1 : 0);      if (alarmflag)      {        digitalWrite(beepPin, LOW);      }      else      {        digitalWrite(beepPin, HIGH);      }      break;    case ALARM_LONG_PRESS:      digitalWrite(ledPin, HIGH); //设置为高电平      digitalWrite(relayPin1, HIGH); //设置为高电平      digitalWrite(relayPin2, HIGH); //设置为高电平      break;    default:      break;  }}//获取温度数据int dht_11_gettemperature() {  int chk = myDHT_11.read11(temhumPin);  int value = myDHT_11.temperature;  return value;}//获取湿度数据int dht_11_gethumidity() {  int chk = myDHT_11.read11(temhumPin);  int value = myDHT_11.humidity;  return value;}/**  Serial Init , Gizwits Init  @param none  @return none*/void setup() {  // put your setup code here, to run once:  mySerial.begin(115200);  pinMode(KEY1, INPUT_PULLUP);  pinMode(KEY2, INPUT_PULLUP);  pinMode(smokePin, INPUT);  //设置为数字输入模式  pinMode(movePin, INPUT);  pinMode(alarmPin, INPUT_PULLUP);  pinMode(firePin, INPUT);  pinMode(lightPin, INPUT); //模拟量为输入模式  pinMode(beepPin, OUTPUT); //设置为数字输出模式  pinMode(ledPin, OUTPUT);  pinMode(relayPin1, OUTPUT);  pinMode(relayPin2, OUTPUT);  pinMode(con_ledPin, OUTPUT);  digitalWrite(beepPin, HIGH); //设置为高电平  myGizwits.begin();}void wifiStatusHandle(){  if (myGizwits.wifiHasBeenSet(WIFI_CON_ROUTER))  {    digitalWrite(con_ledPin, HIGH); //设置为低电平    digitalWrite(ledPin, HIGH); //设置为高电平    digitalWrite(relayPin1, HIGH); //设置为高电平    digitalWrite(relayPin2, HIGH); //设置为高电平  }}/**  Arduino loop  @param none  @return none*/void loop() {  //Configure network  //if(XXX) //Trigger Condition  //myGizwits.setBindMode(0x02);  //0x01:Enter AP Mode;0x02:Enter Airlink Mode  bool varW_Alarm = alarmflag;//Add Sensor Data Collection  myGizwits.write(VALUE_Alarm, varW_Alarm);  unsigned long varW_fire = analogRead(firePin);//Add Sensor Data Collection  myGizwits.write(VALUE_fire, varW_fire);  unsigned long varW_Move_Test = digitalRead(movePin);//Add Sensor Data Collection  myGizwits.write(VALUE_Move_Test, varW_Move_Test);  unsigned long varW_Tem = dht_11_gettemperature();//Add Sensor Data Collection  myGizwits.write(VALUE_Tem, varW_Tem);  unsigned long varW_Hum = dht_11_gethumidity();//Add Sensor Data Collection  myGizwits.write(VALUE_Hum, varW_Hum);  unsigned long varW_Light = analogRead(lightPin);//Add Sensor Data Collection  myGizwits.write(VALUE_Light, varW_Light);  bool varR_LED_OnOff = 0;  if (myGizwits.hasBeenSet(EVENT_LED_OnOff))  {    myGizwits.read(EVENT_LED_OnOff, &varR_LED_OnOff); //Address for storing data    if (varR_LED_OnOff == 0)    {      digitalWrite(ledPin, HIGH);    }    else    {      digitalWrite(ledPin, LOW);    }  }  bool varR_Relay_1 = 0;  if (myGizwits.hasBeenSet(EVENT_Relay_1))  {    myGizwits.read(EVENT_Relay_1, &varR_Relay_1); //Address for storing data    if (varR_Relay_1 == 0)    {      digitalWrite(relayPin1, HIGH);    }    else    {      digitalWrite(relayPin1, LOW);    }  }  bool varR_Relay_2 = 0;  if (myGizwits.hasBeenSet(EVENT_Relay_2))  {    myGizwits.read(EVENT_Relay_2, &varR_Relay_2); //Address for storing data    if (varR_Relay_2 == 0)    {      digitalWrite(relayPin2, HIGH);    }    else    {      digitalWrite(relayPin2, LOW);    }  }  bool varR_beep = 0;  if (myGizwits.hasBeenSet(EVENT_beep))  {    myGizwits.read(EVENT_beep, &varR_beep); //Address for storing data    if (varR_beep == 0 )    {      digitalWrite(beepPin, HIGH);    }    else    {      digitalWrite(beepPin, LOW);    }  }  KEY_Handle();  wifiStatusHandle();  myGizwits.process();}
 
五、下载Android开发代码包

5.1 在应用开发生成APP代码包

Arduino

通过Android Studio软件导入项目进行编译,因版本问题需要修改关键词,compile 均替换为implementation,testcompile均替换为testimplementation,可参考以下代码:


apply plugin: 'com.android.application'
android {    compileSdkVersion 25    buildToolsVersion '28.0.2'    useLibrary 'org.apache.http.legacy'    defaultConfig {        applicationId "com.gizwits.opensource.appkit"        minSdkVersion 16        targetSdkVersion 25        versionCode 1        versionName "2.5.1.072715"        multiDexEnabled true    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }}
dependencies {    implementation fileTree(dir: 'libs', include: ['*.jar'])    implementation 'com.android.support.constraint1.1.0'    testImplementation 'junit4.12'    implementation 'com.android.support24.2.1'    implementation files('libs/BLEasyConfig.jar')    implementation files('libs/easylink2.0.jar')    implementation files('libs/GizWifiSDK.jar')    implementation files('libs/jpush-android-2.1.5.jar')    implementation files('libs/Lark7618SDK_noudp1606031910_0101.jar')    implementation files('libs/libammsdk.jar')    implementation files('libs/libForEElink.jar')    implementation files('libs/LSFSK_1.0.jar')    implementation files('libs/ltlink2.jar')    implementation files('libs/mta-sdk-1.6.2.jar')    implementation files('libs/OneShotConfig.jar')    implementation files('libs/open_sdk_r5756.jar')    implementation files('libs/pushservice-5.6.0.30.jar')    implementation files('libs/simpleconfigwizardlib.jar')    implementation files('libs/xUtils-2.6.14.jar')    implementation files('libs/zxing.jar')    implementation 'com.android.support25.3.0'    implementation 'com.yanzhenjie1.0.5'}

同时可参考“基于机智云AIoT平台的Android开源app修改教程”https://www.sohu.com/a/476083681_486728
 

使项目成功编译

 

审核编辑 :李倩


打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分