Notifications
Clear all

ESP32S3 Serial Communications

25 Posts
4 Users
7 Likes
1,125 Views
huckOhio
(@huckohio)
Member
Joined: 5 years ago
Posts: 180
Topic starter  

   
Quote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

@huckohio Prof google says to the query "arduino receive usb" https://forum.arduino.cc/t/how-to-read-data-from-any-of-the-usb-ports-using-the-arduino/228207

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

@huckohio For the esp32s3 board under examples USB I see the following. I think what you want is the USBSerial example but I am not sure.

Screenshot 2022 12 20 at 11.41.22

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

@huckohio Here is the page you want https://www.arduino.cc/reference/en/language/functions/communication/serial/available/

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
huckOhio
(@huckohio)
Member
Joined: 5 years ago
Posts: 180
Topic starter  

@zander Thanks Ron!  When I looked under examples I was looking for serial but never thought to look under USB.  I looked at your first response and I did see that one before, but it's sending an integer.  I am looking to see if there is a read line option under serial.  I am sending strings from the Pi4 to the ESP32S3.  I'll look at the USB serial example.  Thanks!


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

@huckohio Don't worry about strings or integers, they are easily converted one to the other. If you didn't pick up on it, the serial monitor you use to watch your output via Serial.println(" Hello WORLD "); Has an input box at the top where you can enter text and read with

String teststr = Serial.readString();

there is supporting code to go with that but that is the concept. See the following page for all the available functions, one or more will solve your problem I think.

https://www.arduino.cc/reference/en/language/functions/communication/serial/

 

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
huckOhio
(@huckohio)
Member
Joined: 5 years ago
Posts: 180
Topic starter  

@zander Thanks Ron.  I tried that exact statement and all I received at the far end is boxes 😆 

My approach of randomly trying example code is facing another challenge.  The communications between the Pi4 and the ESP32S3 is via USB cable.  I cannot open the Arduino IDE on the Pi4 to watch for print statements because the port is also being used to send data back and forth.  When I connect the Arduino IDE serial monitor Python throws an exception.  The PySerial library/API in Python requires me to encode the string I am sending over the port.  I am not sure what I have to do on the ESP32S3 end (more googling required).  I was able to send something all they way to the second ESP32S3 (the rabbit shed), but as I mentioned before it was a line of boxes.

Thanks for the assistance.  But now I have a 2 yr old granddaughter calling my name.  I'll get back to this tomorrow.

 

Thanks, Mike


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

@huckohio Can you connect another computer or tablet? As far as the boxes, that means the data is not ascii text, you will need to either make a test system where the data is all ascii (ascii is what this is) and get everything working then make whatever changes are needed to process the data in it's native format, probably float since most of your data as I recall was of the 149.34 format. OR start off processing the native format and build some sort of way of viewing the data. Is this helping?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
huckOhio
(@huckohio)
Member
Joined: 5 years ago
Posts: 180
Topic starter  

@zander Absolutely this is helping!  I was thinking of trying to see if I could do a loopback test on the Pi4 and them use a dummy set of data and verify the ESP32S3 to ESP32S3 is working back from the house to the rabbit shed.  I'll get back to this tomorrow.  

Thanks!


   
ReplyQuote
Will
 Will
(@will)
Member
Joined: 3 years ago
Posts: 2527
 

@huckohio 

That "line of boxes" could also result from a baud rate mismatch.

Anything seems possible when you don't know what you're talking about.


   
Ron reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

@huckohio I have a couple of other things we can try, but first I need to go back and re-read this thread .

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

@huckohio Unless I am very confused (entirely possible) if you are getting data from the house esp32 via usb to the pi4, then just reverse that code to get it back to the esp32.

As far as the boxes, print in HEX to see correct values.

See https://www.arduino.cc/reference/en/language/functions/communication/serial/println/

for how to print in hex.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
huckOhio
(@huckohio)
Member
Joined: 5 years ago
Posts: 180
Topic starter  

@zander Ron, here is an update.  I have been playing with this for a couple hours.  Since I cannot view the serial output on the house ESP32S3 I added three LEDs.  One to show when the ESP32S3 is receiving data from the bunny shed, one to show that the ESP32S3 received serial data from the Pi4, and one to show that the data was successfully send to the bunny shed ESP32S3.  Based on the flashing LEDs it looks like I have data flow.

Here is a bit more information on this project.

Here is a simple diagram that includes the struc structure.  In the following code from the Pi4 to the house ESP32S3 I changed the first two entries to text for testing.  I am also including some code segments related to sending and receiving the data.

221220 Comm Diagram

The following is the Bunny Shed ESP32S3 code to send the data to the house ESP32S3 via ESP NOW.  This segment works. 

(BS_ESP32S3 -> House_ESP32S3)

if (updateHouse)
    {
    shackReadings.dateInfo = myDate;
    shackReadings.timeInfo = myTime;
    shackReadings.temp = f;
    shackReadings.fan1Status = fan1Status;
    shackReadings.fan2Status = fan2Status;
    shackReadings.lampStatus = lampStatus;
    shackReadings.waterStatus = waterStatus;
    shackReadings.fan1Override = fan1Override;
    shackReadings.fan2Override = fan2Override;
    shackReadings.lampOverride = lampOverride;
    shackReadings.waterOverride = waterOverride;
    shackReadings.fan1Dis = fan1Disable;
    shackReadings.fan2Dis = fan2Disable;
    shackReadings.lampDis = lampDisable;
    shackReadings.waterDis = waterDisable;
    shackReadings.fan1Temp = fan1Temp;
    shackReadings.fan2Temp = fan2Temp;
    shackReadings.lampTemp = lampTemp;
    shackReadings.waterTemp = waterTemp;
    shackReadings.sensorError = tempSensorError;

    //  Send message via ESP-NOW
    esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &shackReadings, sizeof(shackReadings));
  
    if (result == ESP_OK) 
      {
      Serial.println("Sent with success");
      digitalWrite(commLinkBad, LOW);
      commLinkError = false;
      }
    else 
      {
      Serial.println("Error sending the data");
      digitalWrite(commLinkBad, HIGH);
      commLinkError = true;
      }
    updateHouse = false;  
    }  

Here is the code in the house ESP32S3 that receives the data from the Bunny Shed. I am taking the data from the receive buffer (?) and using a "Serial.Print" statement to send the data to the Pi4.  This part of the code also works.

(House_ESP32S3 -> Pi4)

// Callback when data is received from the Bunny Shack via ESP NOW- Serial.println statements are sending data to Pi4
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) 
  {
  memcpy(&incomingBunnyData, incomingData, sizeof(incomingBunnyData));
  digitalWrite(BSdata, HIGH);
  Serial.print("Bytes received: ");
  Serial.println(len);
  Serial.println("Date");
  Serial.println(incomingBunnyData.dateInfo);
  Serial.println("Time");
  Serial.println(incomingBunnyData.timeInfo); 
  Serial.println("Temp");
  Serial.println(incomingBunnyData.temp);
  Serial.println("Fan1Status");
  Serial.println(incomingBunnyData.fan1Status);
  Serial.println("Fan2Status");
  Serial.println(incomingBunnyData.fan2Status);
  Serial.println("LampStatus");
  Serial.println(incomingBunnyData.lampStatus);
  Serial.println("WaterStatus");
  Serial.println(incomingBunnyData.waterStatus);
  Serial.println("Fan1OR");
  Serial.println(incomingBunnyData.fan1Override);
  Serial.println("Fan2OR");
  Serial.println(incomingBunnyData.fan2Override);
  Serial.println("LampOR");
  Serial.println(incomingBunnyData.lampOverride);
  Serial.println("WaterOR");
  Serial.println(incomingBunnyData.waterOverride);
  Serial.println("Fan1Dis");
  Serial.println(incomingBunnyData.fan1Dis);
  Serial.println("Fan2Dis");
  Serial.println(incomingBunnyData.fan2Dis);
  Serial.println("LampDis");
  Serial.println(incomingBunnyData.lampDis);
  Serial.println("WaterDis");
  Serial.println(incomingBunnyData.waterDis);
  Serial.println("Fan1Temp");
  Serial.println(incomingBunnyData.fan1Temp);
  Serial.println("Fan2Temp");
  Serial.println(incomingBunnyData.fan2Temp);
  Serial.println("LampTemp");
  Serial.println(incomingBunnyData.lampTemp);
  Serial.println("WaterTemp");
  Serial.println(incomingBunnyData.waterTemp);
  Serial.println("SensorError");
  Serial.println(incomingBunnyData.sensorError);
  delay(1000);
  digitalWrite(BSdata, LOW);
  }

Here is the python code from the Pi4.  I created the GUI in QT and I am using to thread to grab the data when it arrived.  This part of the code also works.

(Pi4)

def run(self):
        #capture data sent from the Arduino ESP32S3
        ser=serial.Serial('/dev/ttyUSB0',9600, timeout=1)
        ser.reset_input_buffer()
        ser.write(b"Send Data\n")
        while True:
            line=ser.readline().decode('utf-8').rstrip()
            if line == "Date":
                newD=ser.readline().decode('utf-8').rstrip()
                print("Date : ", newD)
                self.newDate.emit(newD)
            elif line == "Time":
                newT = ser.readline().decode('utf-8').rstrip()
                print("Time : ", newT)
                self.newTime.emit(newT)
            elif line == "Temp":
                newTemp=ser.readline().decode('utf-8').rstrip()
                self.newTemp.emit(newTemp)
            elif line == "Fan1Status":
                F1S=ser.readline().decode('utf-8').rstrip()
                self.Fan1Status.emit(F1S)
            elif line == "Fan2Status":
                F2S=ser.readline().decode('utf-8').rstrip()
                self.Fan2Status.emit(F2S)
            elif line == "LampStatus":
                LS=ser.readline().decode('utf-8').rstrip()
                self.LampStatus.emit(LS)
            elif line == "WaterStatus":
                WS=ser.readline().decode('utf-8').rstrip()
                self.WaterStatus.emit(WS)
            elif line == "Fan1OR":
                F1OR=ser.readline().decode('utf-8').rstrip()
                self.Fan1OR.emit(F1OR) 
            elif line == "Fan2OR":
                F2OR=ser.readline().decode('utf-8').rstrip()
                self.Fan2OR.emit(F2OR)
            elif line == "LampOR":
                LOR=ser.readline().decode('utf-8').rstrip()
                self.LampOR.emit(LOR)
            elif line == "WaterOR":
                WOR=ser.readline().decode('utf-8').rstrip()
                self.WaterOR.emit(WOR)
            elif line == "Fan1Dis":
                F1Dis=ser.readline().decode('utf-8').rstrip()
                self.Fan1Dis.emit(F1Dis) 
            elif line == "Fan2Dis":
                F2Dis=ser.readline().decode('utf-8').rstrip()
                self.Fan2Dis.emit(F2Dis)
            elif line == "LampDis":
                LDis=ser.readline().decode('utf-8').rstrip()
                self.LampDis.emit(LDis)
            elif line == "WaterDis":
                WDis=ser.readline().decode('utf-8').rstrip()
                self.WaterDis.emit(WDis)    
            elif line == "SensorError":
                SE = ser.readline().decode('utf-8').rstrip()
                self.SensorError.emit(SE)
            elif line == "Fan1Temp":
                F1T = ser.readline().decode('utf-8').rstrip()
                self.Fan1Temp.emit(F1T)
            elif line == "Fan2Temp":
                F2T = ser.readline().decode('utf-8').rstrip()
                self.Fan2Temp.emit(F2T)    
            elif line == "LampTemp":
                LT = ser.readline().decode('utf-8').rstrip()
                self.LampTemp.emit(LT)    
            elif line == "WaterTemp":
                WT = ser.readline().decode('utf-8').rstrip()
                self.WaterTemp.emit(WT)    

The above code populates the GUI below.

Screenshot from 2022 12 21 14 35 58

The following python code is also from the Pi4 and is what I am working on now to send data back through the house ESP32S3 to the Bunny Shed ESP32S3.  I've commented out most of the code and I am only testing with a couple data items (two ser.write statements for each data item.  First is the identifier and the second is the value).

(Pi4 -> House_ESP32S3)

    def sendUpdate(self):
        #return
        #send updates to the Arduino ESP32S3
        print("Inside sendUpdate")
        ser=serial.Serial('/dev/ttyUSB0',9600, timeout=1)
        ser.reset_output_buffer()
        
        ser.write(str.encode("Fan1OR"))
        if F1ORValue == True:
            ser.write(str.encode("1"))
        else:
            ser.write(str.encode("0"))
        
        ser.write(str.encode("Fan2OR"))    
        if F2ORValue == True:
            ser.write(str.encode("1"))
        else:
            ser.write(str.encode("0"))    
        
        #ser.write(str.encode("LampOR"))
        #if LampORValue == True:
        #    ser.write(str.encode("1"))
        #else:
        #    ser.write(str.encode("0"))
        #    
        #ser.write(str.encode("WatrerOR"))
        #if WaterORValue == True:
        #    ser.write(str.encode("1"))
        #else:
        #    ser.write(str.encode("0"))
        #    
        #ser.write(str.encode("Fan1Dis"))
        #if F1DisValue == True:
        #    ser.write(str.encode("1"))
        #else:
        #    ser.write(str.encode("0"))
        #    
        #ser.write(str.encode("Fan2Dis"))
        #if F2DisValue == True:
        #    ser.write(str.encode("1"))
        #else:
        #    ser.write(str.encode("0"))
        #    
        #ser.write(str.encode("LampDis"))
        #if LampDisValue == True:
        #    ser.write(str.encode("1"))
        #else:    
        #    ser.write(str.encode("0"))
        #    
        #ser.write(str.encode("WaterDis"))
        #if WaterDisValue == True:
        #    ser.write(str.encode("1"))
        #else:
        #    ser.write(str.encode("0"))
        #   
        #ser.write(str.encode("Fan1Temp"))
        #ser.write(str.encode(str(F1Temp)))
        #ser.write(str.encode("Fan2Temp"))
        #ser.write(str.encode(str(F2Temp)))
        #ser.write(str.encode("LampTemp"))
        #ser.write(str.encode(str(LampTemp)))
        #ser.write(str.encode("WaterTemp"))
        #ser.write(str.encode(str(WaterTemp)))

Here is the receiving code in the house ESP32S3.  Because the Pi4 is communicating with the ESP32S3 via the USB cable I cannot open the Arduino IDE on the Pi4 to check print statements. I am using LEDs to at least identify that the code execution entered "while" and "if (sendUpdate)".

(House_ESP32S3 -> BS_ESP32S3)

void loop() 
  {
  String resp;
  bool sendUpdate = false;

  while (Serial.available() == 0)
    sendUpdate = true;    
    resp = String(Serial.readString());
    if (resp = "Fan1OR")
      {
      digitalWrite(rcvLED, HIGH);  
      resp = String(Serial.readString());  
      updateSettings.fan1Data = resp;  
      delay(1000);
      digitalWrite(rcvLED, LOW);
      }
    if (resp = "Fan2OR")    
      {
      digitalWrite(rcvLED, HIGH);  
      resp = String(Serial.readString());  
      updateSettings.fan2Data = resp;    
      delay(1000);
      digitalWrite(rcvLED, LOW);
      }    
  if (sendUpdate) 
    { 
    //  Send message via ESP-NOW
    esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &updateSettings, sizeof(updateSettings));
 
    if (result == ESP_OK) 
      {
      Serial.println("Sent with success");
      digitalWrite(xmitLED, HIGH);
      delay(1000);
      digitalWrite(xmitLED, LOW);
      }
    else 
      {
      Serial.println("Error sending the data");
      }
    sendUpdate = false;
    }

  }

Here is the code in the Bunny Shed ESP32S3 that is receiving the data from the House ESP32S3.

// Callback when data is received from the house unit
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) 
  {
  memcpy(&updateSettings, incomingData, sizeof(updateSettings));
  Serial.print("Bytes received: ");
  Serial.println(len);
  Serial.println(updateSettings.fan1Data);
  Serial.println(updateSettings.fan2Data);
  // if (updateSettings.fan1Override != fan1Override)
  //   {
  //   fan1Override = updateSettings.fan1Override;
  //   fan1StateChange();
  //   }
  // if (updateSettings.fan2Override != fan2Override)
  //   {
  //   fan2Override = updateSettings.fan2Override;  
  //   fan2StateChange();    
  //   }
  // if (updateSettings.lampOverride != lampOverride)
  //   {
  //   lampOverride = updateSettings.lampOverride;  
  //   lampStateChange();    
  //   }
  // if (updateSettings.waterOverride != waterOverride)
  //   {
  //   waterOverride = updateSettings.waterOverride;  
  //   waterStateChange();    
  //   }
  // if (updateSettings.fan1Dis != fan1Disable)
  //   {
  //   fan1Disable = updateSettings.fan1Dis;
  //   fan1StateChange();
  //   }
  // if (updateSettings.fan2Dis != fan2Disable)
  //   {
  //   fan2Disable = updateSettings.fan2Dis;  
  //   fan2StateChange();    
  //   }
  // if (updateSettings.lampDis != lampDisable)
  //   {
  //   lampDisable = updateSettings.lampDis;  
  //   lampStateChange();    
  //   }
  // if (updateSettings.waterDis != waterDisable)
  //   {
  //   waterDisable = updateSettings.waterDis;  
  //   waterStateChange();    
  //   }
  // if (updateSettings.fan1Temp != fan1Temp)
  //   {
  //   //update the variable       
  //   fan1Temp = updateSettings.fan1Temp;   
  //   //update temp data is EPROM
  //   preferences.begin("BS_temp", false);
  //   preferences.putUInt("fan1Temp", fan1Temp);
  //   preferences.end();
  //   }
  // if (updateSettings.fan2Temp != fan2Temp)
  //   {
  //   //update the variable       
  //   fan2Temp = updateSettings.fan2Temp;   
  //   //update temp data is EPROM
  //   preferences.begin("BS_temp", false);
  //   preferences.putUInt("fan2Temp", fan2Temp);
  //   preferences.end();  
  //   }    
  // if (updateSettings.lampTemp != lampTemp)
  //   {
  //   //update the variable       
  //   lampTemp = updateSettings.lampTemp;   
  //   //update temp data is EPROM
  //   preferences.begin("BS_temp", false);
  //   preferences.putUInt("lampTemp", lampTemp);
  //   preferences.end();  
  //   }   
  // if (updateSettings.waterTemp != waterTemp)
  //   {
  //   //update the variable       
  //   waterTemp = updateSettings.waterTemp;   
  //   //update temp data is EPROM
  //   preferences.begin("BS_temp", false);
  //   preferences.putUInt("waterTemp", waterTemp);
  //   preferences.end();  
  //  }    
  }

 And finally here is the serial monitor output from the bunny shed ESP32S3.  Note: the boxes are gone.  I think that is because I was sending all of the data versus the two strings I was using for testing.  You can see that I received 32 bytes from the house ESP32S3, but the following two line are blank.

image

I'll continue working on this tomorrow.  Thanks again for you input.  

Mike

 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

@huckohio Sounds like you are on the right track. Waaaaaaaay too much code for me to try to understand, but just sending text as you did will allow you to test. BTW, pretty good work for a non programmer.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
huckOhio reacted
ReplyQuote
huckOhio
(@huckohio)
Member
Joined: 5 years ago
Posts: 180
Topic starter  

@zander Ron,  If you would, could you look at the encode and decode statements in the code?  I've never used encode and decode and I know the 'encode" works because I can can see the data in the GUI.  I have a decode in the Pi4 code, but no decode in the House ESP32S3.  Really not sure what they are doing.  Thanks, Mike.


   
ReplyQuote
Page 1 / 2