CAN SENDER CODE
#include <SPI.h> //Library for using SPI Communication
#include <mcp2515.h> //Library for using CAN Communication (GitHub - autowp/arduino-mcp2515: Arduino MCP2515 CAN interface library) //Library for using DHT sensor
#include <Wire.h>
//#include <LiquidCrystal_I2C.h>
//#include <SPI.h>
struct can_frame canMsg;
int var1;
int var2;
MCP2515 mcp2515(10);
float m=-0.3376;//slope
float b=0.7165;//y intercept
float R0=2.82;//SENSOR RESISTANCE IN FRESH AIR
float m1 = -0.6527; //Slope
float b1 = 1.30; //Y-Intercept
float R01 = 7.22; //Sensor Resistance in fresh air
double ppm_1;
double ppm_2;
void setup()
{
while (!Serial);
Serial.begin(9600);
SPI.begin(); //Begins SPI communication
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
mcp2515.setNormalMode();
canMsg.can_id = 0x036; //CAN id as 0x036
canMsg.can_dlc = 8; //CAN data length as 8
}
void loop()
{
float sensor_volt_1; //Define variable for sensor voltage
float RS_gas_1; //Define variable for sensor resistance
float ratio_1; //Define variable for ratio
float sensor_volt_2; //Define variable for sensor voltage
float RS_gas_2; //Define variable for sensor resistance
float ratio_2; //Define variable for ratio
var1=analogRead(A0);
var2=analogRead(A1);
//Just for conversion
Serial.print("GAS SENSOR RAW VALUE OF MQ135 = ");
Serial.println(var1);
sensor_volt_1 = var1*(5.0/1023.0); //Convert analog values to voltage
Serial.print("Sensor value in volts = ");
Serial.println(sensor_volt_1);
RS_gas_1 = ((5.0*10.0)/sensor_volt_1)-10.0; //Get value of RS in a gas
Serial.print("Rs value = ");
Serial.println(RS_gas_1);
ratio_1 = RS_gas_1/R0; // Get ratio RS_gas/RS_air
Serial.print("Ratio = ");
Serial.println(ratio_1);
double ppm_log_1 = (log10(ratio_1)-b)/m; //Get ppm value in linear scale according to the the ratio value
ppm_1 = pow(10, ppm_log_1); //Convert ppm value to log scale
Serial.print("Our desired PPM for MQ135 = ");
Serial.println(ppm_1);
Serial.print("GAS SENSOR RAW VALUE OF MQ7 = ");
Serial.println(var2);
sensor_volt_2 = var2*(5.0/1023.0); //Convert analog values to voltage
Serial.print("Sensor value in volts = ");
Serial.println(sensor_volt_2);
RS_gas_2 = ((5.0*10.0)/sensor_volt_2)-10.0; //Get value of RS in a gas
Serial.print("Rs value = ");
Serial.println(RS_gas_2);
ratio_2 = RS_gas_2/R01; // Get ratio RS_gas/RS_air
Serial.print("Ratio = ");
Serial.println(ratio_2);
double ppm_log_2 = (log10(ratio_2)-b1)/m1; //Get ppm value in linear scale according to the the ratio value
ppm_2 = pow(10, ppm_log_2); //Convert ppm value to log scale
Serial.print("Our desired PPM for MQ7 = ");
Serial.println(ppm_2);
//canMsg.can_id = 0x036; //CAN id as 0x036
//canMsg.can_dlc = 8; //CAN data length as 8
canMsg.data[0] = ppm_1; //Update humidity value in [0]
canMsg.data[1] = ppm_2; //Update temperature value in [1]
canMsg.data[2] = 0x00; //Rest all with 0
canMsg.data[3] = 0x00;
canMsg.data[4] = 0x00;
canMsg.data[5] = 0x00;
canMsg.data[6] = 0x00;
canMsg.data[7] = 0x00;
if (mcp2515.sendMessage(&canMsg) == MCP2515::ERROR_OK) {
Serial.println("Messages sent");
}
else
{
Serial.println("Msg1 TX error");
}
}
**CAN RECEIVER CODE**
#include <SPI.h> //Library for using SPI Communication
#include <mcp2515.h> //Library for using CAN Communication (https://github.com/autowp/arduino-mcp2515/)
#include <Wire.h>
//#include <LiquidCrystal_I2C.h>
//LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x3F for a 16 chars and 2 line display
struct can_frame canMsg;
MCP2515 mcp2515(10); // SPI CS Pin 10
void setup()
{
Serial.begin(115200); //Begins Serial Communication at 9600 baudrate
SPI.begin(); //Begins SPI communication
Serial.println("CAN BUS STARTING");
mcp2515.reset();
mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz
mcp2515.setNormalMode();//Sets CAN at normal mode
//Serial.println("CAN WORKING");
}
void loop()
{
Serial.println("Inside Msg Loop");
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) // To receive data (Poll Read)
{
int x = canMsg.data[0];
int y = canMsg.data[1];
Serial.println("Messages Received");
Serial.print("MQ135 CO2: "); //Display Temp & Humidity value received at 16x2 LCD
Serial.print(x);
Serial.println("PPM");
Serial.print("MQ7 CO: "); //Display Temp & Humidity value received at 16x2 LCD
Serial.print(y);
Serial.println("PPM");
}
else{
Serial.println("Messages not Received");
}
}
MCP2515 at the transmitter side is able to send msg.But the issue is in receiver CAN where on powering up the control is not going to polling statement mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK.I am getting Messages not received at the output.
For further info,i have connected
Vcc to 5V,GND to GND,CS to D10,SO to D12,SI to D11,SCK to D13,INT to D2.
Arduino board which holds CAN Tx is connected to external power supply and Arduino board which holds CAN Rx is connected using USB flashing Cable.
Please to respond as soon as possible