espnowSimple 1
Loading...
Searching...
No Matches
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
12String myString; v = "hej" +"noget andet"; // etc
13char dataAr[20];
14myString.toCharArray(dataAr,20);
15* and then tx dataAr
16*
17*/
18
19typedef struct myPackageType {
20 int seqNr; // you must maintain yourself
21 int i1;
22 float f[3];
23 char txt[5];
24 // ...
26
27
29
30
31
32// getting my callbacks up and running
33void espSimpleNowCallbackDataSent(const bool success) {
34 uint8_t scs;
35 scs = success;
36 Serial.print("tx(0 == ok) ");
37 Serial.println(scs);
38}
39
40void 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
67uint8_t testMac[6] = { 1, 2, 3, 4, 5, 6 };
68
69char testData[12];
70
71
72
73
74void 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)")
84
85 // init data
86 txPkg.seqNr = 100; // just a number to TX
87 txPkg.seqNr = 0; // nothing received yet
88}
89
90void 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
96
97 digitalWrite(5, !digitalRead(5)); // toogle LED to indicate pkg tx'ed
98}
99
100void loop() {
101 //txTest(); // uncomment if you are server
102 delay(500);
103}
uint8_t espSimpleNowBroadcastData(char *data, uint16_t len)
uint8_t espSimpleNowInit(uint8_t macAddrLastByte)
void espSimpleNowCallbackBinRcvd(const uint8_t *data, int len)
myPackageType txPkg
void espSimpleNowCallbackDataSent(const bool success)
uint8_t testMac[6]
void setup()
void txTest()
myPackageType rxPkg
char testData[12]
void loop()