-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathM0Rx.ino
More file actions
208 lines (187 loc) · 5.06 KB
/
M0Rx.ino
File metadata and controls
208 lines (187 loc) · 5.06 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* -----------------------------------------------------------------------------
Name: M0Rx
Purpose: RC receiver with 'Cortex M0 Feather' board, WiFi
and PCA9685
Author: Bernd Hinze
github https://github.com/monbera/M0Rx
Created: 25.02.2020
Copyright: (c) Bernd Hinze 2020
Licence: MIT see https://opensource.org/licenses/MIT
----------------------------------------------------------------------------
*/
#include <MyRC.h>
#include <PCA9685.h>
#include <SPI.h>
#include <WiFi101.h>
#include <WiFiUdp.h>
#include "cfg.h"
#define VBATPIN A7
int status = WL_IDLE_STATUS;
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
int keyIndex = 0;
boolean ipset = false;
int cntcycle = 0;
long rssi = 0;
int tmp = 0;
unsigned int localPort = 6100; // local port to listen on
IPAddress lip;
int localip[4];
IPAddress bcip;
unsigned long sendTime = millis();
unsigned long timeout = millis();
unsigned long FStimeout = 1000;
unsigned long BCcycle = 1000; // 1 sec
char CodeTable[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?'};
char packetBuffer[100]; //buffer to hold incoming data
char BCBuffer[30] = {0}; // sending buffer
int contrlbuffer[3 * 16 + 1] = {0}; // control values for 16 Channels
int Conf[16 * CFGlen];
int Fdat[16];
WiFiUDP Udp;
void setup() {
WiFi.setPins(8, 7, 4, 2);
#ifdef DEBUG
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
#endif
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
#ifdef DEBUG
Serial.println("WiFi shield not present");
#endif
while (true);
}
// attempt to connect to WiFi network:
status = WiFi.begin(ssid, pass);
delay(5000);
while ( status != WL_CONNECTED) {
#ifdef DEBUG
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
#endif
status = WiFi.begin(ssid, pass);
delay(5000);
}
#ifdef DEBUG
Serial.println("Connected to wifi");
#endif
lip = getWiFiStatus();
for (int i = 0; i < 4 ; i++) {
localip[i] = lip[i];
}
//appendIP (localip, BCBuffer);
BCTel(CodeTable, localip, BCBuffer);
#ifdef DEBUG
Serial.println("");
Serial.print("BCDuffer :");
Serial.println(BCBuffer);
#endif
// Broadcast IP berechnen
bcip = lip;
bcip[3] = 255;
#ifdef DEBUG
Serial.println(bcip);
Serial.println("\nStarting connection to server...");
#endif
Udp.begin(localPort);
// Preparation of configuration data
CtrData_init(Conf);
//config_channels using 'cfg.h'
config_chan_all(CHcfg, CHcfg_P, Conf);
// setup PCA9685 board
Wire.begin();
Wire.setClock(400000);
software_reset();
pwm_init();
set_pwm_freq(50);
fail_safe(Conf);
}
void loop() {
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize)
{
IPAddress remoteIp = Udp.remoteIP();
if (!ipset)
{
bcip[3] = remoteIp[3]; // setting the bcip with the correct remote ip
ipset = true;
}
// read the packet into packetBufffer
int len = Udp.read(packetBuffer, 255);
if (len > 0) packetBuffer[len] = 0;
tmp = teltoarr(packetBuffer, contrlbuffer);
#ifdef DEBUG
Serial.println();
for (int i = 0; i < tmp + 1 ; i++) {
Serial.print(contrlbuffer[i]);
}
#endif
timeout = millis();
update(contrlbuffer, Fdat, Conf);
}
if ((millis() - timeout) > FStimeout) {
fail_safe(Conf);
}
if ((millis() - sendTime) > BCcycle) {
Broadcast(BCBuffer);
cntcycle += 1;
if (cntcycle > 10) {
update_V(CodeTable, getVBAT(VBATPIN), BCBuffer);
cntcycle = 0;
//long rssi = WiFi.RSSI();
//Serial.println(rssi);
}
sendTime = millis();
}
}
// ------------------------ functions --------------------------------
// read analog value
float getVBAT(int batterypin) {
float measuredvbat = analogRead(batterypin);
measuredvbat *= 2;
measuredvbat *= 3.3; // Multiply by 3.3V, our reference voltage
measuredvbat /= 1024; // convert to voltage
return measuredvbat;
#ifdef DEBUG
Serial.print(" VBat: " );
Serial.print(measuredvbat);
#endif
}
// send a reply, to the IP address and port that sent us the packet we received
void Broadcast(char *buf) {
Udp.beginPacket(bcip, 6000);
Udp.write(buf);
Udp.endPacket();
}
void PrintRecvPaket(int pSize) {
Serial.print("Received packet of size ");
Serial.println(pSize);
Serial.print("From ");
}
void PrintRSSI(long val) {
Serial.print("signal strength (RSSI):");
Serial.print(val);
Serial.println(" dBm");
}
IPAddress getWiFiStatus() {
// print the SSID of the network you're attached to:
#ifdef DEBUG
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
#endif
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
long rssi = WiFi.RSSI();
#ifdef DEBUG
Serial.print("IP Address: ");
Serial.println(ip);
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
#endif
return ip;
}