primary objective

1. From windy weather <>API Get weather data , After analysis, it is sent to warship development board through serial port
2. Warship development board drives sensors to collect real-time weather data , Through serial port PC machine

Reference Engineering

Thanks for the post of smart cloud IOT developer community , This system STM32 End very short code reference from this God post : warship V3 adopt ESP8266 Get online weather
<>, Computer end Java Main reference of serial port compiling ( download ): use Java Realize serial communication
, Although a lot of code is not written by me , But a lot of efforts have been made to set up the course , I hope it can be recorded . Thank you again to the two gods !

Project notes

Json String parsing

Windy weather API Back to a Json Strings like
{"HeWeather6":[{"basic":{"cid":"CN101010100","location":" Beijing ","parent_city":" Beijing "
,"admin_area":" Beijing ","cnty":" China ","lat":"39.90498734","lon":"116.4052887","tz":
"+8.0"},"update":{"loc":"2018-01-27 16:50","utc":"2018-01-27 08:50"},"status":
" cloudy ","cond_txt_n":" cloudy ","date":"2018-01-27","hum":"34","mr":"13:16","ms":"02:41"
tmp_min":"-9","uv_index":"2","vis":"16","wind_deg":"224","wind_dir":" Southwest wind ","
wind_sc":" breeze ","wind_spd":"8"},{"cond_code_d":"100","cond_code_n":"100","
cond_txt_d":" Fine ","cond_txt_n":" Fine ","date":"2018-01-28","hum":"15","mr":"14:05","ms
wind_dir":" north wind ","wind_sc":" breeze ","wind_spd":"3"},{"cond_code_d":"100","cond_code_n
":"100","cond_txt_d":" Fine ","cond_txt_n":" Fine ","date":"2018-01-29","hum":"21","mr":
,"wind_dir":" Southwest wind ","wind_sc":" breeze ","wind_spd":"4"}],"sunrise_sunset":[{"$ref":
It can be seen that , To get the right data , You have to parse this Json character string , Online formatting can be found through Google Json tool , It's like this , Attached conveyor door

Here we use online formatting just to facilitate the next step of parsing , first , Create a JSONObject object
JSONObject json1 = JSONObject.fromObject(sb.toString());
establish JSONArray
JSONArray array1 = json1.getJSONArray("HeWeather6");
use array1 The first element in is created again JSONObject object
JSONObject json2 = JSONObject.fromObject(array1.getString(0));
Create a file named daily_forecast Of JSONArray
JSONArray array2 = json2.getJSONArray("daily_forecast");
Output partial weather data of the last three days
for(int i = 0;i < array2.size();i++){ //System.out.println(array2.getString(i))
; String string3 = array2.getString(i); JSONObject json3 = JSONObject.fromObject
(string3); System.out.print(" time :"+json3.getString("date")+" "); System.out
.print(" Minimum temperature :"+json3.getString("tmp_min")+" "); System.out.print(" maximum temperature :"+json3
.getString("tmp_max")+" "); System.out.print(" weather :"+json3.getString("cond_txt_d"
)+" "); System.out.println(" humidity :"+json3.getString("hum")); }
console output
time :2018-01-27 Minimum temperature :-8 maximum temperature :-2 weather : fine with occasional clouds humidity :34 time :2018-01-28 Minimum temperature :-9 maximum temperature :-1 weather : Fine
humidity :15 time :2018-01-29 Minimum temperature :-8 maximum temperature :1 weather : Fine humidity :21

My analysis Json It's very simple , Continuous creation JSONObject and JSONArray object , Layer by layer analysis , Finally, get the required data , click Json Format explanation

Processing of data sent by serial port

First of all, we need to know ,Java Write serial port tools serial port send data of the type is byte[], In this way, we only need to change the data to be sent into byte[] Just the type ,

Get weather data
WeatherData weatherDataHF = new WeatherData(
Processing of weather data

Next, it seems that we only need to pass these data through getBytes() Method transformation is OK, but let's think about the data we got before , Is there anything wrong ? you 're right ! Serial port cannot send Chinese characters !!! So we need to process the data here , Convert to number or string , I learned computer architecture this semester , So I referred to the look-up table method , Turn weather into a specific combination of numbers , If it's sunny 001, Cloudy days 010, The temperature is also represented by three digits , The first digit is 0 Is higher than 0℃, by 1 It means less than 0℃, Such as minimum temperature -1℃ Expressed as 101.OK, The first problem is solved .

Xingchongchong to test serial data transmission , But the data sent doesn't show any ghost , It's strange that the data is not transmitted after each transmission , But after using the serial port to send the assistant, click the first time and you will get the result , Turn over the serial port receiving code of the warship ,
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Receive interrupt ( Received data must be 0x0d
0x0a ending )
Receive interrupt must be ox0d 0x0a ending ! So we need to put the data \r\n ending ,
StringBuilder weatherData = new StringBuilder(); weatherData.delete(0,
weatherData.length()); weatherData.append(weatherDataHF.getWeatherData());
byte[] order = weatherData.toString().getBytes(); SerialTool.sendToPort
(serialPort, order);
Please ignore my brain circuit code ,Java Beginners can't afford it .


In general, many codes of this course are copied and pasted from the Internet , But there are still a lot of problems and they are basically solved , The outstanding problems are :
PC Terminal UI The interface sometimes crashes , Guess it's a data overflow too fast
The data transmission on the warship side needs to be higher than a certain frequency , Otherwise PC Data not received at end , Guess it's a software problem , Serial assistant can work normally

Complete engineering documents

be based on STM32 And windy weather API Interaction system of <>
notes : Warship development board needs dht11 Temperature and humidity sensor , Without sensors, you can keil Comment out the related initialization call function in the file .