单片机气象网站api接口(天气预报api接口)

网友投稿 280 2023-01-06

本篇文章给大家谈谈单片机气象网站api接口,以及天气预报api接口对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。 今天给各位分享单片机气象网站api接口的知识,其中也会对天气预报api接口进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

如何实现单片机通过WIFI获取时间和天气数据?

有两种方案解决这个问题。
1:单片机链接WIFI,WIFI接入互联网,自己买一个天气平台网站API接口,从网站上读取。
2:自己写一个软件获取天气、时间等信息,把软件放到服务器里面,WIFI做客户端,接入服务器,服务器把天气时间数据发给客户端,客户端就是你的WIFI模块。

天气预报API接口是什么?

天气预报API接口是气象服务商为客户提供服务的唯一接口。拿墨迹赤必为例,跟客户充分沟通后了解客户的实际需求,从而对现有的数据进行调整,符合客户的需求形成API接口,让客户接入,这样就形成了一个完整的服务。所以说API接口并不是一个很神秘的东西,只不过是企业和客户之间的一条服务纽带。

​android软件开发:中国气象局api接口(显示4、5天的天气),能实际有用的,日期也得正确

中国国家气象局天气预报接口总共提供了三个:

http://www.weather.com.cn/data/sk/101010100.html

http://www.weather.com.cn/data/cityinfo/101010100.html

http://m.weather.com.cn/data/101010100.html

最详细的信息来自第三个接口。上面url中的101010100是城市代码,这里是北京的城市代码。只需要改变城市代码,就可以得到所在城市的天气信息。笔者在福州,所以选择的城市代码是福州101230101。
在浏览器上输入url:http://m.weather.com.cn/data/101230101.html得到信息,天气信息是json的数据格式,数据如

{"weatherinfo":{"city":"福州","city_en":"fuzhou","date_y":"2012年5月14 日","date":"","week":"星期 一","fchh":"08","cityid":"101230101","temp1":"29℃~23℃","temp2":"26℃~20℃","temp3":"24℃~20℃","temp4":"25℃~20℃","temp5":"24℃~21℃","temp6":"25℃~22℃","tempF1":"84.2℉~73.4℉","tempF2":"78.8℉~68℉","tempF3":"75.2℉~68℉","tempF4":"77℉~68℉","tempF5":"75.2℉~69.8℉","tempF6":"77℉~71.6℉","weather1":" 阵雨转中雨","weather2":"中雨转小雨","weather3":"小雨","weather4":"小雨","weather5":"小雨 转阵雨","weather6":"阵雨转小 雨","img1":"3","img2":"8","img3":"8","img4":"7","img5":"7","img6":"99","img7":"7","img8":"99","img9":"7","img10":"3","img11":"3","img12":"7","img_single":"3","img_title1":" 阵雨","img_title2":"中雨","img_title3":"中雨","img_title4":"小雨","img_title5":" 小雨","img_title6":"小雨","img_title7":"小雨","img_title8":"小雨","img_title9":" 小雨","img_title10":"阵雨","img_title11":"阵雨","img_title12":"小 雨","img_title_single":"阵雨","wind1":"微风","wind2":"微风","wind3":"微 风","wind4":"微风","wind5":"微风","wind6":"微风","fx1":"微风","fx2":"微风","fl1":"小 于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3 级","index":"热","index_d":"天气较热,建议着短裙、短裤、短套装、T恤等夏季服装。年老体弱者宜着长袖衬衫和单 裤。","index48":"暖","index48_d":"较凉爽,建议着长袖衬衫加单裤等春秋过渡装。年老体弱者宜着针织长袖衬衫、马甲和长 裤。","index_uv":"弱","index48_uv":"最弱","index_xc":"不宜","index_tr":"适 宜","index_co":"较不舒 适","st1":"27","st2":"21","st3":"24","st4":"18","st5":"22","st6":"18","index_cl":" 较不宜","index_ls":"不太适宜","index_ag":"不易发"}}[/code]
我们可以解析json数据去得到自己想用的天气信息。
天气信息解释:
{
"weatherinfo":{
<!-- 基本信息 --
"city":"福州",
"city_en":"fuzhou",
"date_y":"2012年5月14日",
"date":"",
"week":"星期一",
"fchh":"08",
"cityid":"101230101",
<!-- 从今天开始到第六天的每天的天气情况,这里的温度是摄氏温度 --
"temp1":"29℃~23℃","temp2":"26℃~20℃","temp3":"24℃~20℃","temp4":"25℃~20℃","temp5":"24℃~21℃","temp6":"25℃~22℃",
<!-- 从今天开始到第六天的每天的天气情况,这里的温度是华氏温度 --
"tempF1":"84.2℉~73.4℉","tempF2":"78.8℉~68℉","tempF3":"75.2℉~68℉","tempF4":"77℉~68℉","tempF5":"75.2℉~69.8℉","tempF6":"77℉~71.6℉",
<!-- 天气描述 --
"weather1":"阵雨转中雨","weather2":"中雨转小雨","weather3":"小雨","weather4":"小雨","weather5":"小雨转阵雨","weather6":"阵雨转小雨",
<!-- 天气描述图片序号 --
"img1":"3","img2":"8","img3":"8","img4":"7","img5":"7","img6":"99","img7":"7","img8":"99","img9":"7","img10":"3","img11":"3","img12":"7","img_single":"3",
<!-- 图片名称 --
"img_title1":"阵雨","img_title2":"中雨","img_title3":"中雨","img_title4":"小雨","img_title5":"小雨","img_title6":"小雨","img_title7":"小雨","img_title8":"小雨","img_title9":"小雨","img_title10":"阵雨","img_title11":"阵雨","img_title12":"小雨","img_title_single":"阵雨",
<!-- 风速描述 --
"wind1":"微风","wind2":"微风","wind3":"微风","wind4":"微风","wind5":"微风","wind6":"微风","fx1":"微风","fx2":"微风",
<!-- 风速级别描述 --
"fl1":"小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级",
<!-- 今天穿衣指数 --
"index":"热",
"index_d":"天气较热,建议着短裙、短裤、短套装、T恤等夏季服装。年老体弱者宜着长袖衬衫和单裤。",
<!-- 48小时穿衣指数 --
"index48":"暖","index48_d":"较凉爽,建议着长袖衬衫加单裤等春秋过渡装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",
<!-- 紫外线及48小时紫外线 --
"index_uv":"弱","index48_uv":"最弱",
<!-- 洗车 --
"index_xc":"不宜",
<!-- 旅游 --
"index_tr":"适宜",、
<!-- 舒适指数 --
"index_co":"较不舒适",
"st1":"27","st2":"21","st3":"24","st4":"18","st5":"22","st6":"18",
<!-- 晨练 --
"index_cl":"较不宜",
<!-- 晾晒 --
"index_ls":"不太适宜",
<!-- 过敏 --
"index_ag":"不易发"
}
}

如何使用中国气象局API开发天气APP

前期的准备工作:
一、申请API(拿好appid和private_key)

二、解读《SmartWeatherAPI<Lite WebAPI版接口使用说明书》

三、准备好areaid、type、date、appid、urlencode($key)(注意,这里经加密的key是需要encodeurl之后的才能成为接口链接的一部分)
好了下面的编码开始:
1、从附件中的areaid_list中找到你想要的地方的areaid,并且选择要查询天气的类型
NSString *areaid = @"101010100";
NSString *type =
@"index_f";
/**
* 官方文档更新的数据类型号
* 指数:index_f(基础接口);index_v(常规接口)
3天预报:forecast_f(基础接口);forecast_v(常规接口)
*
*/
2、获得当前天气date
NSDate
*_date = [NSDate date];
NSDateFormatter *dateFormatter =
[[NSDateFormatter alloc] init];
[dateFormatter
setDateFormat:@"yyyyMMddHHmmss"];//注意日期的格式
NSString *date =
[[dateFormatter stringFromDate:_date]
substringToIndex:12];//用到的精确到分,24小时制60分钟制
3、申请的appid,和private_key
NSString *appid =
@"15ds45s13a465s";//这里是楼主随便输入的,瞎编的
NSString *private_key =
@"46s4ds_SmartWeatherAPI_45s44d6";//也是瞎编的
4、算出经过urlencode后的key,这步比较重要,步骤也多,请耐心看完。
在原来的的基础上是在PHP的环境中算出的,代码如下,可在“
http://writecodeonline.com/php/ ”下进行算法的检验
echo
urlencode(base64_encode(hash_hmac('sha1', " http://open.weather.com.cn/data/?areaid=101010100type=index_fdate=201409041509appid=15ds45s13a465s",
"46s4ds_SmartWeatherAPI_45s44d6",
TRUE)));
首先定义得到public_key和API的方法,还有就是对key进行encodeurl操作的方法
注意,这里的方法都是被我定义在getTime的类里面,后面是在main中实例化出来的
//获得publicky
- (NSString*)
getPublicKey:(NSString*)areaid :(NSString*)type :(NSString*)date
:(NSString*)appid {

NSString *Key = [[NSString alloc]
initWithFormat:@"http://open.weather.com.cn/data/?areaid=%@type=%@date=%@appid=%@",
areaid, type, [date substringToIndex:12], appid];

return
Key;
}
//获得完整的API
- (NSString*) getAPI:(NSString*)areaid
:(NSString*)type :(NSString*)date :(NSString*)appid :(NSString*)key
{

NSString *API = [[NSString alloc]
initWithFormat:@"http://open.weather.com.cn/data/?areaid=%@type=%@date=%@appid=%@key=%@",
areaid, type, [date substringToIndex:12], [appid substringToIndex:6],
key];
//-------------这里需要主要的是只需要appid的前6位!!!

return
API;

}
//将获得的key进性urlencode操作
- (NSString
*)stringByEncodingURLFormat:(NSString*)_key{
NSString *encodedString
= (__bridge NSString
*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)_key,
nil, (CFStringRef) @"!$'()*+,-./:;=?@_~%#[]",
kCFStringEncodingUTF8);
//由于ARC的存在,这里的转换需要添加__bridge,原因我不明。求大神讲解
return
encodedString;

}
重点来了,在oc下的算法如下,记得把附件的Base64.h
加进来并引用到工程里面
//对publickey和privatekey进行加密
- (NSString *)
hmacSha1:(NSString*)public_key :(NSString*)private_key{

NSData*
secretData = [private_key
dataUsingEncoding:NSUTF8StringEncoding];
NSData* stringData = [public_key
dataUsingEncoding:NSUTF8StringEncoding];

const void* keyBytes =
[secretData bytes];
const void* dataBytes = [stringData
bytes];

///#define CC_SHA1_DIGEST_LENGTH 20 /* digest
length in bytes */
void* outs =
malloc(CC_SHA1_DIGEST_LENGTH);

CCHmac(kCCHmacAlgSHA1, keyBytes,
[secretData length], dataBytes, [stringData length], outs);

//
Soluion 1
NSData* signatureData = [NSData dataWithBytesNoCopy:outs
length:CC_SHA1_DIGEST_LENGTH freeWhenDone:YES];

return
[signatureData
base64EncodedString];

}
这里只是初步算出来的key,还未encodeurl,链接不能被浏览器识别,所以现在经过算法得到的_key还有一步操作才能的到真正的key。
NSString *_key = [getTime hmacSha1:[getTime
getPublicKey:areaid :type :date :appid] :private_key];
NSString *key =
[getTime
stringByEncodingURLFormat:_key];
最后一步了吧!拼接API
NSString *weatherAPI = [getTime getAPI:areaid :type :date
:appid
:key];
//OK,我们的API就可以用啦。
最后,通过API返回的值是JSON文件,通过解析,就能得到我们想要的数据了,下面拿一个开发的接口举例
NSDictionary *weatherDic = [getTime
getWeatherDic:@"http://www.weather.com.cn/data/cityinfo/101010100.html"];

// weatherDic字典中存放的数据也是字典型,从它里面通过键值取值
NSDictionary
*weatherInfo = [weatherDic
objectForKey:@"weatherinfo"];
NSLog(@"今天是 %@ %@ %@ 的天气状况是:%@ %@ -
%@",[newDateOne substringWithRange:NSMakeRange(0, 4)],[newDateOne
substringWithRange:NSMakeRange(4, 2)] ,[newDateOne
substringWithRange:NSMakeRange(6, 2)],[weatherInfo
objectForKey:@"weather"],[weatherInfo objectForKey:@"temp1"],[weatherInfo
objectForKey:@"temp2"]);
输出:2014-09-04 23:40:23.243
WeatherAPP[5688:201108] 今天是 2014-09-04 的天气状况是:晴 17℃ - 30℃
weatherInfo字典里面的内容是---{"weatherinfo":{"city":"北京","cityid":"101010100","temp1":"17℃","temp2":"30℃","weather":"晴","img1":"n0.gif","img2":"d0.gif","ptime":"18:00"}} 关于单片机气象网站api接口和天气预报api接口的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。 单片机气象网站api接口的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于天气预报api接口、单片机气象网站api接口的信息别忘了在本站进行查找喔。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:详解Mybatis核心配置文件
下一篇:详解SpringBoot是如何整合SpringDataRedis的?
相关文章

 发表评论

暂时没有评论,来抢沙发吧~