Giter Club home page Giter Club logo

http_get_and_post_with_json's Introduction

HTTP_GET_and_POST_with_JSON

Overview

Example of HTTP GET and POST with JSON on STM32F746 Discovery Board.

Development environment

Name Description Note
STM32F746G-DISCO Development board with ARM Cortex-M7 provided by STMicroelectronics Link
STM32 Cube IDE IDE Version 1.5.1
LwIP_HTTP_Server_Netconn_RTOS Example code from SDK provided by STMicroelectronics

cJSON API

Use the cJSON API for JSON.

For Parsing JSON Data

JSON_Parse() function in main.c have parse JSON syntax using cJSON API.

For Create JSON Data

JSON_Make() function in main.c have make JSON syntax using cJSON API.

Settings

Setting for IP

Set IP in Firmware for Connect to Internet. You can set it in "inc/main.h".
If you use DHCP, enable define about USE_DHCP.

#define USE_DHCP /* enable DHCP, if disabled static address is used */

If you use static IP, disable define about USE_DHCP and modify information of your IP/Netmask/Gateway.
If in case IP address is 192.68.20.113, Netmask is 255.255.255.0, Gateway address is 192.68.20.1,

//#define USE_DHCP /* enable DHCP, if disabled static address is used */

/*Static IP ADDRESS*/
#define IP_ADDR0   192
#define IP_ADDR1   68
#define IP_ADDR2   20
#define IP_ADDR3   113
   
/*NETMASK*/
#define NETMASK_ADDR0   255
#define NETMASK_ADDR1   255
#define NETMASK_ADDR2   255
#define NETMASK_ADDR3   0

/*Gateway Address*/
#define GW_ADDR0   192
#define GW_ADDR1   68
#define GW_ADDR2   20
#define GW_ADDR3   1

Setting for lwIP

Change LWIP_SOCKET value 0 to 1 in "inc/lwipopts.h" because We will communicate HTTP protocol using TCP socket of lwIP.

#define LWIP_SOCKET 1

HTTP

Send GET Reqeust to HTTP Server

You can send HTTP GET request to HTTP Server using HTTP_GET_Reqeust() function in "main.c".
This example is connect to "openweathermap.org" and then receive weather information of Seoul after send HTTP GET request.

GET /data/2.5/weather?q=Seoul,KR&units=metric&appid=9dacd623d8637f89267d33608c32700c HTTP/1.0\r\nHost:api.openweathermap.org\r\n\r\n\r\n
// Configure HTTP header for GET request
uint8_t http_head[]="GET /data/2.5/weather?q=Seoul,KR&units=metric&appid=9dacd623d8637f89267d33608c32700c HTTP/1.0\r\nHost:api.openweathermap.org\r\n\r\n\r\n";
// Send HTTP header to HTTP server
ret = send(CControl, http_head, sizeof(http_head), 0);

Receive response data for HTTP GET request.

// Check response
do {
	// Read response from HTTP server
	ret = read(CControl, &u8DataBuffer[nCounter], 1024);
	if (ret != 0) {
		if ((nCounter + ret) > 1023) {
			// Overrun
		}
		else {
			nCounter += ret;
		}
	}
} while(ret != 0);

JSON data received from HTTP Server is output to LCD. image

Send POST Reqeust to HTTP Server

You can send HTTP POST request to HTTP Server using HTTP_POST_Reqeust() function in "main.c".
This example is connect to "webhook.site" and then check data using web browser after send HTTP POST request.

POST http://webhook.site/9aa08aba-4ead-4fd9-9278-1ac46e0f3224 HTTP/1.0\r\nHost:webhook.site\r\nContent-Type:application/json\r\nContent-Length:[Size of body data]\r\n\r\n[Body data]
// Configure HTTP header for POST request
uint8_t http_head[1024];
memset(&http_head[0], 0, sizeof(http_head));
sprintf(&http_head[0], "POST /9aa08aba-4ead-4fd9-9278-1ac46e0f3224 HTTP/1.0\r\nHost:webhook.site\r\nContent-Type:application/json\r\nContent-Length:%d\r\n\r\n%s",
	strlen(gu8DataBuffer),
	gu8DataBuffer);

// Send HTTP header to HTTP server
ret = send(CControl, http_head, sizeof(http_head), 0);

You can check data in link below.
https://webhook.site/#!/9aa08aba-4ead-4fd9-9278-1ac46e0f3224/a1f651c7-f298-430a-8105-e0737f080b63/1 image

Thanks to

cJSON

http_get_and_post_with_json's People

Contributors

super-thomas avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

mox19

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.