Skip to content

13. Networking and Communications

This week’s assignment included the following tasks:

  • Send a message between two projects

Increase the beat

For this week’s assignment we decided to use the ESP NOW protocol to increase the heartbeat of Emily’s teaching heart by using the wireless. Please find the complete code here.

Define to whom and what message is sent

First we had to define the message the heart is receiving: current heartbeat/min as well as define the address: HEX address.

typedef struct struct_message {
  int value;
} struct_message;

struct_message myData;

// Define the peer address (MAC address of the receiver)
uint8_t broadcastAddress[] = {0xD4, 0xF9, 0x8D, 0x00, 0xF9, 0x18}; // Replace with the receiver's MAC address

Define how to communicate with the screen of pugboy

Screen

We used Arduino’s graphics library to see the beats/min in a numerical value using the function sendUpdate.

Communication functions

  • repaint: function that displays the number of heartbeats on the screen
void repaint(String direction){
  tft.fillScreen(ST77XX_BLACK);
  tft.setTextColor(ST77XX_BLACK, ST77XX_WHITE);
  tft.setTextSize(3);
  tft.setCursor(0, 0);
  tft.print(direction);
}
  • peerCode: sets up the connection between pugboy and the heart
esp_now_peer_info_t peerInfo;
void peerCode(){
    // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);
  Serial.println("WiFi mode set to STA");

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

  // Register the send callback
  esp_now_register_send_cb(OnDataSent);

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

  // Add peer        
  if (esp_now_add_peer(&peerInfo) != ESP_OK) {
    Serial.println("Failed to add peer");
    return;
  }
}
  • sendUpdate: sends the new heartrate to the heart
void sendUpdate(int beats){
    // Send message via ESP-NOW
    myData.value = beats;
    esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));

    if (result == ESP_OK) {
      Serial.println("Sent with success");
    } else {
      Serial.println("Error sending the data");
    }
}
  • loop: checking if up/down button is pressed and increment by 10 beats/min
void loop() {
  inputs();
  bool btnPressed = controller.up != ButtonState::pressed && controller.down != ButtonState::pressed && controller.right != ButtonState::pressed && controller.left != ButtonState::pressed;
  if (controller.up == ButtonState::pressed && debounce(UP_BTN)){

    currBeats += 10;
    currBeats = min(currBeats, HIGH_RANGE);
    repaint(String(currBeats));
    sendUpdate(currBeats);
  } else if (controller.down == ButtonState::pressed && debounce(DOWN_BTN)) {
    currBeats -= 10;
    currBeats = max(currBeats, LOW_RANGE);
    repaint(String(currBeats));
    sendUpdate(currBeats);
  }else if (controller.left == ButtonState::pressed && debounce(LEFT_BTN)) {
    repaint("Left");
  }else if (controller.right == ButtonState::pressed && debounce(RIGHT_BTN)) {
    repaint("Right");
  } else if (btnPressed) {
    currDebounced = false;
  }
}

In action