Bryan NgondoNicolas DAILLYfcaron
Published

KickMetric

KickMetric: Advancing football with real-time performance tracking for optimized gameplay and injury prevention.

IntermediateProtipOver 2 days170
KickMetric

Things used in this project

Hardware components

sodaq explorer
×1
SparkFun Single Lead Heart Rate Monitor - AD8232
SparkFun Single Lead Heart Rate Monitor - AD8232
×1
flex sensor
×1
accelerometer MPU 6050
×1

Software apps and online services

Arduino IDE
Arduino IDE
vmware
Node-RED
Node-RED
The Things Stack
The Things Industries The Things Stack
php myadmin

Story

Read more

Code

Final Code

C#
#include <Sodaq_RN2483.h>
#include <Sodaq_wdt.h>
#include <Utils.h>

#define DEBUG
#define uplinkCnt 10

#define LED_RED_CUST    0x00
#define LED_GREEN_CUST  0x99
#define LED_BLUE_CUST   0xFF

#define debugSerial SerialUSB
#define loraSerial  Serial2

#ifdef OTAA
const uint8_t devEUI[8]  = {0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x06, 0x46, 0xFB};
const uint8_t appEUI[8]  = {0x23, 0x10, 0x20, 0x02, 0x23, 0x10, 0x20, 0x02};
const uint8_t appKey[16] = {0x2B, 0x80, 0x59, 0x87, 0x2F, 0x13, 0xF6, 0x55, 0x1F, 0x5D, 0xF2, 0xFB, 0xDD, 0x7D, 0x98, 0x1C};
#endif

#define TEMP_SENSOR A0
#define LED_RED     2
#define LED_GREEN   3
#define LED_BLUE    4
#define FLEX_SENSOR A1

void setup() {
    debugSerial.begin(57600);
    loraSerial.begin(LoRaBee.getDefaultBaudRate());

    SerialUSB.begin(9600);

    pinMode(10, INPUT); // Setup for leads off detection LO +
    pinMode(11, INPUT); // Setup for leads off detection LO -
    

    pinMode(TEMP_SENSOR, INPUT);
    analogReadResolution(12);

    pinMode(LED_RED, OUTPUT);
    pinMode(LED_GREEN, OUTPUT);
    pinMode(LED_BLUE, OUTPUT);

    digitalWrite(LED_RED, HIGH);
    digitalWrite(LED_GREEN, HIGH);
    digitalWrite(LED_BLUE, HIGH);

    analogWrite(LED_RED, LED_RED_CUST);
    analogWrite(LED_GREEN, LED_GREEN_CUST);
    analogWrite(LED_BLUE, LED_BLUE_CUST);

    pinMode(FLEX_SENSOR, INPUT);

    delay(10000);

    digitalWrite(LED_RED, HIGH);
    digitalWrite(LED_GREEN, HIGH);
    digitalWrite(LED_BLUE, HIGH);

    LoRaBee.setDiag(debugSerial);

    #ifdef OTAA
    if (LoRaBee.initOTA(loraSerial, devEUI, appEUI, appKey, true)) {
        debugSerial.println("OTAA Keys Accepted.");
    } else {
        debugSerial.println("OTAA Keys Setup Failed!");
    }
    #endif

    debugSerial.println("Sleeping for 5 seconds before starting sending out test packets.");
    for (uint8_t i = 5; i > 0; i--) {
        debugSerial.println(i);
        delay(1000);
    }
}

void loop() {
    float sum = 0.0;
    float voltage = 0.0;
    float temperature = 0.0;
    uint8_t samples = 10;
    uint8_t payload[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    uint8_t data;

    sum = 0.0;
    for (int i = 0; i < samples; i++) {
        sum += 3.3 / 4095.0 * (float)analogRead(TEMP_SENSOR);
    }
    voltage = sum / samples;
    temperature = (voltage - 0.5) * 100.0;

    debugSerial.print("Temperature: ");
    debugSerial.print(temperature, 1);
    debugSerial.println(" C");

    if ((digitalRead(10) == 1) || (digitalRead(11) == 1)) {
    SerialUSB.println('!'); // Leads off detection
  } else {
    // Read the value of analog input 0:
    int sensorValue = analogRead(A0);
    
    // Send the sensor value as a byte array
    byte payload[2];
    payload[0] = highByte(sensorValue);
    payload[1] = lowByte(sensorValue);
    
    SerialUSB.write(payload, sizeof(payload));
  }
  
  // Wait for a bit to keep serial data from saturating
  delay(1000); // Adjust delay according to your needs



    

    if (temperature >= 0) {
        payload[0] = '+';
    } else {
        payload[0] = '-';
    }
    data = (int)temperature;
    payload[2] = '0' + (data % 10);
    data /= 10;
    payload[1] = '0' + (data % 10);

    // Lecture des données du poux (à adapter selon le format réel)
    payload[3] = analogRead(FLEX_SENSOR) >> 8; // Les 8 bits de poids forts
    payload[4] = analogRead(FLEX_SENSOR) & 0xFF; // Les 8 bits de poids faibles

    switch (LoRaBee.send(1, payload, 6)) {
        case NoError:
            debugSerial.println("Successful transmission.");
            break;
        case NoResponse:
            debugSerial.println("There was no response from the device.");
            break;
        case Timeout:
            debugSerial.println("Connection timed-out. Check your serial connection to the device! Sleeping for 20sec.");
            delay(20000);
            break;
        case PayloadSizeError:
            debugSerial.println("The size of the payload is greater than allowed. Transmission failed!");
            break;
        case InternalError:
            debugSerial.println("Oh No! This shouldn't happen. Something is really wrong! Try restarting the device!\r\nThe program will now halt.");
            while (1) {}
            break;
        case Busy:
            debugSerial.println("The device is busy. Sleeping for 10 extra seconds.");
            delay(10000);
            break;
        case NetworkFatalError:
            debugSerial.println("There is a non-recoverable error with the network connection. You should re-connect.\r\nThe program will now halt.");
            while (1) {}
            break;
        case NotConnected:
            debugSerial.println("The device is not connected to the network. Please connect to the network before attempting to send data.\r\nThe program will now halt.");
            while (1) {}
            break;
        case NoAcknowledgment:
            debugSerial.println("There was no acknowledgment sent back!");
            break;
        default:
            break;
    }

    delay(10000);
}

Credits

Bryan Ngondo
1 project • 0 followers
Nicolas DAILLY
41 projects • 26 followers
Associated Professor at UniLaSalle - Amiens / Head of the Computer Network Department / Teach Computer and Telecommunication Networks
fcaron
22 projects • 7 followers

Comments