Skip to content

Commit 9128938

Browse files
Add example for Wireless Bridge
1 parent 9d9268f commit 9128938

File tree

2 files changed

+282
-0
lines changed

2 files changed

+282
-0
lines changed
Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
/*
2+
* HelTec Automation(TM) Wireless Bridge dedicated test code, witch includ
3+
* follow functions:
4+
*
5+
* - Transfer WiFi and BLE message to each other;
6+
*
7+
* Because WiFi and BLE need working at the same time, this example can only
8+
* run with Heltec Wireless Bridge:
9+
* https://heltec.org/project/wireless-bridge/
10+
*
11+
* Detail description about this demo code, please refer to this document:
12+
*
13+
*
14+
* by Aaron.Lee from HelTec AutoMation, ChengDu, China
15+
* 成都惠利特自动化科技有限公司
16+
* www.heltec.cn
17+
*
18+
* this project also realess in GitHub:
19+
* https://github.com/HelTecAutomation/Heltec_ESP32
20+
*/
21+
22+
#include <BLEDevice.h>
23+
#include <BLEServer.h>
24+
#include <BLEUtils.h>
25+
#include <BLE2902.h>
26+
#include <WiFi.h>
27+
#include <WiFiClient.h>
28+
#include <WebServer.h>
29+
#include <ESPmDNS.h>
30+
#include <Update.h>
31+
32+
BLEServer *pServer = NULL;
33+
BLECharacteristic * pTxCharacteristic;
34+
bool deviceConnected = false;
35+
bool oldDeviceConnected = false;
36+
37+
const char* ssid = "Your_WiFi_Name";
38+
const char* password = "Your_WiFi_Password";
39+
String First_data = "ABCD";
40+
41+
42+
// See the following for generating UUIDs:
43+
// https://www.uuidgenerator.net/
44+
45+
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
46+
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
47+
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
48+
49+
50+
class MyServerCallbacks: public BLEServerCallbacks {
51+
void onConnect(BLEServer* pServer) {
52+
deviceConnected = true;
53+
};
54+
55+
void onDisconnect(BLEServer* pServer) {
56+
deviceConnected = false;
57+
}
58+
};
59+
60+
61+
String BLE_data;
62+
uint16_t num=0;
63+
bool BLEDownLink = false;
64+
uint32_t BLEdonwlinkTime;
65+
class MyCallbacks: public BLECharacteristicCallbacks {
66+
void onWrite(BLECharacteristic *pCharacteristic) {
67+
BLE_data = "";
68+
std::string rxValue = pCharacteristic->getValue();
69+
if (rxValue.length() > 0) {
70+
Serial.println("Received Value: ");
71+
for (int i = 0; i < rxValue.length(); i++)
72+
{
73+
Serial.print(rxValue[i]);
74+
BLE_data = BLE_data + (String)rxValue[i];
75+
}
76+
BLEDownLink = true;
77+
BLEdonwlinkTime = millis();
78+
num++;
79+
Serial.println();
80+
Serial.print(num);
81+
Serial.print(":");
82+
Serial.println(BLE_data);
83+
}
84+
}
85+
};
86+
87+
WebServer server(80);
88+
String htmlS ="<html>\
89+
<head>\
90+
<title>Wireless_Bridge</title>\
91+
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\
92+
</head>\
93+
<style type=\"text/css\">\
94+
.header{display: block;margin-top:10px;text-align:center;width:100%;font-size:10px}\
95+
.input{display: block;text-align:center;width:100%}\
96+
.input input{height: 20px;width: 300px;text-align:center;border-radius:15px;}\
97+
.input select{height: 26px;width: 305px;text-align:center;border-radius:15px;}\
98+
.btn,button{width: 305px;height: 40px;border-radius:20px; background-color: #000000; border:0px; color:#ffffff; margin-top:20px;}\
99+
</style>\
100+
<script type=\"text/javascript\">\
101+
function myrefresh()\
102+
{\
103+
window.location.reload();\
104+
}\
105+
window.onload=function(){\
106+
setTimeout('myrefresh()',10000);\
107+
} \
108+
</script>\
109+
<body>\
110+
<div style=\"width:100%;text-align:center;font-size:25px;font-weight:bold;margin-bottom:20px\">Wireless_Bridge</div>\
111+
<div style=\"width:100%;text-align:center;\">\
112+
<div class=\"header\"><span>(Note 1: The default refresh time of this page is 10s. If you need to modify the refresh time, you can modify it in the 'setTimeout' function.)</span></div>\
113+
<div class=\"header\"><span>(Note 2: The refresh time needs to be modified according to the data sending frequency.)</span></div>\
114+
<div class=\"header\"><span>Data: ";
115+
String htmlF = "</span></div>\
116+
</form>\
117+
</div>\
118+
</body>\
119+
</html>";
120+
121+
String htmlW = "<html>\
122+
<head>\
123+
<title>Wireless_Bridge</title>\
124+
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\
125+
</head>\
126+
<style type=\"text/css\">\
127+
.header{display: block;margin-top:10px;text-align:center;width:100%;font-size:10px}\
128+
.input{display: block;text-align:center;width:100%}\
129+
.input input{height: 20px;width: 300px;text-align:center;border-radius:15px;}\
130+
.input select{height: 26px;width: 305px;text-align:center;border-radius:15px;}\
131+
.btn,button{width: 305px;height: 40px;border-radius:20px; background-color: #000000; border:0px; color:#ffffff; margin-top:20px;}\
132+
</style>\
133+
<script type=\"text/javascript\">\
134+
window.onload=function(){\
135+
document.getElementsByName(\"server\")[0].value = \"\";\
136+
} \
137+
</script>\
138+
<body>\
139+
<div style=\"width:100%;text-align:center;font-size:25px;font-weight:bold;margin-bottom:20px\">Wireless_Bridge</div>\
140+
<div style=\"width:100%;text-align:center;\">\
141+
<div class=\"header\"><span>(Note : The data received by Bluetooth needs to be viewed with a Bluetooth debugging tool.)</span></div>\
142+
<form method=\"POST\" action=\"\" onsubmit=\"\">\
143+
<div class=\"header\"><span>DATA</span></div>\
144+
<div class=\"input\"><input type=\"text\" name=\"server\" value=\"\"></div>\
145+
<div class=\"header\"><input class=\"btn\" type=\"submit\" name=\"submit\" value=\"Submit\"></div>\
146+
</form>\
147+
</div>\
148+
</body>\
149+
</html>";
150+
151+
152+
String Page_data="";
153+
String symbol=":";
154+
void ROOT_HTML()
155+
{
156+
157+
Page_data =Page_data+ (String)num+ symbol + BLE_data +"<br>";
158+
String html = htmlS + Page_data + htmlF;
159+
server.send(200,"text/html",html);
160+
}
161+
162+
bool WiFiDownLink = false;
163+
uint32_t WiFidonwlinkTime;
164+
String Write_data;
165+
void ROOT_HTMLW()
166+
{
167+
if(server.hasArg("server"))
168+
{
169+
Serial.println(server.arg("server"));
170+
Write_data=server.arg("server");
171+
WiFiDownLink = true;
172+
WiFidonwlinkTime = millis();
173+
pTxCharacteristic->setValue((uint8_t *)Write_data.c_str(), strlen(Write_data.c_str()));
174+
pTxCharacteristic->notify();
175+
}
176+
server.send(200,"text/html",htmlW);
177+
}
178+
179+
180+
void setup() {
181+
Serial.begin(115200);
182+
pinMode(WIFI_LED, OUTPUT);
183+
pinMode(BLE_LED, OUTPUT);
184+
digitalWrite(WIFI_LED, HIGH);
185+
digitalWrite(BLE_LED, HIGH);
186+
delay(1000);
187+
digitalWrite(WIFI_LED, LOW);
188+
digitalWrite(BLE_LED, LOW);
189+
Serial.println();
190+
Serial.println("Booting Sketch...");
191+
WiFi.mode(WIFI_STA);
192+
WiFi.begin(ssid, password);
193+
if (WiFi.waitForConnectResult() == WL_CONNECTED) {
194+
server.on("/", ROOT_HTML);
195+
server.on("/w", ROOT_HTMLW);
196+
server.begin();
197+
MDNS.addService("http", "tcp", 80);
198+
Serial.println("");
199+
Serial.println("WiFi connected.");
200+
Serial.println("View page IP address: ");
201+
Serial.println(WiFi.localIP());
202+
Serial.println("Write page IP address: ");
203+
Serial.print(WiFi.localIP());
204+
Serial.println("/w");
205+
} else {
206+
Serial.println("WiFi Failed");
207+
}
208+
// Create the BLE Device
209+
BLEDevice::init("UART Service");
210+
211+
// Create the BLE Server
212+
pServer = BLEDevice::createServer();
213+
pServer->setCallbacks(new MyServerCallbacks());
214+
215+
// Create the BLE Service
216+
BLEService *pService = pServer->createService(SERVICE_UUID);
217+
218+
// Create a BLE Characteristic
219+
pTxCharacteristic = pService->createCharacteristic(
220+
CHARACTERISTIC_UUID_TX,
221+
BLECharacteristic::PROPERTY_NOTIFY
222+
);
223+
224+
pTxCharacteristic->addDescriptor(new BLE2902());
225+
226+
BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
227+
CHARACTERISTIC_UUID_RX,
228+
BLECharacteristic::PROPERTY_WRITE
229+
);
230+
231+
pRxCharacteristic->setCallbacks(new MyCallbacks());
232+
233+
// Start the service
234+
pService->start();
235+
236+
// Start advertising
237+
pServer->getAdvertising()->start();
238+
Serial.println("Waiting a client connection to notify...");
239+
}
240+
241+
bool State = false;
242+
void loop() {
243+
server.handleClient();
244+
if (deviceConnected && State == false) {
245+
State = true;
246+
delay(1000);
247+
pTxCharacteristic->setValue((uint8_t *)First_data.c_str(), strlen(First_data.c_str()));
248+
pTxCharacteristic->notify();
249+
}
250+
251+
// disconnecting
252+
if (!deviceConnected && oldDeviceConnected) {
253+
State = false;
254+
delay(500); // give the bluetooth stack the chance to get things ready
255+
pServer->startAdvertising(); // restart advertising
256+
Serial.println("start advertising");
257+
oldDeviceConnected = deviceConnected;
258+
}
259+
// connecting
260+
if (deviceConnected && !oldDeviceConnected) {
261+
// do stuff here on connecting
262+
oldDeviceConnected = deviceConnected;
263+
}
264+
if(BLEDownLink)
265+
{
266+
BLEDownLink = false;
267+
digitalWrite(BLE_LED,HIGH);
268+
}
269+
else if(digitalRead(BLE_LED) && (millis()-BLEdonwlinkTime)> 1000)
270+
{
271+
digitalWrite(BLE_LED,LOW);
272+
}
273+
if(WiFiDownLink)
274+
{
275+
WiFiDownLink = false;
276+
digitalWrite(WIFI_LED,HIGH);
277+
}
278+
else if(digitalRead(WIFI_LED) && (millis()-WiFidonwlinkTime)> 1000)
279+
{
280+
digitalWrite(WIFI_LED,LOW);
281+
}
282+
}

0 commit comments

Comments
 (0)