Esp32 serial2 to photon serial

Hello to all.

Having an issue with reading the serial2 of an esp32 TTGO LoRa board connected to a Photon serial1

Some assistance would be appreciated.

#include <blynk.h>
#include "Particle.h"
#include <Arduino.h>                             
#include <ArduinoJson.h>                           

SYSTEM_MODE(MANUAL)
SYSTEM_THREAD(ENABLED);

char auth[] = "XXXXX";

StaticJsonDocument<200> doc;                            

#define ESP32_PORT Serial1    // Photon tx to esp32 rx2 GPIO16 and Photon rx to esp32 tx2 GPIO17

float val1 = 0.0;
float val2 = 0.0;
float val3 = 0.0;
String conn = "";

#define BLYNK_PRINT Serial

WidgetTerminal terminal(V21);

unsigned long lastmillis = 0;

void setup() {
  Serial.begin(115200);
  ESP32_PORT.begin(115200);                            // LORA
  WiFi.on();
  WiFi.connect();
  waitFor(WiFi.ready, 90000);
  Blynk.config(auth);
  Particle.process();
  Particle.connect();
  Blynk.connect(); 
  System.enableUpdates();
}

void print_a_value(String name , String val){                            // LORA
    Serial.print(name);
    Serial.print(" : ");
    Serial.println(val);
}

void loop(){
  static uint32_t ms500 = 0;
  if (millis() - ms500 < 500) return;
  ms500 = millis();
  Particle.process();
  terminal_task();  
  Blynk.run();
  
if(ESP32_PORT.available() > 0){                            // LORA
        String incoming  = ESP32_PORT.readString();

        deserializeJson(doc, incoming);
        
        val1 = doc["v1"];
    //    val2 = doc["v2"];
    //    val3 = doc["v3"];
        conn = (const char*)doc["status"];
        
        print_a_value("value 1" , String(val1));
    //    print_a_value("value 2" , String(val2));
    //    print_a_value("value 3" , String(val3));
        print_a_value("connection status" , conn);
        Serial.println("\r\n");
     }
}


void terminal_task(){
    if ((millis() - lastmillis) > 2000) 
  {
   lastmillis = millis();
 
        terminal.println();
        terminal.print("LoRa val1: = " );
        terminal.print(val1);
        terminal.println();
        terminal.print("Status: = " );
        terminal.print(conn);
        
        terminal.println();
        terminal.flush();  
  }
}

Some more elaborate symptom description would be helpful.
Also an absolute minimum test script would be much better to tackle a specific issue - don't add anything to the test that doesn't immediately belong to the topic at hand.

Thanks ScruffR. Will do that later.

#include <Arduino.h>                             
#include <ArduinoJson.h>                           

SYSTEM_MODE(MANUAL)
SYSTEM_THREAD(ENABLED);

StaticJsonDocument<200> doc;                            

#define ESP32_PORT Serial1    // Photon tx to esp32 rx2 GPIO16 and Photon rx to esp32 tx2 GPIO17

float val1 = 0.0;
String conn = "";

void setup() {
  Serial.begin(115200);
  ESP32_PORT.begin(115200);                            
  WiFi.on();
  WiFi.connect();
  waitFor(WiFi.ready, 90000);
  Particle.process();
  Particle.connect();
  System.enableUpdates();
}

void print_a_value(String name , String val){                            
    Serial.print(name);
    Serial.print(" : ");
    Serial.println(val);
}

void loop(){
  static uint32_t ms500 = 0;
  if (millis() - ms500 < 500) return;
  ms500 = millis();
  Particle.process();

if(ESP32_PORT.available() > 0){                            
        String incoming  = ESP32_PORT.readString();

        deserializeJson(doc, incoming);
        
        val1 = doc["v1"];
        conn = (const char*)doc["status"];
        
        print_a_value("value 1" , String(val1));
        print_a_value("connection status" , conn);
        Serial.println("\r\n");
     }
}

particle serial monitor only displays val1 0.00000 and nothing for conn

When I said something about minimal test script I was rather thinking of something along this line :wink:

void setup() {
    Serial.begin();
    Serial1.begin(115200);
    while(Serial.read()  >= 0);    // flush Serial  input buffer
    while(Serial1.read() >= 0);    // flush Serial1 input buffer
}

void loop() {
    if (Serial.available()) Serial1.write(Serial.read());
    if (Serial1.available()) Serial.write(Serial1.read());
}

This will pass through USB Serial to Serial1 and vice versa.
Hence you can send and receive via any serial terminal on your computer to and from the ESP32.
When you then also post your ESP32 code we might see whether the issue is coming from that side.

3 Likes

Thanks ScruffR

I have tested with that code and receive exactly what the esp32 is sending.
The result is the contents of the doc.
{“version”:“6.7”,“filename”:“1.ino.ttgo-lora32-v1.bin”,“v1”:3.299194,“status”:“connected”}

Now I want to be able to grab and store as a float v1: which is analogRead 3.2999194 and the status: connected

This is the part which I am having some trouble.

Here is my code.

#include <Arduino.h>                             
#include <ArduinoJson.h>                           
#include "Particle.h"
SYSTEM_MODE(MANUAL)
SYSTEM_THREAD(ENABLED);

DynamicJsonDocument doc(512);                            

#define ESP32_PORT Serial1    // Photon tx to esp32 rx2 GPIO16 and Photon rx to esp32 tx2 GPIO17

float val1 = 0.0;
float val2 = 0.0;
float val3 = 0.0;
String conn = "";

String incoming  = ESP32_PORT.readString();


void setup() {
  Serial.begin(115200);
  ESP32_PORT.begin(115200);                            
  WiFi.on();
  WiFi.connect();
  waitFor(WiFi.ready, 90000);
  Particle.process();
  Particle.connect();
  System.enableUpdates();
}

void parseString(String str){
  String temp = "";
  int count = 0;
  for(int i=0; i < str.length() ; i++){
    
    if(str[i] == '\n'){
     temp = "";   

    }else if(str[i] == ','){

      float v = temp.toFloat();
    
      if(count == 0){
        val1 = v;
      }
      if(count == 1){
        val2 = v;
      }
      if(count == 2){
        val3 = v;
      }
 
      count++;
      temp ="";
    }else{
      temp += str[i];
    }
    
    
  }
}

void print_a_value(String name , String val){                            
    Serial.print(name);
    Serial.print(" : ");
    Serial.println(val);
}

void loop(){
  static uint32_t ms500 = 0;
  if (millis() - ms500 < 500) return;
  ms500 = millis();
  Particle.process();

if(ESP32_PORT.available() > 0){                           
        String incoming  = ESP32_PORT.readString();

        deserializeJson(doc, incoming);
        
        val1 = doc["v1"];
        val2 = doc["v2"];
        val3 = doc["v3"];
        conn = (const char*)doc["status"];
        
//        print_a_value("value 1" , String(val1));
//        print_a_value("value 2" , String(val2));
//        print_a_value("value 3" , String(val3));
//        print_a_value("connection status" , conn);
//        Serial.println("\r\n");
        Serial.println(val1,2);

    }
}

The outcome in particle serial monitor is 3.20
I can only get the number 3 and first decimal 2
I also want to find out how to store the status: which is either connected or not connected

Code on esp32(acting as a LoRa receiver which receives analogRead from esp32 transmitter via LoRa). So esp32 receiver is connected to Photon

#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <Update.h>
#include <ArduinoJson.h>
#include <ESPmDNS.h>
#include <HTTPClient.h>
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
//Libraries for LoRa
#include <SPI.h>
#include <LoRa.h>


#include "gpio_map.h"

DynamicJsonDocument doc(512);


#define HOST_NAME "esp2"
#include "ota.h"
#include "lora.h"
#include "http_firmware.h"

#define UPDATE_FREQUNCY 30000 // every 5 sec

#define SERIAL2 Serial2 // assume you have connected the particle board to esp32 serial port 2

unsigned long lora_timer = 0; // to store time
unsigned long update_timer = 0; // to store time

String analog_reads = ""; //we reserve string in memory to store analog read value
unsigned long last_update = 0;      //to store last time transmitter had activity
String connection = "not connected";//string to store status
float value1 = 0.0;
float value2 = 0.0;
float value3 = 0.0;






void setup() {

  //initialize Serial Monitor
  Serial.begin(115200);
  SERIAL2.begin(115200);

  Serial.println("LoRa Receiver Test");

  //SPI LoRa pins
  SPI.begin(SCK, MISO, MOSI, SS);
  //setup LoRa transceiver module
  LoRa.setPins(SS, RST, DIO0);

  if (!LoRa.begin(BAND)) {
    Serial.println("Starting LoRa failed!");
    //while (1);
  }
  Serial.println("LoRa Initializing OK!");
  

  OTA_Init();        // instalize OTA setup

}

void loop() {

  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//  if((millis() - last_update) > 3000){  // if 3 seconds passed since last update
//    connection = "not connected";
//    SERIAL2.print(connection);
//  }

  if ((millis() - lora_timer) > 2000) { // every 2 seconds
    lora_timer = millis();           // reset time
//    connection = "not connected";
//    SERIAL2.print(connection);
//    Serial.println("THIS IS VERSION 7.9");
  }

   if ((millis() - update_timer) > UPDATE_FREQUNCY) { // every x seconds
    check_for_new_update();
    update_timer = millis();               // reset time
  }
  
  //try to parse packet
  int packetSize = LoRa.parsePacket();  // packetSize will equal to more than 0 if we have packets stored in memory
  if (packetSize > 0) {                 // if packet size more than 0
    //received a packet
    last_update = millis();             // update last activity time
    connection = "connected";           // now we know that we have a connection

    //read packet
    while (LoRa.available() > 0 ) {     // read all packets in memory until we have 0 packets
      String incoming = LoRa.readString(); // read the packet
      deserializeJson(doc, incoming);
      value1 = doc["v1"];
//      value2 = doc["v2"];
//      value3 = doc["v3"];
      doc["status"] = connection;
      
    //  analog_reads = String(value1) + " " + String(value2) + " " + String(value3);
      analog_reads = String(value1);
//      Serial.print("Analog Val : ");    // print some text
//      Serial.println(analog_reads);     // print the value on serial
      
      String outgoing = "";
      serializeJson(doc, outgoing);
      SERIAL2.print(outgoing);    // send the value to particle board via serial 2 port
      Serial.println(outgoing);
    }


  
  }
 
  OTA_handler();                  // handle web server events

}

So the first take-away from this is that it’s not an issue with the serial connection as you originally asserted :wink:
But rather how you did read it and now how you are trying to parse the incoming data.

Since you are already including ArduinoJson.h I don’t quite see why you there is this zombi-function parseString().
Also there is little point trying to use the doc[] without even knowing whether the deserialization worked at all.

This sample here shows how you’d check whether deserialization was successful or not. Only when it was, you should proceed.

If it wasn’t successful, you should ensure the incoming data is valid before passing it to the deserializer.

With the sample data you provided above, I’d first flush the entire RX buffer, then wait for the final } of a given (potientially fragmented) transmission and only then read the incoming data into a buffer until the closing } is received again. After that you can try to deserialize that string.

I’d also use ESP32_PORT.find("}", 1) to trigger for the end of the discardable transmission and after that ESP32_PORT.readStringUntil('}'); to capture everything up to the closing brace of the desired transmission.

Alternatively you could use the ArduinoStreamReader as shown here

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.