-
Notifications
You must be signed in to change notification settings - Fork 1
/
esp-now-usb-pc-dongle.ino
executable file
·169 lines (145 loc) · 3.84 KB
/
esp-now-usb-pc-dongle.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
/*
ESP command receiver dongle
The ESP32 device is connected to PC over USB serial port.
It receives commands over esp-now wifi protocol from
other ESP32 macro boards. Received commands are sent to
pc-serial-macro-server over the serial port.
The receiver dongle also receives commands from PC to be executed
on its GPIO pins. You can achieve other automations using GPIO pins on
the dongle ESP32. For now commands are to make sounds on buzzer module.
created 10th May 2024
by Onkar Ruikar
*/
#include <esp_now.h>
#include <WiFi.h>
#include <BluetoothSerial.h>
#define BUZZER_PIN 6
int inCommand = 0;
typedef struct struct_message {
int key;
} struct_message;
struct_message data;
/*
* Handle received command from keypad over esp-now.
*/
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&data, incomingData, sizeof(data));
neopixelWrite(21, 0, 0, 64);
// convert MAC numer to string
char cMac[18];
snprintf(cMac, sizeof(cMac), "%02X-%02X-%02X-%02X-%02X-%02X",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.println("got:" + String(cMac) + ":" + String(data.key));
// indicate command sent to PC
delay(100);
neopixelWrite(21, 0, 0, 0);
delay(100);
neopixelWrite(21, 0, 0, 64);
delay(100);
neopixelWrite(21, 5, 0, 0);
}
// Play beep on buzzer
void beep(uint32_t duration) {
digitalWrite(BUZZER_PIN, HIGH);
delay(duration);
digitalWrite(BUZZER_PIN, LOW);
}
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(115200);
setCpuFrequencyMhz(80);
btStop();
delay(5000);
// set device as a wifi station
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("log: esp-now init failed!");
neopixelWrite(21, 0, 5, 0);
return;
}
esp_now_register_recv_cb(OnDataRecv);
neopixelWrite(21, 5, 0, 0);
Serial.println("log: setup done");
}
void loop() {
// handle commands received from PC
if (Serial.available()) {
inCommand = Serial.read();
Serial.println("log: got command " + String(inCommand));
switch(inCommand) {
// beep
case 49:
beep(80);
break;
// double beep
case 50:
beep(150);
delay(80);
beep(200);
break;
// EOD beep
case 51:
beep(400);
delay(100);
beep(100);
delay(50);
beep(100);
break;
// buzzer on
case 52:
digitalWrite(BUZZER_PIN, HIGH);
break;
// buzzer off
case 53:
digitalWrite(BUZZER_PIN, LOW);
break;
default:
digitalWrite(BUZZER_PIN, LOW);
}
inCommand = 0;
}
}
// debug scripts
/* ----------------------------------------------
* script to get MAC address of the device
* so that it could be used by sender devices
* to send commands to this device
#include "WiFi.h"
void setup(){
Serial.begin(115200);
WiFi.mode(WIFI_MODE_STA);
Serial.println(WiFi.macAddress());
}
void loop(){}
*/
/* ----------------------------------------------
* script to test usb serial communication with PC
String inputString = ""; // a String to hold incoming data
bool stringComplete = false; // whether the string is complete
void setup() {
// initialize serial
Serial.begin(115200);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}
void loop() {
if (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
// print the string when a newline arrives:
if (stringComplete) {
Serial.println("got: " + inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}
*/