// This #include statement was automatically added by the Particle IDE. #include /* CAN Receive though SPI * This receives messages on the RNET system * */ // This content needs to be in the header of the main program PRODUCT_ID(8286); //This is the number found in the console PRODUCT_VERSION(2); // Increase this number each time something is changed String fwrev = "1.1"; // This is the value checked to determine if an update is required //system_thread enables the dual processing SYSTEM_THREAD(ENABLED); //must call Particle.connect() to connect to cloud SYSTEM_MODE(SEMI_AUTOMATIC); // ------------------------------------------------------------- #include #include // CAN TX Variables unsigned long prevTX = 0; // Variable to store last execution time const unsigned int invlTX = 1000; // One second interval constant byte data[] = {0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8}; // Generic CAN data to send // CAN RX Variables long unsigned int rxId; unsigned char len; unsigned char rxBuf[8]; int bufferstatus; // Serial Output String Buffer char msgString[128]; // CAN0 INT and CS #define CAN0_INT D5 // Set INT to pin D5 MCP_CAN CAN0(A5); // Set CS to pin A5 void setup() { Serial.begin(115200); // CAN is running at 500,000BPS; 115,200BPS is SLOW, not FAST, thus 9600 is crippling. // Initialize MCP2515 running at 20MHz with a baudrate of 125kb/s and the masks and filters disabled. if(CAN0.begin(MCP_ANY, CAN_125KBPS, MCP_20MHZ) == CAN_OK) Serial.println("MCP2515 Initialized Successfully!"); else Serial.println("Error Initializing MCP2515..."); // We can be in LISTENONLY, LOOPBACK and NORMAL mode. Normal mode sends acks back CAN0.setMode(MCP_LISTENONLY); pinMode(CAN0_INT, INPUT_PULLUP); // Configuring pin for /INT input Serial.println("MCP2515 Library receive"); } void loop() { if(!digitalRead(CAN0_INT)) // If CAN0_INT pin is low, read receive buffer //if(CAN0.checkReceive() == CAN_MSGAVAIL) // Another way to see if a message is available outside of the interrupts { bufferstatus = CAN0.mcp2515_readStatus(); if((CAN0.readMsgBuf(&rxId, &len, rxBuf, 1)) == CAN_OK){ // Read data: len = data length, buf = data byte(s) if((rxId & 0x80000000) == 0x80000000) // Determine if ID is standard (11 bits) or extended (29 bits) sprintf(msgString, "Extended ID: 0x%.8lX DLC: %1d Data:", (rxId & 0x1FFFFFFF), len); else sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len); Serial.print(msgString); if((rxId & 0x40000000) == 0x40000000){ // Determine if message is a remote request frame. sprintf(msgString, " REMOTE REQUEST FRAME"); Serial.print(msgString); } else { for(byte i = 0; i= invlTX){ // Send this at a one second interval. prevTX = millis(); byte sndStat = CAN0.sendMsgBuf(0x100, 8, data); if(sndStat == CAN_OK) Serial.println("Message Sent Successfully!"); else Serial.println("Error Sending Message..."); }*/ } } /********************************************************************************************************* END FILE *********************************************************************************************************/