Repository navigation
Expand file tree
/
Copy pathapplication.cpp
More file actions
104 lines (86 loc) · 2.4 KB
/
Copy pathapplication.cpp
File metadata and controls
104 lines (86 loc) · 2.4 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
/* Serial and TCP CAN bus logger for Carloop
*
* Reads CAN messages at 500 kbit and outputs them formatted as JSON to
* the USB Serial and a network port
*
* To see the USB serial port dump, read the port with cat, screen or
* PuTTY on Windows.
* cat /dev/tty.usbmodem1411 (update for your port number)
*
* To start the network server, call the function "startServer" on your
* device:
* particle call my_carloop startServer
*
* To get the IP address and port of the Carloop:
* particle get my_carloop ip
* particle get my_carloop port
*
* You must be on the same WiFi network as the Carloop to be able to
* connect to the server. You can use telnet or PuTTY in raw mode to
* connect with the IP address and port from above:
* telnet 192.168.0.127 9000
*
* Use tee to save the data dump:
* cat /dev/tty.usbmodem1411 | tee my_drive.log
* telnet 192.168.0.127 9000 | tee my_drive.log
*
* Copyright 2016 Julien Vanier
*
* Distributed under the MIT license. See LICENSE.txt for more details.
*/
#include "application.h"
#include "carloop/carloop.h"
#include <memory>
using namespace std;
SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);
String dumpMessage(const CANMessage &message);
Carloop<CarloopRevision2> carloop;
String myIp;
const int myPort = 9000;
unique_ptr<TCPServer> server;
TCPClient client;
int startServer(String portStr) {
client.stop();
server.reset(new TCPServer(myPort));
server->begin();
return myPort;
}
void setup() {
Serial.begin(9600);
carloop.begin();
Particle.variable("ip", myIp);
Particle.variable("port", myPort);
Particle.function("startServer", startServer);
Particle.connect();
}
void loop() {
myIp = WiFi.localIP();
String dump;
CANMessage message;
while(carloop.can().receive(message)) {
// If the serial port or TCP server is overwhelmed, limit messages
// if(message.id >= 0x700)
dump += dumpMessage(message);
}
Serial.write(dump);
if(server) {
if(client.connected()) {
client.write(dump);
} else {
client = server->available();
}
}
}
String dumpMessage(const CANMessage &message) {
String str;
str = String::format("{\"timestamp\":%f,\"bus\":1,\"id\":\"0x%03x\",\"data\":\"", millis() / 1000.0, message.id, message.len);
for(int i = 0; i < message.len; i++) {
if(i == 0) {
str += "0x";
}
str += String::format("%02x", message.data[i]);
}
str += "\"}\n";
return str;
}