1
1
const char compile_date[] = __DATE__ " " __TIME__;
2
- #define ESP_DRD_USE_LITTLEFS false
3
- #define ESP_DRD_USE_SPIFFS false
4
- #define ESP_DRD_USE_EEPROM true
2
+ #define ESP_DRD_USE_LITTLEFS false
3
+ #define ESP_DRD_USE_SPIFFS false
4
+ #define ESP_DRD_USE_EEPROM true
5
5
6
- #define DOUBLERESETDETECTOR_DEBUG false // false
6
+ #define DOUBLERESETDETECTOR_DEBUG false // false
7
7
8
- #include < ESP_DoubleResetDetector.h> // https://github.com/khoih-prog/ESP_DoubleResetDetector
8
+ #include < ESP_DoubleResetDetector.h> // https://github.com/khoih-prog/ESP_DoubleResetDetector
9
9
#include < Arduino.h>
10
10
#include < WiFi.h>
11
- #include < AsyncTCP.h> // https://github.com/me-no-dev/AsyncTCP
12
- #include < ESPAsyncWebServer.h> // https://github.com/me-no-dev/ESPAsyncWebServer
13
- #include < AsyncElegantOTA.h> // https://github.com/ayushsharma82/AsyncElegantOTA
14
- #include < ESP32CAN.h> // https://github.com/miwagner/ESP32-Arduino-CAN
11
+ #include < AsyncTCP.h> // https://github.com/me-no-dev/AsyncTCP
12
+ #include < ESPAsyncWebServer.h> // https://github.com/me-no-dev/ESPAsyncWebServer
13
+ #include < AsyncElegantOTA.h> // https://github.com/ayushsharma82/AsyncElegantOTA
14
+ #include < ESP32CAN.h> // https://github.com/miwagner/ESP32-Arduino-CAN
15
15
#include < CAN_config.h>
16
16
17
17
// Number of seconds after reset during which a
@@ -32,7 +32,7 @@ uint8_t temprature_sens_read();
32
32
float temp = 0.0 ;
33
33
34
34
volatile int interruptCounter;
35
- hw_timer_t * timer = NULL ;
35
+ hw_timer_t * timer = NULL ;
36
36
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
37
37
38
38
void IRAM_ATTR onTimer0 () {
@@ -42,11 +42,11 @@ void IRAM_ATTR onTimer0() {
42
42
}
43
43
44
44
#ifndef LED_BUILTIN
45
- #define LED_BUILTIN 2 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED
45
+ #define LED_BUILTIN 2 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED
46
46
#endif
47
47
48
- #define LED_OFF LOW
49
- #define LED_ON HIGH
48
+ #define LED_OFF LOW
49
+ #define LED_ON HIGH
50
50
51
51
#define VOLTAGE_PIN 36
52
52
float voltage = 0.0 ;
@@ -62,15 +62,15 @@ AsyncWebServer server(80);
62
62
63
63
BluetoothSerial SerialBT;
64
64
65
- CAN_device_t CAN_cfg; // CAN Config
66
- const int rx_queue_size = 10 ; // Receive Queue size
65
+ CAN_device_t CAN_cfg; // CAN Config
66
+ const int rx_queue_size = 10 ; // Receive Queue size
67
67
68
68
void setup (void ) {
69
69
Serial.begin (250000 );
70
70
pinMode (LED_BUILTIN, OUTPUT);
71
- timer = timerBegin (0 , 40000 , true ); // 40MHz Xtal clock
71
+ timer = timerBegin (0 , 40000 , true ); // 40MHz Xtal clock
72
72
timerAttachInterrupt (timer, &onTimer0, true );
73
- timerAlarmWrite (timer, 120000 , true ); // 120000 = 60 seconds
73
+ timerAlarmWrite (timer, 12000 , true ); // 120000 = 6 seconds
74
74
timerAlarmEnable (timer);
75
75
CAN_cfg.speed = CAN_SPEED_250KBPS;
76
76
CAN_cfg.tx_pin_id = GPIO_NUM_5;
@@ -89,7 +89,7 @@ void setup(void) {
89
89
p_filter.ACR2 = 0 ;
90
90
p_filter.ACR3 = 0 ;
91
91
92
- p_filter.AMR0 = 0x5FF ;
92
+ p_filter.AMR0 = 0xFF ;
93
93
p_filter.AMR1 = 0xFF ;
94
94
p_filter.AMR2 = 0xFF ;
95
95
p_filter.AMR3 = 0xFF ;
@@ -101,8 +101,7 @@ void setup(void) {
101
101
Serial.println (" \n ESP_DoubleResetDetector" );
102
102
drd = new DoubleResetDetector (DRD_TIMEOUT, DRD_ADDRESS);
103
103
104
- if (drd->detectDoubleReset ())
105
- {
104
+ if (drd->detectDoubleReset ()) {
106
105
Serial.println (" Double Reset Detected" );
107
106
digitalWrite (LED_BUILTIN, LED_ON);
108
107
// SerialBT.end();
@@ -124,12 +123,10 @@ void setup(void) {
124
123
request->send (200 , " text/plain" , " Hi! I am ESP32 Interface for CPX Motorcycle. \n Firmware: " __FILE__ " \n Compiled: " __DATE__ " " __TIME__);
125
124
});
126
125
127
- AsyncElegantOTA.begin (&server); // Start ElegantOTA
126
+ AsyncElegantOTA.begin (&server); // Start ElegantOTA
128
127
server.begin ();
129
128
Serial.println (" HTTP server started" );
130
- }
131
- else
132
- {
129
+ } else {
133
130
Serial.println (" No Double Reset Detected" );
134
131
digitalWrite (LED_BUILTIN, LED_OFF);
135
132
WiFi.mode (WIFI_OFF);
@@ -138,24 +135,17 @@ void setup(void) {
138
135
SerialBT.begin (" ESP32_CPX" );
139
136
Serial.println (" The device started, now you can pair it with bluetooth!" );
140
137
141
-
142
138
pinMode (VOLTAGE_PIN, INPUT);
143
139
Serial.print (" Compile timestamp: " );
144
140
Serial.println (compile_date);
145
- voltage = ((analogRead (VOLTAGE_PIN)) * 0.00174 );
146
- Serial.print (" Power Supply Voltage: " ); Serial.print (voltage, 3 ); Serial.println (" V" );
141
+ voltage = ((analogRead (VOLTAGE_PIN)) * 0.00174 );
142
+ Serial.print (" Power Supply Voltage: " );
143
+ Serial.print (voltage, 3 );
144
+ Serial.println (" V" );
147
145
Serial.print (" Internal Core Temperature: " );
148
146
Serial.print ((temprature_sens_read () - 32 ) / 1.8 );
149
147
Serial.println (" ºC" );
150
148
delay (4000 );
151
- SerialBT.print (" Compile timestamp: " );
152
- SerialBT.println (compile_date);
153
- SerialBT.print (" Power Supply Voltage: " );
154
- SerialBT.println (voltage, 3 );
155
- SerialBT.print (" Internal Core Temperature: " );
156
- SerialBT.print ((temprature_sens_read () - 32 ) / 1.8 );
157
- SerialBT.println (" C" );
158
- SerialBT.write (0x0D );
159
149
}
160
150
161
151
void loop (void ) {
@@ -165,51 +155,58 @@ void loop(void) {
165
155
166
156
if (rx_frame.FIR .B .FF == CAN_frame_std) {
167
157
// printf("New standard frame");
168
- }
169
- else {
170
- printf (" New extended frame" );
158
+ } else {
159
+ // printf("New extended frame");
171
160
}
172
161
173
162
if (rx_frame.FIR .B .RTR == CAN_RTR) {
174
- printf (" RTR from 0x%04X, DLC %d\r\n " , rx_frame.MsgID , rx_frame.FIR .B .DLC );
175
- }
176
- else {
177
- printf (" from 0x%04X, DLC %d, Data " , rx_frame.MsgID , rx_frame.FIR .B .DLC );
178
- SerialBT.write ((rx_frame.MsgID >> 8 ) & 255 );
179
- SerialBT.write (rx_frame.MsgID & 255 );
180
- delay (1 );
181
- SerialBT.write (0x88 );
182
- for (int i = 0 ; i < rx_frame.FIR .B .DLC ; i++) {
183
- printf (" 0x%02X " , rx_frame.data .u8 [i]);
184
- SerialBT.write (rx_frame.data .u8 [i]);
163
+ // printf(" RTR from 0x%04X, DLC %d\r\n", rx_frame.MsgID, rx_frame.FIR.B.DLC);
164
+ } else {
165
+ if ((0x500 != rx_frame.MsgID ) && (0x501 != rx_frame.MsgID ) && (0x503 != rx_frame.MsgID ) && (0x510 != rx_frame.MsgID )) {
166
+ // printf(" from 0x%04X, DLC %d, Data ", rx_frame.MsgID, rx_frame.FIR.B.DLC);
167
+ SerialBT.write ((rx_frame.MsgID >> 8 ) & 255 );
168
+ delay (5 );
169
+ SerialBT.write (rx_frame.MsgID & 255 );
170
+ delay (20 );
171
+ SerialBT.write (0x88 );
172
+ delay (20 );
173
+ for (int i = 0 ; i < rx_frame.FIR .B .DLC ; i++) {
174
+ // printf("0x%02X ", rx_frame.data.u8[i]);
175
+ SerialBT.write (rx_frame.data .u8 [i]);
176
+ }
177
+ // printf("\n");
178
+ delay (20 );
179
+ SerialBT.write (0x0D );
180
+ delay (20 );
185
181
}
186
- printf (" \n " );
187
- delay (1 );
188
- SerialBT.write (0x0D );
189
182
}
190
- // delay(70);
191
- // respond to sender
192
- // ESP32Can.CANWriteFrame(&rx_frame);
183
+ delay (70 );
193
184
}
194
185
drd->loop ();
195
- if (interruptCounter > 9 ) { // 10 minutes pass
186
+ if (interruptCounter > 19 ) { // 120 secs pass
196
187
portENTER_CRITICAL (&timerMux);
197
188
interruptCounter = 0 ;
198
189
portEXIT_CRITICAL (&timerMux);
199
- voltage = ((analogRead (VOLTAGE_PIN)) * 0.00174 );
200
- if (voltage <= 4.0 || voltage >= 5.8 ) {
201
- SerialBT.print (" Power Supply Voltage: " );
202
- SerialBT.println (voltage, 3 );
203
- Serial.println (voltage, 3 );
204
- SerialBT.write (0x0D );
205
- }
190
+ voltage = ((analogRead (VOLTAGE_PIN)) * 0.00174 );
191
+ unsigned int n = voltage * 100 ;
192
+ byte V1 = n >> 8 ;
193
+ byte V2 = n & 0xFF ;
206
194
temp = ((temprature_sens_read () - 32 ) / 1.8 );
207
- if (temp >= 70.0 ) {
208
- SerialBT.print (" Internal Core Temperature: " );
209
- SerialBT.print (temp);
210
- Serial.println (temp);
211
- SerialBT.println (" C" );
212
- SerialBT.write (0x0D );
213
- }
195
+ unsigned int t = temp * 10 ;
196
+ byte T1 = t >> 8 ;
197
+ byte T2 = t & 0xFF ;
198
+ SerialBT.write (0x0D );
199
+ SerialBT.write (0x05 );
200
+ SerialBT.write (0x50 );
201
+ SerialBT.write (0x88 );
202
+ SerialBT.write (T2);
203
+ SerialBT.write (T1);
204
+ SerialBT.write (0x0 );
205
+ SerialBT.write (0x0 );
206
+ SerialBT.write (0x0 );
207
+ SerialBT.write (V2);
208
+ SerialBT.write (V1);
209
+ SerialBT.write (0x0 );
210
+ SerialBT.write (0x0D );
214
211
}
215
212
}
0 commit comments