-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCanBus_SendForArduino.ino
100 lines (83 loc) · 2.22 KB
/
CanBus_SendForArduino.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// demo: CAN-BUS Shield, send data
#include <mcp_can.h>
#include <SPI.h>
#include "Seeed_SHT35.h"
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SERIAL SerialUSB
#else
#define SERIAL Serial
#endif
// 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;
/*SAMD core*/
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SDAPIN 20
#define SCLPIN 21
#define RSTPIN 7
#else
#define SDAPIN A4
#define SCLPIN A5
#define RSTPIN 2
#endif
MCP_CAN CAN(SPI_CS_PIN); // Set CS pin
SHT35 sensor(SCLPIN);
void setup()
{
SERIAL.begin(115200);
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!");
if(sensor.init())
{
SERIAL.println("sensor init failed!!!");
}
}
unsigned char stmp[8] = {0, 0, 0, 0, 0, 0, 0, 0};
void loop()
{
float temp,hum;
int light = analogRead(A0);
light = light/(1024.0) * 100.0;
if(NO_ERROR!=sensor.read_meas_data_single_shot(HIGH_REP_WITH_STRCH,&temp,&hum))
{
SERIAL.println("read temp failed!!");
SERIAL.println(" ");
SERIAL.println(" ");
SERIAL.println(" ");
}
else
{
// send data: id = 0x00, standrad frame, data len = 8, stmp: data buf
stmp[7] = (u8)temp;
stmp[6] = 0;
stmp[5] = (u8)hum;
stmp[4] = 0;
stmp[3] = (u8)light;
stmp[2] = 0;
stmp[1] = random(30);
stmp[0] = 0;
CAN.sendMsgBuf(0x00, 0, 8, stmp);
SERIAL.println("read data :");
SERIAL.print("temperature = ");
SERIAL.print(temp);
SERIAL.println(" ℃ ");
SERIAL.print("humidity = ");
SERIAL.print(hum);
SERIAL.println(" % ");
SERIAL.print("light = ");
SERIAL.print(light);
SERIAL.println(" % ");
SERIAL.println(" ");
SERIAL.println(" ");
SERIAL.println(" ");
}
delay(100); // send data per 100ms
}
// END FILE