espnowSimple 1
Loading...
Searching...
No Matches
espnowsimple.cpp
Go to the documentation of this file.
1/* ----------------------------------------------------------------------------
2* "THE BEER-WARE LICENSE":
3* <jensd@jensd.dk/jdn@es.aau.dk> wrote this file. As long as you retain this notice you
4* can do whatever you want with this stuff. If we meet some day, and you think
5* this stuff is worth it, you can buy me a beer in return.
6* Jens Dalsgaard Nielsen April 2024
7* ----------------------------------------------------------------------------
8*
9* Heavily inspired by
10* Copyright (c) 2023 Samuli Nyman
11* SPDX-License-Identifier: MIT
12* http://cansat.fi
13*/
14
15#include <espnowsimple.h>
16
17uint8_t broadcastMAC[6] = { 0x02, 0x2a, 0x56, 0x72, 0x2b, 0x00 }; // from cansat.fi
18esp_now_peer_info_t peerInfo;
20
21
22
23uint8_t espSimpleNowRunning = false;
24
25
26// callbacks required from esp-now library
27// our interface - radio callback interface
28// empty for now
29//JDN __attribute__((weak)) void callbackDataReceived(String data) {}
30//
31// The following two you can(or must) overrule by your own
32
33__attribute__((weak)) void espSimpleNowCallbackBinRcvd(const uint8_t *data, int len) {}
34__attribute__((weak)) void espSimpleNowCallbackDataSent(const bool success) {}
35
36
37
38void internalDataSentCallback(const uint8_t *mac_addr, esp_now_send_status_t status) {
39 bool success = status == ESP_NOW_SEND_SUCCESS; //ESP_NOW_SE... == 0
41}
42
43
44void internalDataReceivedCallback(const unsigned char *mac, const unsigned char *incomingData, int len) {
45 /*
46 String receivedData = ""; cnrt to string
47 for (int i = 0; i < len; i++) {
48 receivedData += (char)incomingData[i];
49 }
50
51 JDN NASTY - call both ?
52 JDN onDataReceived(receivedData); String... we dont want to pasre Strings ...
53 */
54 espSimpleNowCallbackBinRcvd(incomingData, len);
55}
56
57void setMacAddr(uint8_t lastByte, uint8_t mac[6]) {
58 mac[0] = 0x02; // NB it seems thats [0] must be even
59 mac[1] = 0x2A;
60 mac[2] = 0x56;
61 mac[3] = 0x72;
62 mac[4] = 0x2B;
63 mac[5] = lastByte;
64}
65
66uint8_t initESPNowMac(uint8_t *mac) {
67
68 espSimpleNowRunning = false;
69
70 WiFi.mode(WIFI_STA);
71
72 // set receive address
73 espNowSimpleErr = esp_wifi_set_mac(WIFI_IF_STA, broadcastMAC);
74 if (ESP_OK != espNowSimpleErr) {
75 return 1;
76 }
77
78 espNowSimpleErr = esp_now_init();
79 if (ESP_OK != espNowSimpleErr) {
80 return 2;
81 }
82
83 espNowSimpleErr = esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_LR);
84 if (ESP_OK != espNowSimpleErr) {
85 return 3;
86 }
87
88 espNowSimpleErr = esp_now_register_send_cb(internalDataSentCallback);
89 if (ESP_OK != espNowSimpleErr) {
90 return 4;
91 }
92
93 espNowSimpleErr = esp_now_register_recv_cb(internalDataReceivedCallback);
94 if (ESP_OK != espNowSimpleErr) {
95 return 5;
96 }
97
98 // Register peer
99 memcpy(peerInfo.peer_addr, broadcastMAC, 6);
100 peerInfo.channel = 0;
101 peerInfo.encrypt = false;
102
103 espNowSimpleErr = esp_now_add_peer(&peerInfo);
104
105 if (ESP_OK != espNowSimpleErr) {
106 return 6;
107 }
108
109 espSimpleNowRunning = true;
110
111 return 0;
112}
113
114uint8_t espSimpleNowInit(uint8_t macAddrLastByte) {
115
116 //sanity chack last bit must not be 1 acc to expressif
117 if (macAddrLastByte != ((macAddrLastByte >> 1) << 1)) {
118 return -1;
119 }
120
121 setMacAddr(macAddrLastByte, broadcastMAC);
122
124}
125
126
127//usr call II
128
129
130uint8_t espSimpleNowBroadcastData(char *data, uint16_t len) {
131 if (250 < len) {
132 return -1;
133 }
134
136 return esp_now_send(broadcastMAC, (uint8_t *)data, len);
137 } else {
138 return -2;
139 }
140}
141
142void espSimpleNowSetMacAddr(uint8_t mac[6]) {
143 for (int i = 0; i < 6; i++) {
144 broadcastMAC[i] = mac[i];
145 }
146}
147
148void espSimpleNowGetMacAddr(uint8_t mac[6]) {
149 for (int i = 0; i < 6; i++) {
150 mac[i] = broadcastMAC[i];
151 }
152}
void espSimpleNowCallbackBinRcvd(const uint8_t *data, int len)
void internalDataReceivedCallback(const unsigned char *mac, const unsigned char *incomingData, int len)
void espSimpleNowCallbackDataSent(const bool success)
uint8_t espSimpleNowBroadcastData(char *data, uint16_t len)
esp_now_peer_info_t peerInfo
uint8_t espSimpleNowInit(uint8_t macAddrLastByte)
void espSimpleNowSetMacAddr(uint8_t mac[6])
esp_err_t espNowSimpleErr
void internalDataSentCallback(const uint8_t *mac_addr, esp_now_send_status_t status)
uint8_t broadcastMAC[6]
void setMacAddr(uint8_t lastByte, uint8_t mac[6])
void espSimpleNowGetMacAddr(uint8_t mac[6])
uint8_t espSimpleNowRunning
uint8_t initESPNowMac(uint8_t *mac)