forked from NomadicGeek369/node.js-websocket-sensor-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ino
95 lines (78 loc) · 2.05 KB
/
main.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
/*
ESP-NOW Remote Sensor - Receiver (Multiple Version)
esp-now-rcv.ino
Receives Temperature & Humidity data from other ESP32 via ESP-NOW
DroneBot Workshop 2022
https://dronebotworkshop.com
*/
// Include required libraries
#include <WiFi.h>
#include <esp_now.h>
// Define data structure
typedef struct struct_message
{
uint8_t photoBytes[160]; // ESP Now supports max of 250 bytes at each packet
} struct_message;
struct_message incomingReadings;
struct_message incomingData;
uint8_t r_color = 50;
uint8_t g_color = 50;
uint8_t b_color = 50;
unsigned int pixelCounter = 0;
int rowCounter = 0;
unsigned int zeroCounter = 0;
// Callback function
void OnDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len)
{
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.println("!");
pixelCounter = 0;
if (zeroCounter > 100)
{
zeroCounter = 0;
rowCounter = 0;
pixelCounter = 0;
}
for (uint8_t i = 0; i < 160; i++)
{
if (incomingReadings.photoBytes[i] == 0)
{
zeroCounter++;
}
if (rowCounter >= 159)
{
rowCounter = 0;
pixelCounter = 0;
}
uint16_t RGB565 = (((incomingReadings.photoBytes[i] & 0b11111000) << 8) + ((incomingReadings.photoBytes[i] & 0b11111100) << 3) + (incomingReadings.photoBytes[i] >> 3));
Serial.println(RGB565);
pixelCounter += 1;
}
rowCounter++;
Serial.println("");
Serial.println("?");
}
void setup()
{
// Set up Serial Monitor
Serial.begin(115200);
// Start ESP32 in Station mode
WiFi.mode(WIFI_STA);
delay(3000);
// Print MAC Address to Serial monitor
Serial.print("MAC Address: ");
Serial.println(WiFi.macAddress());
// Initalize ESP-NOW
if (esp_now_init() != 0)
{
Serial.println("Error initializing ESP-NOW");
return;
}
// Register callback function
esp_now_register_recv_cb(OnDataRecv);
}
void loop()
{
}