firebase-realtime-databasestm32esp8266at-command

I can't send AT command to my backend server from ESP-01. Error 400 Bad Request


Hi I have been trying to use stm and esp-01 and cominicate with firebase with backend server. my backend server js is this:

const express = require('express');
const app = express();
const firebaseAdmin = require('firebase-admin');
const serviceAccount = require('.myFirebaseAdminSDK.json');
firebaseAdmin.initializeApp({
    credential: firebaseAdmin.credential.cert(serviceAccount),
    databaseURL: 'myFirebaseURL' 
});

app.use(express.json());

app.post('/sendData', (req, res) => {
    const { temperature } = req.body;


    const db = firebaseAdmin.database();
    db.ref('bme280').push({ temperature }) 
        .then(() => {
            res.send('Data sent successfully');
        })
        .catch(error => {
            console.error('Error sending data:', error);
            res.status(500).send('Error sending data');
        });
});
const PORT = process.env.PORT || 6355;
app.listen(PORT, () => {
    console.log(`Server started on port ${PORT}`);
});

and I send AT commands

AT+CIPSTART="TCP","192.168.1.38",6355
AT+CIPSEND=113
POST /sendData 
Host: 192.168.1.38
Content-Type: application/json
Content-Length: 20


{"temperature":"25"}

but I receive from my ESP-01 this

AT+CIPSTART="TCP","192.168.1.38",6355

CONNECT

OK
AT+CIPSEND=113


OK
> 
Recv 113 bytes

SEND OK

+IPD,47:HTTP/1.1 400 Bad Request
Connection: close

CLOSED

I check that my server is working from windowsshellWindowsShell ss and this worked for my firebase realtime data base as well Firebase ss So I couldnt figure out. I found this sending Json data to server using AT commands but I already send like this. Why am I using backend server ? Because Firebase switched to TLS 1.2 and AT firmware doesn't support it. If you have a better solution, please let me know.


Solution

  • Okay I solve the issue.

    AT+CIPSTART="TCP","192.168.1.38",6355
    AT+CIPSEND=113
    POST /sendData 
    Host: 192.168.1.38
    Content-Type: application/json
    Content-Length: 20
    
    
    {"temperature":"25"}
    

    I sent like this and I got bad request. Then I send like this.

    AT+CIPSTART="TCP","192.168.1.38",6355
    AT+CIPSENDEX=500
    POST /sendData 
    Host: 192.168.1.38
    Content-Type: application/json
    Content-Length: 23
    
    
    {"temperature":"25"}
    \0
    

    now it takes the request. I think content length got me before. the code that sends the AT commands like this

    sprintf(value,"{\"value\":%d}",sampleData);
    sprintf(postRequest, "POST /sendDataTemperature HTTP/1.1\r\nHost: ");
    strcat(postRequest, urlOfServer);
    strcat(postRequest, "\r\nContent-Type: application/json\r\n");
    sprintf(contentLenght, "Content-Length: %d\r\n", strlen(value)+3);
    strcat(postRequest, contentLenght);
    strcat(postRequest, "\r\n\r\n");
    strcat(postRequest, value);
    
    Uart_flush();
    Uart_sendstring("AT+CIPSTART=\"TCP\",\"");
    Uart_sendstring(urlOfServer);
    sprintf(port,"\",%s\r\n",portOfServer);
    Uart_sendstring(port);
    HAL_Delay(500);
    
    char chipsend[50];
    sprintf(chipsend, "AT+CIPSENDEX=%d\r\n", 500);
    Uart_sendstring(chipsend);
    HAL_Delay(500);
    Uart_sendstring(postRequest);
    HAL_Delay(200);
    Uart_sendstring("\r\n\\0\r\n");
    HAL_Delay(500);
    Uart_sendstring("AT+CIPCLOSE\r\n");
    while(!(Wait_for("OK\r\n")));
    HAL_Delay(200);