Fun Things MQTT OneNET platform of services ⑦ - Remote Control LED (unlimited number) + Android App control optimization First Edition

Give a man a fish than giving the fishing, the purpose is not to teach you to develop specific projects, but to learn the ability to learn. I hope you share with friends or classmates around you need, maybe there is a large growth path God cornerstone Boge. . .

QQ technology group interaction: ESP8266 & 32 things develop group number 622 368 884, do not like do not spray

First, if you want to learn Arduino-based development technology ESP8266

First, the Basics

  1. ESP8266 Development Basics ① trip into the world of ESP8266
  2. ESP8266 Development Basics Tour ② how to install ESP8266 the Arduino development environment
  3. ESP8266 development trip Basics ③ ESP8266 and the Arduino development notes
  4. ESP8266 development trip Basics ④ ESP8266 and EEPROM
  5. ESP8266 development trip Basics ⑤ ESP8266 SPI and I2C communication communication
  6. ESP8266 development trip Basics ⑥ Ticker - ESP8266 regular library

Second, the network articles

  1. ESP8266 network development trip to articles ① Meet Arduino Core For ESP8266
  2. ESP8266 Development Journey articles ② ESP8266 network operation mode and ESP8266WiFi library
  3. ESP8266 network development trip to articles ③ Soft-AP - use ESP8266WiFiAP library
  4. ESP8266 network development trip to articles ④ Station - use ESP8266WiFiSTA library
  5. ESP8266 network development trip to articles ⑤ Scan WiFi - Use ESP8266WiFiScan library
  6. ESP8266 network development trip to the library foundation piece ⑥ ESP8266WiFiGeneric--
  7. ESP8266 network development trip to articles ⑦ TCP Server & TCP Client
  8. ESP8266 network development trip to articles ⑧ SmartConfig-- a key distribution network
  9. ESP8266 network development trip to articles ⑨ HttpClient - use ESP8266HTTPClient library
  10. ESP8266 Development Journey articles ⑩ UDP network services
  11. Tour of network development articles ESP8266 ⑪ WebServer - use ESP8266WebServer library
  12. ESP8266 network development trip ⑫ articles Domain Name Service --ESP8266mDNS library
  13. ESP8266 network development trip to articles ⑬ SPIFFS - ESP8266 Flash File System
  14. ESP8266 network development trip to articles ⑭ web distribution network
  15. ESP8266 network development trip to articles ⑮ true Domain Name Service --DNSServer
  16. ESP8266 development trip ⑯ wireless network articles --OTA update firmware update

Third, the application papers

  1. ESP8266 development trip application piece ① LAN applications - cool RGB lights
  2. ESP8266 development application piece Journey ② OLED display screen weather
  3. ESP8266 development application piece ③ Tour Lite car WiFi

Fourth, advanced piece

  1. ESP8266 development journey Advanced articles ① code optimization - ESP8266 Memory Management
  2. ESP8266 development journey Advanced articles ② chat Arduino IDE For ESP8266 configuration
  3. ESP8266 development journey Advanced articles ③ chat ESP8266 Flash
  4. ESP8266 development journey Advanced articles ④ common problems - to solve problems
  5. ESP8266 development journey Advanced articles ⑤ code standards - like writing articles as beautiful
  6. ESP8266 development journey Advanced articles ⑥ ESP-specific APIs Description

1 Introduction

    In the previous blog post Fun Things OneNET platform of MQTT service ④ - Remote Control LED (unlimited number) + Android App control , the blogger just generally about the structure of the entire small projects. However, if, as a product to develop, then there is still a lot of problems. Here a list of a few I think the more important problem:

  • Question 1: App as a special device, in theory, should support the self-registration feature, should not be by the developer or the user additional API call to create a device debugging tools, at least you have to simplify this process ;
  • Problems 2: the App No processing apparatus is switched from the online state to the offline state, the device needs to update the status in real time ;

Next, the bloggers will be for these two important ideas and solve problems solved step explanation, the reader is thinking side edge experiments.

2. Solve Problem 1

  • App as a special device, in theory, should support the self-registration feature, should not be by the developer or the user additional API call to create a device debugging tools, at least you have to simplify this process

    2.1 Solutions

  • Our purpose is to get DeviceID a real, since OneNet platform provides us with a new device API, so we can create the device through it and get the device ID.
    Here Insert Picture Description
  • Because the serial number of equipment used to create the device requires unique identification device, and the uniqueness of the android phone thing very much, we here consider

Android系统2.3版本以上可以通过下面的方法得到Serial Number,且非手机设备也可以通过该接口获取。

String serial= android.os.Build.SERIAL;

Here Insert Picture Description

2.2 troubleshooting steps

  • Modify the app logic, plus the registration method
/**
 * 新增OneNet设备
 */
public class RegisterOneNetDeviceEntity extends BaseResponseEntity {

    public dataModel data;

    public static class dataModel{
        public String device_id;
    }

    @Override
    protected String createArgs(Object... params) {
        OneNetDeviceModel model = (OneNetDeviceModel) params[0];
        JSONObject object = new JSONObject();
        try {
            object.put("title",model.getTitle());
            object.put("auth_info",model.getAuth_info());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return object.toString();
    }

    @Override
    protected String makeUrl() {
        return API_POST_REGISTER_DEVICE;
    }

    public String request(OneNetDeviceModel model) {
        method = HttpUtilCore.Method.Post;
        String json = requestJson(model);
        //内部消化
        return handleResponse(json, new OnResponseListener<RegisterOneNetDeviceEntity>() {

            @Override
            public void onSuccess(String json, RegisterOneNetDeviceEntity response) {
               data = response.data;
            }

            @Override
            public void onFailed(String reason) {
                Logger.d(reason);
            }

            @Override
            public void onTimeout() {
                Logger.d("请求超时");
            }
        });
    }
}
  • Users click on the registration, plus the device serial number
    @Override
    public void onInitView(Bundle savedInstanceState) {
        PreferenceUtil preference = PreferenceUtil.getInstance();
        if(!TextUtils.isEmpty(preference.getDeviceId())){
            this.onConfigConfirm();
            return;
        }

        getDefaultNavigation().setTitle("配置智能灯");
        getDefaultNavigation().getLeftButton().hide();

        etDevice.setText("Android_" + android.os.Build.SERIAL);
        etProduct.setText(preference.getProductId());
        etApikey.setText(preference.getApiKey());
    }
  • After the success of the new storage server returns device_id
@Override
    public void register(Context context, final OneNetConfigDALEx config, final ICallBack<String> callBack) {
        if(task != null && task.getStatus()== AsyncTask.Status.RUNNING){
            task.cancel(true);
        }

        task = new SimpleTask() {

            OneNetDeviceModel device;
            RegisterOneNetDeviceEntity entity;

            @Override
            protected void onPreExecute() {
                PreferenceUtil preference = PreferenceUtil.getInstance();
                preference.writePreferences(PreferenceUtil.ApiKey,config.getApikey());
                preference.writePreferences(PreferenceUtil.ProductId,config.getProductId());

                entity = new RegisterOneNetDeviceEntity();
                device = new OneNetDeviceModel();
                device.setTitle(config.getDeviceId());
                device.setAuth_info(config.getDeviceId());
            }

            @Override
            protected Object doInBackground(String... strings) {
                return entity.request(device);
            }

            @Override
            protected void onPostExecute(Object o) {
                String result = (String) o;
                PreferenceUtil preference = PreferenceUtil.getInstance();
                if("200".equals(result)){//保存设备id
                    preference.writePreferences(PreferenceUtil.DeviceId,entity.data.device_id);
                    callBack.onSuccess("");
                }else {
                    callBack.onFaild(result);
                }
            }
        };
        task.startTask();
  • This completes the registration device functions from a mobile phone

Here Insert Picture Description
This approach has the advantage that:

  • For simultaneous control of a plurality of members is a set of smart lights, and do not generate a dropped call .

However, the Note to reader:

  • If you register once, and then uninstall the app, and then re-register, this time to fail (the reason the reader self-analysis). This time to go back to manually delete Android_xxxx equipment .

  • App Download

3. Solve Problem 2

  • Problem: App is no processing equipment to switch from online to offline process that requires a real-time update the device status

3.1 Solution

Currently there are about several options:

  • Program A : mobile terminal continuously polls the device list acquisition batch interface, you can set a large time interval, the high accuracy of this scheme, but consume traffic. Currently the program is relatively reliable .
  • Plan b : esp8266 end device status topic and publish information, mobile terminal subscribing to the topic, to update the status by mqtt;
  • Program c : news mqtt will use, but this Boge verified OneNet, and feel good to use.
  • Program d : If mqtt wills relatively fast real-time messages, you can consider a combination of program b + c, and personally feel this is an ideal solution. (However, I have not achieved very good)
  • Program E : This project is not the pursuit of real-time, we can manually or automatically refresh the drop-down brush again in the activity of onshow, basically meet the requirements, to take the program.

4. Summary

  • Question 1: App as a special device, in theory, should support the self-registration feature, it should not be by the developer or the user additional API call to create a device debugging tools, at least have to simplify this process ; can be resolved.
  • Question 2: App is no processing equipment to switch from online to offline, we need to update the device status in real time ; praise not the perfect solution, the program can only reveal all the details.
  • However, we are getting closer and away from commercialization.

Guess you like

Origin www.cnblogs.com/danpianjicainiao/p/11769440.html