primary objective

1. From windy weather <https://www.heweather.com/>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 portPC machine

Reference project

Thanks for the post of smart cloud IOT developer community, This systemSTM32 End very short code reference from this God post: WarshipV3 adoptESP8266 Get online weather
<http://club.gizwits.com/thread-8049-1-1.html>, Computer terminalJava Main reference of serial port compiling( download): UseJava Realize serial communication
<http://blog.csdn.net/kong_gu_you_lan/article/details/52302075>
, 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

Wind weatherAPI Back to aJson 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":
"ok","daily_forecast":[{"cond_code_d":"101","cond_code_n":"101","cond_txt_d":
" Cloudy","cond_txt_n":" Cloudy","date":"2018-01-27","hum":"34","mr":"13:16","ms":"02:41"
,"pcpn":"0.0","pop":"0","pres":"1032","sr":"07:26","ss":"17:28","tmp_max":"-2","
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
":"03:50","pcpn":"0.0","pop":"0","pres":"1035","sr":"07:25","ss":"17:29","
tmp_max":"-1","tmp_min":"-8","uv_index":"2","vis":"20","wind_deg":"350","
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":
"15:03","ms":"04:57","pcpn":"0.0","pop":"0","pres":"1032","sr":"07:24","ss":
"17:31","tmp_max":"1","tmp_min":"-8","uv_index":"2","vis":"20","wind_deg":"259"
,"wind_dir":" Southwest wind","wind_sc":" Breeze","wind_spd":"4"}],"sunrise_sunset":[{"$ref":
"$[0].daily_forecast[0]"},{"$ref":"$[0].daily_forecast[1]"},{"$ref":
"$[0].daily_forecast[2]"}]}]}
It can be seen that, To get the right data, You have to parse thisJson Character string, Online formatting can be found through GoogleJson tool, It's like this, Attached portal
<https://www.bejson.com/>

Here we use online formatting just to facilitate the next step of parsing, First, Create aJSONObject object
JSONObject json1 = JSONObject.fromObject(sb.toString());
EstablishJSONArray
JSONArray array1 = json1.getJSONArray("HeWeather6");
usearray1 The first element in is created againJSONObject object
JSONObject json2 = JSONObject.fromObject(array1.getString(0));
Create namedaily_forecast OfJSONArray
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
Summary

My analysisJson It's very simple, Constantly createJSONObject andJSONArray object, Layer upon layer analysis, Finally, get the required data, clickJson Format explanation
<http://blog.csdn.net/xukai871105/article/details/32346797>.

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 isbyte[], In this way, we only need to change the data to be sent intobyte[] Just the type,

Get weather data
WeatherData weatherDataHF = new WeatherData(
"key=db3e861fc7654111800271e316414820&location="+cityName.toString(),
"https://free-api.heweather.com/s6/weather/forecast?parameters");
Processing of weather data


Next, it seems that we only need to pass these data throughgetBytes() 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, For sunny days001, Cloudy day010, The temperature is also represented by three digits, The first digit is0 Is higher than0℃, by1 It means less than0℃, Such as minimum temperature-1℃ Expressed as101.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) // Interruption of reception( Received data must be0x0d
0x0a Ending)
Receive interrupt must beox0d 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.

summary

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 TerminalUI 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, OtherwisePC Data not received at end, Guess it's a software problem, Serial assistant can work normally

Complete engineering documents

Be based onSTM32 And windy weatherAPI Interaction system of <http://download.csdn.net/download/m0_37620545/10227385>
notes: Warship development board needsdht11 Temperature and humidity sensor, Without sensors, you cankeil Comment out the related initialization call function in the file.