Help needed with int conversion to char

I've researched this extensively and still can't solve it.

I am trying to write a sketch that will take an analogue reading every 100ms over two minutes and at the end send the collection of data to a file via ftp.

Ideally, the ftp file will look like "400;401;309;.... etc

My problem is with converting the integer value from A0, then concatenating with ";" to produce a single string as shown above.

Any help would be appreciated.

Where are you stuck ?

Read a value
Append it to the file using print
Append a semicolon to the file using print
Wait 100 milliseconds
Repeat for 2 minutes

Please post your best effort sketch, using code tags when you do

Which arduino ? How do you reach the FTP server ?

Print will convert the int into ascii and you can also print a char like a comma

Thanks for the replies. The ftp process writes a file to my WD myCloud nas device. It takes about 1 second to complete so I need to collect the data first and transfer it as one set when complete.
What I'm looking for is:-

  1. take a reading (as integer)
  2. convert to char
  3. add ";" to separate the values
    repeat the above, say 200 times at 100ms intervals.
    When complete, send the data by ftp.
    Thanks again.

So, will whatever Arduino you're using(?) have enough RAM to hold a buffer 120 * 10 * 5 plus whatever overhead you need for this string you're building? Are you pre-allocating the whole buffer, or using something like String+ to append? Could you send it in smaller blocks?
Maybe if you show us your code so far, we'll have a better understanding of where your problem lies.

You don't need to do that . Building the String as you go is memory intensive, you could delay that to later.

You could Loop to acquire your samples every 100ms over 2 minutes and store them in RAM (2400 bytes, so you'll need an somewhat capable Arduino, not an UNO or Nano).

once you have acquired the 1200 samples you open your FTP channel and dump the data

here is how this could work, dumping the data on the Serial monitor

constexpr unsigned long samplePeriodMs = 100ul;               // Sampling period in milliseconds
constexpr unsigned long durationMs = 5000ul;                  // use 120000ul for 2 minutes (Total duration (2 minutes in milliseconds))
constexpr size_t sampleCount = durationMs / samplePeriodMs;   // Total number of samples
uint16_t samples[sampleCount];                                 // Array to store samples in RAM

void setup() {
    Serial.begin(115200);
    Serial.println("Starting sampling...");

    unsigned long startTime = millis();
    for (size_t i = 0; i < sampleCount; i++) {
        samples[i] = analogRead(A0);                // Acquire sample from  A0
        delay(samplePeriodMs);                      // Wait for next sample time
    }

    Serial.println("Sampling complete.");
    Serial.println("Sampled data:");
    for (unsigned int i = 0; i < sampleCount; i++) {
        Serial.print(samples[i]);                   // Print each sample to Serial
        if (i != sampleCount-1) Serial.write(',');  // add the comma where needed 
    }
    Serial.println();                               // terminate by a new line
}

void loop() {}

(this code would only run for 5 seconds, so acquiring 50 samples if you don't want to wait for 2 minutes).

Thanks a lot but I should have said that the device will be operating well away from any computer so serial output is no use. Yes, I need to check available RAM on the esp8266
Apologies.
I've so many sketches on this, need to sort out a sensible one and post it here.

as I said, thats just sample code.

Instead of dumping the data to the Serial stream, just dump it iteratively into the FTP stream (libraries have ways to write to the output) and then close the stream.

an ESP8266 should be fine, you need to find a good FTP client library for it. (I don't use the 8266 anymore, there are FTP clients for ESP32)

Ah yes. I'm using FTPClient_Generic. When I timed the ftp process it included the opening and closing functions. This took about 1 second, clearly the write process alone will be so much quicker.
Thanks for all your help, I'll get cracking now. The end is in sight!

this approach formats a string for each valus and concatenates it to a larger string that can be transmitted when full.


char buf [1000];
char tmp [5];
int  cnt;

void
loop (void)
{
    int val = analogRead (A0);
    sprintf (tmp, "%d;", val);
    strcat  (buf, tmp);
    cnt++;
    int len = strlen (buf);
    Serial.print   (cnt);
    Serial.print   (" ");
    Serial.println (len);

    if (990 < strlen (buf))  {
        Serial.print   ("send ");
        Serial.println (buf);
        cnt = 0;
        buf [0] = 0;
    }
    delay (100);
}

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

Although you are on the safe side, the use of snprintf() and strlcat() would probably bring peace of mind for such a code

1 Like

Thanks again, here is my working sketch.
I learned a lot from this.

I am glad that you got it working

Please post your sketch here (in code tags) to avoid the need to download it


// 2024-10-28
#include <ESP8266WiFi.h>        // Include the Wi-Fi library
#include <FTPClient_Generic.h>

char ftp_server[] = "192.168.1.101";
char ftp_user[]   = "esp8226";
char ftp_pass[]   = "XXXXX";
char dirName[]    = "meters";
char newDirName[] = "/home/ftp_test/NewDir";
FTPClient_Generic ftp (ftp_server, ftp_user, ftp_pass, 60000);

char fileName[] = "flashtest01.txt";

const char* ssid     = "XXXX";         // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "XXXXXX";     // The password of the Wi-Fi network

const int analogInPin = A0;  // ESP8266 Analog Pin ADC0 = A0
int timer1 =10000;  //10 seconds
char tmp [5];

void setup() {
  Serial.begin(9600);
  while (!Serial);            // wait for Serial Monitor to open
  
  WiFi.begin(ssid, password);             // Connect to the network
  Serial.print("Connecting to ");
  Serial.print(ssid); Serial.println(" ...");

  
  while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
    delay(1000);
    Serial.print('.');
  }

  Serial.println('\n');
  Serial.println("Connection established!");  
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());         // Send the IP address of the ESP8266 to the computer

  ftp.OpenConnection();
  ftp.ChangeWorkDir(dirName); //Change directory
  ftp.InitFile(COMMAND_XFER_TYPE_ASCII); // Create a new file to use as the download example below:
  ftp.AppendFile(fileName);

}

void loop() {
  while (timer1 > 0){
    int val = analogRead(analogInPin);
    sprintf (tmp, "%d;", val);
    ftp.Write(tmp);
    --timer1;
    delay(100);
  }
  ftp.CloseFile();
  Serial.println("Done");
  delay(10000);
}
  

Thank you