UPDATED: ArduinoMEGA+SeeedStudio CanShield, Sending GPS through CANBUS

trying to get GPS speed into CAN-BUS to create a gauge on my ECU

basically, i am trying to use a GPS module attached to Serial1 which pushes CAN data over the Seeed CANBUS and is picked up by a generic sensor on the ECU.

i am able to send random values and the gauge updates, but i cannot figure out how to obtain GPS MPH speed and throw it into the CAN

this is mostly copy&paste from other projects, it does kinda what i need it to do.
ive never done C++ in my life and i'm learning as i go. so don't lose your mind over the "cleanliness" of my code, but any critique is welcomed.

//GPS
#include <TinyGPSPlus.h>

// CAN receiving
#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg;
MCP2515 mcp2515(9);  // SPI CS Pin 10

// The TinyGPSPlus object
TinyGPSPlus gps;
//int cantxValue = 0;

// megasquirt variables
int MAP, RPM, TPS, CLT, AFR, BATT, BARO, MAT, EGO, EGOC, FUELP, OILP, OILT, KNOCK, ETH, VVT;

// can variables
unsigned long previousMillis = 0;
int delayPeriod = 1000;

void setup() {
  // Serial INIT
  Serial.begin(115200);
  Serial1.begin(115200);
  Serial2.begin(115200);

  //CAN INIT
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS);
  mcp2515.setNormalMode();  
  
//Create CAN Message
  uint8_t data[8];
  float speed = gps.speed.mph();
  uint32_t gpsSpeed = *(uint32_t*)(&speed);       
//CAN Messages for Sensor 4,5 and 6
  canMsg.can_id  = 0x01;
  canMsg.can_dlc = 8;
  canMsg.data[0] = (gps.speed.mph());
  //canMsg.data[1] = &sats;
 
}

void loop() {  

  //
  mcp2515.sendMessage(&canMsg); //Send Data to CAN Bus

while (Serial2.available() > 0) // Read GPS ninfo
    gps.encode(Serial2.read());
  
  if (gps.speed.isUpdated())
  {
    Serial.print(F(" MPH="));
    Serial.println(gps.speed.mph());
  }  
  {


    switch (canMsg.can_id) {
      case 1520:  // group 0 - RPM
        RPM = (float)(word(canMsg.data[6], canMsg.data[7]));
        break;

      case (1520 + 2):  // BARO, MAT, CLT,
        BARO = (float)(word(canMsg.data[0], canMsg.data[1]));
        BARO = (BARO / 10);
        MAP = (float)(word(canMsg.data[2], canMsg.data[3]));
        MAP = (MAP / 10 );
        MAT = (float)(word(canMsg.data[4], canMsg.data[5]));
        MAT = (MAT / 10);
        CLT = (float)(word(canMsg.data[6], canMsg.data[7]));
        CLT = (CLT / 10);
        break;

      case (1520 + 3):  // TPS, BATT, EGO
        TPS = (float)(word(canMsg.data[0], canMsg.data[1]));
        TPS = (TPS / 10);
        BATT = (float)(word(canMsg.data[2], canMsg.data[3]));
        BATT = (BATT / 10);
        EGO = (float)(word(canMsg.data[4], canMsg.data[5]));
        EGO = (EGO / 10);

        break;
      case (1520 + 4):  // EGO Correction / Knock Sensor
        EGOC = (float)(word(canMsg.data[2], canMsg.data[3]));
        EGOC = (EGOC / 10);
        KNOCK = (float)(word(canMsg.data[5], canMsg.data[6]));
        KNOCK = (KNOCK / 10);        

        break;
      case (1520 + 13):  // Fuel Pressure, Oil Pressure, Oil Temperature - Generic Sensors 1,2,3
        FUELP = (float)(word(canMsg.data[0], canMsg.data[1]));
        FUELP = (FUELP / 10);
        OILP = (float)(word(canMsg.data[2], canMsg.data[3]));
        OILP = (OILP / 10);
        OILT = (float)(word(canMsg.data[4], canMsg.data[5]));
        OILT = (OILT / 10);

      case (1520 + 44):  // VVT Angle 1
        VVT = (float)(word(canMsg.data[0], canMsg.data[1]));
        VVT = (VVT / 10);
        break;
      case (1520 + 47):  // Ethanol Content %
        ETH = (float)(word(canMsg.data[0], canMsg.data[1]));
        ETH = (ETH / 10);      
    }

    previousMillis = 0;   // reset no data timer
  } 
  
  {                       // no CAN bus data coming in
    unsigned long currentMillis = millis();
    if (previousMillis == 0) 
    
    {
      previousMillis = currentMillis;                           // entered no data timer
    } else if (currentMillis - previousMillis > delayPeriod) 
    
    {  // no data timer expired
      previousMillis = currentMillis;
      MAP = -999; //Fake data to test gauge even if data is being received
      RPM = -999; //Fake data to test gauge even if data is being received
      TPS = -999; //Fake data to test gauge even if data is being received
      CLT = -999; //Fake data to test gauge even if data is being received
      AFR = -999; //Fake data to test gauge even if data is being received
      BATT = -999; //Fake data to test gauge even if data is being received
      BARO = -999;  //Fake data to test gauge even if data is being received
      MAT = -999; //Fake data to test gauge even if data is being received
      EGO = -999; //Fake data to test gauge even if data is being received
      EGOC = -999;  //Fake data to test gauge even if data is being received
      FUELP = -999;  //Fake data to test gauge even if data is being received
      OILP = -999; //Fake data to test gauge even if data is being received
      OILT = -999; //Fake data to test gauge even if data is being received
      KNOCK = -999;  //Fake data to test gauge even if data is being received
      ETH = -999; //Fake data to test gauge even if data is being received
      VVT = -999; //Fake data to test gauge even if data is being received
    }
  } 

   
    //Serial1.print(F("page: main."));
    Serial1.print(F("rpm.txt=\""));
    Serial1.print(RPM);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);

    Serial1.print(F("mat.txt=\""));
    Serial1.print(MAT);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);

    Serial1.print(F("map.txt=\""));
    Serial1.print(MAP);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);    

    Serial1.print(F("tps.val="));
    Serial1.print(TPS);
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);

    Serial1.print(F("clt.txt=\""));
    Serial1.print(CLT);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);

    Serial1.print(F("afr.txt=\""));
    Serial1.print(EGO);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);

    Serial1.print(F("fuelp.txt=\""));
    Serial1.print(FUELP);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);

    Serial1.print(F("oilp.txt=\""));
    Serial1.print(OILP);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);

    Serial1.print(F("oilt.txt=\""));
    Serial1.print(OILT);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);

    Serial1.print(F("batt.txt=\""));
    Serial1.print(BATT);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);

    Serial1.print(F("knock.txt=\""));
    Serial1.print(KNOCK);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);    

    Serial1.print(F("eth.txt=\""));
    Serial1.print(ETH);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);     

    Serial1.print(F("vvt.txt=\""));
    Serial1.print(VVT);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);   

    Serial1.print(F("mph.txt=\""));
    Serial1.println(gps.speed.mph());
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);     
    
}

You need to know the format, the syntax, for the speed parameter. Likely also some data identifying the message as speed in MPH.
Study the ECU documentation and find out how the CAN message must be formatted.

EDIT: I don't see the code reading the GPS. Please cut out the unnecessary blank lines.

i am an idiot......i will come back with a better drafted quetsion.

No hard feelings. You're welcome.

i updated original post with the correct code i am using now

ok, i was able to get the GPS working, what an idiot...but anyways

i get speed as 0.00 (or whatever speed i am going)
i need to conver that into hex.

the CANBUS accepts data as

//Create CAN Message
  uint8_t data[8];
  float speed = gps.speed.mph();
  uint32_t gpsSpeed = *(uint32_t*)(&speed);       
//CAN Messages for Sensor 4,5 and 6
  canMsg.can_id  = 0x01;
  canMsg.can_dlc = 8;
  canMsg.data[0] = (0x00);

however, in data [0] i need to put the speed, so i need a way to convert the speed given by gps into that hex so can can translate it.

i'm pulling my hair trying to figure out how.

Most likely not. Know that alla numbers are zeroes and ones down in the silicon. Integer, words, float.... alla consists of 0 and 1.
That's annoying to read so we use different ways to show that ant stack of o and 1 instead of binary. There is octal, decimal, hexadecimal, ASCII......
Floats are never printed in hex.

No, You don't! data is an array of uint8-t, also named "byte". The range is o up to 255. Just use can-Msg.data[0] = speed. The compiler will create the code needed to go from float to uint8.

ok, so i did as you mentioned

//DHT Sensor
#include <DHT.h>

//GPS
#include <TinyGPSPlus.h>

// CAN receiving
#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg;
MCP2515 mcp2515(9);  // SPI CS Pin 10

// The TinyGPSPlus object
TinyGPSPlus gps;
//int cantxValue = 0;

//DHT Pins
#define dataPin 22
#define DHTType DHT22
DHT dht = DHT(dataPin, DHTType);

// megasquirt variables
int MAP, RPM, TPS, CLT, AFR, BATT, BARO, MAT, EGO, EGOC, FUELP, OILP, OILT, KNOCK, ETH, VVT;

// can variables
unsigned long previousMillis = 0;
int delayPeriod = 1000;

void setup() {
  // Serial INIT
  Serial.begin(115200);
  Serial1.begin(115200);
  Serial2.begin(115200);
  dht.begin(9600);

  //CAN INIT
  mcp2515.reset();
  mcp2515.setBitrate(CAN_500KBPS);
  mcp2515.setNormalMode();  
  
}

void loop() {  
//DHT
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    if (isnan(h) || isnan(t)) {
        Serial.println("Failed to read from the DHT sensor, check wiring.");
        return;
    }

    Serial.print("Humidity: ");
    Serial.print(h);
    //Print out the Temperature
    Serial.print("% || Temperature: ");
    Serial.print(t);
    Serial.print("°C ");

    //Print new line
    Serial.println();

//Create CAN Message
  uint8_t data[8];
  float speed = gps.speed.mph();

  uint32_t gpsSpeed = *(uint32_t*)(&speed);       
//CAN Messages for Sensor 4,5 and 6
  canMsg.can_id  = 0x01; //ECU ID
  canMsg.can_dlc = 8; // Bits
  canMsg.data[0] = speed; //Sensor 1
  canMsg.data[1] = h; //Sensor 2
  canMsg.data[2] = t;  //Sensor 3
  mcp2515.sendMessage(&canMsg); //Send Data to CAN Bus

while (Serial2.available() > 0) // Read GPS ninfo
    gps.encode(Serial2.read());
  
  if (gps.speed.isUpdated())
  {
    Serial.print(F(" MPH="));
    Serial.println(gps.speed.mph());
  }  
  {


    switch (canMsg.can_id) {
      case 1520:  // group 0 - RPM
        RPM = (float)(word(canMsg.data[6], canMsg.data[7]));
        break;

      case (1520 + 2):  // BARO, MAT, CLT,
        BARO = (float)(word(canMsg.data[0], canMsg.data[1]));
        BARO = (BARO / 10);
        MAP = (float)(word(canMsg.data[2], canMsg.data[3]));
        MAP = (MAP / 10 );
        MAT = (float)(word(canMsg.data[4], canMsg.data[5]));
        MAT = (MAT / 10);
        CLT = (float)(word(canMsg.data[6], canMsg.data[7]));
        CLT = (CLT / 10);
        break;

      case (1520 + 3):  // TPS, BATT, EGO
        TPS = (float)(word(canMsg.data[0], canMsg.data[1]));
        TPS = (TPS / 10);
        BATT = (float)(word(canMsg.data[2], canMsg.data[3]));
        BATT = (BATT / 10);
        EGO = (float)(word(canMsg.data[4], canMsg.data[5]));
        EGO = (EGO / 10);

        break;
      case (1520 + 4):  // EGO Correction / Knock Sensor
        EGOC = (float)(word(canMsg.data[2], canMsg.data[3]));
        EGOC = (EGOC / 10);
        KNOCK = (float)(word(canMsg.data[5], canMsg.data[6]));
        KNOCK = (KNOCK / 10);        

        break;
      case (1520 + 13):  // Fuel Pressure, Oil Pressure, Oil Temperature - Generic Sensors 1,2,3
        FUELP = (float)(word(canMsg.data[0], canMsg.data[1]));
        FUELP = (FUELP / 10);
        OILP = (float)(word(canMsg.data[2], canMsg.data[3]));
        OILP = (OILP / 10);
        OILT = (float)(word(canMsg.data[4], canMsg.data[5]));
        OILT = (OILT / 10);

      case (1520 + 44):  // VVT Angle 1
        VVT = (float)(word(canMsg.data[0], canMsg.data[1]));
        VVT = (VVT / 10);
        break;
      case (1520 + 47):  // Ethanol Content %
        ETH = (float)(word(canMsg.data[0], canMsg.data[1]));
        ETH = (ETH / 10);      
    }

    previousMillis = 0;   // reset no data timer
  } 
  
  {                       // no CAN bus data coming in
    unsigned long currentMillis = millis();
    if (previousMillis == 0) 
    
    {
      previousMillis = currentMillis;                           // entered no data timer
    } else if (currentMillis - previousMillis > delayPeriod) 
    
    {  // no data timer expired
      previousMillis = currentMillis;
      MAP = -999; //Fake data to test gauge even if data is being received
      RPM = -999; //Fake data to test gauge even if data is being received
      TPS = -999; //Fake data to test gauge even if data is being received
      CLT = -999; //Fake data to test gauge even if data is being received
      AFR = -999; //Fake data to test gauge even if data is being received
      BATT = -999; //Fake data to test gauge even if data is being received
      BARO = -999;  //Fake data to test gauge even if data is being received
      MAT = -999; //Fake data to test gauge even if data is being received
      EGO = -999; //Fake data to test gauge even if data is being received
      EGOC = -999;  //Fake data to test gauge even if data is being received
      FUELP = -999;  //Fake data to test gauge even if data is being received
      OILP = -999; //Fake data to test gauge even if data is being received
      OILT = -999; //Fake data to test gauge even if data is being received
      KNOCK = -999;  //Fake data to test gauge even if data is being received
      ETH = -999; //Fake data to test gauge even if data is being received
      VVT = -999; //Fake data to test gauge even if data is being received
    }
  } 

   
    //Serial1.print(F("page: main."));
    Serial1.print(F("rpm.txt=\""));
    Serial1.print(RPM);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);

    Serial1.print(F("mat.txt=\""));
    Serial1.print(MAT);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);

    Serial1.print(F("map.txt=\""));
    Serial1.print(MAP);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);    

    Serial1.print(F("tps.val="));
    Serial1.print(TPS);
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);

    Serial1.print(F("clt.txt=\""));
    Serial1.print(CLT);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);

    Serial1.print(F("afr.txt=\""));
    Serial1.print(EGO);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);

    Serial1.print(F("fuelp.txt=\""));
    Serial1.print(FUELP);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);

    Serial1.print(F("oilp.txt=\""));
    Serial1.print(OILP);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);

    Serial1.print(F("oilt.txt=\""));
    Serial1.print(OILT);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);

    Serial1.print(F("batt.txt=\""));
    Serial1.print(BATT);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);

    Serial1.print(F("knock.txt=\""));
    Serial1.print(KNOCK);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);    

    Serial1.print(F("eth.txt=\""));
    Serial1.print(ETH);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);     

    Serial1.print(F("vvt.txt=\""));
    Serial1.print(VVT);
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);   

    Serial1.print(F("mph.txt=\""));
    Serial1.println(gps.speed.mph());
    Serial1.print(F("\""));
    Serial1.write(0xff); Serial1.write(0xff); Serial1.write(0xff);     
    
}

i have a can usb sniffer and i cna see the cna messages

i added a DHT sensor for humidty and temp and i have a heat gun to change the values of the tmep
however, the CAN message remains the same:

e9 ed f4 00 00 00 00 00

i'm so lost.

it would be nice to even just get the temp value correct...at least it could prove my theory!

i know my values work because i see this in the serial console...

20:25:41.352 -> Humidity: 57.80% || Temperature: 30.00��C 
20:25:41.352 -> Humidity: 57.80% || Temperature: 30.00°C 
20:25:41.386 -> Humidity: 57.80% || Temperature: 30.00°C 
20:25:41.386 -> Humidity: 57.80% || Temperature: 30.00°C 
20:25:41.421 -> Humidity: 57.80% || Temperature: 30.00°C 
20:25:41.421 -> Humidity: 57.80% || Temperature: 30.00°C 
20:25:41.458 -> Humidity: 57.80% || Temperature: 30.00°C 
20:25:41.458 -> Humidity: 57.80% || Temperature: 30.00°C 
20:25:41.458 ->  MPH=0.26
20:25:41.490 -> Humidity: 57.80% || Temperature: 30.00°C 
20:25:41.490 -> Humidity: 57.80% || Temperature: 30.00°C 
20:25:41.524 -> Humidity: 57.80% || Temperature: 30.00°C 
20:25:41.524 -> Humidity: 57.80% || Temperature: 30.00°C 
20:25:41.564 -> Humidity: 57.80% || Temperature: 30.00°C 
20:25:41.564 -> Humidity: 57.80% || Temperature: 30.00°C 
20:25:41.597 -> Humidity: 57.80% || Temperature: 30.00°C 

OMG, i found a bug!!
while i have Serial.print on, CAN message doens't update!!!!!
WTF??

i disabled Serial and now can works and updates the gauges...
however, the math on the numbers is wrong, but this is progress!!!