基于esp8266和Android studio的智能灯控制及APP开发(四)

本篇使用Android studio开发APP,并与阿里云建立连接,实现APP控制智能灯开关。

一、主界面设置

采用线性布局,设置两个按钮用来进行开关操作,效果如下。

二、连接阿里云

因为控制灯具的开关需要的业务类型只有云到设备消息,所以使用SetDeviceProperty即可。

package com.example.setdeviceproperty_demo;


import com.aliyun.iot20180120.Client;
import com.aliyun.iot20180120.models.SetDevicePropertyRequest;
import com.aliyun.iot20180120.models.SetDevicePropertyResponse;

import com.aliyun.tea.TeaModel;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.Common;


public class SetDevicePropertyUtil {
    // Access Keys
    public static final String accessKeyId = "XXXXX";
    public static final String accessKeySecret = "XXXX";

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "iot.cn-shanghai.aliyuncs.com";
        return new Client(config);
    }

    // 发起调用
    public static String setDevicePropertyOpen() throws Exception {
        Client client = SetDevicePropertyUtil.createClient(accessKeyId, accessKeySecret);
        SetDevicePropertyRequest setDevicePropertyRequest = new SetDevicePropertyRequest()
                .setIotInstanceId("iot-xxxx")
                .setProductKey("xxxx")
                .setDeviceName("xxxx")
                .setItems("{\"OF\":1}");
        SetDevicePropertyResponse resp = client.setDeviceProperty(setDevicePropertyRequest);
        return Common.toJSONString(TeaModel.buildMap(resp));
    }
    // 发起调用
    public static String setDevicePropertyClose() throws Exception {
        Client client = SetDevicePropertyUtil.createClient(accessKeyId, accessKeySecret);
        SetDevicePropertyRequest setDevicePropertyRequest = new SetDevicePropertyRequest()
                .setIotInstanceId("iot-xxxx")
                .setProductKey("xxxx")
                .setDeviceName("xxxx")
                .setItems("{\"OF\":0}");
        SetDevicePropertyResponse resp = client.setDeviceProperty(setDevicePropertyRequest);
        return Common.toJSONString(TeaModel.buildMap(resp));
    }
}

三、主函数

主函数定义两个按钮,与布局文件里的button绑定,并设置按下OPEN按钮后,执行“开灯”;按下CLOSE按钮后,执行“关灯”。

package com.example.setdeviceproperty_demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button mButtonOpen;//花生,做两个按钮,一开一关
    private Button mButtonClose;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //接下来绑定按钮变量和按钮组件
       mButtonOpen = (Button)findViewById(R.id.button1);

        mButtonOpen.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                new Thread(new Runnable() { // 匿名类的Runnable接口
                    @Override
                    public void run() {
                        try {
                            String resultJsonStr = SetDevicePropertyUtil.setDevicePropertyOpen();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });

        mButtonClose = (Button)findViewById(R.id.button2);

        mButtonClose.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                new Thread(new Runnable() { // 匿名类的Runnable接口
                    @Override
                    public void run() {
                        try {
                            String resultJsonStr = SetDevicePropertyUtil.setDevicePropertyClose();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });

    }
}

四、测试 

编写完成后,将APP下载到手机上。按下OPEN,小灯点亮;按下CLOSE,小灯熄灭。

至此,成功实现项目目标。

  • 11
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
基于ESP8266模块的智能可以实现多种功能,下面是一些常见的功能和实现方法: 1. 远程控制:通过在手机或电脑上安装相应的APP或浏览器插件,可以实现远程控制的开关、调节亮度和颜色等功能。具体实现方法是在ESP8266模块上运行一个Web服务器程序,通过WiFi连接到家庭网络,然后在APP或浏览器中访问该服务器,发送控制命令,最终控制的开关、亮度和颜色等参数。 2. 语音控制:通过连接语音识别模块(如百度语音识别模块)和ESP8266模块,可以实现通过语音控制的开关、亮度和颜色等参数。具体实现方法是在ESP8266模块上运行语音识别程序,将语音指令转换为控制命令,然后发送给台控制器,最终实现控制的开关、亮度和颜色等参数。 3. 定时开关:通过在ESP8266模块上设置定时器,可以实现定时开关台的功能。具体实现方法是在ESP8266模块上运行一个定时器程序,设置开关时间和周期,然后控制的开关。 4. 传感器控制:通过连接传感器(如光敏电阻、温度传感器等)和ESP8266模块,可以实现自动调节台的亮度和颜色等参数。具体实现方法是在ESP8266模块上运行一个传感器读取程序,读取传感器的数值,然后根据数值控制的亮度和颜色等参数。 需要注意的是,ESP8266模块的GPIO引脚数量有限,如果需要连接多个外设,需要仔细规划和设计引脚的分配和使用。同时,需要编写相应的程序和控制逻辑,确保各个功能能够正常运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值