-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttgo-twatch-ublox-gps-wifi.ino
186 lines (148 loc) · 3.11 KB
/
ttgo-twatch-ublox-gps-wifi.ino
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <WiFi.h>
#include "config.h"
#define PORT 27000
#define PACKET_TIMEOUT 5
#define BUFFER_SIZE 8192
const char* ssid = "u-blox NEO-M8N";
const char* password = "1234567890";
TTGOClass *ttgo;
TFT_eSPI *tft ;
AXP20X_Class *power;
bool irq = false;
HardwareSerial *hwSerial = nullptr;
WiFiServer server(PORT);
WiFiClient client;
uint8_t bufferReceive[BUFFER_SIZE];
uint16_t i1 = 0;
uint8_t bufferSend[BUFFER_SIZE];
uint16_t i2 = 0;
void pressed()
{
ttgo->setBrightness(170);
}
void released()
{
ttgo->setBrightness(85);
}
void setup(void)
{
Serial.begin(115200);
ttgo = TTGOClass::getWatch();
ttgo->begin();
ttgo->openBL();
ttgo->setBrightness(85);
tft = ttgo->tft;
power = ttgo->power;
tft->fillScreen(TFT_BLACK);
tft->setTextFont(2);
pinMode(AXP202_INT, INPUT_PULLUP);
attachInterrupt(AXP202_INT, [] {
irq = true;
}, FALLING);
power->enableIRQ(AXP202_PEK_SHORTPRESS_IRQ, true);
power->clearIRQ();
// GPS
tft->println("Initializing u-blox NEO-M8N...");
ttgo->enableLDO3();
if (hwSerial == nullptr)
{
hwSerial = new HardwareSerial(1);
}
hwSerial->begin(GPS_BAUD_RATE, SERIAL_8N1, GPS_RX, GPS_TX);
tft->setTextColor(TFT_GREEN);
tft->println("Done.");
// WiFi
tft->setTextColor(TFT_WHITE);
tft->println("Connecting to WiFi with SSID: ");
tft->setTextColor(TFT_YELLOW);
tft->println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
tft->setTextColor(TFT_GREEN);
tft->println("Done.");
tft->setTextColor(TFT_WHITE);
tft->print("Local IP address: ");
tft->setTextColor(TFT_YELLOW);
tft->println(WiFi.localIP());
// Server
tft->setTextColor(TFT_WHITE);
tft->println("Initializing local server...");
server.begin();
tft->setTextColor(TFT_GREEN);
tft->println("Done.");
tft->setTextColor(TFT_WHITE);
tft->print("Port: ");
tft->setTextColor(TFT_YELLOW);
tft->println(PORT);
tft->setTextColor(TFT_WHITE);
// User button
ttgo->button->setPressedHandler(pressed);
ttgo->button->setReleasedHandler(released);
}
void loop(void)
{
if (irq)
{
irq = false;
power->readIRQ();
if (power->isPEKShortPressIRQ() )
{
if (ttgo->bl->isOn())
{
ttgo->closeBL();
ttgo->displaySleep();
}
else
{
ttgo->displayWakeup();
ttgo->openBL();
}
}
power->clearIRQ();
}
ttgo->button->loop();
if (!client.connected())
{
client = server.available();
return;
}
if (client.available())
{
while (client.available())
{
bufferReceive[i1] = (uint8_t)client.read();
if (i1 < BUFFER_SIZE - 1)
{
i1++;
}
}
hwSerial->write(bufferReceive, i1);
i1 = 0;
}
if (hwSerial->available())
{
while (1) {
if (hwSerial->available())
{
bufferSend[i2] = (char)hwSerial->read();
if (i2 < BUFFER_SIZE - 1)
{
i2++;
}
}
else
{
delay(PACKET_TIMEOUT);
if (!hwSerial->available())
{
break;
}
}
}
client.write((char*)bufferSend, i2);
i2 = 0;
}
}