-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAirCare_AirPurifier.ino
77 lines (66 loc) · 2.13 KB
/
AirCare_AirPurifier.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
#include <esp_now.h>
#include <WiFi.h>
// Replace with your network credentials (STATION)
const char* ssid = "olleh_WiFi_215A";
const char* password = "000000486a";
typedef struct esp_struct {
int x;
int y;
} esp_struct;
//Create a struct_message called myData
esp_struct esp_received;
// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac_addr, const uint8_t *incomingData, int len) {
// Copies the sender mac address to a string
char macStr[18];
Serial.print("Packet received from: ");
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.println(macStr);
memcpy(&esp_received, incomingData, sizeof(esp_received));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.print("x: ");
Serial.println(esp_received.x);
Serial.print("y: ");
Serial.println(esp_received.y);
Serial.println();
if(esp_received.x == 1) {
Serial.print("HIGH");
digitalWrite(27, HIGH);
} else {
digitalWrite(27, LOW);
}
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Set the device as a Station and Soft Access Point simultaneously
WiFi.mode(WIFI_AP_STA);
// Set device as a Wi-Fi Station
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Setting as a Wi-Fi Station..");
}
Serial.print("Station IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("Wi-Fi Channel: ");
Serial.println(WiFi.channel());
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(OnDataRecv);
pinMode(27,OUTPUT);
}
void loop() {
static unsigned long lastEventTime = millis();
static const unsigned long EVENT_INTERVAL_MS = 5000;
if ((millis() - lastEventTime) > EVENT_INTERVAL_MS) {
lastEventTime = millis();
}
}