I have not finished trying to patch together all the snapshots so here is the javascript equivalent
Edited by moderator to add code tags. (Please use code tags!)
enum RadioMessage {
ack = 20929,
message1 = 49434
}
/**
* Note: The wind direction is a string. We are not able to save the string as a number. For consistency, we will be using the same string and number pair to send data. Therefore, we will assign a number to each of the directions. If we are in an unknown state "???", we will send a value of 0.
*
* https://learn.sparkfun.com/tutorials/wireless-remote-weather-station-with-microbit/experiment-5-real-time-wireless-data-logging
*
* MOISTURE
*
* Soil Moisture The Soil Moisture block returns an analog value range of 0 - 1023 (a 10 bit number). The lower the number is the less moisture the sensor detects. The "dryness" and "wetness" level depends on your climate, humidity, etc. so we cannot give you a concrete range of what is wet and dry, that is something you will have to fiddle with yourself.
*/
/**
* 1) €Date,Time,
*
* 2) BME280 Temp degC, (/100)
*
* 3) BME280 Humidity [rH], (/1024)
*
* 4) BME280 Pressure [hPa], (/25,600 )
*
* 5) BME280 Altitude [m],
*
* 6) Rain [in],
*
* 7) Wind Speed [mph],
*
* 8) Wind Direction,
*
* 9) Soil Moisture,
*
* 10) Soil Temp [degC]
*
* NOTE: hPa is hectopascal
*
* 1 inches of mercury? 3386.3886666667 pascals in one inch Hg
*/
/**
* iVeiwOLED=1
*
* TM:nn HUM: nn
*
* PR:nnn:: Rain: nn
*
* WS:nnn WDIR: nnn
*
* iViewOLED = 2
*
* SM: nn sT: nn
*/
function getHeader () {
sHeader = "Date" + "," + "Time" + "," + "BME280 Temp degC" + "," + "BME280 Humidity [rH]" + "," + "BME280 Pressure [hPa]" + "," + "BME280 Altitude [m]" + "," + "Rain [in]" + "," + "Wind Speed [mph]" + "," + "Wind Direction" + "," + "Soil Moisture" + "," + "Soil Temp [degC]"
}
// Clear flag by emptying the array
input.onButtonPressed(Button.A, function () {
if (!(bView1OLED) && !(bView2OLED)) {
bView1OLED = true
OLED12864_I2C.on()
// "A" pressed - view Part 2 (dos)
basic.showLeds(`
# # . . .
# # . . .
# . # . #
# . # . #
. . # # #
`)
} else if (bView1OLED && !(bView2OLED)) {
bView1OLED = false
bView2OLED = true
OLED12864_I2C.on()
// "A" pressed - view Part 2 (dos)
basic.showLeds(`
# # . . .
# # . . #
# . . . #
# . . # #
. . . # #
`)
OLED12864_I2C.clear()
} else if (!(bView1OLED) && bView2OLED) {
bView1OLED = false
bView2OLED = false
OLED12864_I2C.clear()
OLED12864_I2C.off()
// Start over
basic.showLeds(`
. . . . .
# # . . #
# . # . #
# . . # #
# . . . #
`)
}
basic.pause(100)
})
function myOLED (showTxt: string, xLoc: number, yLoc: number, iView: number, bView1OLED: boolean, bView2OLED: boolean) {
if (bView1OLED && iView == 1) {
OLED12864_I2C.showString(
xLoc,
yLoc,
showTxt,
1
)
} else if (bView2OLED && iView == 2) {
OLED12864_I2C.showString(
xLoc,
yLoc,
showTxt,
1
)
} else {
basic.pause(100)
}
}
function getDateTime () {
sMyDate = ""
sMonth = MonthArray[gatorRTC.getTimeComponent(TimeType.Month) - 1]
basic.pause(100)
sDay = convertToText(gatorRTC.getTimeComponent(TimeType.Date))
iLength = sDay.length
if (iLength == 1) {
sDay = "0" + sDay
}
sMyDate = "0" + sDay
basic.pause(100)
sHours = convertToText(gatorRTC.getTimeComponent(TimeType.Hours))
sMinutes = convertToText(gatorRTC.getTimeComponent(TimeType.Minutes))
sSeconds = convertToText(gatorRTC.getTimeComponent(TimeType.Seconds))
iLength = sMinutes.length
if (iLength == 1) {
sMinutes = "0" + sMinutes
}
iLength = sSeconds.length
if (iLength > 2) {
sSeconds = sSeconds.substr(0, 1)
}
sTime = "" + sHours + sMinutes + sSeconds
sDateTime = "" + sMonth + sDay + ":" + sTime
}
radio.onReceivedValue(function (name, value) {
if (item > 9) {
item = 0
}
item += 1
basic.pause(100)
getDateTime()
myOLED(sDateTime, 0, 0, 1, bView1OLED, bView2OLED)
basic.pause(100)
if (name == "time") {
basic.pause(100)
} else if (name == "tmpC") {
tempFH = value * 1.8 + 32
textDisplay(tempFH, 1)
sTempC = sReturn
myOLED("TP:" + sTempC, 0, 1, 1, bView1OLED, bView2OLED)
serial.writeString("" + sTempC + ",")
} else if (name == "humidity") {
iOHumid = value
textDisplay(iOHumid, 1)
sHumidity = sReturn
myOLED("HU:" + sHumidity, xOLED, 1, 1, bView1OLED, bView2OLED)
serial.writeString("" + sHumidity + ",")
} else if (name == "pressure") {
oPressure = value / 3386
oPressure = oPressure * 100
textDisplay(oPressure, 1)
sPressure = sReturn
myOLED("PR:" + sPressure, 0, 2, 1, bView1OLED, bView2OLED)
serial.writeString("" + sPressure + ",")
} else if (name == "altitude") {
serial.writeString("" + convertToText(value) + ",")
} else if (name == "rain") {
textDisplay(value, 1)
sRain = sReturn
myOLED("RN:" + sRain, xOLED, 2, 1, bView1OLED, bView2OLED)
serial.writeString("" + sRain + ",")
} else if (name == "wind spe") {
textDisplay(value, 1)
sWindSpeed = sReturn
myOLED("WS:" + sWindSpeed, 0, 3, 1, bView1OLED, bView2OLED)
serial.writeString("" + sWindSpeed + ",")
} else if (name == "wind dir") {
sWinDir = lWinDir[value]
serial.writeString("" + sWinDir + ",")
sWinDir = "WD:" + sWinDir
myOLED(sWinDir, xOLED, 3, 1, bView1OLED, bView2OLED)
} else if (name == "soilMstr") {
textDisplay(value, 1)
sSoilMoisture = sReturn
serial.writeString("" + sSoilMoisture + ",")
myOLED("SM:" + sSoilMoisture, 0, 2, 2, bView1OLED, bView2OLED)
} else if (name == "soilTmpC") {
soilTempF = value * 1.8 + 32
textDisplay(soilTempF, 1)
sSoilTemp = sReturn
myOLED("ST:" + sSoilTemp, xOLED, 2, 2, bView1OLED, bView2OLED)
serial.writeString("" + sSoilTemp + ",")
serial.writeLine("")
}
basic.showLeds(`
. . . . .
. . . . .
. . # . .
. . . . .
. . . . .
`)
basic.pause(100)
basic.clearScreen()
})
// 0 digits after decimal nnn (no decimal)
// if iDigits = 0 then default is 1st 3 char.
// if iDigits = 1 then deciaml plus 2 characters
function textDisplay (iRawVal: number, iDigits: number) {
let aValue: string[] = []
sString = convertToText(iRawVal)
iLength = sString.length
idxString = sString.indexOf(".")
if (idxString < 0) {
aValue.insertAt(0, sString.substr(0, iLength))
aValue.insertAt(1, sString.substr(0, iLength))
sReturn = sString.substr(0, iLength)
} else {
aValue.insertAt(0, sString.substr(0, idxString - 1))
sReturn = sString.substr(0, idxString)
if (iRawVal < 1) {
sReturn = sString.substr(idxString, iLength - (idxString + 1))
aValue.insertAt(1, sString.substr(idxString, iLength - (idxString + 1)))
}
}
return sReturn
}
let insideHumid = 0
let insideTemp = 0
let iRowCnt = 0
let idxString = 0
let sString = ""
let sSoilTemp = ""
let soilTempF = 0
let sSoilMoisture = ""
let sWinDir = ""
let sWindSpeed = ""
let sRain = ""
let sPressure = ""
let oPressure = 0
let sHumidity = ""
let iOHumid = 0
let sReturn = ""
let sTempC = ""
let tempFH = 0
let item = 0
let sTime = ""
let sSeconds = ""
let sMinutes = ""
let sHours = ""
let iLength = 0
let sDay = ""
let sMyDate = ""
let sMonth = ""
let sHeader = ""
let MonthArray: string[] = []
let lWinDir: string[] = []
let bView2OLED = false
let bView1OLED = false
let xOLED = 0
let sDateTime = ""
radio.setGroup(37)
let Interval = 30000
let viewOLED = 0
// 23 characters wide
// 4 Lines
// Color: 0 or 1
// x, X alis position, 0 - 63 in zoom mode, 0 - 127 in normal mode.
// y, Y alis position, 0 - 31 in zoom mode, 0 - 63 in normal mode.
// color, draw color, it can be 1 or 0.Zoom == True - readable
// Zoom == False - tiny
OLED12864_I2C.init(60)
basic.pause(100)
OLED12864_I2C.zoom(true)
// Inside Temp
weatherbit.startWeatherMonitoring()
let Set_RTC = 1
sDateTime = "date"
if (Set_RTC == 1) {
gatorRTC.set1224Mode(TimeMode.Military)
gatorRTC.set24Time(18, 0, 0)
gatorRTC.setDate(
dayNames.Tuesday,
Months.October,
6,
20
)
}
serial.redirect(
SerialPin.P15,
SerialPin.P14,
BaudRate.BaudRate9600
)
xOLED = 7
bView1OLED = false
bView2OLED = false
sDateTime = ""
lWinDir = ["N ", "NE", "E ", "SE", "SE", "SW", "W ", "NW", "??"]
MonthArray = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C"]
basic.pause(100)
serial.writeString(sHeader)
getDateTime()
serial.writeString(sDateTime)
sMonth = MonthArray[gatorRTC.getTimeComponent(TimeType.Month) - 1]
let iMonth = gatorRTC.getTimeComponent(TimeType.Seconds)
// iViewOLED = 2
//
// SM: nn sT: nn Row=1
//
// IT:nn IH:nn Row=2
//
// iVeiwOLED=1
//
// TM:nn HUM: nn
//
// PR:nnn:: Rain: nn
//
// WS:nnn WDIR: nnn
basic.forever(function () {
radio.sendValue("Request", 1)
iRowCnt += 1
if (iRowCnt > 999999) {
iRowCnt = 1
}
getDateTime()
myOLED(sDateTime, 0, 0, 2, bView1OLED, bView2OLED)
insideTemp = weatherbit.temperature() / 100
insideTemp = insideTemp * 1.8 + 32
textDisplay(insideTemp, 1)
myOLED("IT:" + sReturn, 0, 1, 2, bView1OLED, bView2OLED)
basic.pause(100)
insideHumid = weatherbit.humidity() / 1024
textDisplay(insideHumid, 1)
myOLED("IH:" + sReturn, xOLED, 1, 2, bView1OLED, bView2OLED)
basic.pause(Interval)
}