Commit 6d3d0170 by Tref

update with better structure

parent f11975ed
Java project requires Java11. <br/>
ESP32 is built for WEMOS minikit.
...@@ -4,10 +4,11 @@ ...@@ -4,10 +4,11 @@
#include <Arduino.h> #include <Arduino.h>
#define PORT 1883 #define PORT 1883
#define LED 2
const char* WIFI_SSID = "Telia-9E1E41"; const char* WIFI_SSID = "Telia-9E1E41";
const char* WIFI_PASS = "XPC47UPGRT"; const char* WIFI_PASS = "XPC47UPGRT";
const char* server = "192.168.1.188"; const char* server = "192.168.1.188";
long lastMsg = 0;
WiFiClient client; WiFiClient client;
PubSubClient mqttClient(client); PubSubClient mqttClient(client);
void setup() { void setup() {
...@@ -18,29 +19,49 @@ void setup() { ...@@ -18,29 +19,49 @@ void setup() {
{ {
delay(3000); delay(3000);
} }
pinMode(LED, OUTPUT);
mqttClient.setServer(server, PORT); mqttClient.setServer(server, PORT);
// put your setup code here, to run once: // put your setup code here, to run once:
delay(5000); delay(5000);
while(1){ while(1){
if(mqttClient.connect("ardu")){ if (mqttClient.connect("ardu")) {
break; break;
} else {
delay(1000);
} }
} }
mqttClient.setCallback(subscribeReceive); mqttClient.setCallback(subscribeReceive);
mqttClient.subscribe("/master/app/server/diagnostics/browser/blockreading");
mqttClient.subscribe("/master/app/server/diagnostics/browser/errorreading");
} }
void loop() { void loop() {
// put your main code here, to run repeatedly: if (!mqttClient.connected()) {
while (Serial.available()) { reconnect();
String s = Serial.readString(); }
int n = s.length(); long now = millis();
char c[n + 1]; if (now - lastMsg > 2000) {
strcpy(c, s.c_str()); mqttClient.loop();
switch(c[0]){ lastMsg = now;
case 'p': }
mqttClient.publish("/master/app/server/diagnostics/ecu/errorreading", c); }
Serial.write("done");
break; void reconnect() {
// Loop until we're reconnected
while (!mqttClient.connected()) {
// Attempt to connect
if (mqttClient.connect("ardu")) {
mqttClient.subscribe("/master/app/server/diagnostics/browser/blockreading");
mqttClient.subscribe("/master/app/server/diagnostics/browser/errorreading");
} else {
delay(500);
} }
} }
} }
...@@ -55,9 +76,118 @@ void MQTTdataParser(byte* payload, char str[], unsigned int length){ ...@@ -55,9 +76,118 @@ void MQTTdataParser(byte* payload, char str[], unsigned int length){
void subscribeReceive(char* topic, byte* payload, unsigned int length) void subscribeReceive(char* topic, byte* payload, unsigned int length)
{ {
char str[length+1]; char str[length+1];
MQTTdataParser(payload, str, length); MQTTdataParser(payload, str, length);
if(strcmp(topic, "/master/app/server/diagnostics/user/grpreading")){ long value = strtol(str, NULL, 10);
//Serial.print(value);
//Serial.print(topic);
if(strcmp(topic, "/master/app/server/diagnostics/browser/blockreading") == 0){
grpReading((int)value);
}else if(strcmp(topic, "/master/app/server/diagnostics/browser/errorreading") == 0){
value = strtol("203", NULL, 10);
errorReading((int)value);
}
}
void errorReading(int block){
int i = 0;
Serial.write(block);
while (!Serial.available()) {
i++;
if(i == 9999999)
return;
}
delay(10000);
int buf = Serial.read();
if (buf == 'D'){
char c[256];
i = 0;
while (Serial.available()) {
c[i] = Serial.read();
i++;
}
//c[i-1] = '\0';
mqttClient.publish("/master/app/server/diagnostics/ecu/errorreading", c);
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
buf = Serial.read();
} }
} }
void grpReading(int block){
Serial.write(block);
long i = 0;
while (!Serial.available()) {
i++;
if(i == 9999999)
return;
}
delay(10000);
int buf = Serial.read();
if (buf == 'G'){
int dat[12] = {0};
char h[5];
int i = 0;
int flag1 = 0;
int k = 0;
//Serial.print("\n");
do{
if(Serial.available()) {
delay(5);
buf = Serial.read();
//Serial.write(buf);
if(buf != ';'){
flag1 = 0;
//dat[i] += (buf)*k;
h[k] = buf;
//Serial.write(dat[i]);
//Serial.print(" ");
k++;
}else if(!flag1 && buf == ';'){
//Serial.print("\n");
dat[i] = atoi(h);
h[0] = '\0';
h[1] = '\0';
h[2] = '\0';
h[3] = '\0';
h[4] = '\0';
i++;
flag1 = 1;
k = 0;
}else if(flag1 && buf == ';'){
//Serial.print("\n");
//Serial.print("\n");
break;
}
}
}while(i<12);
char str[256] = "\0";
char temp[5];
int j = 0;
char ix = i + '0';
//Serial.print(ix);
//Serial.print("\n");
for(;j<i;j++){
sprintf(temp, "%d;", dat[j]);
//itoa(dat[j], temp, 10);
strcat(str, temp);
//strcat(str, ";");
}
mqttClient.publish("/master/app/server/diagnostics/ecu/blockreading", str);
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED, LOW);
delay(500);
buf = Serial.read();
//Serial.write(str);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment