Obtain the weather and display it on the OLED through STM32 and Esp8266

Obtain the weather and display it on the OLED through STM32 and Esp8266

Experiment overview: send AT command to esp8266 serial port through stm32 to connect it to its own hotspot to obtain the weather, and send it back and display it on OLED through serial port

Serial communication

Let stm32 communicate with esp8266 through serial port, which will not be repeated here. In fact, it is the most common usart. Just remember to change Tx and Rx when connecting.

Get weather data

Send the following command to ESP 8266 to configure it to obtain weather data

AT+CWMODE=1
/Set working mode 1: station mode 2: AP mode 3: compatible with AP+station mode/
:AT+CIPMUX=0
/0: single connection, 1: multi connection/
AT+CWJAP="ssid","password"
/Set the WIFI network name / encryption method / password connected to (encryption method is not set here)/
At this point, we have configured the 8266 network, and the next step is to send the api
AT+CIPSTART="TCP","api.seniverse.com",80
The api for knowing the weather is used here
AT+CIPMODE=1
Turn on transparent transmission mode
Next, just send and get json
GET https://api.seniverse.com/v3/weather/now.json?key= Put your private password here & location = Pinyin of the address you want & language = en & unit = C
(the password can be found in the personal information on the official website of Xinzhi weather after registration)

In this way, the json can be obtained through 8266, and then transmitted back to stm32 through the serial port. The next thing is stm32

Parsing json

We use C language to compile MCU, but it is difficult for C language to parse json, so it is not so easy to parse json format data, but sometimes we need to deal with such data format. Here is a function library to parse cjson for us.
Here is a connection to download the cJSON library
https://files.cnblogs.com/files/piaoyang/cJSONFiles.rar
Use the cjson H and cjson Just C

Let me explain the main functions inside

typedef struct cJSON {

  struct cJSON *next,*prev;      /* A forward or backward list pointer that traverses an array or object chain*/

  struct cJSON *child;                   /*Child node of an array or object*/

   int type;                                     /* key Type of*/

  char *valuestring;                       /*String value*/

  int valueint;                                /* Integer value*/

  double valuedouble;                    /* Floating-Point Number */

   char *string;                               /* key Name of*/

} cJSON;

This is a very important structure. The storage mode of cJSON is a two-way linked list. I won't explain the linked list more here (I'll talk about it later when I write RTOS notes)
The type here is the type of key. There are seven types: False, true, NULL, Number, String, Array and Object.
The Number is stored in valueint or valuedouble. The specific location depends on what is inside (int or double)
String is stored in valuestring, which is the name of the key

Important interface functions inside

cJSON *cJSON_Parse(const char *value); 
   //Parsing JSON packets
cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
//Gets the object member specified by json
cJSON *cJSON_GetArrayItem(cJSON *array,int item);
//If the second obtained array uses this to obtain the subscript object specified by the array
cJSON_Delete();
//Free memory

Next, drop my custom parsing function

int Parsejson(char *JSON, Results *results)//result
{
	cJSON *json,*arrayItem,*object,*subobject,*item;
	json = cJSON_Parse(JSON); //Parsing JSON packets
	if(json == NULL)		  //Returning NULL means that the packet is invalid. Check whether there is an error in the packet
	{
		OLED_ShowString(0,0,(unsigned char*)"false",12);
		return 1;
	}
	else
	{
		if((arrayItem = cJSON_GetObjectItem(json,"results")) != NULL);
		{
			int size = cJSON_GetArraySize(arrayItem);     //Gets the number of objects in the array
			if((object = cJSON_GetArrayItem(arrayItem,0)) != NULL)//Get parent object content
			{
				if((subobject = cJSON_GetObjectItem(object,"now")) != NULL)//Match sub objects
				{
					if((item = cJSON_GetObjectItem(subobject,"temperature")) != NULL) //Match sub object 2 member "temperature"
					{
						printf("cJSON_GetObjectItem: type=%d, string is %s,valuestring=%s \r\n",item->type,item->string,item->valuestring);
						memcpy(results[0].now.temperature,item->valuestring,strlen(item->valuestring));
					}	
				}
			}
		}
		cJSON_Delete(json); //Free up memory space
		return 0;
	}

You can use this to analyze the temperature, but you can not only analyze the temperature, but also the address and weather. The method is the same. Just add it in it. There are no more examples here.

data processing

Through the above user-defined function, we have put the results into results. Just take them out of the structure. Let's talk about how to define the structure
Structure is defined as follows

typedef struct
{
	char text[32];
	char code[32];
	char temperature[32];
}Now;

typedef struct
{
	Location location;		//Sub object 1
	Now now;				//Sub object 2
	char last_update[64];	//Sub object 3
}Results;

Results results[] = {{0}};

So the temperature value after can be used

results[0].now.temperature //  So you can take it out

This is my previous oled program
Just display the data according to the code in the link

OLED_ShowString(60, 6, results[0].now.temperature, 16);

Pay attention to the debugging of delay function
The experiment is mainly to parse json, which is fresh, and others are cliches, so this is the end
If I am not talented, there must be omissions. I hope I can correct it.

Keywords: stm32

Added by Mykasoda on Sun, 23 Jan 2022 03:02:17 +0200