-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSender.ino
169 lines (125 loc) · 4.12 KB
/
Sender.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*********
This instantiates an ESPNow Wifi connection from one Thales controller to the receiver unit.
Upload as is on the first controller (left hand). When uploading on the second controller change the board ID to 2 (line 162).
Code is based on Rui Santos ESPNow project at https://RandomNerdTutorials.com/esp-now-many-to-one-esp32/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*********/
#include <esp_now.h>
#include <Wire.h>
#include <WiFi.h>
#include <Adafruit_BNO055.h>
#include "Adafruit_MLX90393.h"
Adafruit_MLX90393 sensor = Adafruit_MLX90393();
Adafruit_BNO055 bno = Adafruit_BNO055(55, 0x28);
#define MLX90393_CS 10
//timer for transmission freq
unsigned long timer = 0;
// REPLACE WITH THE RECEIVER'S MAC Address
uint8_t broadcastAddress[] = {0x94, 0xB9, 0x7E, 0xD2, 0x14, 0x1C};
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
int id; // must be unique for each sender board
//magnet
float hallx;
float hally;
float hallz;
//BNO055 angle
float anglex;
float angley;
float anglez;
//BNO055accelerometer
float accx;
float accy;
float accz;
int battery;
} struct_message;
// Create a struct_message called myData
struct_message myData;
// Create peer interface
esp_now_peer_info_t peerInfo;
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail");
}
void setup() {
// Init Serial Monitor
Serial.begin(115200);
analogReadResolution(12);
if (!bno.begin())
{
while (1); { delay(10); }
}
if (! sensor.begin_I2C()) {
while (1) { delay(10); }
}
delay(1000);
/* Use external crystal for better accuracy */
bno.setExtCrystalUse(true);
//Set gain
sensor.setGain(MLX90393_GAIN_1X);
// Set resolution, per axis
sensor.setResolution(MLX90393_X, MLX90393_RES_19);
sensor.setResolution(MLX90393_Y, MLX90393_RES_19);
sensor.setResolution(MLX90393_Z, MLX90393_RES_19);
// Set oversampling
sensor.setOversampling(MLX90393_OSR_2);
// Set digital filtering
sensor.setFilter(MLX90393_FILTER_0);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
//esp_now_register_send_cb(OnDataSent);
// Register peer
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
}
void loop() {
if((millis()-timer)>5)
{
int Voltout = analogRead(A2);
Voltout = Voltout * 5000 / 4096; //read battery charge
//Get MXL event, normalized to uTesla */
sensors_event_t event, orientationData, linearAccelData;
sensor.getEvent(&event);
bno.getEvent(&orientationData, Adafruit_BNO055::VECTOR_EULER);
bno.getEvent(&linearAccelData, Adafruit_BNO055::VECTOR_LINEARACCEL);
// Set values to send
myData.id = 1; //Set ID board number
myData.hallx = event.magnetic.x;
myData.hally = event.magnetic.y;
myData.hallz = event.magnetic.z;
myData.anglex = orientationData.orientation.x;
myData.angley = orientationData.orientation.y;
myData.anglez = orientationData.orientation.z;
myData.accx = linearAccelData.orientation.x;
myData.accy = linearAccelData.orientation.y;
myData.accz = linearAccelData.orientation.z;
myData.battery = Voltout;
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
if (result == ESP_OK) {
Serial.println("Delivered");
}
else {
Serial.println("Error sending the data");
}
timer = millis();
}
}