tcp2ble.ino
3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
A Freematics ESPRIT demo sketch
This sketch demonstrates simplest TCP-to-BLE transparent communication
Instructions:
- Update WiFi SSID and password as necessary
- Import ESPRIT and TinyGPS library (once)
- Compile and flash the sketch to the ESPRIT board
Developed by Stanley Huang https://www.facebook.com/stanleyhuangyc
Distributed under BSD license
Visit http://freematics.com/products/freematics-esprit for product information
*/
#include <Arduino.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiClient.h>
#include <ESPRIT.h>
#define WIFI_SSID "SSID"
#define WIFI_PASSWORD "PASSWORD"
WiFiServer server(8000);
class MyGATTServer : public GATTServer
{
public:
size_t onRequest(uint8_t* buffer, size_t len)
{
return sprintf((char*)buffer, "%u", millis());
}
void onReceive(uint8_t* buffer, size_t len)
{
for (size_t i = 0; i < len; i++) {
char c = buffer[i];
Serial.print(c);
if (data.length() < 24) data += c;
}
}
String data;
};
uint32_t lastTick = 0;
MyGATTServer ble;
void setup(void)
{
Serial.begin(115200);
// BLE
if (ble.init("FREEMATICS")) {
Serial.println("BLE initialized");
}
else {
Serial.println("BLE failed");
}
// Connect to WiFi network
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(200);
Serial.print(".");
}
Serial.println();
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Set up mDNS responder
if (!MDNS.begin("esprit")) {
Serial.println("Error setting up MDNS responder!");
while(1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
// Start TCP (HTTP) server
server.begin();
Serial.println("TCP server started");
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
}
void processSerial()
{
if (Serial.available()) {
static uint8_t buffer[128];
static uint8_t len = 0;
char c = Serial.read();
buffer[len++] = c;
if (len >= sizeof(buffer) - 1 || millis() - lastTick > 100 || c == '\r' || c == '\n') {
// send BLE data
ble.send(buffer, len);
len = 0;
lastTick = millis();
}
}
}
void loop(void)
{
processSerial();
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("");
Serial.println("New client");
// Wait for data from client to become available
while(client.connected()){
if (ble.data.length() > 0) {
client.write(ble.data.c_str(), ble.data.length());
ble.data = "";
}
if (client.available()) {
String req = client.readStringUntil('\r');
ble.send((uint8_t*)req.c_str(), req.length());
}
processSerial();
}
client.stop();
Serial.println("Done with client");
}