Skip to content

Instantly share code, notes, and snippets.

@electricbaka
Created Apr 12, 2018
Embed
What would you like to do?
モーション起動スイッチで超低消費電力IoT:FlashAirのファームウェア(リトライ有り)
--SPI Init
function spi_init()
fa.spi("init", 1) --1.6MHz@W-04
fa.spi("mode", 0) --MODE=0
sleep(1000)
end
-- SPI Write
function spi_write(reg, data)
fa.spi("cs", 0) --CS : ON
fa.spi("write", 0x0A) --Reg Write
fa.spi("write", reg)
fa.spi("write", data)
fa.spi("cs", 1) --CS :OFF
sleep(100)
end
-- SPI Read
function spi_read(reg)
fa.spi("cs", 0) --CS : ON
fa.spi("write", 0x0B) --Reg Read
fa.spi("write", reg)
data = fa.spi("read")
fa.spi("cs", 1) --CS :OFF
sleep(100)
return data
end
---------- Main ----------
-- SPI Initial Setting
spi_init()
-- ADXL362 Setting
spi_write(0x20, 0x50) -- Activity threshold Lower : 80
spi_write(0x21, 0x00) -- Activity threshold Upper : 80
spi_write(0x22, 0x00) -- Activity timer (only 1Byte) : 0
spi_write(0x23, 0xFF) -- Inactivity threshold Lower : MAX(0x07FF)
spi_write(0x24, 0x07) -- Inactivity threshold Upper : MAX(0x07FF)
spi_write(0x25, 0x06) -- Inactivity timer Lower : 1sec
spi_write(0x26, 0x00) -- Inactivity timer Upper : 1sec
spi_write(0x27, 0x1F) -- Motion detection, activity/inactivity detection : LINK mode
spi_write(0x2B, 0xC0) -- INT2 MAP : AWAKE bit, set ActiveLow
spi_write(0x2D, 0x0E) -- Power Control : WAKEUP, AUTOSLEEP, Start measurement
-- Retry loop
for i = 1, 5 do
-- Wait for FlashAir WiFi Access stable
for j = 1, 30 do
sleep(1000)
result = fa.control("fioget")
if result == 1 then
sleep(2000)
break
end
end
-- Send HTTP GET request
url_string = "https://***********************************************************************"
b, c, h = fa.request{ url = url_string, method = "GET" }
-- HTTP GET request Success
if c ~= -1 then
break
end
-- WiFi OFF/ON
fa.control("fioset", 0) --wifi off
sleep(1000)
fa.control("fioset", 1) --wifi on
end
--WiFi OFF
fa.control("fioset", 0)
sleep(100)
-- ADXL362 INT2 flag clear
spi_read(0x0B)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment