--[[ 2 --------------------------------------------------------- 3 RFID application reads Arduino + RC522 MIFARE tags from 4 battery and stores information to logfile. 5 6 This is minimized DC/DS-16 version, requires firmware 7 4.20 or newer. 8 9 RC-Thoughts Jeti RFID-Sensor and RFID-Battery application 10 is compatible with Revo Bump and does not disturb 11 Robbe BID usage (Onki's solution) 12 13 Requires RFID-Sensor with firmware 1.7 or up 14 15 Italian translation courtesy from Fabrizio Zaini 16 --------------------------------------------------------- 17 RFID application is part of RC-Thoughts Jeti Tools. 18 --------------------------------------------------------- 19 Released under MIT-license by Tero @ RC-Thoughts.com 2017 20 --------------------------------------------------------- 21 --]] 22 collectgarbage() 23 ---------------------------------------------------------------------- 24 -- Locals for the application 25 local rfidVersion, tCurRFID, tStrRFID = "2.4", 0, 0 26 local rfidId, rfidParam, rfidSens, mahId, mahParam, mahSens 27 local capaAlarm, capaAlarmTr, alarmVoice, vPlayed, tagID 28 local rfidTime, annGo, annSw, tagCapa, alarm1Tr 29 local tagValid, tSetAlm, percVal, annTime = 0, 0, "-", 0 30 local sensorLa1list = { "..." } 31 local sensorId1list = { "..." } 32 local sensorPa1list = { "..." } 33 local trans8 34 ---------------------------------------------------------------------- 35 -- Read translations 36 local function setLanguage() 37 local lng=system.getLocale() 38 local file = io.readall("Apps/Lang/RCT-Rfid.jsn") 39 local obj = json.decode(file) 40 if(obj) then 41 trans8 = obj[lng] or obj[obj.default] 42 end 43 end 44 ---------------------------------------------------------------------- 45 -- Read available sensors for user to select 46 local function readSensors() 47 local sensors = system.getSensors() 48 local format = string.format 49 local insert = table.insert 50 for i, sensor in ipairs(sensors) do 51 if (sensor.label ~= "") then 52 insert(sensorLa1list, format("%s", sensor.label)) 53 insert(sensorId1list, format("%s", sensor.id)) 54 insert(sensorPa1list, format("%s", sensor.param)) 55 end 56 end 57 end 58 ---------------------------------------------------------------------- 59 -- Draw the telemetry windows 82 ---------------------------------------------------------------------- 83 -- Store settings when changed by user 84 -- 85 local function capaAlarmChanged(value) 86 local pSave = system.pSave 87 88 capaAlarm = value 89 pSave("capaAlarm", value) 90 91 alarm1Tr = string.format("%.1f", capaAlarm) 92 pSave("capaAlarmTr", capaAlarmTr) 93 system.registerTelemetry(1, trans8.telLabel, 2, printBattery) 94 end 95 96 local function alarmVoiceChanged(value) 97 alarmVoice = value 98 system.pSave("alarmVoice", value) 99 end 100 101 -- 102 local function sensorIDChanged(value) 103 local pSave = system.pSave 104 local format = string.format 105 106 rfidSens = value 107 pSave("rfidSens", value) 108 rfidId = format("%s", sensorId1list[rfidSens]) 109 rfidParam = format("%s", sensorPa1list[rfidSens]) 110 if (rfidId == "...") then 111 rfidId = 0 112 rfidParam = 0 113 end 114 pSave("rfidId", rfidId) 115 pSave("rfidParam", rfidParam) 116 end 117 118 local function sensorMahChanged(value) 119 local pSave = system.pSave 120 local format = string.format 121 122 mahSens = value 123 pSave("mahSens", value) 124 mahId = format("%s", sensorId1list[mahSens]) 125 mahParam = format("%s", sensorPa1list[mahSens]) 126 if (mahId == "...") then 127 mahId = 0 128 mahParam = 0 129 end 130 pSave("mahId", mahId) 131 pSave("mahParam", mahParam) 132 end 133 134 local function annSwChanged(value) 135 annSw = value 136 system.pSave("annSw", value) 137 end 138 ---------------------------------------------------------------------- 139 -- Draw the main form (Application inteface) 140 local function initForm() 141 local form = form 142 local addRow = form.addRow 143 local addLabel = form.addLabel 144 145 form.setButton(1, ":tools") 146 147 addRow(1) 148 addLabel({ label = "--- RC-Thoughts Jeti Tools ---", font = FONT_BIG }) 149 150 addRow(1) 151 addLabel({ label = trans8.labelCommon, font = FONT_BOLD }) 152 153 addRow(2) 154 addLabel({ label = trans8.sensorID }) 155 form.addSelectbox(sensorLa1list, rfidSens, true, sensorIDChanged) 156 157 addRow(2) 158 addLabel({ label = trans8.sensorMah }) 159 form.addSelectbox(sensorLa1list, mahSens, true, sensorMahChanged) 160 161 addRow(1) 162 addLabel({ label = trans8.labelAlarm, font = FONT_BOLD }) 163 164 addRow(2) 165 addLabel({ label = trans8.AlmVal }) 166 form.addIntbox(capaAlarm, 0, 100, 0, 0, 1, capaAlarmChanged) 167 168 addRow(2) 169 addLabel({ label = trans8.selAudio }) 170 form.addAudioFilebox(alarmVoice, alarmVoiceChanged) 171 172 addRow(2) 173 addLabel({ label = trans8.annSw, width = 220 }) 174 form.addInputbox(annSw, true, annSwChanged) 175 176 addRow(1) 177 addLabel({ label = "Powered by RC-Thoughts.com - v." .. rfidVersion .. " ", font = FONT_MINI, alignRight = true }) 178 179 form.setFocusedRow(1) 180 end 181 182 ---------------------------------------------------------------------- 183 local function loop() 184 local system = system 185 -- RFID reading and battery-definition 186 if (rfidSens > 1) then 187 rfidTime = system.getTime() 188 tagID = system.getSensorByID(rfidId, 1) 189 tagCapa = system.getSensorByID(rfidId, 2) 190 annGo = system.getInputsVal(annSw) 191 if (tagID and tagID.valid) then 192 tagValid = 1 193 tagID = tagID.value 194 tagCapa = tagCapa.value 195 else 196 percVal = "-" 197 tagValid = 0 198 end 199 -- Capacity percentage calculation and voice alert config 200 if (mahSens > 1) then 201 local mahCapa = system.getSensorByID(mahId, mahParam) 202 if (mahCapa and mahCapa.valid) then 203 mahCapa = mahCapa.value 204 if (tagValid == 1) then 205 if (tSetAlm == 0) then 206 tCurRFID = rfidTime 207 tStrRFID = rfidTime + 5 208 tSetAlm = 1 209 else 210 tCurRFID = system.getTime() 211 end 212 local resRFID = (((tagCapa - mahCapa) * 100) / tagCapa) 213 if (resRFID < 0) then 214 resRFID = 0 215 else 216 if (resRFID > 100) then 217 resRFID = 100 218 end 219 end 220 percVal = string.format("%.1f", resRFID) 221 if (alarm1Tr == 0) then 222 vPlayed = 0 223 tStrRFID = 0 224 else 225 if (resRFID <= capaAlarm) then 226 if (tStrRFID <= tCurRFID and tSetAlm == 1) then 227 if (vPlayed == 0 or vPlayed == nil and alarmVoice ~= "...") then 228 system.playFile(alarmVoice, AUDIO_QUEUE) 229 vPlayed = 1 230 end 231 end 232 else 233 vPlayed = 0 234 end 235 end 236 else 237 percVal = "-" 238 vPlayed = 0 239 tSetAlm = 0 240 end 241 end 242 end 243 else 244 rfidTime = 0 245 end 246 if(annGo == 1 and resRFID >= 0 and resRFID <= 100 and annTime < rfidTime) then 247 system.playNumber(percVal, 0, "%", trans8.annCap) 248 annTime = rfidTime + 10 249 end 250 collectgarbage() 251 end 252 253 ---------------------------------------------------------------------- 254 -- Application initialization 255 local function init() 256 local pLoad = system.pLoad 257 rfidId = pLoad("rfidId", 0) 258 rfidParam = pLoad("rfidParam", 0) 259 rfidSens = pLoad("rfidSens", 0) 260 mahId = pLoad("mahId", 0) 261 mahParam = pLoad("mahParam", 0) 262 mahSens = pLoad("mahSens", 0) 263 capaAlarm = pLoad("capaAlarm", 0) 264 capaAlarmTr = pLoad("capaAlarmTr", 1) 265 alarmVoice = pLoad("alarmVoice", "...") 266 annSw = pLoad("annSw") 267 readSensors() 268 system.registerForm(1, MENU_APPS, trans8.appName, initForm, keyPressed) 269 system.registerTelemetry(1, "RFID-Battery", 2, printBattery) 270 collectgarbage() 271 end 272 273 ---------------------------------------------------------------------- 274 setLanguage() 275 collectgarbage() 276 return { init = init, loop = loop, author = "RC-Thoughts", version = rfidVersion, name = trans8.appName }