Hi, I am using a Particle Electron to send and receive variables to and from Ubidots. I have tested sending and receiving data in separate firmware however when I attempt to combine sending and receiving in a single piece of code I get a SOS assertion failure. If someone could assist that would be very much appreciated as I have tried my best to solve the issue, here is my code:
#include <Ubidots.h>
#include <math.h>
#ifndef TOKEN
#define TOKEN "..."
#endif
#ifndef DEVICE_LABEL
#define DEVICE_LABEL "..."
#endif
#ifndef VAR_ID_1
#define VAR_ID_1 "..." // motorOn
#endif
CANChannel can(CAN_D1_D2);
Ubidots ubidots(TOKEN);
//Motor Controller Variables
bool motor1F = 0;
bool motor2F = 0;
bool motor3F = 0;
float motorRpm = 0;
float motorCrnt = 0;
float motorFlt = 0;
float contrlT = 0;
float motorT = 0;
float motorFltSub = 0;
float motorOn = 0;
// Other variables
int lastPublishL = 0;
int lastPublishS = 0;
void setup() {
can.begin(250000);
}
void loop() {
CANMessage message;
if(can.available() > 0){
can.receive(message);
if (message.id == 0x130){ //Motor Controller 1
motorRpm = canToValue(message.data[0], message.data[1], 1, 0);
motorCrnt = canToValue(message.data[6], message.data[7], 0.1, 0);
motor1F = 1;
}
if (message.id == 0x140){ //Motor Controller 2
motorFlt = canToValue1(message.data[6], 1, 0);
motor2F = 2;
}
if (message.id == 0x150){ //Motor Controller 3
contrlT = canToValue1(message.data[0], 1, -50);
motorT = canToValue1(message.data[1], 1, -50);
motorFltSub = canToValue(message.data[3], message.data[4], 1, 0);
motor3F = 1;
}
}
//Create ubidots variables - 10s upload period
if (millis()-lastPublishS > 10000) {
lastPublishS = millis();
//Motor variables - Transmit only if CAN message received
if (motor1F == 1){
ubidots.add("RPM", motorRpm);
ubidots.add("Motor Current", motorCrnt);
ubidots.sendAll();
motor1F = 0;
}
if (motor2F == 1){
ubidots.add("Motor fault code", motorFlt);
ubidots.sendAll();
motor2F = 0;
}
if (motor3F == 1){
ubidots.add("Controller temp", contrlT);
ubidots.add("Motor temp", motorT);
ubidots.add("Motor fault subcode", motorFltSub);
ubidots.sendAll();
motor3F = 0;
}
motorOn = ubidots.getValue(VAR_ID_1);
CANMessage control;
control.extended = true;
control.len = 1;
control.id = 0x160;
control.data[0] = motorOn;
can.transmit(control);
}
}
// Convert 2 byte data to a decimal value
float canToValue (int dec1, int dec2, float dis, int off) {
int i = floor(dec1/16);
int j = dec1 - (16*i);
int k = floor(dec2/16);
int l = dec2 - (16*k);
float value = ((i*4096 + j*256 + k*16 + l)*dis)+off;
return value;
}
// Convert 1 byte data to a decimal value
float canToValue1 (int dec1, float dis, int off) {
float value = (dec1*dis)+off;
return value;
}