我是靠谱客的博主 小巧大船,最近开发中收集的这篇文章主要介绍安卓接入onenet的控制软件及调试助手@安卓接入onenet的控制软件及调试助手,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

@安卓接入onenet的控制软件及调试助手

内容:文章叙述了安卓向onenet服务器上传数据,获取数据点,通过onenet向单片机发送命令的内容

关于硬件的问题大家可以问一下我学长大佬(QQ:815794369)
软件源码地址:https://pan.baidu.com/s/1hZjlW6Vnb45Kd3x5c_KQ2Q
开发软件:Android Studio (发送的格式特别严格)
**软件照片和硬件照片 通过onenet平台去控制四个灯的亮灭:
在这里插入图片描述

在这里插入图片描述
数据格式:
大家应该都用到过小黄人的串口调试z助手,下面这个图片就是通过小黄人向单片机发送开灯的指令(当然指令是自己规定的),这个数据包的格式是固定的,并且官方文档的介绍并不详细。* 后面也会介绍上传数据点和获得数据的格式

下面这个就是利用串口调试助手让单片机上的绿灯亮:

返回的结果是error;0 success 表示成功了,当然你要想更加人性化的话官方的文档里面会介绍返回的数据是什么意思,比如设备是否在线,设备是否响应之类的东西。

**发送命令格式:

  在输入框里面输入:
  POST http://api.heclouds.com/cmds?device_id=522417343 HTTP/1.1
  api-key:HQgSUzUeFhAY=Ict2ildbRkb388=
  Host:api.heclouds.com
  Content-Length:12
  
  <"greenled:1">
  //-------你只要吧device_id, api-key写成你自己产品的就可以了
  //注意,注意!!!!!!!!!!!!!!!!!!
  POST 后面有一个空格,HTTP/1.1前面有一个空格,  Content-Length:下面有一个空行,并且空行里面不能有空格,Content-Length:的大小就是<>里面的字符数,一定要准确不然就会出错
  dff这段话的意思就是表示向单片机发送数据greenled:1

在这里插入图片描述

**获取数据格式:

      在closeContent这一行后面必须要点两次换行,
      并且Connection:close下面的空行中不能有空格

      GET /devices/522417343/datastreams/Red_Led HTTP/1.1 
      api-key:HQgSUzUeFhAY=Ict2ildbRkb388=
      Host:api.heclouds.com 
      Connection:close
       
      //--这个 Connection:close下面必须要有一个一个空行,并且空行里面不能有空格,
      不然就会出错。
        表示获取Red_Led
       获取的是onenet平台上Red_Led最新的一次数据

**上传数据格式:

 POST /devices/522417343/datapoints?type=3 HTTP/1.1 
 api-key:HQgSUzUeFhAY=Ict2ildbRkb388=
 Host:api.heclouds.com
 Content-Length:40

  {"GreenLed":22,"RedLed":33,"BlueLed":44}
  一定要注意json数据的长度是包含大括号的长度
  并且在最后一行的数据前面一定要有一个空行 并且这个
  空行是不能有空格的

**onenet上面的数据展示:
onenet上面的数据展示:如果你没有在onenet上面创建数据流模板就发送数据流模板就开始发送数据的话平台会自己帮你创建一个,但是没有创建就获取数据的话就有可能会出错。
在这里插入图片描述
**注意:
上传数据和发送命令是不一样的

**我觉得上传数据就是往平台上上传数据,但是发送命令就是给单片机发送指令

软件代码:
:只是贴出来开灯关灯页面的代码

package com.example.hp_9470m.onenet_light;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class SecondActivity extends AppCompatActivity {

private SendThread sendthread;
String receive_Msg;

private  int  main_contral = 0 , red_contral = 0 ,green_contral = 0 ,blue_contral = 0 ,yellow_contral = 0 ;
private String mip,port,id,apikey;

private  First_event first_event;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    setTitle("远程灯光控制");


    //从xml文件里面获得设备id和apikey
    SharedPreferences pref = getSharedPreferences("data" , MODE_PRIVATE);

     mip = pref.getString("ip" , "");
     port = pref.getString("port" , "");
     id = pref.getString("id" , "");
      apikey = pref.getString("apikey" , "");

  System.out.println("ip"+mip);
    System.out.println("port"+port);
    System.out.println("id"+id);
    System.out.println("apiket"+apikey);


    //把端口转化为数字
    int mport = Integer.parseInt(port);




    /***************连接*****************/
    sendthread = new SendThread(mip, mport,mHandler);
    Thread1();
    new Thread().start();
    /**********************************/

    //获取调试助手哪里发送过来的消息,替他发送出去
    EventBus.getDefault().register(this);


    //实例化按钮
    final Button main = (Button)  findViewById(R.id.Main);

    //目前还没有办法实现全部关灯 ,就是不知道怎样同时发送多条命令
    main.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         if (main_contral == 0 ){
             //sendthread.send("0");

             try {
                 //红灯开
             sendthread.send("POST http://api.heclouds.com/cmds?device_id="+id+" HTTP/1.1n" +
                     "api-key:"+apikey+"n" +
                     "Host:api.heclouds.comn" +
                     "Content-Length:10n" +
                     "n" +
                     "<"redled:1">");

                 Thread.sleep(500);   //如果发送太快服务器接受不到


                 //绿灯开
                 sendthread.send("POST http://api.heclouds.com/cmds?device_id="+id+" HTTP/1.1n" +
                         "api-key:"+apikey+"n" +
                         "Host:api.heclouds.comn" +
                         "Content-Length:12n" +
                         "n" +
                         "<"greenled:1">");

                 Thread.sleep(500);

                 //蓝灯开
                 sendthread.send("POST http://api.heclouds.com/cmds?device_id="+id+" HTTP/1.1n" +
                         "api-key:"+apikey+"n" +
                         "Host:api.heclouds.comn" +
                         "Content-Length:11n" +
                         "n" +
                         "<"blueled:1">");
                 Thread.sleep(500);


                 //黄灯开
                 sendthread.send("POST http://api.heclouds.com/cmds?device_id="+id+" HTTP/1.1n" +
                         "api-key:"+apikey+"n" +
                         "Host:api.heclouds.comn" +
                         "Content-Length:13n" +
                         "n" +
                         "<"yellowled:1">");





             } catch (InterruptedException e) {
                 e.printStackTrace();
             }


             main_contral = 1 ;
             main.setBackgroundResource(R.drawable.open);
         }else{
           //sendthread.send("1");


             try {
                 //红灯关
                 sendthread.send("POST http://api.heclouds.com/cmds?device_id="+id+" HTTP/1.1n" +
                         "api-key:"+apikey+"n" +
                         "Host:api.heclouds.comn" +
                         "Content-Length:10n" +
                         "n" +
                         "<"redled:0">");

                 Thread.sleep(500);


                 //绿灯关
                 sendthread.send("POST http://api.heclouds.com/cmds?device_id="+id+" HTTP/1.1n" +
                         "api-key:"+apikey+"n" +
                         "Host:api.heclouds.comn" +
                         "Content-Length:12n" +
                         "n" +
                         "<"greenled:0">");

                 Thread.sleep(500);

                 //蓝灯关
                 sendthread.send("POST http://api.heclouds.com/cmds?device_id="+id+" HTTP/1.1n" +
                         "api-key:"+apikey+"n" +
                         "Host:api.heclouds.comn" +
                         "Content-Length:11n" +
                         "n" +
                         "<"blueled:0">");
                 Thread.sleep(500);


                 //黄灯关
                 sendthread.send("POST http://api.heclouds.com/cmds?device_id="+id+" HTTP/1.1n" +
                         "api-key:"+apikey+"n" +
                         "Host:api.heclouds.comn" +
                         "Content-Length:13n" +
                         "n" +
                         "<"yellowled:0">");





             } catch (InterruptedException e) {
                 e.printStackTrace();
             }

             main_contral = 0 ;
             main.setBackgroundResource(R.drawable.close);
         }
        }
    });

    //四个小的开关
    final Button red = (Button) findViewById(R.id.Red);
    final Button green = (Button) findViewById(R.id.Green);
    final Button blue = (Button) findViewById(R.id.Blue);
    final Button yellow = (Button) findViewById(R.id.Yellow);


    red.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (red_contral == 0 ){
                sendthread.send("POST http://api.heclouds.com/cmds?device_id="+id+" HTTP/1.1n" +
                        "api-key:"+apikey+"n" +
                        "Host:api.heclouds.comn" +
                        "Content-Length:10n" +
                        "n" +
                        "<"redled:1">");
                red_contral = 1 ;
                red.setBackgroundResource(R.drawable.red_open);
            }else{
                sendthread.send("POST http://api.heclouds.com/cmds?device_id="+id+" HTTP/1.1n" +
                        "api-key:"+apikey+"n" +
                        "Host:api.heclouds.comn" +
                        "Content-Length:10n" +
                        "n" +
                        "<"redled:0">");
                red_contral = 0 ;
                red.setBackgroundResource(R.drawable.red_close);
            }

        }
    });

    green.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (green_contral == 0 ){
                sendthread.send("POST http://api.heclouds.com/cmds?device_id="+id+" HTTP/1.1n" +
                        "api-key:"+apikey+"n" +
                        "Host:api.heclouds.comn" +
                        "Content-Length:12n" +
                        "n" +
                        "<"greenled:1">");
                green_contral = 1 ;
                green.setBackgroundResource(R.drawable.green_open);

            }else{
                sendthread.send("POST http://api.heclouds.com/cmds?device_id="+id+" HTTP/1.1n" +
                        "api-key:"+apikey+"n" +
                        "Host:api.heclouds.comn" +
                        "Content-Length:12n" +
                        "n" +
                        "<"greenled:0">");
                green_contral = 0 ;
                green.setBackgroundResource(R.drawable.green_close);
            }

        }
    });


    blue.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (blue_contral == 0 ){
                sendthread.send("POST http://api.heclouds.com/cmds?device_id="+id+" HTTP/1.1n" +
                        "api-key:"+apikey+"n" +
                        "Host:api.heclouds.comn" +
                        "Content-Length:11n" +
                        "n" +
                        "<"blueled:1">");
                blue_contral = 1 ;
                blue.setBackgroundResource(R.drawable.blue_open);
            }else{
                sendthread.send("POST http://api.heclouds.com/cmds?device_id="+id+" HTTP/1.1n" +
                        "api-key:"+apikey+"n" +
                        "Host:api.heclouds.comn" +
                        "Content-Length:11n" +
                        "n" +
                        "<"blueled:0">");
                blue_contral = 0 ;
                blue.setBackgroundResource(R.drawable.blue_close);
            }

        }
    });

    yellow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (yellow_contral == 0 ){
                sendthread.send("POST http://api.heclouds.com/cmds?device_id="+id+" HTTP/1.1n" +
                        "api-key:"+apikey+"n" +
                        "Host:api.heclouds.comn" +
                        "Content-Length:13n" +
                        "n" +
                        "<"yellowled:1">");
                yellow_contral = 1 ;
                yellow.setBackgroundResource(R.drawable.yellow_open);
            }else{
                sendthread.send("POST http://api.heclouds.com/cmds?device_id="+id+" HTTP/1.1n" +
                        "api-key:"+apikey+"n" +
                        "Host:api.heclouds.comn" +
                        "Content-Length:13n" +
                        "n" +
                        "<"yellowled:0">");
                yellow_contral = 0 ;
                yellow.setBackgroundResource(R.drawable.yellow_close);
            }

        }
    });




}


//引入menu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main ,menu);
    return  true;
}


//menu的点击事件

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.Detial:
            Intent intent = new Intent(SecondActivity.this , MainActivity.class);
            startActivity(intent);
            break;
        case R.id.Third:
            Intent intent2 = new Intent(SecondActivity.this , ThirdActivity.class);
            startActivity(intent2);
            break;
            default:
    }
    return  true;
}


//使用eventbus让第三个活动可以发送信息  ,当然这个紧急

@Override
protected void onDestroy() {
    super.onDestroy();

    //取消注册事件
    EventBus.getDefault().unregister(this);
}

//在这里添加EventBUs的消息处理事件
@Subscribe(threadMode = ThreadMode.MAIN)    //这里如果不添加注释就会闪退
public void fasong(First_event first_event){

    sendthread.send(first_event.getMessage());

}



/*接收线程*******************************************************************************/
/**
 * 开启socket连接线程
 */   void Thread1(){
 //     sendthread = new SendThread(mIp, mPort, mHandler);
    new Thread(sendthread).start();//创建一个新线程
}

//下面这个方法在活动里面是可以使用的,但是在碎片里面怎么就用不了了  难道是因为我把他放在了子线程里面吗

//这个是在碎片里面接受数据  看看学长是怎么写de

//我日我好像知道了  这个就是说你不能在子线程里面去接收数据  说以你要在主线程里面去接收到这些信息
//然后通过busevent传送过来


//这里其实是可以用的但是  只可以接收十进制的数据,如果是以16进制传过来的数据就会崩溃
Handler mHandler = new Handler()
{
    public void handleMessage(Message msg)
    {



        super.handleMessage(msg);
        if (msg.what == 0x00) {

            Log.i("mr_收到的数据: ", msg.obj.toString());
            receive_Msg = msg.obj.toString();
            String l = receive_Msg;
            //  String s1 = l.substring(0,30 );
           // Toast.makeText(SecondActivity.this , l , Toast.LENGTH_SHORT).show();

            //使用Second_eventBUs 吧数据传送到调试助手那个活动
            EventBus.getDefault().post(new Second_event(l));

        }
    }
};


//在这个活动点击返回的时候让他返回桌面
//下面这个是  仅仅是返回桌面但是不销毁活动

//当然上面的方法也是可以使用的
public boolean onKeyDown (int keyCode , KeyEvent event) {


    if (keyCode == KeyEvent.KEYCODE_BACK) {

        Intent home = new Intent(Intent.ACTION_MAIN);
        home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        home.addCategory(Intent.CATEGORY_HOME);
        startActivity(home);

        return true;

    }
    return super.onKeyDown(keyCode, event);
}
}

最后

以上就是小巧大船为你收集整理的安卓接入onenet的控制软件及调试助手@安卓接入onenet的控制软件及调试助手的全部内容,希望文章能够帮你解决安卓接入onenet的控制软件及调试助手@安卓接入onenet的控制软件及调试助手所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部