cancel
Showing results for 
Search instead for 
Did you mean: 

Can't Send POST Request

hidirektor
Associate

I'm trying to send POST request to my docker & node.js based server. But i cant send. Im using STM32F103RCT6 & ESP8266 (ESP-01)

void connectWifi() {
	HAL_UART_Transmit(&huart1, (uint8_t *)"AT\r\n", strlen("AT\r\n"), HAL_MAX_DELAY);
	HAL_Delay(1000);
	HAL_UART_Transmit(&huart1, (uint8_t *)"AT+RST\r\n", strlen("AT+RST\r\n"), HAL_MAX_DELAY);
	HAL_Delay(1000);
	HAL_UART_Transmit(&huart1, (uint8_t *)"AT+CWMODE=1\r\n", strlen("AT+CWMODE=1\r\n"), HAL_MAX_DELAY);
	HAL_Delay(1000);

	char wifiCommand[50];
	sprintf(wifiCommand, "AT+CWJAP=\"%s\",\"%s\"\r\n", WIFI_SSID, WIFI_PASSWORD);
	HAL_UART_Transmit(&huart1, (uint8_t *)wifiCommand, strlen(wifiCommand), HAL_MAX_DELAY);
	HAL_Delay(2000);

	char response[100];
	HAL_UART_Receive(&huart1, (uint8_t *)response, sizeof(response), HAL_MAX_DELAY);
}

void sendHTTPRequest() {
    char tcpCommand[50];
    sprintf(tcpCommand, "AT+CIPSTART=\"TCP\",\"%s\",%d\r\n", SERVER_IP, SERVER_PORT);
    HAL_UART_Transmit(&huart1, (uint8_t *)tcpCommand, strlen(tcpCommand), HAL_MAX_DELAY);
    HAL_Delay(1000);

    char httpCommand[200];
    char postData[200];
    sprintf(postData, "{\"machineID\":\"%s\",\"machineData\":\"%s\"}", MACHINE_ID, MACHINE_DATA);
    sprintf(httpCommand, "AT+CIPSEND=%d\r\n", strlen(postData) + 94);
    HAL_UART_Transmit(&huart1, (uint8_t *)httpCommand, strlen(httpCommand), HAL_MAX_DELAY);
    HAL_Delay(1000);
    HAL_UART_Transmit(&huart1, (uint8_t *)"POST /api/machine/updateMachineData HTTP/1.1\r\n",
                     strlen("POST /api/machine/updateMachineData HTTP/1.1\r\n"), HAL_MAX_DELAY);
    HAL_UART_Transmit(&huart1, (uint8_t *)"Host: x.x.x.x:3000\r\n", strlen("Host: x.x.x.x:3000\r\n"), HAL_MAX_DELAY);
    HAL_UART_Transmit(&huart1, (uint8_t *)"Content-Type: application/json\r\n",
                     strlen("Content-Type: application/json\r\n"), HAL_MAX_DELAY);
    HAL_UART_Transmit(&huart1, (uint8_t *)"Content-Length: ", strlen("Content-Length: "), HAL_MAX_DELAY);
    char contentLength[4];
    sprintf(contentLength, "%d\r\n", strlen(postData));
    HAL_UART_Transmit(&huart1, (uint8_t *)contentLength, strlen(contentLength), HAL_MAX_DELAY);
    HAL_UART_Transmit(&huart1, (uint8_t *)"\r\n", strlen("\r\n"), HAL_MAX_DELAY);
    HAL_UART_Transmit(&huart1, (uint8_t *)postData, strlen(postData), HAL_MAX_DELAY);
    HAL_Delay(2000);

    char responseBuffer[20];
    memset(responseBuffer, 0, sizeof(responseBuffer));

    HAL_UART_Receive(&huart1, (uint8_t *)responseBuffer, sizeof(responseBuffer) - 1, HAL_MAX_DELAY);
    if (strncmp(responseBuffer, "HTTP/1.1 200 OK", 15) == 0) {
        lcd_print(2, 1, "İşlem Başarılı");
    } else {
        lcd_print(2, 1, "Hata Oluştu");
    }
}


static void MX_USART1_UART_Init(void)
{

  /* USER CODE BEGIN USART1_Init 0 */

  /* USER CODE END USART1_Init 0 */

  /* USER CODE BEGIN USART1_Init 1 */

  /* USER CODE END USART1_Init 1 */
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */

  /* USER CODE END USART1_Init 2 */

}

 

5 REPLIES 5
liaifat85
Senior II

Please check if  you're receiving expected responses for each AT command. You should print the responses to the UART to help with debugging. This will help you identify any issues with the ESP8266 setup.

I tried debugging using the LCD as shown below, but it still does not throw the query I want and I cannot solve it. Do you have any suggestions on this issue? I am having a problem with the POST query, that is, the last query.

HAL_UART_Transmit(&huart1, (uint8_t *)"AT+CIPSTART=\"TCP\",\"85.95.231.92\",80\r\n", strlen("AT+CIPSTART=\"TCP\",\"85.95.231.92\",80\r\n"), HAL_MAX_DELAY);
	      HAL_Delay(1000);
	      lcd_print(1, 5, "E");

	      HAL_UART_Transmit(&huart1,
	                        (uint8_t *)"POST /api/machine/updateMachineData HTTP/1.1\r\nHost: http://85.95.231.92:3000\r\nContent-Type: application/json\r\nContent-Length: 50\r\n\r\n{\"machineID\":\"12345\",\"machineData\":\"111001011021210101001210000102012345678923456\"}",
	                        strlen("POST /api/machine/updateMachineData HTTP/1.1\r\nHost: 85.95.231.92:3000\r\nContent-Type: application/json\r\nContent-Length: 50\r\n\r\n{\"machineID\":\"12345\",\"machineData\":\"111001011021210101001210000102012345678923456\"}"),
	                        HAL_MAX_DELAY);
	      HAL_Delay(1000);

	      if (!wait_for_response("200 OK", 10000)) {
	          handle_error();
	          continue;
	      } else {
	    	  lcd_print(1, 6, "F");
	    	  lcd_print(2, 1, "Islem Basarili");
	      }

 

 

Pavel A.
Evangelist III

As @liaifat85 answered, you need to read the wi-fi thing replies and handle possible errors. What does wait_for_response function?

You can get help with coding or debugging issues here.

 

I think printing the queries in the UART will be better. Because, the LCD may have an issue with itself too. If you are sure that the LCD is functioning properly, that's OK.  

Piranha
Chief II

 

HAL_UART_Transmit(...);
HAL_Delay(2000);
HAL_UART_Receive(...);

 

And how is this nonsense supposed to work at all?

 

Also write at least a somewhat normal code instead of those repeated constant strings:

 

const char *pszData;
size_t nbData;

pszData = "POST /api/machine/updateMachineData HTTP/1.1\r\n";
nbData = strlen(pszData);
HAL_UART_Transmit(&huart1, (uint8_t *)pszData, nbData, HAL_MAX_DELAY);

 

 Or even better just wrap it into a function:

 

void SendStr(const char *pszStr)
{
	HAL_UART_Transmit(&huart1, (uint8_t *)pszStr, strlen(pszStr), HAL_MAX_DELAY);
}

SendStr("POST /api/machine/updateMachineData HTTP/1.1\r\n");