Hi, I have a RS232 serial device which works when I connect to my computer, using Realterm to send the command and receive back the data. Now I want to replace the computer with ESP32, so could I just program the ESP32 first, to serial.write() the command, and when it returns the data, I could serial.read the bytes in an array and upload the elements in the array to the cloud. Now my question is, is it possible to just use the same USB port that I use for Serial-to-USB converter to program my ESP32, to connect to my RS232 device using a USB-to-RS232 converter? So I will just be connecting the 5V and ground pins to external power supply.
//copy this link and put in File, Preferences, Additional Boards Manager:
//https://dl.espressif.com/dl/package_esp32_index.json
#include <WiFi.h>
#include "Esp32MQTTClient.h"
//Wifi SSID and password
const char* ssid = "";
const char* password = "";
//Azure IoT Hub device primary connection string
static const char* connectionString = "";
static bool hasIoTHub = false;
void setup() {
Serial.begin(115200, SERIAL_8N1);
Serial.setTimeout(10);
delay(10);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
delay(500);
if (!Esp32MQTTClient_Init((const uint8_t*)connectionString))
{
hasIoTHub = false;
Serial.println("Initializing IoT hub failed.");
return;
}
hasIoTHub = true;
}
void loop() {
Serial.println("Loop Begin");
delay(1000);
byte command[] = {0xFC, 0x74, 0x01, 0x00, 0xFE, 0x71};
int commandLength = sizeof(command) / sizeof(command[0]);
Serial.write(command, commandLength); // Send the command
delay(1000);
int availableBytes = Serial.available();
byte buffer[availableBytes];
for (int i = 0; i < availableBytes; i++) {
buffer[i] = Serial.read();
}
delay(500);
String jsonString = "{";
jsonString += "\"values\":[";
for (int i = 0; i < availableBytes; i++) {
jsonString += String(buffer[i]);
if (i < availableBytes - 1) {
jsonString += ",";
}
}
jsonString += "]";
jsonString += "}";
jsonString.replace("\\", "");
char buff[256];
snprintf(buff, sizeof(buff), "%s", jsonString.c_str());
if (Esp32MQTTClient_SendEvent(buff)) {
;
} else {
;
}
delay(2000);
}