-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsender_code.ino
64 lines (56 loc) · 1.73 KB
/
sender_code.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
#include <esp_now.h>
#include <WiFi.h>
#define PULSE_SENSOR_PIN 34
#define CHANNEL 1
#define DATA_SIZE 4
uint8_t macPenerima[] = { 0xB4, 0x8A, 0x0A, 0x46, 0x57, 0x00 };
uint8_t macSender2[] = { 0x40, 0x22, 0xD8, 0xE8, 0x72, 0xA0 };
uint8_t data[DATA_SIZE] = { 0x01, 0x02, 0x03, 0x04 };
bool data_diterima = false;
bool data_terkirim = false;
bool esp32modemsleep = false;
void OnDataSent(const uint8_t *macPenerima, esp_now_send_status_t status) {
Serial.print("Packet sent to: ");
for (int i = 0; i < 6; i++) {
Serial.print(macPenerima[i], HEX);
if (i < 5) Serial.print(':');
}
Serial.println();
}
void setup() {
Serial.begin(115200);
pinMode(PULSE_SENSOR_PIN, INPUT);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_peer_info_t peerInfo;
memset(&peerInfo, 0, sizeof(peerInfo));
memcpy(peerInfo.peer_addr, macPenerima, sizeof(macPenerima));
peerInfo.channel = CHANNEL;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer 1");
return;
}
WiFi.setSleep(true);
WiFi.setTxPower(WIFI_POWER_MINUS_1dBm);
}
void loop() {
int sensorValue = analogRead(PULSE_SENSOR_PIN);
int bpm = 0.024 * sensorValue;
//esp_now_send(macSender2, (uint8_t *)&bpm, sizeof(bpm)) != ESP_OK;
if (bpm == 42 || bpm == 43 || bpm < 42 || bpm == 44) {
Serial.print("\nESP32 masih dalam mode Modem sleep");
WiFi.setSleep(true);
WiFi.setTxPower(WIFI_POWER_MINUS_1dBm);
} else {
if (esp_now_send(macPenerima, (uint8_t *)&bpm, sizeof(bpm)) != ESP_OK) {
Serial.println("Failed to send data");
} else {
Serial.println("\nData sent successfully");
}
delay(3000);
}
}