CAN-BUS shield send data problem

Hi all,



I have a seeed canbus shield v2.0 attached on an arduino uno.

My project is to send several data from sensors (analog/digital/i2c) from arduino to an external ECU via can bus protocol.



At the moment I have managed to connect the shield using can L & can H and declare an identifier as 0 (zero) running a send example from shield and I can see that there is a communication between the two devices, which is good.

Although, I cannot send the correct data.

To make it easier I have just loaded a temp/hum DHT11 sketch merged with some code lines of the shield’s send example.



Any idea how to declare and send the temperature or humidity via can bus? I have tried several combinations but still nothing.

I don’t have any experience on can bus, so any comment would be helpful.

You can find attached the code.



Thanks

[code]#include “DHT.h”

#define DHTPIN 2 // what pin we’re connected to

// Uncomment whatever type you’re using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

DHT dht(DHTPIN, DHTTYPE);

#include <mcp_can.h>
#include <SPI.h>

// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;

MCP_CAN CAN(SPI_CS_PIN); // Set CS pin

void setup()
{
Serial.begin(115200);
Serial.println(“DHTxx test!”);

dht.begin();

while (CAN_OK != CAN.begin(CAN_500KBPS))              // init can bus : baudrate = 500k
{
    Serial.println("CAN BUS Shield init fail");
    Serial.println(" Init CAN BUS Shield again");
    delay(100);
}
Serial.println("CAN BUS Shield init ok!");

}

unsigned char smtp[8] = {0,1,2,3,4,5,6,7};
void loop()
{
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)
float h = dht.readHumidity();
float t = dht.readTemperature();

// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) 
{
    Serial.println("Failed to read from DHT");
} 
else 
{
    Serial.print("Humidity: "); 
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: "); 
    Serial.print(t);
    Serial.println(" *C");
}

CAN.sendMsgBuf(0, 0, 8, smtp);
delay (10);

}[/code]
canbus_test.png

First, I suggest you read the pinmap and find the free pins, and use them for your sensors. If example sketches of those sensors use occupied pins, there would be something wrong.



Secondly, you need to learn some basic knowledge about data types and how to organize float data into an unsigned char array. Also, you need to learn the basics knowledge of arrays. You can find them in any introductory programming book.

Good luck!

Hi,



Like bestlux said the pin 2 of board is not free pin so you cannot use pin 2 for DHT sensor. So you need to change this line of code:

</s><i> </i>#define DHTPIN 2 // what pin we're connected to <e>

Second thing temperature and humidity data is in float to first you need to covert float value to char Array. Try this solution.

</s><i> </i>float temperature = 25; char smtp[30]; String temp = String(temperature); temp.toCharArray(buf , temp.length()+1); <e>

I hope it will help you.