ESP32 and Teensy 3.2 I2C data exchange issue with SPI interface

Hello,

I was looking to use a Teensy 3.2 MCU to communicate between an E paper display and RFID module to provide an interactive scale payment system for my project. I have used I2C protocols to communicate with an ESP32 to retrieve and send different variables which are:

  • Card ID from Teensy 3.2 to ESP32 (Not yet configured)
  • Weight from ESP 32 to Teensy 3.2

I have set up the E paper display to only switch to my sale screen when the RFID module receives a card ID where then it would continuously update the weight on the sale screen as long as the calculated weight is greater than the previous weight as found here:

Slave code:

/**
   A simple sketch to test and demo the dispenser's current features
*/

#include "DispenserData.h"
#include "DispenserUI.h"
#include "Dispenser.h"
#include <Wire.h>

// Const
const int dispensingTimout = 5000; //in milliseconds
int weight; 
int prevWeight = 0;


Dispenser dp;

void setup()
{
  // Initialise dispenser

  dp.begin();
  // Show product screen at startup
  dp.clearScreen();
  dp.showProductScreen();
  Serial.begin(115200);
}

void loop()
{
      // Continuosly check for rfid cards
      if (dp.isCardPresent()) {
        Serial.print("CardID: ");
        Serial.println(dp.getCardID());
        // If one is detected, toggle LED
        // and display sale screen
        dp.ledOn();
        dp.clearScreen();
        dp.showSaleScreen(0);

        // Keep filling container until timout is reached
         unsigned long startTime = millis();
         while (millis() - startTime < dispensingTimout) {
             Wire.begin(8);                // join i2c bus with address #8
             Wire.onReceive(receiveEvent);
              
          // Toggle motor depending on whether
          // button is pressed
          if (dp.isButtonPressed()) {
            startTime = millis();
            Serial.println("Dispensing.");
            dp.addWeight();  // For Testing only
            dp.motorOn(255);
          } else {
            dp.motorOff();
          }
          delay(500);
       }

        // After timout, "stop dispensing",
        // and return to product screen
        dp.motorOff();
        //dp.showSaleScreen(weight);
        dp.ledOff();
        //dp.resetWeight(); //reset


        delay(2500);
        dp.clearScreen();
        dp.showProductScreen();
      }
}
void receiveEvent(int howmany) {
  while(1 < Wire.available()) // loop through all but the last
  {
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
   int weight = Wire.read(); // respond with message of 6 bytes // as expected by master
   //Serial.println(weight);
   if (weight > prevWeight) {
    dp.showSaleScreen(weight);
    weight == prevWeight; 
  }
}

Master code:

/**
   A simple sketch to test and demo the dispenser's current features
*/

#include "Dispenser.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>

// Const
const int dispensingTimout = 5000; //in milliseconds
int prevWeight = 0;

// Wifi constants
const char* wifiSsid = "Crunchy";
const char* wifiPassword = "muffin4me";
const int httpConnectionTimeout = 500;
const char* cartServiceURL = "http://10.0.0.88:8080/Cart";
const char* pulseServiceURL = "http://10.0.0.88:8080/Pulse";

Dispenser dp;

void setup()
{
  Wire.begin();               
  Serial.begin(115200);
  // Initialise dispenser

  // Connect to WiFi
  WiFi.begin(wifiSsid, wifiPassword);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("Connected to WiFi...");
  Serial.println(WiFi.localIP());

  dp.begin();
}

void loop()
{
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient pulseClient;
    pulseClient.setConnectTimeout(httpConnectionTimeout);
    pulseClient.begin(pulseServiceURL);
    int pulseResponseCode = pulseClient.GET();
    pulseClient.end();

    //if (pulseResponseCode == 200) {
      // Continuosly check for rfid cards
       // getcardID();
       
        
        // Keep filling container until timout is reached
        unsigned long startTime = millis();
        while (millis() - startTime < dispensingTimout) {
          int weight = dp.getWeight();
          if (weight > prevWeight) {
          prevWeight == weight;
          Serial.println(weight);
          Wire.beginTransmission(8); // transmit to device #8
          Wire.write("Weight is ");
          Wire.write(weight);
          Wire.endTransmission();    // stop transmitting
          }
          else{
          Wire.endTransmission();    // stop transmitting
          }
          delay(500);
        }

        // Network sent data
//        String cardID = dp.getCardID();
//        String machineID = WiFi.macAddress();
//        unsigned int weightGrams = dp.getWeight();

        // Debug echo
//        Serial.println("Exiting dispensing mode.");
//        Serial.print("CardID: ");
//        Serial.println(cardID);
//        Serial.print("machineID: ");
//        Serial.println(machineID);
//        Serial.print("weightGrams: ");
//        Serial.println(weightGrams);

        // Send data over network
      //  if (weightGrams > 0) {
          Serial.println("Sending Data to web service.");
//          String weightString = String(weightGrams);
          //itoa(weightGrams, weightString, 10);

          String xml1 = "<CartItem xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/Dispenser.WebService.Model\">\r\n";
          String xml2a = "    <CardId>";
          String xml2b = "</CardId>\r\n";
          String xml3a = "    <MachineId>";
          String xml3b = "</MachineId>\r\n";
          String xml4a = "    <Weight>";
          String xml4b = "</Weight>\r\n";
          String xml5 = "</CartItem>";

          String payload = xml1 + xml2a + xml2b + xml3a + xml3b + xml4a + xml4b + xml5;
          //String payload = xml1 + xml2a + cardID + xml2b + xml3a + machineID + xml3b + xml4a + weightString + xml4b + xml5;
          Serial.println(payload);

          HTTPClient webclient;
          webclient.setConnectTimeout(httpConnectionTimeout);
          webclient.begin(cartServiceURL);
          webclient.addHeader("Content-Type", "application/xml");
          webclient.addHeader("Accept", "application/xml");
          int httpResponseCode = webclient.POST(payload);

          if (httpResponseCode == 200) {
            Serial.println("Sending data: success");
          } else {
            Serial.println("Sending data: failed");
          }
          webclient.end();
       }
//
//        dp.resetWeight();
        
    //  }
    //} 
    //else {
      //Serial.println("ERROR - Web service not available");
      // TO DO
      // Show on dispenser screen as that the dispense is offline
   // }
    //delay(500);
    //else {
    //Serial.println("ERROR - WiFi not connected...");
    // TO DO
    // Show on dispenser screen as no WiFi available
   // delay(500);
  //}
}
//void getcardID(){
//  Wire.requestFrom(8,11);    // request 11 bytes from slave device #8
//  while (Wire.available()) { // slave may send less than requested
//    char c = Wire.read(); // receive a byte as character
//   
//      Serial.print(c);         // print the character
//  }
//
//  Serial.println();
//}

The variables are being sent by Master slave I2C between the ESP32 and Teensy 3.2, my issue is that my Epaper display continues displaying the weight values whether <,=, > the previous weight value and is stuck in this loop indefinitely without restarting from the beginning of my void loop()

My code does consist of classes so I went ahead and attached it at the bottom of the document. The class functions used to measure, send, display & receive the weight measurements can be found there

firmware_slave.zip (7.44 KB)

Firmware_master.zip (5.86 KB)