1

I need to send a get request using a string, and so I need to pass a float and a char value to the string in order to send it.Im trying to connect a PIC18F4550 to wifi using a ESP8266 module I also need to read and write into a database. I have been using this function I made to send AT Commands and its been working fine :

void send (char dato[]){
    int i = 0;
    while (dato[i]!=0){
        TXREG=dato[i];
        i++;
        while(TRMT==0);
    }
    TXREG = 0x0D; 
    while(TRMT==0);
    TXREG = 0x0A;
}

The problem I have is that i need to send :

send("GET /ESPic/index3.php?temp=temp&luz=luz");

But luz is char and temp is float.Using a FTDI232 and Arduino IDE I am reading the data between the PIC and the ESP8266.I dont really know how to do what I need.

Hugo Bevilacqua
  • 362
  • 4
  • 12
Sebastián Mayorga
  • 151
  • 1
  • 3
  • 17
  • 2
    Please indent your code correctly. Where is `luz` and `temp` in your code? – Jabberwocky Oct 02 '18 at 15:41
  • 1
    Can you build the message string with `sprintf` before transmitting it? Aside, the code is missing an initial or final status check. – Weather Vane Oct 02 '18 at 15:43
  • Note that you might have to enable the `printf` function family in the cross-compiler. Also please see [Using floats with sprintf() in embedded C](https://stackoverflow.com/questions/905928/using-floats-with-sprintf-in-embedded-c). – Weather Vane Oct 02 '18 at 16:18

2 Answers2

3

Assumimg your platform supports sprintf, you probably want this:

float temp;
char luz;
...
char buffer[200];
sprintf(buffer, "GET /ESPic/index3.php?temp=%f&luz=%c", temp, luz);
send(buffer);
alk
  • 69,737
  • 10
  • 105
  • 255
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • If temp has a 37.5 value and luz a 0 value, what would be the buffer´s length? I need it for the `send("AT+CIPSEND=AA")` in which AA is the length – Sebastián Mayorga Oct 02 '18 at 19:17
  • Use strlen(buffer) to get the length of the buffer. In your CIPSEND=AA, is AA always 2 characters with leading zero if necessary? Note that leading zero in C means octal. – cup Oct 02 '18 at 20:37
  • The buffer will be NUL terminated. Read the chapter dealing with strings in your C text book. – Jabberwocky Oct 02 '18 at 20:48
1

Convert the float to a string first.


When sending a textual version of float, best to use avoid "%f" and use "%e", "%g", or "%a" with enough precision.

"%f" can be very long for large numbers. It coverts to an uninformative +/- "0.000000" for about half of all float (the small ones).

These 3 formats e,g,a have better control on the maximum length and easier to insure the needed precision is used.

float temp;
char luz;
// send("GET /ESPic/index3.php?temp=temp&luz=luz");

#define SEND_FC_FMT "GET /ESPic/index3.php?temp=%.*e&luz=%c"
//                     -   d   .    ddd...ddd            e   - d...d \0 
#define FLT_ESTR_SIZE (1 + 1 + 1 + (FLT_DECIMAL_DIG-1) + 1 + 1 + 5 + 1)
char buffer[sizeof SEND_FC_FMT + FLT_ESTR_SIZE];

sprintf(buffer, SEND_FC_FMT, FLT_DECIMAL_DIG-1, temp, luz);
send (buffer);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256