Port an ESP32 code to Arduino UNO

I want to use the Stone touch screen with Arduino UNO to do a weather station project. I saw the article on the web is done by using an ESP32. I want to port the ESP32 code to Arduino UNO. But I changed half of it and found it is not complete. Although the basic functions are written out the code inside the setup is not given.

I am now hesitant about what code should be written in the setup? I have written a baud rate myself.
This is the code I wrote halfway through.

void setup() {
  Serial.begin(115200);
}

void loop() {

int lightState = 0;
bool TemperatureBool = false; 
bool HumidityBool = false;
bool illuminationBool = false;
bool illuminationState = false;

uint8_t TemperatureValueInteger = 0;
uint8_t TemperatureValueDecimal = 0;
uint8_t TemperatureValue = 0;

uint8_t HumidityValue = 0;
uint8_t illuminationValue = 0;

uint8_t TemperatureOutput[8] = {0xA5, 0x5A, 0x05, 0x82, 0x00, 0x10, 0x00, 0x00};
uint8_t HumidityOutput[8] = {0xA5, 0x5A, 0x05, 0x82, 0x00, 0x15, 0x00, 0x00};
uint8_t illuminationOutput[8] = {0xA5, 0x5A, 0x05, 0x82, 0x00, 0x20, 0x00, 0x00};
uint32_t cout_i;
uint8_t RecievedTemp[40];

  
 if(Serial.available() != 0)
  {
    for(cout_i = 0; cout_i < 9; cout_i ++)
    {
        RecievedTemp[cout_i] = Serial.read();
    }
    switch(RecievedTemp[5])
    {
    case 0x0D://Temperature start
        TemperatureBool = true;
        break;
    case 0x0E://Temperature stop
        TemperatureBool = false;
        TemperatureOutput[6] = 0;
        TemperatureOutput[7] = 0;
        Serial.write(TemperatureOutput, 8);
        break;
    case 0x0F://Temperature back
        TemperatureBool = false;
        break;
    case 0x11://Humidity start
      HumidityBool = true;
        break;
    case 0x12://Humidity stop
        HumidityBool = false;
        HumidityValue = 0;
        HumidityOutput[7] = HumidityValue;
        Serial.write(HumidityOutput, 8);
        break;
    case 0x10://Humidity back
        HumidityBool = false;
        break;
    case 0x14://illumination start
      illuminationBool = true;
      illuminationState = false;
        break;
    case 0x15://illumination stop
      illuminationBool = false;
      illuminationValue = 0;
      illuminationOutput[7] = illuminationValue;
      Serial.write(illuminationOutput, 8); 
        break;
    case 0x13://illumination back
      illuminationValue = 0;
      illuminationBool = false;
      break;
    default:
        break;
    }
}
}

This looks like it will compile and pass.

I didn’t get you. It seems like you are porting an ESP32 code to Arduino UNO.

Have you compiled the code? Do you get any errors? Can you post the other code too?

1 Like

Yes, this ESP project is something I saw online, and I see that he is using the Arduino IDE to program the ESP32, so I think the same code is applicable to the Arduino UNO. But the problem is that the code he gives is not complete, he does not give the program inside the setup function. I compiled his sample code in Arduino IDE with no problem and it passed.

Here is part of the code he gave:

int lightState = 0;          //variable for reading push button status
bool TemperatureBool = false; 
bool HumidityBool = false;
bool illuminationBool = false;
bool illuminationState = false;

uint8_t TemperatureValueInteger = 0;
uint8_t TemperatureValueDecimal = 0;
uint8_t TemperatureValue = 0;

uint8_t HumidityValue = 0;
uint8_t illuminationValue = 0;

uint8_t TemperatureOutput[8] = {0xA5, 0x5A, 0x05, 0x82, 0x00, 0x10, 0x00, 0x00};
uint8_t HumidityOutput[8] = {0xA5, 0x5A, 0x05, 0x82, 0x00, 0x15, 0x00, 0x00};
uint8_t illuminationOutput[8] = {0xA5, 0x5A, 0x05, 0x82, 0x00, 0x20, 0x00, 0x00};

if(Serial.available() != 0)
  {
    for(cout_i = 0; cout_i < 9; cout_i ++)
    {
        RecievedTemp[cout_i] = Serial.read();
    }
    switch(RecievedTemp[5])
    {
    case 0x0D://Temperature start
        TemperatureBool = true;
        break;
    case 0x0E://Temperature stop
        TemperatureBool = false;
        TemperatureOutput[6] = 0;
        TemperatureOutput[7] = 0;
        Serial.write(TemperatureOutput, 8);
        break;
    case 0x0F://Temperature back
        TemperatureBool = false;
        break;
    case 0x11://Humidity start
      HumidityBool = true;
        break;
    case 0x12://Humidity stop
        HumidityBool = false;
        HumidityValue = 0;
        HumidityOutput[7] = HumidityValue;
        Serial.write(HumidityOutput, 8);
        break;
    case 0x10://Humidity back
        HumidityBool = false;
        break;
    case 0x14://illumination start
      illuminationBool = true;
      illuminationState = false;
        break;
    case 0x15://illumination stop
      illuminationBool = false;
      illuminationValue = 0;
      illuminationOutput[7] = illuminationValue;
      Serial.write(illuminationOutput, 8); 
        break;
    case 0x13://illumination back
      illuminationValue = 0;
      illuminationBool = false;
      break;
    default:
        break;
    }

Sorry for the late response. I missed your topic in the feeds.

I find nothing wrong with your code except those variables. All those variables are inside the main loop. You should take it outside the loop function. If you keep those variables inside the loop, the value of those variables will be resets backs to its original state on every itration.

yes, that’s right! I have moved them at the beginning.
Here is the code I have modified. Could you please check it?

int lightState = 0;
bool TemperatureBool = false;
bool HumidityBool = false;
bool illuminationBool = false;
bool illuminationState = false;

uint8_t TemperatureValueInteger = 0;
uint8_t TemperatureValueDecimal = 0;
uint8_t TemperatureValue = 0;

uint8_t HumidityValue = 0;
uint8_t illuminationValue = 0;

uint8_t TemperatureOutput[8] = {0xA5, 0x5A, 0x05, 0x82, 0x00, 0x10, 0x00, 0x00}; //A5 5A 06 83 00 0A 01 00 02
uint8_t HumidityOutput[8] = {0xA5, 0x5A, 0x05, 0x82, 0x00, 0x15, 0x00, 0x00}; //A5 5A 06 83 00 14 01 00 05
uint8_t illuminationOutput[8] = {0xA5, 0x5A, 0x05, 0x82, 0x00, 0x20, 0x00, 0x00}; //A5 5A 06 83 00 18 01 00 08
uint32_t cout_i;
uint8_t RecievedTemp[40];

#include "dht11.h"
dht11 DHT;
#define DHT11_PIN 5
#define PIN_A 3


void setup() {
  
  Serial.begin(115200);
  
}

void loop() {

DHT.read(DHT11_PIN);
  
 if(Serial.available() != 0)
  {
    for(cout_i = 0; cout_i < 9; cout_i ++)
    {
        RecievedTemp[cout_i] = Serial.read();
    }
    switch(RecievedTemp[5])
    {
    case 0x0A://Temperature start
        TemperatureBool = true;
        break;
    case 0x0C://Temperature stop
        TemperatureBool = false;
        TemperatureValue = DHT.temperature;
        TemperatureOutput[7] = TemperatureValue;
        Serial.write(TemperatureOutput, 8);
        break;
    case 0x0E://Temperature back
        TemperatureBool = false;
        break;
    case 0x14://Humidity start
      HumidityBool = true;
        break;
    case 0x16://Humidity stop
        HumidityBool = false;
        HumidityValue = DHT.humidity;
        HumidityOutput[7] = HumidityValue;
        Serial.write(HumidityOutput, 8);
        break;
    case 0x13://Humidity back
        HumidityBool = false;
        break;
    case 0x18://illumination start
      illuminationBool = true;
      illuminationState = false;
        break;
    case 0x19://illumination stop
      illuminationBool = false;
      illuminationValue = analogRead(PIN_A);
      illuminationOutput[7] = illuminationValue;
      Serial.write(illuminationOutput, 8); 
        break;
    case 0x17://illumination back
      illuminationValue = 0;
      illuminationBool = false;
      break;
    default:
        break;
    }
}
}

These variable addresses I have modified according to my GUI.

I didn’t understand. What GUI? Is it working now?

Yes, no matter what, it works now.
GUI is the design interface of my TFT LCD. It’s like this.

3 Likes

I thought you were talking about LCD GUI display. Forgot the fact that the stone LCD has design software. Anyway, good to know that everything is fine.