Skip to content

Commit 63a5b17

Browse files
authored
Add files via upload
1 parent 706bb4f commit 63a5b17

File tree

1 file changed

+67
-70
lines changed

1 file changed

+67
-70
lines changed
Lines changed: 67 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
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
55

6-
#define DOUBLERESETDETECTOR_DEBUG false //false
6+
#define DOUBLERESETDETECTOR_DEBUG false //false
77

8-
#include <ESP_DoubleResetDetector.h> //https://github.com/khoih-prog/ESP_DoubleResetDetector
8+
#include <ESP_DoubleResetDetector.h> //https://github.com/khoih-prog/ESP_DoubleResetDetector
99
#include <Arduino.h>
1010
#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
1515
#include <CAN_config.h>
1616

1717
// Number of seconds after reset during which a
@@ -32,7 +32,7 @@ uint8_t temprature_sens_read();
3232
float temp = 0.0;
3333

3434
volatile int interruptCounter;
35-
hw_timer_t * timer = NULL;
35+
hw_timer_t* timer = NULL;
3636
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
3737

3838
void IRAM_ATTR onTimer0() {
@@ -42,11 +42,11 @@ void IRAM_ATTR onTimer0() {
4242
}
4343

4444
#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
4646
#endif
4747

48-
#define LED_OFF LOW
49-
#define LED_ON HIGH
48+
#define LED_OFF LOW
49+
#define LED_ON HIGH
5050

5151
#define VOLTAGE_PIN 36
5252
float voltage = 0.0;
@@ -62,15 +62,15 @@ AsyncWebServer server(80);
6262

6363
BluetoothSerial SerialBT;
6464

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
6767

6868
void setup(void) {
6969
Serial.begin(250000);
7070
pinMode(LED_BUILTIN, OUTPUT);
71-
timer = timerBegin(0, 40000, true); // 40MHz Xtal clock
71+
timer = timerBegin(0, 40000, true); // 40MHz Xtal clock
7272
timerAttachInterrupt(timer, &onTimer0, true);
73-
timerAlarmWrite(timer, 120000, true); // 120000 = 60 seconds
73+
timerAlarmWrite(timer, 12000, true); // 120000 = 6 seconds
7474
timerAlarmEnable(timer);
7575
CAN_cfg.speed = CAN_SPEED_250KBPS;
7676
CAN_cfg.tx_pin_id = GPIO_NUM_5;
@@ -89,7 +89,7 @@ void setup(void) {
8989
p_filter.ACR2 = 0;
9090
p_filter.ACR3 = 0;
9191

92-
p_filter.AMR0 = 0x5FF;
92+
p_filter.AMR0 = 0xFF;
9393
p_filter.AMR1 = 0xFF;
9494
p_filter.AMR2 = 0xFF;
9595
p_filter.AMR3 = 0xFF;
@@ -101,8 +101,7 @@ void setup(void) {
101101
Serial.println("\nESP_DoubleResetDetector");
102102
drd = new DoubleResetDetector(DRD_TIMEOUT, DRD_ADDRESS);
103103

104-
if (drd->detectDoubleReset())
105-
{
104+
if (drd->detectDoubleReset()) {
106105
Serial.println("Double Reset Detected");
107106
digitalWrite(LED_BUILTIN, LED_ON);
108107
//SerialBT.end();
@@ -124,12 +123,10 @@ void setup(void) {
124123
request->send(200, "text/plain", "Hi! I am ESP32 Interface for CPX Motorcycle. \nFirmware: " __FILE__ "\nCompiled: " __DATE__ " " __TIME__);
125124
});
126125

127-
AsyncElegantOTA.begin(&server); // Start ElegantOTA
126+
AsyncElegantOTA.begin(&server); // Start ElegantOTA
128127
server.begin();
129128
Serial.println("HTTP server started");
130-
}
131-
else
132-
{
129+
} else {
133130
Serial.println("No Double Reset Detected");
134131
digitalWrite(LED_BUILTIN, LED_OFF);
135132
WiFi.mode(WIFI_OFF);
@@ -138,24 +135,17 @@ void setup(void) {
138135
SerialBT.begin("ESP32_CPX");
139136
Serial.println("The device started, now you can pair it with bluetooth!");
140137

141-
142138
pinMode(VOLTAGE_PIN, INPUT);
143139
Serial.print("Compile timestamp: ");
144140
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");
147145
Serial.print("Internal Core Temperature: ");
148146
Serial.print((temprature_sens_read() - 32) / 1.8);
149147
Serial.println(" ºC");
150148
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);
159149
}
160150

161151
void loop(void) {
@@ -165,51 +155,58 @@ void loop(void) {
165155

166156
if (rx_frame.FIR.B.FF == CAN_frame_std) {
167157
//printf("New standard frame");
168-
}
169-
else {
170-
printf("New extended frame");
158+
} else {
159+
//printf("New extended frame");
171160
}
172161

173162
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);
185181
}
186-
printf("\n");
187-
delay(1);
188-
SerialBT.write(0x0D);
189182
}
190-
//delay(70);
191-
//respond to sender
192-
//ESP32Can.CANWriteFrame(&rx_frame);
183+
delay(70);
193184
}
194185
drd->loop();
195-
if (interruptCounter > 9 ) { // 10 minutes pass
186+
if (interruptCounter > 19) { // 120 secs pass
196187
portENTER_CRITICAL(&timerMux);
197188
interruptCounter = 0;
198189
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;
206194
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);
214211
}
215212
}

0 commit comments

Comments
 (0)