[Micro channel small hardware control program ⑦] Advanced articles starting the whole network, micro-channel applet ble Bluetooth control esp32, without a network may be controlled to achieve a luminance switch.

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/xh870189248/article/details/101849759

[ Micro-channel hardware control applet first one ] the whole network starting, with emq messaging server with you how to build a micro-channel mqtt server applet, easily control intelligent hardware!
[ Micro-channel hardware applet in Control of 2 ] start micro-channel applets trip, import applet Mqtt client source code, simple authentication and communication to the server!
[ Micro-channel hardware control applet first 3 ] to build a micro-channel applet in Control esp8266 items from software to hardware, custom communications protocol for job interviews and competitions plus!
[ Micro-channel hardware control applet first 4 ] depth analysis of the micro-channel public number distribution network Airkiss principle and process, esp8266 how to customize the callback parameter to the micro-channel, binding equipment to achieve the first step!
[ Micro-channel hardware control applet on 5 ] must take the next sort out the framework for thinking and learning under JavaScript observer pattern, in small micro-channel multi-page program while receiving device to push event!
[ Micro letter applet in Control hardware articles 6 ] how to integrate seven cattle cloud storage server SDK, the user-defined device in the third-party server for storing pictures!
[ Micro-channel hardware control applet first 7 ] to move to do a small micro-channel protocol control framework program Mqtt intelligent hardware for your heart full stack engineers dream watering! !
[ Micro program control hardware small letter of 8 ] Micro letter applet to connect websocket Ali cloud IOT IOT platform mqtt server, encapsulate use is that simple!
[ Micro-channel hardware control applet first 9 ] on the pretext of Ali cloud platform free connection of things, from micro-channel acquisition and control applet color output esp8266 colorful lights effect, a Mid-Autumn Festival to live how? !
[ Micro-channel public number distribution network hardware to control the first 10 ] how to connect mqtt server tutorial in the public micro-channel number on the page! !
[ Micro channel small hardware control program of the 11 ] starting the whole network, micro-channel applet ble Bluetooth control esp32, without a network may be controlled to achieve a luminance switch.



Here Insert Picture Description

I. Introduction;


         Today is National Day, the 70th anniversary of the founding of our great motherland, brush over the circle of friends greetings, today at noon in the dou tone seen a lot of Tiananmen Square parade video, to see the blood boil, tears, this life without regret into China afterlife also do Chinese people ! ! !

         The day before yesterday, Lexin of a serving technicians added a QQ group, I am very pleased, currently Lexin is a lot of hiring, whether it is born, just good enough, you can entry, we see this blog, you can whisper to me, within go forward! I certainly can not go Lexin quit, because and the company has a contract, and Shanghai seems to me really far away ( Cantonese think outside Guangdong are north, ha ha ), and other music Xin in the "Silicon Valley of China" Lexin the creation of work, I consider to try next.

         Having said that, beginning come to the question of domestic current for esp32the start of research esp32 learning Bluetooth, and indeed, the domestic things continue to flourish, but willingly technology the video or is it specializes Preface technology and to share out rarely; I Tencent classroom, college geeks, CSDN College, School of electronic enthusiasts and Mu class network and other teaching sites, there are few things the development of combat video, basically speaking driven development, use of peripherals, we hear most is to stm32 board of punctuality atom such outstanding enterprises, indeed, they are specialized in the teaching of the board. But the combination of things remote control, is less tutorial.

         For domestic energy out some tutorials, I was heart feel uncomfortable. Because I am out of Bowen, they are basic with a project to share out, though some are paid, but did write Bowen is not easy, typography, own ps drawing, a minimum of one hour of it; most troublesome is finishing the code package, but this is my very comfortable of good programmers first step is to code specifications so that they look comfortable, so that others understand it;


Second, the project description;


2.1 Technical Requirements

  • esp32 a development board, I used here is Lexin board:

Corresponding schematics: https: //dl.espressif.com/dl/schematics/esp32-lyrat-v4-schematic.pdf

  • a rgb light board, I'm my own painting, Jia Li Chong hit the board in five shipping ha ha!
  • Small micro-channel application development tools, familiar with the small micro-channel program development;
  • Ble familiar with Bluetooth communication flow principle;

2.2 Connection board

  • To share my painting RGB5050 board schematics, 12v power supply, five mos tube, pwm driver interface to bear:
    Here Insert Picture Description
  • The wiring diagram board, only the esp32 gpio5, gpio25and gpio26rgb pwm terminal as an output, then the schematic above;

Banner

2.3 communication protocol process


2.3.1 micro-channel to send data to the format of the protocol esp32

         As gatt esp32 server, so that the micro-channel applet active connection, the list of services after the connection is successfully read, the write data value to which a certain feature services inside;

Field meaning
byte[0] red value Red Brightness
byte[1] green values Green Brightness
byte[2] blue value Blue Brightness

2.3.2 esp32 transmission data format to the micro-channel protocol

         And as the table above;


Three, esp32 codes;


         For the process ble Bluetooth communication, you must do your homework, that a lot of online tutorials.

         esp32 ble ble study's start with the beginning, alone to see his 《gatt_server》code up and go down to send data to a channel, you can see the log print, just take it on, on this step to learn;

         Amend Bluetooth name, the file has a macro definition, modify manufacturerbroadcast data, modify content structure on it;


3.1 esp32 parsing applet micro-channel data sent by the Bluetooth

         Among other things, I say that the main ESP_GATTS_WRITE_EVTevent callbacks:

//微信端写入数据回调
case ESP_GATTS_WRITE_EVT:
    
 if (!param->write.is_prep)
    {
        ESP_LOGI(GATTS_TAG, "GATT_WRITE_EVT, value len %d, value :", param->write.len);
        esp_log_buffer_hex(GATTS_TAG, param->write.value, param->write.len);
        ESP_LOGE(GATTS_TAG, "param->write.value[0] %d", param->write.value[0]);//就是我们的 red
        ESP_LOGE(GATTS_TAG, "param->write.value[1] %d", param->write.value[1]);//就是我们的 green
        ESP_LOGE(GATTS_TAG, "param->write.value[2] %d", param->write.value[2]);//就是我们的 blue
        rgb_set_pwm(param->write.value[0],param->write.value[1],param->write.value[2]);
    }
 example_write_event_env(gatts_if, &a_prepare_write_env, param);
break;

3.2 esp32 sends data to the micro-channel applet

         Device sends data to the end of the micro channel, taking the characteristics of the notified characteristics. Like this, and it shows that the hexadecimal array of data in the past;

uint8_t data[3] = {pwmRGB[0], pwmRGB[1], pwmRGB[2]};
//主动通知客户端
esp_ble_gatts_send_indicate(gatts_if, param->read.conn_id, param->read.handle, 3, data, false); 

Fourth, the micro-letter code for the applet ends;


         微信小程序早已经支持 ble 发送和接收了,我上个月在公司做了2个微信小程序ble项目,对这个在微信小程序或支付宝小程序使用ble控制蓝牙设备非常熟悉,所以,我用了一天时间,封装了微信小程序的蓝牙连接到控制,以及异常监控等操作成为一个库,我命名为 XBle,后面我会详细出博文介绍我封装的这个蓝牙库,大家先用着先哈!

         【第一步】搜索附近的蓝牙设备:


  //初始化微信蓝牙,后面我会做支付宝小程序适配
  wxBleImplement.initXBLE(); 
  
  //监听全部蓝牙事件,第一个是增加监听回调函数,如果是 false则表示移除这个回调函数
  xBle.listenDeviceMsgEvent(true, this.funListenDeviceMsgEvent); 

         【第二步】 在 回调函数的形参中,有 (type, data, deviceId, serviceId),其中 type是最为重要的,其他是数据相关;

         下面罗列全部 type 事件的含义,是不是很详细:

let TYPE_XBLE = {
    FAIL_INIT: 0, //初始化ble失败
    FAIL_DISCOVERY: 1, //搜索设备失败
    FAIL_CONNECTTING: 2, //连接设备失败
    FAIL_DIS_CONNECTTED: 3, //中途断开设备
    FAIL_READ_SERVICEID: 4, //读取设备失败serviceId
    FAIL_READ_CHARAID: 5, //读取设备特征值失败
    FAIL_NOTIFY_CHARAID: 6, //通知连接设备特征值改变失败
    FAIL_SEND_DATA_CHARAID: 7, //发送数据到连接设备失败
    FAIL_LISTENER_DATA_CHARAID: 8, //监听指定特征值数据失败
    FAIL_STOP_DISCOVERY: 9, //停止搜索失败
    FAIL_DEVICE_USER_DISCONNECT: 10, //用户断开指定设备成功

    OK_DISCOVERY_START: 20, //搜索设备成功开始
    OK_DISCOVERY_RESULT: 21, //搜索设备成功回调
    OK_DISCOVERY_STOP: 22, //搜索设备成功结束
    OK_DEVICE_CONNECTED: 23, //设备成功连接
    OK_DEVICE_USER_DISCONNECT: 24, //用户断开指定设备成功
    OK_READ_SERVICEID: 25, //读取设备成功serviceId
    OK_READ_CHARAID: 26, //读取设备成功CHARAID

    OK_DISCOVERY_OVER: 50, //搜索设备成功回调
    OK_DEVICE_NEW_DATA: 51, //设备成功返回数据
    OK_SEND_DATA_CHARAID: 52, //发送数据到连接设备失败
};

         【第三步】连接设备调用,true 表示连接,false表示断开,入参设备发现列表中的设备 deviceId

 xBle.notifyConnectEvent(true, deviceId)

         【第四步】获取设备的服务列表:

    //获取服务列表
    xBle.notifyReadServiceIdEvent(options.deviceId);

         【第五步】 获取到的服务列表后,又从该服务中获取特征值:

 //监听这个特征(表示主要esp32往这个通道发消息,就会被监听到)
xBle.notifyTheDeviceCharacteristicEvent(deviceId, serviceId,characteristicsId);

 //读取这个特征值,同步状态
 xBle.notifyReadDeviceCharacteristicEvent(deviceId,serviceId, characteristicsId)

         【第六步】 拿到特征值,就可以通讯了啦!比如发送 0x01 0x01 0x01 , 注意入参都是从上面获取的参数,缺一不可!剩余的发送点击事件什么的,请仔细看我的代码即可!

    var ab = new ArrayBuffer(1)
    var u8array = new Uint8Array(ab);
    u8array[0] = 0x01;
    u8array[1] = 0x01;
    u8array[2] = 0x01;
    xBle.notifyWriteDeviceEvent(_this.data.deviceId, serviceId, characteristicsId, ab);
  

         【第七步】 设备端发送的数据可以通过这个回调获取,别忘了ArrayBuffer转16进制字符串;

        let rgb = this.ab2hex(data.value);
        console.log('数据回调data', rgb )
        //同步ui
        this.setData({ 
          lightValueBlue: Number('0x' + rgb[2]),
          lightValueGreen: Number('0x' + rgb[1]),
          lightValueRed: Number('0x' + rgb[0]),
        })

五、其他;


  • 先上个项目截图,因为这里没法传演示视频,演示视频在微信公众号可以看到;

Here Insert Picture Description

  • Bowen source: micro-channel end-side code + esp32 get paid, I've placed a salted it! Just to sincerely deal!
    Here Insert Picture Description

Also, do not put my blog as learning standards, I just note that there was little negligently, if so, please point it out, but also welcome message Ha!

  • Fun esp8266with you fly, plus group paid QQgroup, friends do not like do not spray Do not add: 434 878 850
  • esp8266 source code learning Summary (continuously updated, welcome star): https: //github.com/xuhongv/StudyInEsp8266
  • esp32 source code learning Summary (continuously updated, welcome star): https: //github.com/xuhongv/StudyInEsp32
  • Watch the following micro-channel public number two-dimensional code, a lot of dry goods, the first time to push!

Guess you like

Origin blog.csdn.net/xh870189248/article/details/101849759