快来瞧瞧,Chocho又放大招啦~~~~~~
没错,你没看错,这就是基于废旧的电插板改装的,采用ESP32的BLE通信,控制Beetle BLE主板的智能插板,让废旧的电插板秒变智能家居。一直以来,都说ESP32支持WiFi和蓝牙双重通信方式,但始终没有提供Arduino的库文件。作为Arduino小粉的我,着实捏了把汗。这个ESP32设备可以同时控制多个从机设备,我做了两个,亲测没问题。通过RGB灯显示插板是否可以给外面供电,绿色为使能,红色为不使能导通。
我在想,是不是第一个使用ESP32-BLE来做东西的了???
所需硬件 按键3个 3D外壳
3D文件和相关的代码文件,点击链接下载,DFRobot_ESP32_WiFiBLE库文件点击下载。
制作过程 1、拆卸电源适配器和废旧电插板 在拆卸电源适配器时,需要用镊子或者尖嘴钳撬开,我用的电源适配器没有螺丝,是直接用胶水固定的,拆的时候废了牛劲。
拆废旧电插板的步骤大家还是参考视频吧,各个厂家的电插板不一样。
2、电源连接线的制作 需要找一段家用电线,随便什么型号的都行,然后拆掉外面的漆包线,长度大概在10cm左右。
3、焊接交流电源电线 将电线的一端焊接到交流电输入处,注意焊锡不要太多,否则会漏到下面的插口连接处。
焊接完成后,用斜口钳取下USB供电接口。
然后焊接5V电源线,这是给Beetle主板和继电器供电的,并且用热熔胶固定。
4、主板元器件的连接 电路原理图如下,其中D2控制继电器,D3控制RGB灯:
中间需要用热熔胶固定继电器、Beetle BLE主板、电插板,细节部分可以参考视频。
源码下载连接请往上看,Beetle BLE程序源码如下:
- #include <PlainProtocol.h>
- #include <Adafruit_NeoPixel.h>
-
- #define RELAY_PIN 3
- #define RGB_PIN 4
-
- enum{
- STOP,
- RUN,
- DEFAULTSTOP
- };
-
- enum{
- DEFAULTCOLOR,
- RED,
- GREEN,
- BLUE
- };
-
- PlainProtocol mySerial(Serial,115200);
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(1, RGB_PIN, NEO_GRB + NEO_KHZ800);
-
- boolean rgbBlink = false;
- char currentState = DEFAULTSTOP;
- unsigned long current_millis = 0;
-
- /*
- * 1->Red 2->Green 3->Blue default->close
- */
- void setRGBColor(int value){
- switch(value){
- case 1:
- strip.setPixelColor(0, strip.Color(255, 0, 0));
- break;
- case 2:
- strip.setPixelColor(0, strip.Color(0, 255, 0));
- break;
- case 3:
- strip.setPixelColor(0, strip.Color(0, 0, 255));
- break;
- default:
- strip.setPixelColor(0, strip.Color(0, 0, 0));
- break;
- }
- strip.show();
- }
- void setRelayState(char value){
- if(value == RUN){
- currentState = RUN;
- digitalWrite(RELAY_PIN,HIGH);
- setRGBColor(GREEN);
- }else{
- currentState = STOP;
- digitalWrite(RELAY_PIN,LOW);
- setRGBColor(RED);
- }
- }
-
- void updateState(void){
- mySerial.write("STATE",currentState);
- }
-
- void blinkLoop(void){
- static int iRGB = 0;
- static int times = 0;
- if(rgbBlink){
- if(iRGB){
- setRGBColor(DEFAULTCOLOR);
- }else{
- if(currentState==DEFAULTSTOP){
- setRGBColor(BLUE);
- }else if(currentState==RUN){
- setRGBColor(GREEN);
- }else if(currentState==STOP){
- setRGBColor(RED);
- }
- if(times > 50){
- rgbBlink = false;
- }
- }
- times++;
- }else{
- setRelayState(currentState);
- times = 0;
- }
- iRGB=~iRGB;
- }
-
- void timerRun(void){
- if((millis()- current_millis)>200){
- current_millis = millis();
- blinkLoop();
- updateState();
- }
- }
-
- void setup() {
- Serial.begin(115200);
- current_millis = millis();
- strip.begin();
- strip.show();
- pinMode(RELAY_PIN, OUTPUT);
- digitalWrite(RELAY_PIN,LOW);
- setRGBColor(BLUE);
- }
-
- void loop() {
- if(mySerial.available()){
- if(mySerial.equals("RELAYSTATE")){
- int value = mySerial.read();
- setRelayState(value);
- }else if(mySerial.equals("SCAN")){
- int value = mySerial.read();
- if(value==1)
- rgbBlink = true;
- else
- rgbBlink = false;
- }
- }
- timerRun();
- }
复制代码
FireBeetle Board-ESP32主控板代码如下: - <div align="center"><div align="left">#include "DFRobot_ESP32_BLE.h"
- DFRobot_ESP32_BLE ble;
- char dataName[15] = {'\0'};
-
- boolean relay1 = false;
- boolean relay2 = false;
- boolean scan = false;
-
- void scanKeys(void){
- if(digitalRead(D2) == LOW){
- delay(5);
- while(digitalRead(D2) == LOW);
- String sendStr = "<RELAYSTATE>";
- if(relay1 == false){
- sendStr = sendStr+"1;";
- relay1 = true;
- }else{
- sendStr = sendStr+"0;";
- relay1 = false;
- }
- ble.writedata("Chocho1", sendStr);
- }
-
- if(digitalRead(D5) == LOW){
- delay(5);
- while(digitalRead(D5) == LOW);
- String sendStr = "<RELAYSTATE>";
- if(relay2 == false){
- sendStr = sendStr+"1;";
- relay2 = true;
- }else{
- sendStr = sendStr+"0;";
- relay2 = false;
- }
- ble.writedata("Chocho2", sendStr);
- }
-
- if(digitalRead(D9) == LOW){
- delay(5);
- while(digitalRead(D9) == LOW);
- String sendStr = "<SCAN>";
- if(scan == false){
- sendStr = sendStr+"1;";
- scan = true;
- }else{
- sendStr = sendStr+"0;";
- scan = false;
- }
- ble.writedata("Chocho1", sendStr);
- delay(10);
- ble.writedata("Chocho2", sendStr);
- }
- delay(20);
- }
-
- void setup() {
- //Serial.begin(115200);
- pinMode(D2,INPUT);
- pinMode(D5,INPUT);
- pinMode(D9,INPUT);
- ble.setService(0xdfb0);
- ble.setCharacteristic(0xdfb1);
- ble.setconnummax(2);
- ble.setconnectname0("Chocho1");
- ble.setconnectname1("Chocho2");
- ble.init();
- delay(100);
- ble.begin();
- }
-
- void loop() {
- String bledata = ble.readdata(dataName);
- if (bledata.length() > 0) {
- if (strncmp(dataName, "Chocho1", strlen("Chocho1")) == 0) {
- //Serial.print("Chocho1:");
- //Serial.println(bledata);
- } else if (strncmp(dataName, "Chocho2", strlen("Chocho2")) == 0) {
- //Serial.print("Chocho2:");
- //Serial.println(bledata);
- }
- }
- scanKeys();
- }
- </div></div>
复制代码
ps:怎么下载程序,应该不用我说了吧~~~~~~~~~
6、整机组装
用热熔胶固定插头,然后装进外壳
然后,就没有然后了~~~~~~~~~
ESP32技术交流群: 619558168
欢迎来踩个人博客:www.seemaker.cn
|
|
|
|
|
|