Repository navigation
Expand file tree
/
Copy pathcanbridge.cpp
More file actions
74 lines (58 loc) · 1.42 KB
/
Copy pathcanbridge.cpp
File metadata and controls
74 lines (58 loc) · 1.42 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
#include <Arduino.h>
#include "util.h"
#include "esp8266.h"
#include "mcpc.h"
#ifdef ESP8266
#define SERIAL_BAUD 115200
#else
#define SERIAL_BAUD 9600
#endif
#define POLL_INTERVAL 100
char buf[128];
long last_status = 0;
void setup() {
#if defined(MEGA) || defined(ESP8266)
Serial.begin(SERIAL_BAUD);
#endif
DEBUGP("Starting CANBridge\r\n");
if (init_wifi()>0) {
DEBUGP("Wifi initialized\r\n");
} else {
DEBUGP("Wifi initialization error\r\n");
}
init_mcpc();
if (init_server() > 0) {
DEBUGP("UDP server started\r\n");
}
delay(2000);
DEBUGP("Init complete\r\n");
}
void loop() {
uint8_t dlen;
struct {
unsigned long id;
uint8_t can_frame[64];
} datagram;
if(millis() > last_status + 60000) {
DEBUGP("Uptime: %us\r\n", (int)(millis() / 1000));
DEBUGP("Free memory: %u\r\n", freeMemory());
last_status = millis();
}
if (recv_frame(&datagram.id, datagram.can_frame, &dlen)) {
DEBUGP("received can frame with length %u\r\n", dlen);
wifi_send_data((uint8_t*)&datagram, dlen+4);
}
if ((dlen = recv_datagram((uint8_t*) &datagram, sizeof(datagram))) > 0) {
#ifdef ESP8266
DEBUGP("Received UDP datagram from %s with size %hhu\r\n", Udp.remoteIP().toString().c_str(), dlen);
#endif
#ifdef DEBUG
for(int i = 0; i < dlen; i++) {
DEBUGP("%hhx ", ((char*)&datagram)[i]);
}
#endif
DEBUGP("\r\n");
send_frame(datagram.id, datagram.can_frame, dlen - sizeof(datagram.id));
}
delay(POLL_INTERVAL);
}