espnowSimple
1
Loading...
Searching...
No Matches
examples
simplewrite02
simplewrite02.ino
Go to the documentation of this file.
1
// JDN @ AAU
2
// beer license http://jensd.dk/doc/beerlicense.txt
3
// Apr 2024
4
5
#include <
espnowsimple.h
>
6
7
/* we just want to tx a struct
8
* so you just stuff your data inside
9
* pls avoid Strings due to internal fragmentation due to use of heap
10
* Instead use
11
12
String myString; v = "hej" +"noget andet"; // etc
13
char dataAr[20];
14
myString.toCharArray(dataAr,20);
15
* and then tx dataAr
16
*
17
*/
18
19
typedef
struct
myPackageType
{
20
int
seqNr
;
// you must maintain yourself
21
int
i1
;
22
float
f
[3];
23
char
txt
[5];
24
// ...
25
}
myPackageType
;
26
27
28
myPackageType
txPkg
,
rxPkg
;
29
30
31
32
// getting my callbacks up and running
33
void
espSimpleNowCallbackDataSent
(
const
bool
success) {
34
uint8_t scs;
35
scs = success;
36
Serial.print(
"tx(0 == ok) "
);
37
Serial.println(scs);
38
}
39
40
void
espSimpleNowCallbackBinRcvd
(
const
uint8_t *data,
int
len) {
41
static
int
lastPkgNr = 0;
42
int
pkgLost;
43
// test
44
if
(len ==
sizeof
(
myPackageType
)) {
45
rxPkg
= *((
myPackageType
*)data);
// we can copy struct in one statement !
46
Serial.print(
"rx callback ok: "
);
47
Serial.println(
rxPkg
.i1);
48
pkgLost =
rxPkg
.seqNr - lastPkgNr - 1;
49
if
(0 != pkgLost) {
50
//no package lost since last time
51
}
else
{
52
// at least on pkg lost
53
}
54
lastPkgNr =
rxPkg
.seqNr;
// rdy for next pkg
55
}
else
{
56
Serial.print(
"rx pkg bad size is/should be"
);
57
Serial.print(len);
58
Serial.print(
" "
);
59
Serial.println(
sizeof
(
myPackageType
));
60
}
61
}
62
63
// max max 250 Bytes data acc expressif
64
//uint8_t espSimpleNowBroadcastData(char *data, uint16_t len)
65
66
67
uint8_t
testMac
[6] = { 1, 2, 3, 4, 5, 6 };
68
69
char
testData
[12];
70
71
72
73
74
void
setup
() {
75
76
pinMode(5, OUTPUT);
// led pin on cansat NeXt
77
78
Serial.begin(115200);
79
delay(100);
80
Serial.println(
"test simplewrite02"
);
81
82
// init esp_now til lsb byte 72 (our "net)")
83
espSimpleNowInit
(72);
84
85
// init data
86
txPkg
.seqNr = 100;
// just a number to TX
87
txPkg
.seqNr = 0;
// nothing received yet
88
}
89
90
void
txTest
() {
91
txPkg
.i1++;
92
txPkg
.seqNr++;
// indicate we are tx next package
93
// you could fill din the rest of the struct it's up to you
94
95
espSimpleNowBroadcastData
((
char
*)(&
txPkg
),
sizeof
(
myPackageType
));
96
97
digitalWrite(5, !digitalRead(5));
// toogle LED to indicate pkg tx'ed
98
}
99
100
void
loop
() {
101
//txTest(); // uncomment if you are server
102
delay(500);
103
}
espSimpleNowBroadcastData
uint8_t espSimpleNowBroadcastData(char *data, uint16_t len)
Definition
espnowsimple.cpp:130
espSimpleNowInit
uint8_t espSimpleNowInit(uint8_t macAddrLastByte)
Definition
espnowsimple.cpp:114
espnowsimple.h
espSimpleNowCallbackBinRcvd
void espSimpleNowCallbackBinRcvd(const uint8_t *data, int len)
Definition
simplewrite02.ino:40
txPkg
myPackageType txPkg
Definition
simplewrite02.ino:28
espSimpleNowCallbackDataSent
void espSimpleNowCallbackDataSent(const bool success)
Definition
simplewrite02.ino:33
testMac
uint8_t testMac[6]
Definition
simplewrite02.ino:67
setup
void setup()
Definition
simplewrite02.ino:74
txTest
void txTest()
Definition
simplewrite02.ino:90
rxPkg
myPackageType rxPkg
Definition
simplewrite02.ino:28
testData
char testData[12]
Definition
simplewrite02.ino:69
loop
void loop()
Definition
simplewrite02.ino:100
myPackageType
Definition
simplewrite02.ino:19
myPackageType::txt
char txt[5]
Definition
simplewrite02.ino:23
myPackageType::i1
int i1
Definition
simplewrite02.ino:21
myPackageType::f
float f[3]
Definition
simplewrite02.ino:22
myPackageType::seqNr
int seqNr
Definition
simplewrite02.ino:20
Generated by
1.14.0