Wireless Timing Project

Pages
Contributors: Gior Dior
Favorited Favorite 5

Start Setup

For the start setup, we will need to connect the distance sensor to the ESP32 Thing Plus. This is the “Start”, so be sure this is the ESP32 that we did NOT obtain the MAC address for. Use one qwiic cable connecting the ESP32 to our distance sensor. Your setup should look like this.

alt text

Once we have connected the distance sensor, we are now ready to flash the ESP32 with our “Start” code.

alt text

This code controls sending a message to our “Finish” ESP32 when the distance sensor has been triggered. The distance sensor will continuously scan at 4m. When an object crosses the sensor, the distance will be less than 1500mm. Why 1500mm? It’s what the sensor was reading when there was no object interfering. If the sensor is triggered or distance is less than 1500mm, we will send an integer “1” to the “Finish” ESP32. The “Finish” ESP32 is always looking out for the “1”. When the “1” integer is received, the “Finish” device will begin the time and wait for another distance sensor trigger. This will be the time the object is between the start and finish lines.

/*
*Wireless timing start sensor example, Transmitter sketch
*Giordan Thompson, SparkFun Electronics, September 2022
*This example transmits data to the finish sensor example
*You can find this example in github here ()
*
*The purpose of this code is to provide functionality to our wireless timing system
*This code will send data to the finish sensor, relaying the start sensor has been triggered 
*and we are ready to begin the timing. 
*
*Complete project details can be found here () where we dive into the creation and 
*desciption of this system.
*Please feel free to customize this code to your needs and raise an issue if necessary. 
*
*Enjoy!!!
*
*/



#include <Wire.h> //used to establish serial communication on the I2C bus
#include "SparkFun_VL53L1X.h" //Distance sensor library
#include <esp_now.h> //ESP wireless communication library
#include <WiFi.h> 

SFEVL53L1X distanceSensor;

// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t broadcastAddress[] = {0x7C, 0xDF, 0xA1, 0x55, 0xAE, 0x50};  

// Structure example to send data
// Make sure to match the receiver structure on finish sensor
typedef struct struct_message {
 int outgoingInt;
} struct_message;

// Create a struct_message called myData
struct_message myData;

// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
 Serial.print("\r\nLast Packet Send Status:\t");
 Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}

void setup() {
//Pin for indecator LED
 pinMode(13, OUTPUT);

 Wire.begin();

 Serial.begin(115200);

  // Device ia set as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // Once ESPNow is successfully Init, we will register for Send CB to
  // get the status of Trasnmitted packet
  esp_now_register_send_cb(OnDataSent);

  // Register peer
  esp_now_peer_info_t peerInfo;
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;

  // Add peer otherwise indecate failure        
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }


  //Communicate sensor is online and ready for trigger 
  if (distanceSensor.init() == false)
    Serial.println("Sensor online!");

}

void loop() {
  //Indicator light on, device is ready for trigger
  digitalWrite(13, HIGH);

  distanceSensor.startRanging(); //Write configuration bytes to initiate measurement
  int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
  //Serial.print(distance);
  //After testing, 1500 was a consitent sensor value with NO object interference
  if (distance < 1500){

    //Sensor has been tiggered, send "1" integer to finish sensor
    myData.outgoingInt = 1;

    //Serial communication for troubleshooting
    Serial.print("Outgoing Int Val:");
    Serial.println(myData.outgoingInt);
    esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData.outgoingInt,sizeof(myData.outgoingInt));
    Serial.println("Distance(mm): ");
    Serial.print(distance); 

    //Sensor tiggered and integer has been sent to finish ESP
    //now ready to pause code and wait for reset
    distanceSensor.stopRanging();
    digitalWrite(13, LOW);
    while(1);
  }

  delay(10);

}