Changes for page 2 Script

Last modified by Devin Chen on 2025/06/06 14:03

From version 28.1
edited by Hunter
on 2022/11/21 17:01
Change comment: There is no comment for this version
To version 32.1
edited by Hunter
on 2022/11/22 14:09
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -349,6 +349,104 @@
349 349  end
350 350  {{/code}}
351 351  
352 +== **1.8 How to parse value from HTTP response body** ==
353 +
354 +This example use [[https:~~/~~/www.weatherapi.com/>>https://www.weatherapi.com/]] as example, to show how to parse value from HTTP response body. When we input the city name into address "@HDW5050":
355 +
356 +(% style="text-align:center" %)
357 +[[image:InputHTTPparameter.png]]
358 +
359 +Then the response body would be like as following:
360 +
361 +{{code language="json"}}
362 +{
363 + "location": {
364 + "name": "Madrid",
365 + "region": "Madrid",
366 + "country": "Spain",
367 + "lat": 40.4,
368 + "lon": -3.68,
369 + "tz_id": "Europe/Madrid",
370 + "localtime_epoch": 1669022636,
371 + "localtime": "2022-11-21 10:23"
372 + },
373 + "current": {
374 + "last_updated_epoch": 1669022100,
375 + "last_updated": "2022-11-21 10:15",
376 + "temp_c": 13.0,
377 + "temp_f": 55.4,
378 + "is_day": 1,
379 + "condition": {
380 + "text": "Partly cloudy",
381 + "icon": "//cdn.weatherapi.com/weather/64x64/day/116.png",
382 + "code": 1003
383 + },
384 + "wind_mph": 11.9,
385 + "wind_kph": 19.1,
386 + "wind_degree": 210,
387 + "wind_dir": "SSW",
388 + "pressure_mb": 1015.0,
389 + "pressure_in": 29.97,
390 + "precip_mm": 0.0,
391 + "precip_in": 0.0,
392 + "humidity": 88,
393 + "cloud": 75,
394 + "feelslike_c": 10.8,
395 + "feelslike_f": 51.4,
396 + "vis_km": 10.0,
397 + "vis_miles": 6.0,
398 + "uv": 3.0,
399 + "gust_mph": 22.1,
400 + "gust_kph": 35.6
401 + }
402 +}
403 +{{/code}}
404 +
405 +(% class="wikigeneratedid" %)
406 +So we decode json into lua object to assign the value into addresses HDW6060(temperature), HDW7070(humidity), the code example like follows:
407 +
408 +{{code language="lua"}}
409 +local APIkey = '70faaecf926b4341b1974006221711'
410 +
411 +
412 +local http = require("socket.http")
413 +local json = require("json")
414 +
415 +-- Send http.get request and return response result
416 +function getHttpsUrl(url)
417 + local result_table, code, headers, status = http.request(url)
418 + print("code:"..code)
419 + if code~= 200 then
420 + return
421 + else
422 + return result_table
423 + end
424 +end
425 +
426 +function sendAPI(key, city)
427 + local url = "http://api.weatherapi.com/v1/current.json?key="..key.."&q="..city.."&aqi=no"
428 + --local url = 'http://v-box.net'
429 + --local url = 'https://www.google.com/'
430 + --http://api.weatherapi.com/v1/current.json?key=70faaecf926b4341b1974006221711&q=Barcelona&aqi=no
431 + print("Get the link:"..url)
432 + local body = getHttpsUrl(url)
433 + --print(body)
434 + local jsonBody = json.decode(body)
435 + --print(jsonBody["current"]["temp_c"])
436 + --print(type(jsonBody["current"]["temp_c"]))
437 + --print(type(jsonBody["current"]["humidity"]))
438 + addr_setfloat("@HDW6060", jsonBody["current"]["temp_c"])
439 + addr_setword("@HDW7070", jsonBody["current"]["humidity"])
440 +end
441 +
442 +
443 +function Weather.main()
444 + local cityName = addr_getstring("@HDW5050",6)
445 + print("cityName: "..cityName)
446 + sendAPI(APIkey, cityName)
447 +end
448 +{{/code}}
449 +
352 352  = **2 V-Box connect with third part server** =
353 353  
354 354  V-Box have two mode.One is for V-Net,User need to use WECON server to store data.We call this V-NET platform.
InputHTTPparameter.png
Author
... ... @@ -1,0 +1,1 @@
1 +XWiki.Hunter
Size
... ... @@ -1,0 +1,1 @@
1 +17.9 KB
Content