ESP32 & Arduino Uno data processing from serial communication

byte tempg = 0;
byte humig = 0;

Serial.println(tempg);
Serial.println(humig);

Your UNO sends up to 4 characters-- 3 ascii values and the '\n' new line. "xxx\n"

The for loop in the reading routine is an error. You should just read the incoming data to the new line. You will pick up both values being sent without trying to read through twice.
atoi needs to be null terminated, and its best to add the terminator as you read in order to accommodate less than 3 digit values.

This example reads serial data on pin 16.

const byte numChars = 4;
char readBuf[numChars];
static byte ndx = 0;
byte tempf = 0; 
byte humif = 0; 
byte val = 0;

#define RXD2 16
#define TXD2 17

void setup()
{
  Serial.begin(115200);
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
  Serial.println("Starting");
}
void loop()
{

  if (Serial2.available())
  {
    //for (int i = 0; i < 2; i++)
    //{
      char c = Serial2.read();
      if (c != '\n')
      {
        readBuf[ndx] = c;
        ndx++;
        readBuf[ndx] = '\0'; //null terminate next index
        if (ndx >= numChars)
        {
          ndx = numChars - 1;
        }
      }
      else
      {  
        val += 1;
        switch (val)
        {
          case 1:
            {
              tempf = atoi(readBuf);
              ndx = 0;
              Serial.println(tempf);
              break;
            }
          case 2:
            {
              val = 0;
              humif = atoi(readBuf);
              ndx = 0;
               Serial.println(humif);
              break;
            }
        }
      }
    //}
  }
}

I solved it.

#include <Arduino.h>
#include <WiFi.h>
#include <Firebase_ESP_Client.h>
#include "addons/TokenHelper.h"
#include "addons/RTDBHelper.h"
#include <BME280I2C.h>
#include <Wire.h>

#define RX 16
#define TX 17

#define WIFI_SSID " "
#define WIFI_PASSWORD " "

#define API_KEY " "

#define DATABASE_URL " "

const byte numChars = 4;
char readBuf[numChars];
static byte ndx = 0;

unsigned long sendDataPrevMillis = 0;
unsigned long sendGraphDataPrevMillis = 0;

bool signupOK = false;


int tempf = 0;
int humif = 0;

int val = 0;

BME280I2C bme;
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial1.begin(9600, SERIAL_8N1, RX, TX);
  Wire.begin();
  bme.begin();
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(300);
  }
  Serial.println();
  Serial.print("Connected with IP: ");
  Serial.println(WiFi.localIP());
  Serial.println();

  config.api_key = API_KEY;
  config.database_url = DATABASE_URL;

  if (Firebase.signUp(&config, &auth, "", "")) {
    Serial.println("ok");
    signupOK = true;
  }
  else {
    Serial.printf("%s\n", config.signer.signupError.message.c_str());
  }

  config.token_status_callback = tokenStatusCallback;

  Firebase.begin(&config, &auth);
  Firebase.reconnectWiFi(true);
}
void loop() {
  if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0)) {
    sendDataPrevMillis = millis();
    sendGraphDataPrevMillis = millis();

    if (Serial1.available() > 0) {
      Serial.println("Processing Arduino values");
      while (val != 2) {
        char c = Serial1.read();
        if (c != '\n') {
          readBuf[ndx] = c;
          ndx++;
        }
        else {
          val += 1;
          switch (val) {
            case 1: {
                tempf = atoi(readBuf);
                readBuf[ndx] = '\0';
                ndx = 0;
                break;
              }
            case 2: {
                humif = atoi(readBuf);
                Serial.println(humif);
                Serial.println(tempf);
                readBuf[ndx] = '\0';
                ndx = 0;
                break;
              }
          }
        }
      }
      val = 0;
    }

    float temp(NAN), hum(NAN), pres(NAN);
    BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
    BME280::PresUnit presUnit(BME280::PresUnit_Pa);
    bme.read(pres, temp, hum, tempUnit, presUnit);
    pres = pres / 100;

    Firebase.RTDB.setFloat(&fbdo, "bme/teplota", temp);
    Firebase.RTDB.setFloat(&fbdo, "bme/tlak", pres);
    Firebase.RTDB.setInt(&fbdo, "dht/teplota", tempf);
    Firebase.RTDB.setInt(&fbdo, "dht/vlhkost", humif);


    if (millis() - sendGraphDataPrevMillis > 3600000000 || sendGraphDataPrevMillis == 0) {
      Firebase.RTDB.pushFloat(&fbdo, "graf/teplotag", temp);
      Firebase.RTDB.pushFloat(&fbdo, "graf/tlakg", pres);
      Firebase.RTDB.pushInt(&fbdo, "graf/teplotaf", tempf);
      Firebase.RTDB.pushInt(&fbdo, "graf/vlhkostf", humif);
      sendGraphDataPrevMillis == 0;
    }
  }
}

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