Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : What am I doing wrong
Page 1 of 2 | |||||
Author | Message | ||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
I'm stuck with this, I've been trying to work it out for 4 weeks trying various combinations of code but I cannot get it to work DIM STRING p$=chr$(34) Dim INTEGER a(90000)',k,l dim time1$ system "wget -q -O- "+p$+"https://swd.weatherflow.com/swd/rest/observations/station/10170?api_key=20c70eae-e62f-4d3b-b3a4-8586e90f3ac8"+p$,a() time1$=JSON$(a(),"obs.timestamp")'time received print time1$$ This is what I get when I run the program What the query returns as you can see if you type http://swd.weatherflow.com/swd/rest/observations/station/10170?api_key=20c70eae-e62f-4d3b-b3a4-8586e90f3ac8 click me to see What I'm trying to do is parse the bottom section of this {"station_id":10170,"station_name":"Haughton Green","public_name":"Haughton Green","latitude":53.44007,"longitude":-2.10608,"timezone":"Europe/London","elevation":97.8037109375,"is_public":true,"status":{"status_code":0,"status_message":"SUCCESS"},"station_units":{"units_temp":"c","units_wind":"mph","units_precip":"mm","units_pressure":"mb","units_distance":"mi","units_direction":"cardinal","units_other":"metric"},"outdoor_keys":["timestamp","air_temperature","barometric_pressure","station_pressure","sea_level_pressure","relative_humidity","precip","precip_accum_last_1hr","precip_accum_local_day","precip_accum_local_yesterday","precip_minutes_local_day","precip_minutes_local_yesterday","wind_avg","wind_direction","wind_gust","wind_lull","solar_radiation","uv","brightness","lightning_strike_last_epoch","lightning_strike_last_distance","lightning_strike_count","lightning_strike_count_last_1hr","lightning_strike_count_last_3hr","feels_like","heat_index","wind_chill","dew_point","wet_bulb_temperature","delta_t","air_density"],"obs":[{"timestamp":1595169879,"air_temperature":16.8,"barometric_pressure":1010.4,"station_pressure":1010.4,"sea_level_pressure":1022.7,"relative_humidity":54,"precip":0.0,"precip_accum_last_1hr":0.0,"precip_accum_local_day":0.0,"precip_accum_local_yesterday":3.186342,"precip_minutes_local_day":0,"precip_minutes_local_yesterday":309,"wind_avg":2.1,"wind_direction":294,"wind_gust":3.3,"wind_lull":1.2,"solar_radiation":301,"uv":4.08,"brightness":36142,"lightning_strike_last_epoch":1594307075,"lightning_strike_last_distance":20,"lightning_strike_count":0,"lightning_strike_count_last_1hr":0,"lightning_strike_count_last_3hr":0,"feels_like":16.8,"heat_index":16.8,"wind_chill":16.8,"dew_point":7.4,"wet_bulb_temperature":11.7,"delta_t":5.1,"air_density":1.21395}]} Which is my weather station, so I can use MM to display it properly As the bottom section is preceded by "obs": I thought I could use that but its never simple or easy |
||||
disco4now Guru Joined: 18/12/2014 Location: AustraliaPosts: 843 |
Try time1$=JSON$(a(),"obs[0].timestamp")'time received obs is an array, you need the first item, so obs[0].timestamp Latest F4 Latest H7 |
||||
lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3008 |
I hope this solves OPs question. If it does, is it because in ,"obs":[{"timestamp":1595169879,"air_temperature":16.8,"barometric_pressure":1010.4, etc. "[" indicates an array while "{" indicates an instance or object within that array? There is only one set of "{}" within "[]", so only one object, so obs[0]? And then (I'm not sure of the terminology), "timestamp" is the name of a data-element (perhaps "field" in other contexts), so time1$=JSON$(a(),"obs[0].timestamp") returns the value of the field, "timestamp" in the first array element (record?) of the array, "obs"? Is this parsing roughly correct? PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
Thanks I got it with speed$=JSON$(z(),"obs[0].wind_avg")'pressure angle1$=JSON$(z(),"obs[0].wind_direction")'direction hum1$=JSON$(z(),"obs[0].relative_humidity")'humidity press1$=JSON$(z(),"obs[0].station_pressure")'pressure I didn't understand "[" indicates an array while "{" indicates an instance or object within that array I still don't really understand but I've got it to work now :) |
||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
deleted* Edited 2020-07-21 04:47 by lew247 |
||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
I found what I needed sorry about the deleted post Is there a way to do the same thing with this data? {"status":{"status_code":0,"status_message":"SUCCESS"},"device_id":35103,"type":"obs_sky","source":"cache","summary":{"precip_total_1h":0.0,"precip_accum_local_yesterday":0.0,"precip_analysis_type_yesterday":0},"obs":[[1595271934,7804,1.18,0,0,0.5,1.61,309,3.34,1,65,0,0,15,null,null,0]]} The difference is the values are all comma seperated and have no names preceding them unlike the previous example above so press1$=JSON$(z(),"obs[0].station_pressure")'pressure won't work as there is no "station)pressure instead it's {"status":{"status_code":0,"status_message":"SUCCESS"},"device_id":35102,"type":"obs_air","source":"cache","summary":{"pressure_trend":"steady","strike_count_1h":0,"strike_count_3h":0,"strike_last_dist":20,"strike_last_epoch":1594307075,"feels_like":17.5,"heat_index":17.5,"wind_chill":17.5},"obs":[[1595272805,1015.3,17.5,56,0,0,2.99,1]]} The pressure is the 2nd value = 1015.3 EDIT* Apologies I just realised that stupid code has stretched the screen too wide It seems it's not possible to word wrap on the forum in a code box Sorry Edited 2020-07-21 05:27 by lew247 |
||||
disco4now Guru Joined: 18/12/2014 Location: AustraliaPosts: 843 |
Is it this? press1$=JSON$(z(),"obs[0][1]")'pressure Latest F4 Latest H7 |
||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
Unfortunately not I've tried every combination I can think of to make it work I noticed the difference though the first set which works JSON$(z(),"obs[0].wind_direction") is taken from data that starts with "obs":[{" the later one uses a set of data that starts with "obs":[[ as in "obs":[[1595320188,1017.2,13.4,76,0,0,2.99,1]] There is no { in the "object?" if you click HERE you'll see the data the first set returns and HERE to see the later one which I cannot get working |
||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
I've got it down to 1595320188,1017.2,13.4,76,0,0,2.99,1]] figuring it would be easier by using LONGSTRING TRIM a(), 300 I still cannot parse the comma seperated values though for some reason, no matter what I try DIM STRING p$=chr$(34) Dim INTEGER a(10000)',k,l dim b$ system "wget -q -O- "+p$+"https://swd.weatherflow.com/swd/rest/observations/?device_id=35102&api_key=20c70eae-e62f-4d3b-b3a4-8586e90f3ac8"+p$,a() 'longstring print a() LONGSTRING TRIM a(), 300 longstring print a() the result is I don't need that last value before the ]] so I figured it should be easy to extract nothing ever is it seems I even tried b$=""+ " test-b$" LONGSTRING APPEND a(), b$ print b$ hoping that if it was in a seperate string it would be easier but nothing goes in there b$ is blank Edited 2020-07-22 00:12 by lew247 |
||||
lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3008 |
Kludgy, but after your code how about dim string qt=chr$(34),b b="{ "+qt+"E"+qt+":["+LGETSTR$(a%(), 1, llen(a%())+" }" longstring clear a%() longstring append a%(),b longstring print a%() print JSON$(a%(),"E[0]"),JSON$(a%(),"E[1]") The purpose is to get a%() to contain { "E":[1595320188,1017.2,13.4,76,0,0,2.99,1] } And then maybe you can extract the elements. PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
Thanks I tried it but it returns ERROR: Line is too long I discovered another problem, the length of the preceding bits I deleted by using LONGSTRING TRIM changes at times, so my idea of using Trim won#t work unfortunately |
||||
lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3008 |
Which line does it think is too long? What text is returned with your wget? You should be able to find and extract the portion you need with linstr and longstring mid and then use JSON$(). PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
disco4now Guru Joined: 18/12/2014 Location: AustraliaPosts: 843 |
The FIELD command can be used to pull the correct data from comma separated once you get the last bit in a string. Try this. Dim As integer a(1000) Dim As string q q=Chr$(34) system "wget -q -O- "+q$+"https://swd.weatherflow.com/swd/rest/observations/?device_id=35102&api_key=20c70eae-e62f-4d3b-b3a4-8586e90f3ac8"+q$,a() i=LINSTR(a(),q$+"obs"+q$+":[[")+8 'ie skip over "obs":[[ f$=LGETSTR$(a(),i,255) print f$ ? "status_message", JSON$(a(),"status.status_message") ? "wind_chill", JSON$(a(),"summary.wind_chill") ? "field 1", field$(f$,1) ? "field 2", field$(f$,2) ? "field 3", field$(f$,3) ? "field 4", field$(f$,4) ? "field 5", field$(f$,5) ? "field 6", field$(f$,6) ? "field 7", field$(f$,7) Latest F4 Latest H7 |
||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
You're a genius That works exactly as needed Thank you so much |
||||
lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3008 |
Very timely reference to the FIELD command for me--I wasn't familiar with it. Hmmm, FIELD command not available in DOS. I'd love to see a DOS update with longstrings and FIELD. Edited 2020-07-22 22:36 by lizby PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
Can anyone see what I'm doing wrong? I'm trying to parse the data using comma separated values I'm pretty sure it's something really simple I've missed but I've spent the past 10 hours trying to get it working but it's eluding me I'm receiving data from Weatherflow online using my station id and trying to parse some of the elements I've commented out sections so only the first observation is trying to be parsed at the moment Before I tried to parse the separate fields it was all working fine. This is the error I've spent ages trying to fix Option explicit OPTION DEFAULT NONE Dim As integer a(1000) Dim As integer b(1000) Dim As integer c(1000) DIM STRING q=Chr$(34) '" symbol Open "COM1:9600" As #1 DIM Integer i,j DIM AirObs$,SkyObs$,press1$,temp1$,battery1$,wind$,wind2$,battery2$,precip$ system "wget -q -O- "+q$+"https://swd.weatherflow.com/swd/rest/observations/?device_id=35102&api_key=20c70eae-e62f-4d3b-b3a4-8586e90f3ac8"+q$,a() i=LINSTR(a(),q$+"obs"+q$+":[[")+8 'ie skip over "obs":[[ AirObs$=LGETSTR$(a(),i,255) PRINT AirObs$ print #1, AirObs$ Air pause 1000 system "wget -q -O- "+q$+"https://swd.weatherflow.com/swd/rest/observations/?device_id=35103&api_key=20c70eae-e62f-4d3b-b3a4-8586e90f3ac8"+q$,b() j=LINSTR(b(),q$+"obs"+q$+":[[")+8 'ie skip over "obs":[[ SkyObs$=LGETSTR$(b(),j,255) ' Sky pause 1000 system "wget -q -O- "+q$+"https://swd.weatherflow.com/swd/rest/forecast?api_key=20c70eae-e62f-4d3b-b3a4-8586e90f3ac8&station_id=10170&lat=53.44&lon=-2.10"+q$,c() precip$=JSON$(c(),"forecast_daily[0].precip_percent")'precipitation forecast print #1,press1$,",",temp1$,",",battery1$',",",wind$,",",wind2$,",",battery2$,",",",",precip$ Sub Air press1$ = AirObs$(1) 'Station Pressure (MB) ' temp1$ = AirObs$(2) 'Air Temperature (C) ' battery1$ = AirObs$(6) 'Battery (volts) END SUB Sub Sky wind$ = SkyObs$(2) 'Wind Avg (m/s) wind2$ = SkyObs$(7) 'Wind Direction (degrees) battery2$ = SkyObs$(8) 'Battery (volts) END SUB |
||||
jirsoft Guru Joined: 18/09/2020 Location: Czech RepublicPosts: 532 |
AirObs$ is string, not array of strings. If you need first element CSV, you need press1$ = FIELD$(AbsObs$, 1, ",") Jiri Napoleon Commander and SimplEd for CMM2 (GitHub), Â CMM2.fun |
||||
jirsoft Guru Joined: 18/09/2020 Location: Czech RepublicPosts: 532 |
But to me looks like pressure is second element, so press1$ = FIELD$(AbsObs$, 2, ",") Jiri Napoleon Commander and SimplEd for CMM2 (GitHub), Â CMM2.fun |
||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
Thank you so much Working perfectly now Sent data to HC-12 Received data from PC via HC-12 I'm hoping the spaces between the commas on the receiving micromite once it's set up won't cause a problem when it comes to parsing Edit: I spent over 2 weeks trying to extract that information using Python from a Pi that's got a weather display attached I did it using Pi-Cromite in a day It would have been a couple of hours if I hadn't messed the field$ up Edited 2020-11-26 04:17 by lew247 |
||||
lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1676 |
deleted* Edited 2020-11-26 23:50 by lew247 |
||||
Page 1 of 2 |
Print this page |