converting int or byte to ascii

Dunno if it works in arduino but google atoi function from stdlib library.

I am doing exact oposite of atoi

I am still not convinced of the need to convert the integer to ASCII. I look forward to seeing the example programs that demonstrate the problem.

to send a webpage to esp i need to calculate the size of the content. Its a long string and can differ in size sometimes depending what varibles will hold at that moment so size of webpage will differ too.

But to send size in ascii i need to convert it from int to ascii. Like in my example of “256”.

The other wierd thing that guys noticed is why i cant use serial.print? Cos that will solve that problem as its converting to ascii before sending.

Are my receiving and sending functions wrong? They are working flawlessly no corrupted data no buffer overflow so far.

When are you going to post the example send and receive programs ?

What you mean? I posted in post#13

surepic:
What you mean? I posted in post#13

What happened to the simple, complete examples that you promised that would copy/paste into the IDE, compile, and demonstrate the problem?

Ah now i got it. After work will post exactly whats going on.

Exactly! But not const 256. Any number with length up to 3. I.e. 1-999

I am posting below a two UNOs setup for communication over SUART-UART Ports. UNO-1 sends 3-digit integer ID using Intel-Hex Frame over SUART Port. UNO-2 receives the frame using UART Port; it extracts the ID number from the received frame, saves it in array, and shows it on Serial Monitor.

You may find it useful!
A: Connection Diagram between UNO-1 and UNO-2

Fig-1

B: Example ID 256 has been sent using in Intel-Hex Frame

Fig-2

C: Example ID has been received over Intel-Hex Frame

Fig-3

D: Transmitter (UNO-1) Codes

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); //SRX and STX of UNO-1
byte frameArray[9]; //totl 9-byte in the Intel-Hex Frame
byte dataArray[3] = {0x02,  0x05, 0x06}; //for id = 256

void setup() 
{
  Serial.begin(9600);             // Serial port to computer
  mySerial.begin(9600);               // Serial port to HC12
}

void loop() 
{  
  buildIntelHexFrame();     //INtel-hex Frame as byte
  sendIntelHexFrame();     //send Intel every digit of Frame as ASCII codes
  while(1)        //wait for ever
    ;
}

void sendIntelHexFrame()
{
   mySerial.write(frameArray[0]);       //send : start of frame
   Serial.print((char)frameArray[0]);
  
  for(int i = 1; i<9; i++)
  {
    byte x = frameArray[i];
    byte x1 = x;
    x1 = x1>>4;
    if (x1 <=9)
    {
      x1 = x1+0x30;
      mySerial.write(x1);
       Serial.write(x1);
    }
    else
    {
      mySerial.write(x1+0x37);
       Serial.write(x1+0x37);
    }
  //--------------------------------
    x = frameArray[i];
    x = x & 0x0F;
    if (x <=9)
    {
      x = x+0x30;
      mySerial.write(x);
       Serial.write(x);
    }
    else
    {
      mySerial.write(x+0x37);
       Serial.write(x+0x37);
    }
    
  } 
  Serial.println();
}


void buildIntelHexFrame() //Intel-Hex Frame (fields a, b, c, d, e, f)
{
  frameArray[0] = 0x3A; //field-a (:) Start of a of a frame
  frameArray[1] = 03;   //filed-b = number of information byte inn field-e
  frameArray[2] = 0x20; //field-c EEPROM buffer address - upper byte
  frameArray[3] = 0x00; //EEPROM bufferaddress - lower byte
  frameArray[4] = 0x00; //field-d Not End-of-File Marker (01 = EOF)
  //-------------------
  
  for (int i=5, j=0; i<8, j<3; i++, j++)//filed-e information byte 3
  {
    frameArray[i] =  dataArray[j];
  }
  frameArray[8] = chkSum();
}

byte chkSum()
{
  byte sum=0;
  for (int k = 1; k<8 ; k++)
  {
    sum += frameArray[k]; 
  }
  sum =~sum;
  sum++;
  return sum;
}

E: Received Side (UNO-2) Codes

//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(10, 11); //SRX and stx OF uno-2

bool flag = false;;  //Frame synchronizer has not come
byte frameArray[19];   //
byte x, y, y1;
int i = 0;
int j = 0;

byte idData[3];  //lower-most array elemnet contains lower-most byte

void setup()
{
  Serial.begin(9600);             // Serial port to computer
 //  mySerial.begin(9600);
}

void loop()
{
  if (i == 17) //complete frame is received 8-byte x 2 = 16 + extra post i++ = 17
  {
    buildAndShowIdNumber();  //nuumber in: z, p, and myData[] of union{}
    i = 0;
    flag = false;
    i = 0;
    j = 0;
  }

}

void buildAndShowIdNumber()
{
  Serial.println();//write(frameArray[18]);
  for (i = 9, j = 0; i < 15, j < 3; i++, j++)  //6-character 3-byte
  {
    y = frameArray[i];
    if (y < 0x41)
    {
      y = y & 0x0F;
      y = y << 4;
    }
    else
    {
      y = y - 0x37;
      y = y << 4;
    }
    //-------------------
    i = i + 1; //i++;
    y1 = frameArray[i];
    if (y1 < 0x41)
    {
      y1 = y1 & 0x0F;

    }
    else
    {
      y1 = y1 - 0x37;

    }
    //------------------------
    y = y | y1;      //byte formed 02 05 06
    idData[j] = y;   //received ID in idData[3] array as 02 05 06
  }
  Serial.print(idData[0]);
  Serial.print(idData[1]);
  Serial.print(idData[2]);
  Serial.println();
}


void serialEvent()
{
  if (flag == true)
  {
    while (Serial.available())
    { // UNO-2 has received rame synchronizer
      x = Serial.read();
      Serial.write(x);     // Send the data to Serial monitor
      frameArray[i] = x;    //save in array
      i++;                  //index remains incremented by 1
    }
  }

  else
  {
    byte s = Serial.read();
    if ( s != 0x3A)
    {
      flag = false;
    }
    else
    {
      if ( s == 0x3A)
      {
        Serial.write(s);
        flag = true;
        i++;                  //index incremented
      }
    }

  }
}

BTW: UNO-2 has used Hardware Serial (UART) Port. So, please remove the jumper wires from DPin-0 and DPin-1 while uploading sketch into UNO-2.

outsider:
How 'bout itoa?

void setup()

{
  Serial.begin(9600);
  char buffer[6] = " ";
  int id = 256;
  itoa(id,buffer,10);
  for(byte q = 0;q < 6;q++)
  {
    Serial.write(buffer[q]);
    Serial.println();
  }
  Serial.println(buffer); 
}






2
5
6

256

where is your #include stdlib.h??

@GolamMostafa
Thanks for example. I did couple projects with unos connected with uarts but same approach isnt working with esp. im talking with that thing via at commands converted to ascii if no ascii it doesnt react. If i would have a choice i would definitely switch to xbee. But esp is very cheap even though found it not reliable too sensetive to power(voltage and current).

surepic:
This way I am converting int to ascii if its between 0-9 ascii number.

Serial.write((char)id+48);

but is there easy way to deal with int whose value is >128?

lets say if int id=200.

Serial.print is not working in my case. I need serial.write.

Also tried using modulo in for loop its working but may be shorter way is possible.
thanks.

this is your original request and I have gone thru this thread and I'm lost!

I would like to understand this:

"lets say if int id=200."

If you were to use serial.write to print it to serial monitor u'd get some weird character... that not what you want, right?

what you want to achieve is to somehow use serial.write to achieve what serial.print(200,DEC) can do, right?
FYI to do that the arduino would spit out 4 bytes as ASCII '2' (0x32), '0' (0x30), '0' (0x30) and '\0' (0x00).

Am I correct to understand that all that you want the arduino to spit out is the first 3 bytes?

sherzaad:
this is your original request and I have gone thru this thread and I'm lost!

I would like to understand this:

"lets say if int id=200."

If you were to use serial.write to print it to serial monitor u'd get some weird character... that not what you want, right?

what you want to achieve is to somehow use serial.write to achieve what serial.print(200,DEC) can do, right?
FYI to do that the arduino would spit out 4 bytes as ASCII '2' (0x32), '0' (0x30), '0' (0x30) and '\0' (0x00).

Am I correct to understand that all that you want the arduino to spit out is the first 3 bytes?

You would spit out 3 bytes - the null terminator is not sent to the serial port.

TolpuddleSartre:
You would spit out 3 bytes - the null terminator is not sent to the serial post.

right... got mixed up with atoi and like functions... strange then that the OP can't seem to get the desired output with just using serial.print then...

Sorry guys but i cant copy and paste empty screen. Receiving function is just sitting and waiting for any string no ascii filtering. Serial.available() is waiting 5 whole seconds and not getting anything from esp.Even with serial.println(“AT”) im not getting any reply from esp. seems like esp is freezing.
With serial.write without 1ms delay same thing its freezing. With 1ms delay its working.

With softwareserial example im sending through serial monitor any esp command and am getting reply.

If its cos of long wires that esp cant read at that speed then why via serial monitor its responding.

If you cannot/will not post simple, complete examples of the sending and receiving programs then I don't see how we can provide any more help.

Demonstrating println problem.
Here we go:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX
char string[] = "AT\r\n";
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  mySerial.begin(115200);
  Serial.println("arduino");
  for (byte i = 0; i <= strlen(string) - 1; i++) {
    mySerial.write(string[i]);
    Serial.print(string[i], DEC);
    Serial.print('\t');
    Serial.println(string[i]);
    delay(1);
  }
  //mySerial.write("AT");
  Serial.println("Esp");
}

void loop() { // run over and over
  while (mySerial.available()) {
    Serial.write(mySerial.read());
  }
}

output:

arduino
65	A
84	T
13	

10	

Esp
AT

OK

now with serial.print

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX
//char string[] = "AT\r\n";
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  mySerial.begin(115200);
  Serial.println("arduino");
  mySerial.println("AT");
  //delay(100);
  //mySerial.write("AT");
  Serial.println("Esp");
}

void loop() { // run over and over
  while (mySerial.available()) {
    Serial.write(mySerial.read());
  }
}

output:

arduino
Esp
A⸮⸮⸮

and with 100ms delay added after serial.print

arduino
Esp
A⸮⸮⸮

exact same output.

mySerial.begin(115200);

Have you tried a lower baud rate as SoftwareSerial is generally not trusted at high baud rates

I assume from the pin numbers used that the code posted is running on an Arduino and that it is communicating with an ESP. What code is running on the ESP ? Have you got a level shifter between the Arduino and the ESP ?

At lower baud rates is even worse i tried even 2400. Level shifter is on 2 n type mosfets with resistors.

I ordered 2nd esp just to be sure that the one im using is not defective. Does not make sense to me but this println problem is blowing my brain.

surepic:
Does not make sense to me but this println problem is blowing my brain.

yeah tried your code and there is some weird stuff going on... could you post how you wired your circuit to see if I did the same as you?