Place to put your Basic demos and examples

Moderator: Mmiscool

User avatar
By lew247
#51248 This is the complete code I'd LIKE to get working it fails on Line 4 which is IF A$ LEFT (1) = "*" THEN
I can't figure out what is wrong

*** it's waiting for a serial input from a 7 inch monitor with Micromite basic on it which sends either * or ** when it want's a weather reading from the ESP8266***

The ESP8266 waits for EITHER * or ** as the first character received on the serial port input

IF the first character is * THEN GOSUB TODAY
IF the first character is ** THEN GOSUB WEEK
LOOP

Code: Select allDo
  serialbranch [input]                         ' get a  record
  Print "Searching For start character"
IF A$ LEFT (1) = "*" THEN
GOSUB TODAY
ELSE
IF A$ LEFT (1) = "**" THEN
GOSUB WEEK
END IF
LOOP




TODAY
let appid = "APPID"
let query = "api.openweathermap.org/data/2.5/weather?q=Haughton%20Green,uk&lang=en&units=metric&appid=" & appid
let ret = wget(query)
let icon = json(ret,"main.icon")
serialprintln "today" & "," & icon & "*"
serialflush
RETURN

WEEK
let query = "api.openweathermap.org/data/2.5/forecast/daily?&units=metric&q=HaughtonGreen,uk&appid=" & appid
let ret = readopenweather(query,1)
let temp_min = json(ret,"temp.min")
let temp_max = json(ret,"temp.max")
let tim = json(ret,"dt")
let tim = unixtime(tim)
let speed = json(ret,"speed")
let dir = json(ret,"deg")
let pressure = json(ret,"pressure")
let humid = json(ret,"humidity")
let cloud = json(ret,"clouds")
let rain = json(ret,"description")
let icon = json(ret,"icon")
serialprintln "day1" & "," &tim & "," & temp_min "," & temp_max & "," speed & "," &dir & "," & pressure & "," & humid & "," & rain & "," & icon & "*"
let ret = readopenweather(query,2)
let temp_min = json(ret,"temp.min")
let temp_max = json(ret,"temp.max")
let tim = json(ret,"dt")
let tim = unixtime(tim)
let speed = json(ret,"speed")
let dir = json(ret,"deg")
let pressure = json(ret,"pressure")
let humid = json(ret,"humidity")
let cloud = json(ret,"clouds")
let rain = json(ret,"description")
let icon = json(ret,"icon")
serialprintln "day2" & "," &tim & "," & temp_min "," & temp_max & "," speed & "," &dir & "," & pressure & "," & humid & "," & rain & "," & icon & "*"
let ret = readopenweather(query,3)
let temp_min = json(ret,"temp.min")
let temp_max = json(ret,"temp.max")
let tim = json(ret,"dt")
let tim = unixtime(tim)
let speed = json(ret,"speed")
let dir = json(ret,"deg")
let pressure = json(ret,"pressure")
let humid = json(ret,"humidity")
let cloud = json(ret,"clouds")
let rain = json(ret,"description")
let icon = json(ret,"icon")
serialprintln "day3" & "," &tim & "," & temp_min "," & temp_max & "," speed & "," &dir & "," & pressure & "," & humid & "," & rain & "," & icon & "*"
let ret = readopenweather(query,4)
let temp_min = json(ret,"temp.min")
let temp_max = json(ret,"temp.max")
let tim = json(ret,"dt")
let tim = unixtime(tim)
let speed = json(ret,"speed")
let dir = json(ret,"deg")
let pressure = json(ret,"pressure")
let humid = json(ret,"humidity")
let cloud = json(ret,"clouds")
let rain = json(ret,"description")
let icon = json(ret,"icon")
serialprintln "day4" & "," &tim & "," & temp_min "," & temp_max & "," speed & "," &dir & "," & pressure & "," & humid & "," & rain & "," & icon & "*"
serialflush
RETURN

[input]
let a$ = serialinput
return
User avatar
By Mmiscool
#51303 There is no need for the loop. Set the interrupt branch for the serial input and wait until some thing happens.

When serial input is detected it will goto the [input] branch.

The using the input command place the received characters in to A$. Please note that the input command will block further execution until the cr+lf is received.

Code: Select allserialbranch [input]                         ' get a  record
wait


[input]
input A$
IF A$ LEFT (1) = "*" THEN
   GOSUB [TODAY]
ELSE
IF A$ LEFT (1) = "**" THEN
   GOSUB [WEEK]
END IF
wait




[TODAY]
let appid = "APPID"
let query = "api.openweathermap.org/data/2.5/weather?q=Haughton%20Green,uk&lang=en&units=metric&appid=" & appid
let ret = wget(query)
let icon = json(ret,"main.icon")
serialprintln "today" & "," & icon & "*"
serialflush
RETURN

[WEEK]
let query = "api.openweathermap.org/data/2.5/forecast/daily?&units=metric&q=HaughtonGreen,uk&appid=" & appid
let ret = readopenweather(query,1)
let temp_min = json(ret,"temp.min")
let temp_max = json(ret,"temp.max")
let tim = json(ret,"dt")
let tim = unixtime(tim)
let speed = json(ret,"speed")
let dir = json(ret,"deg")
let pressure = json(ret,"pressure")
let humid = json(ret,"humidity")
let cloud = json(ret,"clouds")
let rain = json(ret,"description")
let icon = json(ret,"icon")
serialprintln "day1" & "," &tim & "," & temp_min "," & temp_max & "," speed & "," &dir & "," & pressure & "," & humid & "," & rain & "," & icon & "*"
let ret = readopenweather(query,2)
let temp_min = json(ret,"temp.min")
let temp_max = json(ret,"temp.max")
let tim = json(ret,"dt")
let tim = unixtime(tim)
let speed = json(ret,"speed")
let dir = json(ret,"deg")
let pressure = json(ret,"pressure")
let humid = json(ret,"humidity")
let cloud = json(ret,"clouds")
let rain = json(ret,"description")
let icon = json(ret,"icon")
serialprintln "day2" & "," &tim & "," & temp_min "," & temp_max & "," speed & "," &dir & "," & pressure & "," & humid & "," & rain & "," & icon & "*"
let ret = readopenweather(query,3)
let temp_min = json(ret,"temp.min")
let temp_max = json(ret,"temp.max")
let tim = json(ret,"dt")
let tim = unixtime(tim)
let speed = json(ret,"speed")
let dir = json(ret,"deg")
let pressure = json(ret,"pressure")
let humid = json(ret,"humidity")
let cloud = json(ret,"clouds")
let rain = json(ret,"description")
let icon = json(ret,"icon")
serialprintln "day3" & "," &tim & "," & temp_min "," & temp_max & "," speed & "," &dir & "," & pressure & "," & humid & "," & rain & "," & icon & "*"
let ret = readopenweather(query,4)
let temp_min = json(ret,"temp.min")
let temp_max = json(ret,"temp.max")
let tim = json(ret,"dt")
let tim = unixtime(tim)
let speed = json(ret,"speed")
let dir = json(ret,"deg")
let pressure = json(ret,"pressure")
let humid = json(ret,"humidity")
let cloud = json(ret,"clouds")
let rain = json(ret,"description")
let icon = json(ret,"icon")
serialprintln "day4" & "," &tim & "," & temp_min "," & temp_max & "," speed & "," &dir & "," & pressure & "," & humid & "," & rain & "," & icon & "*"
serialflush
RETURN
User avatar
By lew247
#51322 Thank you, I didn't realise that the SERIALBRANCH acted like an interrupt and didn't need a loop.

I've "almost" got it working but it's failing on this IF A$ LEFT (1) = "*" THEN

Syntax error in if command
Halted at line 7 'this is the IF statement listed above***

What I want to do is check the 1st character received through the serial port

Any ideas what the Syntax error is? I've tried it without spaces between the LEFT(1) but it made no difference


I even thought it might be related to the LEFT (1) bit so I tried this

Code: Select alllet appid = "APPID"
serialbranch [input]                         ' get a  record
wait
[input]
input A$
LET B$ = LEFT (1) A$
IF B$ = "*" THEN
   GOSUB [TODAY]
ENDIF
IF A$ LEFT(1) = "**" THEN
   GOSUB [WEEK]
ENDIF
wait


But it still fails at the IF statement
Syntax error in if command
Halted at line 7


It still fails if I use IF B$ = * THEN
Changing INPUT A$ to LET A$ = INPUT or INPUT = A$ doesn't change anything either

FULL edited code - still doesn't work though
Code: Select allLET appid = "APPID"
serialbranch [input]                         ' get a  record
WAIT

[input]
LET A$ = input
LET B$ = LEFT (1) A$
IF B$ = * THEN
   GOSUB [TODAY]
ENDIF
IF A$ LEFT(1) = "**" THEN
   GOSUB [WEEK]
ENDIF
RETURN

[TODAY]
let query = "api.openweathermap.org/data/2.5/weather?q=Haughton%20Green,uk&lang=en&units=metric&appid=" & appid
let ret = wget(query)
let icon = json(ret,"main.icon")
serialprintln "today" & "," & icon & "*"
serialflush
RETURN

[WEEK]
let query = "api.openweathermap.org/data/2.5/forecast/daily?&units=metric&q=HaughtonGreen,uk&appid=" & appid
let ret = readopenweather(query,1)
let temp_min = json(ret,"temp.min")
let temp_max = json(ret,"temp.max")
let tim = json(ret,"dt")
let tim = unixtime(tim)
let speed = json(ret,"speed")
let dir = json(ret,"deg")
let pressure = json(ret,"pressure")
let humid = json(ret,"humidity")
let cloud = json(ret,"clouds")
let rain = json(ret,"description")
let icon = json(ret,"icon")
serialprintln "day1" & "," &tim & "," & temp_min "," & temp_max & "," speed & "," &dir & "," & pressure & "," & humid & "," & rain & "," & icon & "*"
let ret = readopenweather(query,2)
let temp_min = json(ret,"temp.min")
let temp_max = json(ret,"temp.max")
let tim = json(ret,"dt")
let tim = unixtime(tim)
let speed = json(ret,"speed")
let dir = json(ret,"deg")
let pressure = json(ret,"pressure")
let humid = json(ret,"humidity")
let cloud = json(ret,"clouds")
let rain = json(ret,"description")
let icon = json(ret,"icon")
serialprintln "day2" & "," &tim & "," & temp_min "," & temp_max & "," speed & "," &dir & "," & pressure & "," & humid & "," & rain & "," & icon & "*"
let ret = readopenweather(query,3)
let temp_min = json(ret,"temp.min")
let temp_max = json(ret,"temp.max")
let tim = json(ret,"dt")
let tim = unixtime(tim)
let speed = json(ret,"speed")
let dir = json(ret,"deg")
let pressure = json(ret,"pressure")
let humid = json(ret,"humidity")
let cloud = json(ret,"clouds")
let rain = json(ret,"description")
let icon = json(ret,"icon")
serialprintln "day3" & "," &tim & "," & temp_min "," & temp_max & "," speed & "," &dir & "," & pressure & "," & humid & "," & rain & "," & icon & "*"
let ret = readopenweather(query,4)
let temp_min = json(ret,"temp.min")
let temp_max = json(ret,"temp.max")
let tim = json(ret,"dt")
let tim = unixtime(tim)
let speed = json(ret,"speed")
let dir = json(ret,"deg")
let pressure = json(ret,"pressure")
let humid = json(ret,"humidity")
let cloud = json(ret,"clouds")
let rain = json(ret,"description")
let icon = json(ret,"icon")
serialprintln "day4" & "," &tim & "," & temp_min "," & temp_max & "," speed & "," &dir & "," & pressure & "," & humid & "," & rain & "," & icon & "*"
serialflush
RETURN
User avatar
By Mmiscool
#51331 You have the left function incorrect there.

You must put the A$ in side the parenthesis golowed by a comma and then the number of characters.

https://docs.google.com/document/d/1EiY ... 4r1pn3fx0t

left():

Will return a string that contains a specified number of characters from the left side of a string.
Bla = left({string or var name},{length})