Testing HTTP requests with ESP-WROOM-32

Testing HTTP requests with ESP-WROOM-32

Since it became possible to develop with Environment construction of ESP-WROOM-32, I tested the communication function.

Thing you want to do

Send HTTP POST from ESP-WROOM-32 to the web server. The web server receives it and posts it to slack to confirm the success of POST.

What you have prepared

RaspberryPi3 (used as a web server) ESP32-DevKitC ESP-WROOM-32 Development Board

Referenced site

Crafts and Horse Racing HTTP, POST and GET bitlog

Development on the ESP-WROOM-32 side

Since there is a sample of http_request in ESP-IDF, I decided to use it. Copy the sample source.

cd ~/esp
cp -r $IDF_PATH/examples/protocols/http_request .
cd ~/esp/http_request

Modify main / http_request_example_main.c.

http_request_example_main.c


/* HTTP GET Example using plain POSIX sockets

This example code is in the Public Domain (or CC0 licensed, at your option.)

Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"

#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"

/* The examples use simple WiFi configuration that you can set via
'make menuconfig'.

If you'd rather not, just change the below entries to strings with
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD

#define DEFAULT_PS_MODE WIFI_PS_NONE
/* FreeRTOS event group to signal when we are connected & ready to make a request */
static EventGroupHandle_t wifi_event_group;

/* The event group allows multiple bits for each event,
but we only care about one event - are we connected
to the AP with an IP? */
const int CONNECTED_BIT = BIT0;

/* Constants that aren't configurable in menuconfig */
#define WEB_SERVER "192.168.xxx.xxx" //Here is the IP of the WEB server
#define WEB_PORT 80
#define WEB_URL "http://192.168.xxx.xxx" //URL here

static const char *TAG = "test";

//HTTP POST message
//Request body is text=test
static const char *REQUEST = "POST " WEB_URL " HTTP/1.0\r\n"
							"Host: "WEB_SERVER"\r\n"
							"User-Agent: esp-idf/1.0 esp32\r\n"
							"Content-Type: application/x-www-form-urlencoded\r\n"
							"Content-Length: 9\r\n"
							"\r\n"
							"text=test\r\n"
							;


static esp_err_t event_handler(void *ctx, system_event_t *event)
{
	switch(event->event_id) {
		case SYSTEM_EVENT_STA_START:
			esp_wifi_connect();
			break;
		case SYSTEM_EVENT_STA_GOT_IP:
			xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
			break;
		case SYSTEM_EVENT_STA_DISCONNECTED:
			/* This is a workaround as ESP32 WiFi libs don't currently
			auto-reassociate. */
			esp_wifi_connect();
			xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
			break;
		default:
			break;
	}
	return ESP_OK;
}

static void initialise_wifi(void)
{
	tcpip_adapter_init();
	wifi_event_group = xEventGroupCreate();
	ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
	wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
	ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
	ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
	wifi_config_t wifi_config = {
		.sta = {
		.ssid = EXAMPLE_WIFI_SSID,
		.password = EXAMPLE_WIFI_PASS,
		},
	};
	ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
	ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
	ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
	ESP_ERROR_CHECK( esp_wifi_start() );
}

static void http_post_task(void *pvParameters)
{
	const struct addrinfo hints = {
		.ai_family = AF_INET,
		.ai_socktype = SOCK_STREAM,
	};
	struct addrinfo *res;
	struct in_addr *addr;
	int s, r;
	char recv_buf[64];

	/* Wait for the callback to set the CONNECTED_BIT in the
	event group.
	*/
	xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
	false, true, portMAX_DELAY);
	ESP_LOGI(TAG, "Connected to AP");

	int err = getaddrinfo(WEB_SERVER, "80", &hints, &res);

	if(err != 0 || res == NULL) {
		ESP_LOGE(TAG, "DNS lookup failed err=%d res=%p", err, res);
		vTaskDelay(1000 / portTICK_PERIOD_MS);
		return;
	}

	/* Code to print the resolved IP.

	Note: inet_ntoa is non-reentrant, look at ipaddr_ntoa_r for "real" code */
	addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
	ESP_LOGI(TAG, "DNS lookup succeeded. IP=%s", inet_ntoa(*addr));

	s = socket(res->ai_family, res->ai_socktype, 0);
	if(s < 0) {
		ESP_LOGE(TAG, "... Failed to allocate socket.");
		freeaddrinfo(res);
		vTaskDelay(1000 / portTICK_PERIOD_MS);
		return;
	}
	ESP_LOGI(TAG, "... allocated socket\r\n");

	if(connect(s, res->ai_addr, res->ai_addrlen) != 0) {
		ESP_LOGE(TAG, "... socket connect failed errno=%d", errno);
		close(s);
		freeaddrinfo(res);
		vTaskDelay(4000 / portTICK_PERIOD_MS);
		return;
	}

	ESP_LOGI(TAG, "... connected");
	freeaddrinfo(res);

	//Send HTTP message here
	if (write(s, REQUEST, strlen(REQUEST)) < 0) {
		ESP_LOGE(TAG, "... socket send failed");
		close(s);
		vTaskDelay(4000 / portTICK_PERIOD_MS);
		return;
	}
	ESP_LOGI(TAG, "... socket send success");

	/* Read HTTP response */
	do {
		bzero(recv_buf, sizeof(recv_buf));
		r = read(s, recv_buf, sizeof(recv_buf)-1);
		for(int i = 0; i < r; i++) {
			putchar(recv_buf[i]);
		}
	} while(r > 0);

	ESP_LOGI(TAG, "... done reading from socket. Last read return=%d errno=%d\r\n", r, errno);
	close(s);

	ESP_LOGI(TAG, "esp_wifi_set_ps().");
	esp_wifi_set_ps(DEFAULT_PS_MODE);

	while(1);
}

void app_main()
{
	ESP_ERROR_CHECK( nvs_flash_init() );
	initialise_wifi();
	xTaskCreate(&http_post_task, "http_post_task", 4096, NULL, 5, NULL);
}

This completes the test program on the ESP-WROOM-32 side.

Development on the Raspberry Pi side

Since RaspberryPi is used as a Web server, install Apache2 etc. as needed. Here, the details of Web server construction are omitted. Describe the HTTP request response processing in PHP on the server side.

index.php


<?php
function func_esp(){
	exec("python3 /home/pi/python_code/slack_esp.py");
	return 'test';
}

if($_POST['text'] == "test"):
	func_esp();
endif;
?>

If text = test is detected in the POST request Run a Python script.

Next, create a python script to post to Slack. I'm using Slacker.

slack_esp.py


import sys
from slacker import Slacker

token = "xxxx-xxxx-xxxxxxxx"
slacker = Slacker(token)
channel_name = "#" + "general"

message = 'esp32 test\n'

slacker.chat.post_message(channel_name, message)

If the HTTP POST is successful, you will receive the message "esp32 test". This completes the program creation on the Raspberry Pi side.

test

Write the program to ESP-WROOM-32 and execute it.

cd ~/esp/http_request
make flash

Notify your iPhone here! IMG_0845.png The test was successful.

Recommended Posts

Testing HTTP requests with ESP-WROOM-32
Testing Elasticsearch with python-tcptest
HTTP communication with Python
Retry with python requests
Automate python testing with CircleCI
Pre-try local testing with Travis
Easy HTTP server with Python
Testing with random numbers in Python
Automate smartphone app testing with Appium-Python
Introducing MicroPython (Mac) shell with ESP-WROOM-02
[Python] POST wav files with requests [POST]
Try http-prompt with interactive http access
Get the weather with Python requests
Get the weather with Python requests 2
Easy AWS S3 testing with MinIO
Send multipart / form-data with python requests
Automate Windows Application Testing with Windows Application Driver-Python