An instrument at work outputs data on CANbus. I used an MCP2515 CAN Bus Module TJA1050 Receiver to decode enough to get the data I want to log.
The mcp_can library reader program gives result like this
ID: 413 DLC: 8 Data: 00 05 21 33 00 00 B2 A2
The 8 different IDs are 4 parameters (TMP, CO2, HUM and RPM) and both (s)et and (m)easure. The values are in Data: byte 2 and 3.
In order to transfer the data to a server for logging, I use a node MCU Esp8266 with WiFi and MQTT. I don't understand why the ID parameters are now different but the DLC and Data are the same.
_______ ESP8266 UNO
RPMm 0x064 0x432
RPMs 0x062 0x431
CO2m 0x046 0x423
CO2s 0x042 0x421
HUMm 0x026 0x413
HUMs 0x022 0x411
TMPm 0x006 0x403
TMPs 0x002 0x401
I'm using a level shifter to connect the 5V MCP2515 to the 3.3V Node MCU
I also tried an Inno-maker USB2CAN device and it also gave IDs like the UNO.
This isn't a problem- I just want to understand what is causing the change.
For reference the NodeMCU code is this
/*
ESP8266 CANbus MQTT WiFi
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <mcp_can.h>
#include <SPI.h>
// Update these with values suitable for your network.
const char* ssid = "mySSID";
const char* password = "mypassword";
// MQTT Broker settings
const char* mqtt_server = "10.10.101.56";
const char *mqtt_topic = "Kuhner/R2"; // MQTT topic
const char *mqtt_username = "esp8266"; // MQTT username for authentication
const char *mqtt_password = "kuhner"; // MQTT password for authentication
const int mqtt_port = 1883; // MQTT port (TCP)
WiFiClient espClient;
PubSubClient client(espClient);
// CAN bus pins settings
#define CAN_CS D4 // D4 used for NodeMCU - was pin GPIO15 (D8)
//#define CAN_INT 2 // Interrupt pin not used
MCP_CAN CAN(CAN_CS); // Initialize the CAN bus object
//handling of CAN data
long unsigned int rxId;
unsigned char len = 0;
unsigned char rxBuf[8];
#define MSG_BUFFER_SIZE (120)
char msg[MSG_BUFFER_SIZE];
unsigned long lastMsg = 0;
int value = 0;
float RPMm ;
float RPMs ;
float CO2m ;
float CO2s ;
float HUMm ;
float HUMs ;
float TMPm ;
float TMPs ;
char RPMm_msg[20] ;
char RPMs_msg[20] ;
char CO2m_msg[20] ;
char CO2s_msg[20] ;
char HUMm_msg[20] ;
char HUMs_msg[20] ;
char TMPm_msg[20] ;
char TMPs_msg[20] ;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected. IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until MQTT reconnected
//randomSeed(micros());
String clientId = "ESP8266Client-";
clientId += String(random(0xFFFF), HEX);
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(clientId.c_str(), mqtt_username, mqtt_password)) {
Serial.println("MQTT connected");
client.publish("Kuhner/R2", "Connected"); // Once connected, publish an announcement...
client.subscribe("inTopic"); // ... and resubscribe
}
else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000); // Wait 5 seconds before retrying
}
}
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
// Initialize CAN bus
if (CAN.begin(MCP_ANY, CAN_20KBPS, MCP_8MHZ) == CAN_OK) {
Serial.println("CAN bus initialized successfully!");
}
else {
Serial.println("Error initializing CAN bus...");
}
CAN.setMode(MCP_NORMAL); // Change to normal mode to allow messages to be transmitted
}
void loop() {
if (!client.connected()) {
reconnect();
}
//if(!digitalRead(CAN_INT)) { // If interrupt pin is low, read receive buffer
if (CAN.checkReceive() == CAN_MSGAVAIL) { // Check if there is a new message available
CAN.readMsgBuf(&rxId, &len, rxBuf); // Read data: len = data length, buf = data byte(s)
unsigned char byte2 = rxBuf[2];
unsigned char byte3 = rxBuf[3];
//uint16_t combinedValue = (uint16_t)(byte2 << 8) | byte3 ;
double combinedValue = (double)((byte2 << 8) | byte3)/100 ;
//unsigned char combinedValue = (uint16_t)(byte2 << 8) | byte3 ;
if((rxId & 0x80000000) != 0x80000000) { // Determine if ID is standard (11 bits) ignore extended frames
if(len == 8) {
switch (rxId) {
case 0x064: {
if (RPMm != combinedValue) {
RPMm = combinedValue;
Serial.print("RPM meas: ");
Serial.println(combinedValue * 10);
dtostrf(RPMm, 3, 2, RPMm_msg);
}}
break;
case 0x062 : {
if (RPMs != combinedValue) {
RPMs = combinedValue;
Serial.print("RPM set: ");
Serial.println(combinedValue * 10 );
dtostrf(RPMs, 3, 2, RPMs_msg);
}}
break;
case 0x046 : {
if (CO2m != combinedValue) {
CO2m = combinedValue;
Serial.print("CO2 meas: ");
Serial.println(combinedValue);
dtostrf(CO2m, 3, 2, CO2m_msg);
}}
break;
case 0x042 : {
if (CO2s != combinedValue) {
CO2s = combinedValue;
Serial.print("CO2 set: ");
Serial.println(combinedValue);
dtostrf(CO2s, 3, 2, CO2s_msg);
}}
break;
case 0x026 : {
if (HUMm != combinedValue) {
HUMm = combinedValue;
Serial.print("Humid meas: ");
Serial.println(combinedValue);
dtostrf(HUMm, 3, 2, HUMm_msg);
}}
break;
case 0x022 : {
if (HUMs != combinedValue) {
HUMs = combinedValue;
Serial.print("Humid set: ");
Serial.println(combinedValue);
dtostrf(HUMs, 3, 2, HUMs_msg);
}}
break;
case 0x006 : {
if (TMPm != combinedValue) {
TMPm = combinedValue;
Serial.print("Temp meas: ");
Serial.println(combinedValue);
dtostrf(TMPm, 3, 2, TMPm_msg);
}}
break;
case 0x002 : {
if (TMPs != combinedValue) {
TMPs = combinedValue;
Serial.print("Temp set: ");
Serial.println(combinedValue);
dtostrf(TMPs, 3, 2, TMPs_msg);
}}
break;
}
}
}
//refresh MQTT connection and transact pub sub messages
unsigned long now = millis();
if (now - lastMsg > 120000) {
lastMsg = now;
++value;
client.publish("Kuhner/R2-RPMm", RPMm_msg);
client.publish("Kuhner/R2-RPMs", RPMs_msg);
client.publish("Kuhner/R2-CO2m", CO2m_msg);
client.publish("Kuhner/R2-CO2s", CO2s_msg);
client.publish("Kuhner/R2-HUMm", HUMm_msg);
client.publish("Kuhner/R2-HUMs", HUMs_msg);
client.publish("Kuhner/R2-TMPm", TMPm_msg);
client.publish("Kuhner/R2-TMPs", TMPs_msg);
client.loop();
}
}}
