espnowSimple 1
Loading...
Searching...
No Matches
simplewrite03.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/* simplewrite03 demonstrates how to send packager of differrent size between two nodes.
8*
9* The packages all has a uint8_t byte in top of package which gives package type ID
10*/
11
12/* we just want to tx a struct
13* so you just stuff your data inside
14* pls avoid Strings due to internal fragmentation due to use of heap
15* Instead use
16
17String myString; v = "hej" +"noget andet"; // etc
18char dataAr[20];
19myString.toCharArray(dataAr,20);
20* and then tx dataAr
21
22*/
23
24/*
25* packed directive packs the struct on byte aligment
26*/
27typedef struct myPackageType {
28 int seqNr;
29 uint8_t pkgTp; // should b 1
30 int i1;
31 float f[3];
32 char txt[5];
33 // ...
35
36typedef struct myPackageType2 {
37 int seqNr;
38 uint8_t pkgTp; // should be 2
39 float f;
40 char txt[7];
41 // ...
43
44// more to come if you need more package types
45
48
49// getting my callbacks up and running
50// seems to have problems if we do tx and rx at same tiem
51// even if no package loss is detected
52
53void espSimpleNowCallbackDataSent(const bool success) {
54 //JDN Serial.print("tx(0 == ok) ");
55 //Serial.println(scs);
56}
57
63
64void espSimpleNowCallbackBinRcvd(const uint8_t *data, int len) {
65 // test
66 // we do know the first element is uint8_t the ppkgTp;
67 // you can check if missing packages - se simplewrite02 /JDN
68 switch (*data) {
69 case 1: // myPackageType
70 // extra chk if type an size fits together
71 if (len == sizeof(myPackageType)) {
72 rxPkg = *((myPackageType *)data);
73
74 Serial.print("rx callback ok type 1 pkg: ");
75 Serial.println(rxPkg.i1);
76 }
77 break;
78 case 2: // myPackageType2
79 if (len == sizeof(myPackageType2)) {
80 rxPkg2 = *((myPackageType2 *)data);
81
82 Serial.print("rx callback ok type 2 pkg: ");
83 Serial.println(rxPkg2.f);
84 }
85 break;
86 default: // MISSING
87 Serial.print("unknown pkg, type field:");
88 Serial.println(*data);
89 }
90}
91
92// max max 250 Bytes data acc expressif
93//uint8_t espSimpleNowBroadcastData(char *data, uint16_t len)
94
95
96void setup() {
97
98 pinMode(5, OUTPUT); // led pin on cansat NeXt
99
100 Serial.begin(115200);
101 delay(100);
102 Serial.println("test");
103
105
106 // init data
107 // so callback rx can see different
108 txPkg.seqNr = 0;
109 txPkg.pkgTp = 1;
110 txPkg.i1 = 40000;
111
112 txPkg2.seqNr = 0;
113 txPkg2.pkgTp = 2;
114 txPkg2.f = 9111.24;
115}
116
117void txTest() {
118 txPkg.i1++;
119 txPkg.seqNr++;
120 espSimpleNowBroadcastData((char *)(&txPkg), sizeof(myPackageType));
121
122 txPkg2.f += 1.0;
123 txPkg2.seqNr++;
125
126
127 digitalWrite(5, !digitalRead(5));
128}
129
130void loop() {
131 txTest(); // comment this line if your are the RX part
132 delay(2000);
133}
uint8_t espSimpleNowBroadcastData(char *data, uint16_t len)
uint8_t espSimpleNowInit(uint8_t macAddrLastByte)
myPackageType txPkg
myPackageType rxPkg
void espSimpleNowCallbackBinRcvd(const uint8_t *data, int len)
void espSimpleNowCallbackDataSent(const bool success)
void setup()
myPackageType2 rxPkg2
void txTest()
myPackageType2 txPkg2
void loop()