├── .gitattributes ├── .gitignore ├── 1_node_soil.lua ├── SSD1306_IP.lua ├── ajax.lua ├── ap.lua ├── app.lua ├── ascii.lua ├── calculator.html ├── coms.lua ├── config.lua ├── dht1.lua ├── dht11.lua ├── dht22.lua ├── dht_lib.lua ├── ds18b20.lua ├── ds3231-example.lua ├── ds3231-web.lua ├── ds3231.lua ├── ds_tem.lua ├── esp8266-projects-master.zip ├── esp8266-projects-master ├── 01d.MONO ├── 02d.MONO ├── 03d.MONO ├── 04d.MONO ├── 09d.MONO ├── 10d.MONO ├── 11d.MONO ├── 13d.MONO ├── 50d.MONO ├── README.md ├── simple-oled-example │ ├── clouds.png │ ├── clouds.xbm │ ├── displayXBM.lua │ ├── simple-oled-sketch.fzz │ └── simple-oled-sketch_bb.png ├── temp.MONO ├── temp.gif ├── temp.png └── weather-station │ ├── 01d.MONO │ ├── 01n.xbm │ ├── 02d.MONO │ ├── 03d.MONO │ ├── 04d.MONO │ ├── 09d.MONO │ ├── 10d.MONO │ ├── 11d.MONO │ ├── 13d.MONO │ ├── 50d.MONO │ ├── README.md │ ├── clouds.xbm │ ├── init.lua │ ├── simple-oled-sketch_bb.png │ ├── src │ ├── 01d.png │ ├── 02d.png │ ├── 03d.png │ ├── 04d.png │ ├── 09d.png │ ├── 10d.png │ ├── 11d.png │ ├── 13d.png │ ├── 50d.png │ ├── display.jpg │ └── simple-oled-sketch.fzz │ ├── temp.XBM │ └── weatherStation.lua ├── hello.lua ├── http.lua ├── http_bridge.lua ├── http_dimmer.lua ├── http_node.lua ├── htu21df.lua ├── init.lua ├── init_bak.lua ├── iothink.lua ├── iothink.zip ├── iothink_dht11.lua ├── iothink_dimmer.lua ├── iothink_ds1820.lua ├── iothink_relay.lua ├── iothink_relay_lcd.lua ├── iothink_rgb.lua ├── iothink_test.lua ├── iothink_trmp.lua ├── iothink_weather.lua ├── magsw.lua ├── main.lua ├── monDHT11.lua ├── mqtt_client.lua ├── mqtt_dht11.lua ├── mqtt_ds1820.lua ├── mqtt_soil.lua ├── mqtt_test.lua ├── mymodule.lua ├── node_ds1820.lua ├── node_light ├── node_light.lua ├── node_ralay.lua ├── node_secur.lua ├── node_sensor.lua ├── node_soil.lua ├── node_temperature.lua ├── oled.lua ├── oled_test.lua ├── palette.lua ├── palette2.lua ├── pir.lua ├── rgb.lua ├── run.lua ├── sample_http.lua ├── sample_http2.lua ├── sample_http3.lua ├── set_config.lua ├── soil.lua ├── ssd1306.lua ├── ssd13061.lua ├── tcp_client_temp.lua ├── tcp_server_temp.lua ├── thingspeak_dh11.lua ├── thingspeak_dht11.lua ├── thingspeak_ds18B20.lua ├── tinyweb.lua ├── training ├── blinking.lua ├── dht.lua ├── for_1.lua ├── for_2.lua ├── for_3.lua ├── http_server.lua ├── if.lua ├── read_dht.lua ├── tcp_server.lua ├── timer.lua ├── while.lua └── wifi.lua ├── u8g_graphics_test.lua ├── uart.lua ├── uart_server.lua ├── weatherStation.lua ├── weathericon ├── 01d.MONO ├── 02d.MONO ├── 03d.MONO ├── 04d.MONO ├── 09d.MONO ├── 10d.MONO ├── 11d.MONO ├── 13d.MONO └── 50d.MONO ├── wifi_sensor.lua ├── www.lua └── xperia.lua /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear on external disk 35 | .Spotlight-V100 36 | .Trashes 37 | 38 | # Directories potentially created on remote AFP share 39 | .AppleDB 40 | .AppleDesktop 41 | Network Trash Folder 42 | Temporary Items 43 | .apdisk 44 | -------------------------------------------------------------------------------- /1_node_soil.lua: -------------------------------------------------------------------------------- 1 | server = "192.168.1.48" 2 | port = 5683 3 | node_name = "Soil-02" 4 | conn=net.createConnection(net.TCP, 0) 5 | gpio.mode(4,gpio.INPUT,gpio.FLOAT) 6 | gpio.mode(5,gpio.INPUT,gpio.FLOAT) 7 | gpio.mode(6,gpio.INPUT,gpio.FLOAT) 8 | soil = "HIGH" 9 | function split(s, delimiter) 10 | result = {}; 11 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 12 | table.insert(result, match); 13 | end 14 | return result; 15 | end 16 | 17 | function open() 18 | print("Open connection...") 19 | conn:on("receive", receive) 20 | conn:on("disconnection", function() 21 | print("Disconnection..") 22 | end); 23 | conn:on("reconnection", function() print("Disconnection..") end); 24 | conn:on("connection", function() print("Connected..") end); 25 | conn:connect(port, server) 26 | conn:send('{ "type": 1, "data": "'.. node.chipid() .. '", "name": "'..node_name..'"}') 27 | end 28 | 29 | function receive(conn, payload) 30 | list=split(payload,"|") 31 | if (list[1] == 'V') then 32 | if (list[2] == 'heap') then 33 | conn:send('{ "type": 2, "data":"'.. node.heap() .. '"}') 34 | end 35 | 36 | if (list[2] == 'soil1') then 37 | soil = "HIGH" 38 | if gpio.read(4)==1 then 39 | soil = "LOW" 40 | end 41 | conn:send('{ "type": 2, "data": { "deviceID": "'..node.chipid()..'", "id": 3, "value": "'..soil..'"}}') 42 | end 43 | end 44 | end 45 | 46 | 47 | status_soil2 = "HIGH" 48 | oldstatus_soil2 = "HIGH" 49 | 50 | function sendalarm(id, status) 51 | conn:send('{ "type": 4, "deviceID": "'.. node.chipid() ..'", "data": {"msg": "'..node_name..' status change", "id": '..id..', "value": "'..status..'"}}') 52 | end 53 | 54 | tmr.alarm(0, 3000, 1, function() -- Set alarm to one second 55 | 56 | if gpio.read(4)==1 then status_soil2="LOW" else status_soil2="HIGH" end 57 | if status_soil2 ~= oldstatus_soil2 then sendalarm (3, status_soil2) end 58 | oldstatus_soil2 = status_soil2 59 | end) 60 | 61 | function close() 62 | conn:close() 63 | end 64 | 65 | tmr.alarm(1, 2000, 1, function() 66 | open() 67 | tmr.stop(1) 68 | end) 69 | 70 | -------------------------------------------------------------------------------- /SSD1306_IP.lua: -------------------------------------------------------------------------------- 1 | 2 | -- SDA and SCL can be assigned freely to available GPIOs 3 | sda = 3 4 | scl = 4 5 | sla = 0x3c 6 | i2c.setup(0, sda, scl, i2c.SLOW) 7 | disp = u8g.ssd1306_128x64_i2c(sla) 8 | -- graphic test components 9 | disp:setFont(u8g.font_6x10) 10 | disp:setFontRefHeightExtendedText() 11 | disp:setDefaultForegroundColor() 12 | disp:setFontPosTop() 13 | if ssid == nil then ssid = "N/A" end 14 | function init() 15 | disp:firstPage() 16 | repeat 17 | disp:drawStr(40, 10, "NodeMCU") 18 | disp:drawStr(5, 28, "SSID: " .. ssid) 19 | disp:drawStr(15, 47, "Connecting ...") 20 | until disp:nextPage() == false 21 | end 22 | function display() 23 | disp:firstPage() 24 | repeat 25 | disp:drawStr(10, 10, "IP Address:") 26 | disp:drawStr(30, 28, wifi.sta.getip().."") 27 | disp:drawStr(20, 47, "ID: "..node.chipid()) 28 | until disp:nextPage() == false 29 | end 30 | 31 | 32 | -------------------------------------------------------------------------------- /ajax.lua: -------------------------------------------------------------------------------- 1 | pin1=3 2 | pin2=4 3 | gpio.mode(pin1,gpio.OUTPUT) 4 | gpio.mode(pin2,gpio.OUTPUT) 5 | function split(s, delimiter) 6 | result = {}; 7 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 8 | table.insert(result, match); 9 | end 10 | return result; 11 | end 12 | function urlencode(payload) 13 | result = {}; 14 | list=split(payload,"\r\n") 15 | --print(list[1]) 16 | list=split(list[1]," ") 17 | --print(list[2]) 18 | list=split(list[2],"\/") 19 | 20 | table.insert(result, list[1]); 21 | table.insert(result, list[2]); 22 | table.insert(result, list[3]); 23 | 24 | return result; 25 | end 26 | 27 | function sendHeader(conn) 28 | conn:send("HTTP/1.1 200 OK\r\n") 29 | conn:send("Access-Control-Allow-Origin: *\r\n") 30 | conn:send("Content-Type: application/json; charset=utf-8\r\n") 31 | conn:send("Server:NodeMCU\r\n") 32 | conn:send("Connection: close\r\n\r\n") 33 | end 34 | 35 | function index(conn) 36 | conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\ 37 | \ 38 | ESP8266\ 39 | \ 40 | \ 41 | \ 47 | \ 48 |

GPIO3


\ 49 |

GPIO4


\ 50 | ') 51 | end 52 | srv=net.createServer(net.TCP) 53 | srv:listen(80,function(conn) 54 | conn:on("receive",function(conn,payload) 55 | print("Http Request..") 56 | list=urlencode(payload) 57 | if (list[2]=="") then 58 | index(conn) 59 | elseif (list[2]=="write") then 60 | local pin = tonumber(list[3]) 61 | local status = tonumber(list[4]) 62 | gpio.write(pin, status) 63 | sendHeader(conn) 64 | conn:send("{\"result\":\"ok\",\"digitalPin\": "..pin..", \"status\": "..gpio.read(pin).."}") 65 | elseif (list[2]=="read") then 66 | sendHeader(conn) 67 | conn:send("{\"result\":\"ok\", \"digitalPins\": [{\"digitalPin\": "..pin1..", \"status\": "..gpio.read(pin1).."},{\"digitalPin\": "..pin2..", \"status\": "..gpio.read(pin2).."}]}") 68 | else 69 | sendHeader(conn) 70 | conn:send("{\"result\":\"error\",\"message\": \"command not found\"}") 71 | end 72 | conn:close() 73 | end) 74 | end) 75 | -------------------------------------------------------------------------------- /ap.lua: -------------------------------------------------------------------------------- 1 | print("set up wifi AP mode") 2 | wifi.setmode(wifi.SOFTAP) 3 | cfg = {} 4 | cfg.ssid = 'ESP8266-'..node.chipid() 5 | cfg.pwd = '1234567890' 6 | wifi.ap.config(cfg) 7 | 8 | tmr.alarm(0, 2000, 0, function() 9 | print ('Start HTTP Server') 10 | dofile('sample_http3.lc') 11 | end) 12 | -------------------------------------------------------------------------------- /app.lua: -------------------------------------------------------------------------------- 1 | 2 | function split(s, delimiter) 3 | result = {}; 4 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 5 | table.insert(result, match); 6 | end 7 | return result; 8 | end 9 | function urlencode(payload) 10 | result = {}; 11 | list=split(payload,"\r\n") 12 | --print(list[1]) 13 | list=split(list[1]," ") 14 | --print(list[2]) 15 | list=split(list[2],"\/") 16 | 17 | table.insert(result, list[1]); 18 | table.insert(result, list[2]); 19 | table.insert(result, list[3]); 20 | 21 | return result; 22 | end 23 | function sendHeader(conn) 24 | conn:send("HTTP/1.1 200 OK\r\n") 25 | conn:send("Access-Control-Allow-Origin: *\r\n") 26 | conn:send("Content-Type: application/json; charset=utf-8\r\n") 27 | conn:send("Server:NodeMCU\r\n") 28 | conn:send("Connection: close\r\n\r\n") 29 | end 30 | function index(conn) 31 | conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\ 32 | \ 33 | ESP8266\ 34 | \ 35 | \ 36 | \ 37 |

ESP8266 GOIP API

\ 38 | ') 39 | end 40 | srv=net.createServer(net.TCP) 41 | srv:listen(80,function(conn) 42 | conn:on("receive",function(conn,payload) 43 | list=urlencode(payload) 44 | if (list[2]=="") then 45 | index(conn) 46 | elseif (list[2]=="write") then 47 | local pin = tonumber(list[3]) 48 | --print("Pin: "..pin) 49 | local status = tonumber(list[4]) 50 | --print("State: "..status) 51 | gpio.write(pin, status) 52 | -- Response Header 53 | sendHeader(conn) 54 | -- Response Content 55 | conn:send('{"result":"ok","digitalPin": '..pin..', "status": '..gpio.read(pin)..'}') 56 | elseif (list[2]=="read") then 57 | -- Response Header 58 | sendHeader(conn) 59 | -- Response Content 60 | conn:send('{"result":"ok", "digitalPins":[\ 61 | {"digitalPin": 4,"status": '..gpio.read(4)..'},\ 62 | {"digitalPin": 5, "status": '..gpio.read(5)..'},\ 63 | {"digitalPin": 6, "status": '..gpio.read(6)..'},\ 64 | {"digitalPin": 7, "status": '..gpio.read(7)..'}\ 65 | ]}') 66 | else 67 | -- Response Header 68 | sendHeader(conn) 69 | -- Response Content 70 | conn:send('{"result":"error","message": "command not found"}') 71 | end 72 | conn:close() 73 | end) 74 | end) 75 | -------------------------------------------------------------------------------- /ascii.lua: -------------------------------------------------------------------------------- 1 | ascii = {} 2 | 3 | ascii[0] = {} 4 | ascii[0][0] = 0x3e 5 | ascii[0][1] = 0x51 6 | ascii[0][2] = 0x49 7 | ascii[0][3] = 0x45 8 | ascii[0][4] = 0x3e 9 | 10 | ascii[1] = {} 11 | ascii[1][0] = 0x00 12 | ascii[1][1] = 0x42 13 | ascii[1][2] = 0x7f 14 | ascii[1][3] = 0x40 15 | ascii[1][4] = 0x00 16 | 17 | ascii[2] = {} 18 | ascii[2][0] = 0x42 19 | ascii[2][1] = 0x61 20 | ascii[2][2] = 0x51 21 | ascii[2][3] = 0x49 22 | ascii[2][4] = 0x46 23 | 24 | ascii[3] = {} 25 | ascii[3][0] = 0x21 26 | ascii[3][1] = 0x41 27 | ascii[3][2] = 0x45 28 | ascii[3][3] = 0x4b 29 | ascii[3][4] = 0x31 30 | 31 | ascii[4] = {} 32 | ascii[4][0] = 0x18 33 | ascii[4][1] = 0x14 34 | ascii[4][2] = 0x12 35 | ascii[4][3] = 0x7f 36 | ascii[4][4] = 0x10 37 | 38 | ascii[5] = {} 39 | ascii[5][0] = 0x27 40 | ascii[5][1] = 0x45 41 | ascii[5][2] = 0x45 42 | ascii[5][3] = 0x45 43 | ascii[5][4] = 0x39 44 | 45 | ascii[6] = {} 46 | ascii[6][0] = 0x3c 47 | ascii[6][1] = 0x4a 48 | ascii[6][2] = 0x49 49 | ascii[6][3] = 0x49 50 | ascii[6][4] = 0x30 51 | 52 | ascii[7] = {} 53 | ascii[7][0] = 0x01 54 | ascii[7][1] = 0x71 55 | ascii[7][2] = 0x09 56 | ascii[7][3] = 0x05 57 | ascii[7][4] = 0x03 58 | 59 | ascii[8] = {} 60 | ascii[8][0] = 0x36 61 | ascii[8][1] = 0x49 62 | ascii[8][2] = 0x49 63 | ascii[8][3] = 0x49 64 | ascii[8][4] = 0x36 65 | 66 | ascii[9] = {} 67 | ascii[9][0] = 0x06 68 | ascii[9][1] = 0x49 69 | ascii[9][2] = 0x49 70 | ascii[9][3] = 0x29 71 | ascii[9][4] = 0x1e 72 | -------------------------------------------------------------------------------- /calculator.html: -------------------------------------------------------------------------------- 1 | 2 | calculator 3 | 4 |
5 | 6 | 12 | 13 | 14 |
15 | {{result}} 16 | 17 | -------------------------------------------------------------------------------- /coms.lua: -------------------------------------------------------------------------------- 1 | ssid = 'Comscicafe' 2 | pwd = 'ArteTalento' 3 | print("set up wifi mode") 4 | wifi.setmode(wifi.STATION) 5 | wifi.sta.config(ssid, pwd) 6 | wifi.sta.connect() 7 | cnt = 0 8 | tmr.alarm(0, 1000, 1, function() 9 | if (wifi.sta.getip() == nil) and (cnt < 200) then 10 | print("IP unavaiable, Waiting...") 11 | cnt = cnt + 1 12 | else 13 | tmr.stop(0) 14 | if (cnt < 20) then 15 | print("Config done, IP is "..wifi.sta.getip()) 16 | tmr.alarm(0, 2000, 1, function() 17 | tmr.stop(0) 18 | print("WIFI connected...") 19 | dofile('tcp_client_temp.lc') 20 | --dofile("iothink_relay.lc") 21 | -- dofile("sample_http3.lua") 22 | --dofile("iothink_weather.lc") 23 | end) 24 | else 25 | print("Wifi setup time more than 10s, Please verify wifi.sta.config() function. Then re-download the file.") 26 | end 27 | end 28 | end) 29 | -------------------------------------------------------------------------------- /config.lua: -------------------------------------------------------------------------------- 1 | see_dum 2 | 0863219053 3 | -------------------------------------------------------------------------------- /dht1.lua: -------------------------------------------------------------------------------- 1 | pin = 4 2 | 3 | Humidity = 0 4 | HumidityDec=0 5 | Temperature = 0 6 | TemperatureDec=0 7 | Checksum = 0 8 | ChecksumTest=0 9 | 10 | 11 | function getTemp() 12 | Humidity = 0 13 | HumidityDec=0 14 | Temperature = 0 15 | TemperatureDec=0 16 | Checksum = 0 17 | ChecksumTest=0 18 | 19 | --Data stream acquisition timing is critical. There's 20 | --barely enough speed to work with to make this happen. 21 | --Pre-allocate vars used in loop. 22 | 23 | bitStream = {} 24 | for j = 1, 40, 1 do 25 | bitStream[j]=0 26 | end 27 | bitlength=0 28 | 29 | gpio.mode(pin, gpio.OUTPUT) 30 | gpio.write(pin, gpio.LOW) 31 | tmr.delay(20000) 32 | --Use Markus Gritsch trick to speed up read/write on GPIO 33 | gpio_read=gpio.read 34 | gpio_write=gpio.write 35 | 36 | gpio.mode(pin, gpio.INPUT) 37 | 38 | --bus will always let up eventually, don't bother with timeout 39 | while (gpio_read(pin)==0 ) do end 40 | 41 | c=0 42 | while (gpio_read(pin)==1 and c<100) do c=c+1 end 43 | 44 | --bus will always let up eventually, don't bother with timeout 45 | while (gpio_read(pin)==0 ) do end 46 | 47 | c=0 48 | while (gpio_read(pin)==1 and c<100) do c=c+1 end 49 | 50 | --acquisition loop 51 | for j = 1, 40, 1 do 52 | while (gpio_read(pin)==1 and bitlength<10 ) do 53 | bitlength=bitlength+1 54 | end 55 | bitStream[j]=bitlength 56 | bitlength=0 57 | --bus will always let up eventually, don't bother with timeout 58 | while (gpio_read(pin)==0) do end 59 | end 60 | 61 | --DHT data acquired, process. 62 | 63 | for i = 1, 8, 1 do 64 | if (bitStream[i+0] > 2) then 65 | Humidity = Humidity+2^(8-i) 66 | end 67 | end 68 | for i = 1, 8, 1 do 69 | if (bitStream[i+8] > 2) then 70 | HumidityDec = HumidityDec+2^(8-i) 71 | end 72 | end 73 | for i = 1, 8, 1 do 74 | if (bitStream[i+16] > 2) then 75 | Temperature = Temperature+2^(8-i) 76 | end 77 | end 78 | for i = 1, 8, 1 do 79 | if (bitStream[i+24] > 2) then 80 | TemperatureDec = TemperatureDec+2^(8-i) 81 | end 82 | end 83 | for i = 1, 8, 1 do 84 | if (bitStream[i+32] > 2) then 85 | Checksum = Checksum+2^(8-i) 86 | end 87 | end 88 | ChecksumTest=(Humidity+HumidityDec+Temperature+TemperatureDec) % 0xFF 89 | 90 | print ("Temperature: "..Temperature.."."..TemperatureDec) 91 | print ("Humidity: "..Humidity.."."..HumidityDec) 92 | print ("ChecksumReceived: "..Checksum) 93 | print ("ChecksumTest: "..ChecksumTest) 94 | end -------------------------------------------------------------------------------- /dht11.lua: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- 2 | -- DHT11 module for NODEMCU 3 | -- LICENCE: http://opensource.org/licenses/MIT 4 | -- zlo2k from code Pigs Fly 5 | -------------------------------------------------------------------------------- 6 | local moduleName = "dht11" 7 | local M = {} 8 | _G[moduleName] = M 9 | 10 | local temp = 0 11 | local hum = 0 12 | local bitStream = {} 13 | 14 | function M.init(pin) 15 | bitStream = {} 16 | for j = 1, 40, 1 do 17 | bitStream[j] = 0 18 | end 19 | bitlength = 0 20 | 21 | gpio.mode(pin, gpio.OUTPUT) 22 | gpio.write(pin, gpio.LOW) 23 | tmr.delay(20000) 24 | --Use Markus Gritsch trick to speed up read/write on GPIO 25 | gpio_read = gpio.read 26 | gpio_write = gpio.write 27 | 28 | gpio.mode(pin, gpio.INPUT) 29 | 30 | while (gpio_read(pin) == 0) do end 31 | 32 | c = 0 33 | while (gpio_read(pin) == 1 and c < 100) do c = c + 1 end 34 | 35 | while (gpio_read(pin) == 0) do end 36 | 37 | c = 0 38 | while (gpio_read(pin) == 1 and c < 100) do c = c + 1 end 39 | 40 | for j = 1, 40, 1 do 41 | while (gpio_read(pin) == 1 and bitlength < 10) do 42 | bitlength = bitlength + 1 43 | end 44 | bitStream[j] = bitlength 45 | bitlength = 0 46 | while (gpio_read(pin) == 0) do end 47 | end 48 | 49 | hum = 0 50 | temp = 0 51 | 52 | for i = 1, 8, 1 do 53 | if (bitStream[i + 0] > 2) then 54 | hum = hum + 2 ^ (8 - i) 55 | end 56 | end 57 | for i = 1, 8, 1 do 58 | if (bitStream[i + 16] > 2) then 59 | temp = temp + 2 ^ (8 - i) 60 | end 61 | end 62 | bitStream = {} 63 | gpio_read = nil 64 | gpio_write = nil 65 | end 66 | 67 | function M.getTemp() 68 | return temp 69 | end 70 | 71 | function M.getHumidity() 72 | return hum 73 | end 74 | 75 | return M 76 | -------------------------------------------------------------------------------- /dht22.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------ 2 | -- DHT11/22 query module 3 | -- 4 | -- LICENCE: http://opensource.org/licenses/MIT 5 | -- Vladimir Dronnikov 6 | -- 7 | -- Example: 8 | -- print("DHT11", dofile("dht22.lua").read(4)) 9 | -- print("DHT22", dofile("dht22.lua").read(4, true)) 10 | -- NB: the very first read sometimes fails 11 | ------------------------------------------------------------------------------ 12 | local M 13 | do 14 | -- cache 15 | local gpio = gpio 16 | local val = gpio.read 17 | local waitus = tmr.delay 18 | -- 19 | local read = function(pin, dht22) 20 | -- wait for pin value 21 | local w = function(v) 22 | local c = 255 23 | while c > 0 and val(pin) ~= v do c = c - 1 end 24 | return c 25 | end 26 | -- NB: we preallocate incoming data buffer 27 | -- or precise timing in reader gets broken 28 | local b = { 0, 0, 0, 0, 0 } 29 | 30 | -- kick the device 31 | gpio.mode(pin, gpio.INPUT, gpio.PULLUP) 32 | gpio.write(pin, 1) 33 | waitus(10) 34 | gpio.mode(pin, gpio.OUTPUT) 35 | gpio.write(pin, 0) 36 | waitus(20000) 37 | gpio.write(pin, 1) 38 | gpio.mode(pin, gpio.INPUT, gpio.PULLUP) 39 | -- wait for device presense 40 | if w(0) == 0 or w(1) == 0 or w(0) == 0 then 41 | return nil, 0 42 | end 43 | -- receive 5 octets of data, msb first 44 | for i = 1, 5 do 45 | local x = 0 46 | for j = 1, 8 do 47 | x = x + x 48 | if w(1) == 0 then return nil, 1 end 49 | -- 70us for 1, 27 us for 0 50 | waitus(30) 51 | if val(pin) == 1 then 52 | x = x + 1 53 | if w(0) == 0 then return nil, 2 end 54 | end 55 | end 56 | b[i] = x 57 | end 58 | -- check crc. NB: calculating in receiver loop breaks timings 59 | local crc = 0 60 | for i = 1, 4 do 61 | crc = (crc + b[i]) % 256 62 | end 63 | if crc ~= b[5] then return nil, 3 end 64 | -- convert 65 | local t, h 66 | -- DHT22: values in tenths of unit, temperature can be negative 67 | if dht22 then 68 | h = b[1] * 256 + b[2] 69 | t = b[3] * 256 + b[4] 70 | if t > 0x8000 then t = -(t - 0x8000) end 71 | -- DHT11: no negative temperatures, only integers 72 | -- NB: return in 0.1 Celsius 73 | else 74 | h = 10 * b[1] 75 | t = 10 * b[3] 76 | end 77 | return t, h 78 | end 79 | -- expose interface 80 | M = { 81 | read = read, 82 | } 83 | end 84 | return M -------------------------------------------------------------------------------- /dht_lib.lua: -------------------------------------------------------------------------------- 1 | local moduleName = 'dht_lib' 2 | local M = {} 3 | _G[moduleName] = M 4 | local humidity 5 | local temperature 6 | local bitStream = {} 7 | 8 | local function read(pin) 9 | local bitlength = 0 10 | humidity = 0 11 | temperature = 0 12 | local gpio_read = gpio.read 13 | for j = 1, 40, 1 do 14 | bitStream[j] = 0 15 | end 16 | gpio.mode(pin, gpio.OUTPUT) 17 | gpio.write(pin, gpio.HIGH) 18 | tmr.delay(100) 19 | gpio.write(pin, gpio.LOW) 20 | tmr.delay(20000) 21 | gpio.write(pin, gpio.HIGH) 22 | gpio.mode(pin, gpio.INPUT) 23 | while (gpio_read(pin) == 0 ) do end 24 | local c=0 25 | while (gpio_read(pin) == 1 and c < 500) do c = c + 1 end 26 | 27 | while (gpio_read(pin) == 0 ) do end 28 | c=0 29 | while (gpio_read(pin) == 1 and c < 500) do c = c + 1 end 30 | 31 | for j = 1, 40, 1 do 32 | while (gpio_read(pin) == 1 and bitlength < 10 ) do 33 | bitlength = bitlength + 1 34 | end 35 | bitStream[j] = bitlength 36 | bitlength = 0 37 | 38 | while (gpio_read(pin) == 0) do end 39 | end 40 | end 41 | function M.read11(pin) 42 | read(pin) 43 | local checksum = 0 44 | local checksumTest 45 | for i = 1, 8, 1 do -- Byte[0] 46 | if (bitStream[i] > 3) then 47 | humidity = humidity + 2 ^ (8 - i) 48 | end 49 | end 50 | for i = 1, 8, 1 do -- Byte[2] 51 | if (bitStream[i + 16] > 3) then 52 | temperature = temperature + 2 ^ (8 - i) 53 | end 54 | end 55 | for i = 1, 8, 1 do --Byte[4] 56 | if (bitStream[i + 32] > 3) then 57 | checksum = checksum + 2 ^ (8 - i) 58 | end 59 | end 60 | 61 | if(checksum ~= humidity+temperature) then 62 | humidity = nil 63 | temperature = nil 64 | end 65 | 66 | end 67 | function M.read22( pin ) 68 | read(pin) 69 | local checksum = 0 70 | local checksumTest 71 | for i = 1, 16, 1 do 72 | if (bitStream[i] > 3) then 73 | humidity = humidity + 2 ^ (16 - i) 74 | end 75 | end 76 | for i = 1, 16, 1 do 77 | if (bitStream[i + 16] > 3) then 78 | temperature = temperature + 2 ^ (16 - i) 79 | end 80 | end 81 | for i = 1, 8, 1 do 82 | if (bitStream[i + 32] > 3) then 83 | checksum = checksum + 2 ^ (8 - i) 84 | end 85 | end 86 | checksumTest = (bit.band(humidity, 0xFF) + bit.rshift(humidity, 8) + bit.band(temperature, 0xFF) + bit.rshift(temperature, 8)) 87 | checksumTest = bit.band(checksumTest, 0xFF) 88 | if temperature > 0x8000 then 89 | temperature = -(temperature - 0x8000) 90 | end 91 | if (checksumTest - checksum >= 1) or (checksum - checksumTest >= 1) then 92 | humidity = nil 93 | end 94 | end 95 | 96 | function M.getTemperature() 97 | return temperature 98 | end 99 | 100 | function M.getHumidity() 101 | return humidity 102 | end 103 | 104 | return M 105 | -------------------------------------------------------------------------------- /ds18b20.lua: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- 2 | -- DS18B20 one wire module for NODEMCU 3 | -- NODEMCU TEAM 4 | -- LICENCE: http://opensource.org/licenses/MIT 5 | -- Vowstar 6 | -------------------------------------------------------------------------------- 7 | -- Set module name as parameter of require 8 | local modname = "ds18b20" 9 | local M = {} 10 | _G[modname] = M 11 | -------------------------------------------------------------------------------- 12 | -- Local used variables 13 | -------------------------------------------------------------------------------- 14 | -- DS18B20 dq pin 15 | local pin = nil 16 | -------------------------------------------------------------------------------- 17 | -- Local used modules 18 | -------------------------------------------------------------------------------- 19 | -- Table module 20 | local table = table 21 | -- String module 22 | local string = string 23 | -- One wire module 24 | local ow = ow 25 | -- Timer module 26 | local tmr = tmr 27 | -- Limited to local environment 28 | setfenv(1,M) 29 | -------------------------------------------------------------------------------- 30 | -- Implementation 31 | -------------------------------------------------------------------------------- 32 | C = 0 33 | F = 1 34 | K = 2 35 | function read(pin) 36 | result = nil 37 | ow.setup(pin) 38 | flag = false 39 | if(addr == nil) then 40 | ow.reset_search(pin) 41 | count = 0 42 | repeat 43 | count = count + 1 44 | addr = ow.search(pin) 45 | tmr.wdclr() 46 | until((addr ~= nil) or (count > 100)) 47 | ow.reset_search(pin) 48 | end 49 | if(addr == nil) then 50 | return result 51 | end 52 | crc = ow.crc8(string.sub(addr,1,7)) 53 | if (crc == addr:byte(8)) then 54 | if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then 55 | -- print("Device is a DS18S20 family device.") 56 | ow.reset(pin) 57 | ow.select(pin, addr) 58 | ow.write(pin, 0x44, 1) 59 | -- tmr.delay(1000000) 60 | present = ow.reset(pin) 61 | ow.select(pin, addr) 62 | ow.write(pin,0xBE,1) 63 | -- print("P="..present) 64 | data = nil 65 | data = string.char(ow.read(pin)) 66 | for i = 1, 8 do 67 | data = data .. string.char(ow.read(pin)) 68 | end 69 | -- print(data:byte(1,9)) 70 | crc = ow.crc8(string.sub(data,1,8)) 71 | -- print("CRC="..crc) 72 | if (crc == data:byte(9)) then 73 | if(unit == nil or unit == C) then 74 | t = (data:byte(1) + data:byte(2) * 256) * 625 75 | elseif(unit == F) then 76 | t = (data:byte(1) + data:byte(2) * 256) * 1125 + 320000 77 | elseif(unit == K) then 78 | t = (data:byte(1) + data:byte(2) * 256) * 625 + 2731500 79 | else 80 | return nil 81 | end 82 | t = t / 10000 83 | -- print("Temperature="..t1.."."..t2.." Centigrade") 84 | -- result = t1.."."..t2 85 | return t 86 | end 87 | tmr.wdclr() 88 | else 89 | -- print("Device family is not recognized.") 90 | end 91 | else 92 | -- print("CRC is not valid!") 93 | end 94 | return result 95 | end 96 | 97 | -- Return module table 98 | return M 99 | -------------------------------------------------------------------------------- /ds3231-example.lua: -------------------------------------------------------------------------------- 1 | require("ds3231") 2 | 3 | -- ESP-01 GPIO Mapping 4 | gpio0, gpio2 = 3, 4 5 | 6 | ds3231.init(gpio0, gpio2) 7 | 8 | second, minute, hour, day, date, month, year = ds3231.getTime(); 9 | 10 | -- Get current time 11 | print(string.format("Time & Date: %s:%s:%s %s/%s/%s", hour, minute, second, date, month, year)) 12 | 13 | -- Don't forget to release it after use 14 | ds3231 = nil 15 | package.loaded["ds3231"]=nil -------------------------------------------------------------------------------- /ds3231-web.lua: -------------------------------------------------------------------------------- 1 | require('ds3231') 2 | 3 | port = 80 4 | 5 | -- ESP-01 GPIO Mapping 6 | gpio0, gpio2 = 3, 4 7 | 8 | days = { 9 | [1] = "Sunday", 10 | [2] = "Monday", 11 | [3] = "Tuesday", 12 | [4] = "Wednesday", 13 | [5] = "Thursday", 14 | [6] = "Friday", 15 | [7] = "Saturday" 16 | } 17 | 18 | months = { 19 | [1] = "January", 20 | [2] = "Febuary", 21 | [3] = "March", 22 | [4] = "April", 23 | [5] = "May", 24 | [6] = "June", 25 | [7] = "July", 26 | [8] = "August", 27 | [9] = "September", 28 | [10] = "October", 29 | [11] = "November", 30 | [12] = "December" 31 | } 32 | 33 | ds3231.init(gpio0, gpio2) 34 | 35 | srv=net.createServer(net.TCP) 36 | srv:listen(port, 37 | function(conn) 38 | 39 | second, minute, hour, day, date, month, year = ds3231.getTime() 40 | prettyTime = string.format("%s, %s %s %s %s:%s:%s", days[day], date, months[month], year, hour, minute, second) 41 | 42 | conn:send("HTTP/1.1 200 OK\nContent-Type: text/html\nRefresh: 5\n\n" .. 43 | "" .. 44 | "" .. 45 | "ESP8266
" .. 46 | "Time and Date: " .. prettyTime .. "
" .. 47 | "Node ChipID : " .. node.chipid() .. "
" .. 48 | "Node MAC : " .. wifi.sta.getmac() .. "
" .. 49 | "Node Heap : " .. node.heap() .. "
" .. 50 | "Timer Ticks : " .. tmr.now() .. "
" .. 51 | "") 52 | conn:on("sent",function(conn) conn:close() end) 53 | end 54 | ) -------------------------------------------------------------------------------- /ds3231.lua: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- 2 | -- DS3231 I2C module for NODEMCU 3 | -- NODEMCU TEAM 4 | -- LICENCE: http://opensource.org/licenses/MIT 5 | -- Tobie Booth 6 | -------------------------------------------------------------------------------- 7 | 8 | local moduleName = 'ds3231' 9 | local M = {} 10 | _G[moduleName] = M 11 | 12 | -- Default value for i2c communication 13 | local id = 0 14 | 15 | --device address 16 | local dev_addr = 0x68 17 | 18 | local function decToBcd(val) 19 | return((((val/10) - ((val/10)%1)) *16) + (val%10)) 20 | end 21 | 22 | local function bcdToDec(val) 23 | return((((val/16) - ((val/16)%1)) *10) + (val%16)) 24 | end 25 | 26 | -- initialize i2c 27 | --parameters: 28 | --d: sda 29 | --l: scl 30 | function M.init(d, l) 31 | if (d ~= nil) and (l ~= nil) and (d >= 0) and (d <= 11) and (l >= 0) and ( l <= 11) and (d ~= l) then 32 | sda = d 33 | scl = l 34 | else 35 | print("iic config failed!") return nil 36 | end 37 | print("init done") 38 | i2c.setup(id, sda, scl, i2c.SLOW) 39 | end 40 | 41 | --get time from DS3231 42 | function M.getTime() 43 | i2c.start(id) 44 | i2c.address(id, dev_addr, i2c.TRANSMITTER) 45 | i2c.write(id, 0x00) 46 | i2c.stop(id) 47 | i2c.start(id) 48 | i2c.address(id, dev_addr, i2c.RECEIVER) 49 | local c=i2c.read(id, 7) 50 | i2c.stop(id) 51 | return bcdToDec(tonumber(string.byte(c, 1))), 52 | bcdToDec(tonumber(string.byte(c, 2))), 53 | bcdToDec(tonumber(string.byte(c, 3))), 54 | bcdToDec(tonumber(string.byte(c, 4))), 55 | bcdToDec(tonumber(string.byte(c, 5))), 56 | bcdToDec(tonumber(string.byte(c, 6))), 57 | bcdToDec(tonumber(string.byte(c, 7))) 58 | end 59 | 60 | --set time for DS3231 61 | function M.setTime(second, minute, hour, day, date, month, year) 62 | i2c.start(id) 63 | i2c.address(id, dev_addr, i2c.TRANSMITTER) 64 | i2c.write(id, 0x00) 65 | i2c.write(id, decToBcd(second)) 66 | i2c.write(id, decToBcd(minute)) 67 | i2c.write(id, decToBcd(hour)) 68 | i2c.write(id, decToBcd(day)) 69 | i2c.write(id, decToBcd(date)) 70 | i2c.write(id, decToBcd(month)) 71 | i2c.write(id, decToBcd(year)) 72 | i2c.stop(id) 73 | end 74 | 75 | return M -------------------------------------------------------------------------------- /ds_tem.lua: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- 2 | -- DS18B20 one wire module for NODEMCU 3 | -- NODEMCU TEAM 4 | -- LICENCE: http://opensource.org/licenses/MIT 5 | -- Vowstar 6 | -------------------------------------------------------------------------------- 7 | -- Set module name as parameter of require 8 | local modname = "ds18b20" 9 | local M = {} 10 | _G[modname] = M 11 | -------------------------------------------------------------------------------- 12 | -- Local used variables 13 | -------------------------------------------------------------------------------- 14 | -- DS18B20 dq pin 15 | local pin = nil 16 | -- DS18B20 default pin 17 | local defaultPin = 9 18 | -------------------------------------------------------------------------------- 19 | -- Local used modules 20 | -------------------------------------------------------------------------------- 21 | -- Table module 22 | local table = table 23 | -- String module 24 | local string = string 25 | -- One wire module 26 | local ow = ow 27 | -- Timer module 28 | local tmr = tmr 29 | -- Limited to local environment 30 | setfenv(1,M) 31 | -------------------------------------------------------------------------------- 32 | -- Implementation 33 | -------------------------------------------------------------------------------- 34 | C = 0 35 | F = 1 36 | K = 2 37 | function setup(dq) 38 | pin = dq 39 | if(pin == nil) then 40 | pin = defaultPin 41 | end 42 | ow.setup(pin) 43 | end 44 | 45 | function addrs() 46 | setup(pin) 47 | tbl = {} 48 | ow.reset_search(pin) 49 | repeat 50 | addr = ow.search(pin) 51 | if(addr ~= nil) then 52 | table.insert(tbl, addr) 53 | end 54 | tmr.wdclr() 55 | until (addr == nil) 56 | ow.reset_search(pin) 57 | return tbl 58 | end 59 | function readNumber(addr, unit) 60 | result = nil 61 | setup(pin) 62 | flag = false 63 | if(addr == nil) then 64 | ow.reset_search(pin) 65 | count = 0 66 | repeat 67 | count = count + 1 68 | addr = ow.search(pin) 69 | tmr.wdclr() 70 | until((addr ~= nil) or (count > 100)) 71 | ow.reset_search(pin) 72 | end 73 | if(addr == nil) then 74 | return result 75 | end 76 | crc = ow.crc8(string.sub(addr,1,7)) 77 | if (crc == addr:byte(8)) then 78 | if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then 79 | -- print("Device is a DS18S20 family device.") 80 | ow.reset(pin) 81 | ow.select(pin, addr) 82 | ow.write(pin, 0x44, 1) 83 | -- tmr.delay(1000000) 84 | present = ow.reset(pin) 85 | ow.select(pin, addr) 86 | ow.write(pin,0xBE,1) 87 | -- print("P="..present) 88 | data = nil 89 | data = string.char(ow.read(pin)) 90 | for i = 1, 8 do 91 | data = data .. string.char(ow.read(pin)) 92 | end 93 | -- print(data:byte(1,9)) 94 | crc = ow.crc8(string.sub(data,1,8)) 95 | -- print("CRC="..crc) 96 | if (crc == data:byte(9)) then 97 | if(unit == nil or unit == C) then 98 | t = (data:byte(1) + data:byte(2) * 256) * 625 99 | elseif(unit == F) then 100 | t = (data:byte(1) + data:byte(2) * 256) * 1125 + 320000 101 | elseif(unit == K) then 102 | t = (data:byte(1) + data:byte(2) * 256) * 625 + 2731500 103 | else 104 | return nil 105 | end 106 | t = t / 10000 107 | -- print("Temperature="..t1.."."..t2.." Centigrade") 108 | -- result = t1.."."..t2 109 | return t 110 | end 111 | tmr.wdclr() 112 | else 113 | -- print("Device family is not recognized.") 114 | end 115 | else 116 | -- print("CRC is not valid!") 117 | end 118 | return result 119 | end 120 | 121 | function read(addr, unit) 122 | t = readNumber(addr, unit) 123 | if (t == nil) then 124 | return nil 125 | else 126 | return t 127 | end 128 | end 129 | 130 | -- Return module table 131 | return M 132 | -------------------------------------------------------------------------------- /esp8266-projects-master.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master.zip -------------------------------------------------------------------------------- /esp8266-projects-master/01d.MONO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/01d.MONO -------------------------------------------------------------------------------- /esp8266-projects-master/02d.MONO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/02d.MONO -------------------------------------------------------------------------------- /esp8266-projects-master/03d.MONO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/03d.MONO -------------------------------------------------------------------------------- /esp8266-projects-master/04d.MONO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/04d.MONO -------------------------------------------------------------------------------- /esp8266-projects-master/09d.MONO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/09d.MONO -------------------------------------------------------------------------------- /esp8266-projects-master/10d.MONO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/10d.MONO -------------------------------------------------------------------------------- /esp8266-projects-master/11d.MONO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/11d.MONO -------------------------------------------------------------------------------- /esp8266-projects-master/13d.MONO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/13d.MONO -------------------------------------------------------------------------------- /esp8266-projects-master/50d.MONO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/50d.MONO -------------------------------------------------------------------------------- /esp8266-projects-master/README.md: -------------------------------------------------------------------------------- 1 | # esp8266-projects 2 | 3 | Collection of projects for the ESP8266. Have a look at the projects for more detailed instructions or go to http://blog.squix.ch 4 | -------------------------------------------------------------------------------- /esp8266-projects-master/simple-oled-example/clouds.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/simple-oled-example/clouds.png -------------------------------------------------------------------------------- /esp8266-projects-master/simple-oled-example/clouds.xbm: -------------------------------------------------------------------------------- 1 | #define clouds_width 60 2 | #define clouds_height 60 3 | static unsigned short clouds_bits[] = { 4 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 5 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 6 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 7 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xf000, 0x000f, 0x0000, 8 | 0x0000, 0xfe00, 0x007f, 0x0000, 0x0000, 0xff80, 0x01ff, 0x0000, 0x0000, 9 | 0xffc0, 0x07ff, 0x0000, 0x0000, 0xffe0, 0x0fff, 0x0000, 0x0000, 0x1ff0, 10 | 0x1ff8, 0x0000, 0x0000, 0x07f8, 0x3fc0, 0x0000, 0x0000, 0x01fc, 0x7f00, 11 | 0x0000, 0x0000, 0x00fe, 0x7e00, 0x0000, 0x0000, 0x007e, 0xfc00, 0x0000, 12 | 0xe000, 0x003f, 0xf800, 0x0001, 0xfc00, 0x001f, 0xf800, 0x0001, 0xff00, 13 | 0x001f, 0xf000, 0x0001, 0xff80, 0x000f, 0xf000, 0x0003, 0xffc0, 0x000f, 14 | 0xe000, 0x0003, 0x3fe0, 0x000c, 0xe000, 0x0003, 0x07f0, 0x0000, 0xe000, 15 | 0x000f, 0x03f0, 0x0000, 0xe000, 0x001f, 0x01f8, 0x0000, 0xe000, 0x003f, 16 | 0x00f8, 0x0000, 0xe000, 0x007f, 0x00fc, 0x0000, 0xa000, 0x00ff, 0x007c, 17 | 0x0000, 0x0000, 0x01fc, 0x007c, 0x0000, 0x0000, 0x01f8, 0x007c, 0x0000, 18 | 0x0000, 0x01f0, 0x007c, 0x0000, 0x0000, 0x03f0, 0x007c, 0x0000, 0x0000, 19 | 0x03e0, 0x007c, 0x0000, 0x0000, 0x03e0, 0x00fc, 0x0000, 0x0000, 0x03e0, 20 | 0x00f8, 0x0000, 0x0000, 0x03e0, 0x01f8, 0x0000, 0x0000, 0x03f0, 0x03f8, 21 | 0x0000, 0x0000, 0x01f0, 0x07f0, 0x01c0, 0x0703, 0x01f8, 0x1fe0, 0x81c0, 22 | 0x0703, 0x00fe, 0xffc0, 0xc0e3, 0xe383, 0x00ff, 0xff80, 0xc0f1, 0xe3c1, 23 | 0x007f, 0xff00, 0xe0f0, 0xe3c1, 0x003f, 0xfe00, 0xe0f8, 0xe1e1, 0x000f, 24 | 0x7000, 0xf078, 0xf1f0, 0x0003, 0x0000, 0xf87c, 0x01f0, 0x0000, 0x0000, 25 | 0xf87e, 0x00f8, 0x0000, 0x0000, 0x7c3e, 0x00f8, 0x0000, 0x0000, 0x7e3e, 26 | 0x00f8, 0x0000, 0x0000, 0x3e1c, 0x0070, 0x0000, 0x0000, 0x3e00, 0x0000, 27 | 0x0000, 0x0000, 0x3e00, 0x0000, 0x0000, 0x0000, 0x0c00, 0x0000, 0x0000, 28 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 29 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 30 | 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000 }; 31 | -------------------------------------------------------------------------------- /esp8266-projects-master/simple-oled-example/displayXBM.lua: -------------------------------------------------------------------------------- 1 | 2 | -- setup I2c and connect display 3 | function init_i2c_display() 4 | -- SDA and SCL can be assigned freely to available GPIOs 5 | sda = 3 -- GPIO14 6 | scl = 4 -- GPIO12 7 | sla = 0x3c 8 | i2c.setup(0, sda, scl, i2c.SLOW) 9 | disp = u8g.ssd1306_128x64_i2c(sla) 10 | end 11 | 12 | function xbm_picture() 13 | disp:setFont(u8g.font_6x10) 14 | disp:drawStr( 0, 62, "Rainy 8C") 15 | disp:drawXBM( 0, -5, 60, 60, xbm_data ) 16 | end 17 | 18 | function bitmap_test(delay) 19 | file.open("13d.MONO", "r") 20 | xbm_data = file.read() 21 | file.close() 22 | 23 | disp:firstPage() 24 | repeat 25 | xbm_picture() 26 | until disp:nextPage() == false 27 | 28 | tmr.wdclr() 29 | end 30 | 31 | init_i2c_display() 32 | bitmap_test() 33 | -------------------------------------------------------------------------------- /esp8266-projects-master/simple-oled-example/simple-oled-sketch.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/simple-oled-example/simple-oled-sketch.fzz -------------------------------------------------------------------------------- /esp8266-projects-master/simple-oled-example/simple-oled-sketch_bb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/simple-oled-example/simple-oled-sketch_bb.png -------------------------------------------------------------------------------- /esp8266-projects-master/temp.MONO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/temp.MONO -------------------------------------------------------------------------------- /esp8266-projects-master/temp.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/temp.gif -------------------------------------------------------------------------------- /esp8266-projects-master/temp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/temp.png -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/01d.MONO: -------------------------------------------------------------------------------- 1 | `������p����������~��>�����? ��������~�>�������?���������?��>�>�|����������? �����>�p~������x���������` 2 | -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/01n.xbm: -------------------------------------------------------------------------------- 1 | `������p����������~��>�����? ��������~�>�������?���������?��>�>�|����������? �����>�p~������x���������` 2 | -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/02d.MONO: -------------------------------------------------------------------------------- 1 | <�| ������?�����`���������??�~�|��|����������������������|p���~��~�>�<��|�|�|��|��|��|�������������������������������?��� 2 | -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/03d.MONO: -------------------------------------------------------------------------------- 1 | �������������?���?���~��?������������? ������������|�|�|�|�|�|�������������������������������?���������� 2 | -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/04d.MONO: -------------------------------------------------------------------------------- 1 | �������������?���?���~��?������������? ������������|�|�|�|�|�|�������������������������������?���������� 2 | -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/09d.MONO: -------------------------------------------------------------------------------- 1 | ���������������?�~~~��?�������������������?����|�|�|�|�|�|��������`��8p��?|�>~�>p>> 2 | -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/11d.MONO: -------------------------------------------------------------------------------- 1 | @�����������?�?��?��������?��?�������?�����?�������������������������������������������?�����?������?������?������������ 2 | -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/13d.MONO: -------------------------------------------------------------------------------- 1 | �����������?���?��~~��?��?����������?�������?��������|�|��p�����������`p ���px���q���c��#������#~|����<�������w?p��#~ ��!��qx�pxp��� 2 | -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/50d.MONO: -------------------------------------------------------------------------------- 1 | ������������?�?�����������?��?��?�����?�����?�������������������������������������������������?��������������������������������������� 2 | -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/README.md: -------------------------------------------------------------------------------- 1 | # Internet Connected Weather Station 2 | 3 | This project only requires three parts 4 | * [NodeMCU V1.0 (~$10)](http://s.click.aliexpress.com/e/JYrbmMVfU) 5 | * [OLED Display 128x64, I2C (~$4.50)] (http://www.banggood.com/0_96-Inch-4Pin-White-IIC-I2C-OLED-Display-Module-12864-LED-For-Arduino-p-958196.html?p=0P21061109440201501M) 6 | * [DuPont Connectors (female-female) (~$1.50)](http://www.banggood.com/40-x-10cm-Female-To-Female-Dupont-Jumper-Wires-Cable-p-89717.html?p=0P21061109440201501M) 7 | 8 | Then follow these steps: 9 | 10 | 1. Download this source code from github (either checkout with git or use the "[Download as ZIP](https://github.com/squix78/esp8266-projects/archive/master.zip)" Button 11 | 2. Download [ESPlorer](http://esp8266.ru/esplorer/) 12 | 3. Open init.lua in ESPlorer and replace SSID and PASSWORD with your Wifi credentials 13 | 4. Open weatherStation.lua in ESPlorer and adapt the city for which to receive weather information 14 | 5. Register at openweathermap.org to get an API key 15 | 6. Use "Safe to ESP" to transfer the file. Use "Safe&Compile" to compile the lua source code into an lc file to safe valuable RAM 16 | 7. Use the "Upload..." button in ESPlorer to transfer all the .MONO files to the ESP 17 | 8. Connect all the wires as depicted in the schema below 18 | 9. Reset the ESP. 19 | 20 | If everything went well you should see the current weather of your city on the tiny OLED display. 21 | 22 | Please let me know, if you see room for improvement. Also visit http://blog.squix.ch for more projects. 23 | 24 | 25 | ![The Weather Station Display](http://2.bp.blogspot.com/-W-UZsPvh-4I/VWCjUVTzxqI/AAAAAAAAATc/bmdILYERtGo/s320/display.jpg) 26 | ![Connecting the wires](https://github.com/squix78/esp8266-projects/blob/master/weather-station/simple-oled-sketch_bb.png) 27 | -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/clouds.xbm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/weather-station/clouds.xbm -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/init.lua: -------------------------------------------------------------------------------- 1 | 2 | print("Connecting to wifi...") 3 | wifi.setmode(wifi.STATION) 4 | wifi.sta.config("SSID","PWD") 5 | 6 | tmr.alarm(0, 1000, 1, function() 7 | print(".") 8 | ip = wifi.sta.getip() 9 | if ( ( ip ~= nil ) and ( ip ~= "0.0.0.0" ) )then 10 | print(ip) 11 | tmr.stop(0) 12 | sk=net.createConnection(net.TCP, 0) 13 | sk:dns("www.google.com",dnsIsWorking) 14 | sk = nil 15 | end 16 | end ) 17 | 18 | function dnsIsWorking(conn,ip) 19 | print("Google IP: "..ip) 20 | collectgarbage("collect") 21 | dofile("weatherStation.lc") 22 | end 23 | -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/simple-oled-sketch_bb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/weather-station/simple-oled-sketch_bb.png -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/src/01d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/weather-station/src/01d.png -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/src/02d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/weather-station/src/02d.png -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/src/03d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/weather-station/src/03d.png -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/src/04d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/weather-station/src/04d.png -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/src/09d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/weather-station/src/09d.png -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/src/10d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/weather-station/src/10d.png -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/src/11d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/weather-station/src/11d.png -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/src/13d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/weather-station/src/13d.png -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/src/50d.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/weather-station/src/50d.png -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/src/display.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/weather-station/src/display.jpg -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/src/simple-oled-sketch.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/esp8266-projects-master/weather-station/src/simple-oled-sketch.fzz -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/temp.XBM: -------------------------------------------------------------------------------- 1 | #define 1433066429998_width 60 2 | #define 1433066429998_height 60 3 | static char 1433066429998_bits[] = { 4 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 5 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 6 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 7 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 8 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 9 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 10 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 11 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 12 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 13 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 14 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 15 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 16 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 17 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 18 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 19 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 20 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 21 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 22 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 23 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 24 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 25 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 26 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 27 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 28 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 29 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 30 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 31 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 32 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 33 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 34 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 35 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 36 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 37 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 38 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 39 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 40 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 41 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 42 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 43 | 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0F, 44 | }; 45 | -------------------------------------------------------------------------------- /esp8266-projects-master/weather-station/weatherStation.lua: -------------------------------------------------------------------------------- 1 | -- your offset to UTC 2 | local UTC_OFFSET = 2 3 | -- Enter your city, check openweathermap.org 4 | local CITY = "Zurich,ch" 5 | -- Get an APP ID on openweathermap.org 6 | local APPID = "YOUR_APP_ID" 7 | -- Update interval in minutes 8 | local INTERVAL = 10 9 | 10 | function init_i2c_display() 11 | -- SDA and SCL can be assigned freely to available GPIOs 12 | sda = 5 -- GPIO14 13 | scl = 6 -- GPIO12 14 | sla = 0x3c 15 | i2c.setup(0, sda, scl, i2c.SLOW) 16 | disp = u8g.ssd1306_128x64_i2c(sla) 17 | end 18 | 19 | function prepare() 20 | disp:setFont(u8g.font_6x10) 21 | disp:setFontRefHeightExtendedText() 22 | disp:setDefaultForegroundColor() 23 | disp:setFontPosTop() 24 | end 25 | 26 | function updateWeather() 27 | print("Updating weather") 28 | local conn=net.createConnection(net.TCP, 0) 29 | conn:on("receive", function(conn, payload) 30 | local lastUpdate = string.sub(payload,string.find(payload,"Date: ")+23,string.find(payload,"Date: ")+31) 31 | local hour = string.sub(lastUpdate, 0, 2) + UTC_OFFSET 32 | lastUpdate = hour..string.sub(lastUpdate, 3, 9) 33 | local payload = string.match(payload, "{.*}") 34 | print(payload) 35 | 36 | weather = nil 37 | weather = cjson.decode(payload) 38 | local icon = weather.weather[1].icon 39 | icon = string.gsub(icon, "n", "d") 40 | file.open(icon..".MONO", "r") 41 | xbm_data = file.read() 42 | file.close() 43 | 44 | payload = nil 45 | conn:close() 46 | conn = nil 47 | drawWeather(xbm_data, weather, lastUpdate) 48 | end ) 49 | 50 | conn:connect(80,"95.85.58.189") 51 | conn:send("GET /data/2.5/weather?q="..CITY.."&units=metric&APPID="..APPID 52 | .." HTTP/1.1\r\n" 53 | .."Host: api.openweathermap.org\r\n" 54 | .."Connection: close\r\n" 55 | .."Accept: */*\r\n" 56 | .."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n" 57 | .."\r\n") 58 | conn = nil 59 | 60 | end 61 | init_i2c_display() 62 | prepare() 63 | updateWeather() 64 | 65 | function drawWeather(xbmData, weather, lastUpdate) 66 | disp:firstPage() 67 | repeat 68 | if weather ~= nil then 69 | disp:drawXBM( 0, 0, 60, 60, xbm_data ) 70 | disp:setScale2x2() 71 | disp:drawStr(35,5, math.floor(weather.main.temp).."C") 72 | disp:drawStr(35,15, weather.main.humidity.."%") 73 | disp:undoScale() 74 | disp:drawStr(70,52, lastUpdate) 75 | end 76 | until disp:nextPage() == false 77 | end 78 | 79 | 80 | tmr.alarm(1, INTERVAL * 60000, 1, function() 81 | ip = wifi.sta.getip() 82 | if ip=="0.0.0.0" or ip==nil then 83 | print("connecting to AP...") 84 | else 85 | print("Loading weather...") 86 | updateWeather() 87 | end 88 | end ) 89 | 90 | -------------------------------------------------------------------------------- /hello.lua: -------------------------------------------------------------------------------- 1 | print('Hello World') -------------------------------------------------------------------------------- /http.lua: -------------------------------------------------------------------------------- 1 | pin1=3 2 | pin2=4 3 | gpio.mode(pin1,gpio.OUTPUT) 4 | gpio.mode(pin2,gpio.OUTPUT) 5 | 6 | function split(s, delimiter) 7 | result = {}; 8 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 9 | table.insert(result, match); 10 | end 11 | return result; 12 | end 13 | 14 | function urlencode(payload) 15 | result = {}; 16 | list=split(payload,"\r\n") 17 | --print(list[1]) 18 | list=split(list[1]," ") 19 | --print(list[2]) 20 | list=split(list[2],"\/") 21 | 22 | table.insert(result, list[1]); 23 | table.insert(result, list[2]); 24 | table.insert(result, list[3]); 25 | 26 | return result; 27 | end 28 | 29 | function sendHeader(conn) 30 | conn:send("HTTP/1.1 200 OK\r\n") 31 | conn:send("Access-Control-Allow-Origin: *\r\n") 32 | conn:send("Content-Type: application/json; charset=utf-8\r\n") 33 | conn:send("Server:NodeMCU\r\n") 34 | conn:send("Connection: close\r\n\r\n") 35 | end 36 | 37 | srv=net.createServer(net.TCP) 38 | srv:listen(80,function(conn) 39 | conn:on("receive",function(conn,payload) 40 | list=urlencode(payload) 41 | if (list[2]=="write") then 42 | local pin = tonumber(list[3]) 43 | --print("Pin: "..pin) 44 | local status = tonumber(list[4]) 45 | --print("State: "..status) 46 | gpio.write(pin, status) 47 | -- Response Header 48 | sendHeader(conn) 49 | -- Response Content 50 | conn:send("{\"result\":\"ok\",\"digitalPin\": "..pin..", \"status\": "..gpio.read(pin).."}") 51 | elseif (list[2]=="read") then 52 | -- Response Header 53 | sendHeader(conn) 54 | -- Response Content 55 | conn:send("{\"result\":\"ok\", \"digitalPins\": [{\"digitalPin\": "..pin1..", \"status\": "..gpio.read(pin1).."},{\"digitalPin\": "..pin2..", \"status\": "..gpio.read(pin2).."}]}") 56 | else 57 | -- Response Header 58 | sendHeader(conn) 59 | -- Response Content 60 | conn:send("{\"result\":\"error\",\"message\": \"command not found\"}") 61 | end 62 | conn:close() 63 | end) 64 | end) 65 | -------------------------------------------------------------------------------- /http_bridge.lua: -------------------------------------------------------------------------------- 1 | pin1=3 2 | pin2=4 3 | gpio.mode(pin1,gpio.OUTPUT) 4 | gpio.mode(pin2,gpio.OUTPUT) 5 | 6 | function sendHeader(conn) 7 | conn:send("HTTP/1.1 200 OK\r\n") 8 | conn:send("Access-Control-Allow-Origin: *\r\n") 9 | conn:send("Content-Type: application/json; charset=utf-8\r\n") 10 | conn:send("Server:NodeMCU\r\n") 11 | conn:send("Connection: close\r\n\r\n") 12 | end 13 | 14 | srv=net.createServer(net.TCP) 15 | srv:listen(80,function(conn) 16 | conn:on("receive",function(conn,payload) 17 | --print(payload) 18 | -- "GET /digital/write/4/1 HTTP" 19 | _, _, method, path, action, pin, state = string.find(payload, "([A-Z]+) /(.+)/(.+)/([0-9]+)/([0-1]+) HTTP") 20 | -- "GET /digital/read HTTP" 21 | if (method == nil) then 22 | _, _, method, path, action = string.find(payload, "([A-Z]+) /(.+)/(.+) HTTP") 23 | end 24 | --print(method) 25 | --print(path) 26 | --print(action) 27 | --print(pin) 28 | --print(state) 29 | 30 | if (method == "GET") then 31 | sendHeader(conn) -- Response Header 32 | if (path == "digital") then 33 | if (action == "write") then 34 | local _pin = tonumber(pin) 35 | local _state = tonumber(state) 36 | gpio.write(_pin, _state) 37 | conn:send("{\"result\":\"ok\",\"digitalPin\": "..pin..", \"status\": "..gpio.read(_pin).."}") -- Response Content 38 | elseif (action == "read") then 39 | conn:send('{"result": "ok"') 40 | elseif (action == "toggle") then 41 | local _pin = tonumber(pin) 42 | local _state = gpio.read(_pin) 43 | if (_state == 0) then 44 | gpio.write(_pin, 1) 45 | else 46 | gpio.write(_pin, 0) 47 | end 48 | conn:send("{\"result\":\"ok\",\"digitalPin\": "..pin..", \"status\": "..gpio.read(_pin).."}") 49 | else 50 | conn:send('{"result": "error", "message": "action not found"}') 51 | end 52 | else 53 | conn:send('{"result": "error", "message": "path not allowed"}') 54 | end 55 | else 56 | conn:send('{"result": "error", "message": "method not allowed"}') 57 | end 58 | conn:close() 59 | end) 60 | end) 61 | -------------------------------------------------------------------------------- /http_dimmer.lua: -------------------------------------------------------------------------------- 1 | pin1=5 2 | pin2=6 3 | gpio.mode(pin1,gpio.OUTPUT) 4 | gpio.mode(pin2,gpio.OUTPUT) 5 | 6 | pwm.setup(7, 1000, 0) 7 | pwm.start(7) 8 | 9 | function sendHeader(conn) 10 | conn:send("HTTP/1.1 200 OK\r\n") 11 | conn:send("Access-Control-Allow-Origin: *\r\n") 12 | conn:send("Content-Type: application/json; charset=utf-8\r\n") 13 | conn:send("Server:NodeMCU\r\n") 14 | conn:send("Connection: close\r\n\r\n") 15 | end 16 | 17 | srv=net.createServer(net.TCP) 18 | srv:listen(80,function(conn) 19 | conn:on("receive",function(conn,payload) 20 | print(payload) 21 | -- "GET /digital/write/4/1 HTTP" 22 | _, _, method, path, action, pin, state = string.find(payload, "([A-Z]+) /(.+)/(.+)/([0-9]+)/([0-9]+) HTTP") 23 | -- "GET /digital/read HTTP" 24 | if (method == nil) then 25 | _, _, method, path, action = string.find(payload, "([A-Z]+) /(.+)/(.+) HTTP") 26 | end 27 | print(method) 28 | print(path) 29 | print(action) 30 | print(pin) 31 | print(state) 32 | 33 | if (method == "GET") then 34 | sendHeader(conn) -- Response Header 35 | if (path == "digital") then 36 | if (action == "dimmer") then 37 | local _pin = tonumber(pin) 38 | local _value = tonumber(state) 39 | if _pin == 7 then 40 | pwm.setduty(_pin, _value) 41 | conn:send('{"result": "ok"}') 42 | else 43 | conn:send('{"result": "error", "msg": "pwm index not found"') 44 | end 45 | else 46 | conn:send('{"result": "error", "message": "action not found"}') 47 | end 48 | else 49 | conn:send('{"result": "error", "message": "path not allowed"}') 50 | end 51 | else 52 | conn:send('{"result": "error", "message": "method not allowed"}') 53 | end 54 | conn:close() 55 | end) 56 | end) 57 | -------------------------------------------------------------------------------- /http_node.lua: -------------------------------------------------------------------------------- 1 | gpio.mode(4,gpio.OUTPUT) 2 | gpio.mode(5,gpio.OUTPUT) 3 | function split(s, delimiter) 4 | result = {}; 5 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 6 | table.insert(result, match); 7 | end 8 | return result; 9 | end 10 | function urldecode(payload) 11 | result = {}; 12 | list=split(payload,"\r\n") 13 | list=split(list[1]," ") 14 | list=split(list[2],"\/") 15 | table.insert(result, list[1]); 16 | table.insert(result, list[2]); 17 | table.insert(result, list[3]); 18 | return result; 19 | end 20 | 21 | function notfound(conn) 22 | conn:send('HTTP/1.1 404 Not Found\r\nConnection: close\r\nCache-Control: private, no-store\r\n\r\n\ 23 | \ 24 | ESP8266\ 25 |

Page Not Found

\ 26 | ') 27 | end 28 | 29 | function sendHeader(conn) 30 | conn:send("HTTP/1.1 200 OK\r\n") 31 | conn:send("Access-Control-Allow-Origin: *\r\n") 32 | conn:send("Content-Type: application/json; charset=utf-8\r\n") 33 | conn:send("Server:NodeMCU\r\n") 34 | conn:send("Connection: close\r\n\r\n") 35 | end 36 | 37 | srv=net.createServer(net.TCP) 38 | srv:listen(80,function(conn) 39 | conn:on("receive",function(conn,payload) 40 | print("Http Request..\r\n") 41 | --print (payload) 42 | list=urldecode(payload) 43 | if (list[2]=="write") then 44 | local pin = tonumber(list[3]) 45 | local status = tonumber(list[4]) 46 | gpio.write(pin, status) 47 | sendHeader(conn) 48 | conn:send("{\"result\":\"ok\",\"digitalPin\": "..pin..", \"status\": "..gpio.read(pin).."}") 49 | elseif (list[2]=="toggle") then 50 | local pin = tonumber(list[3]) 51 | if (gpio.read(pin) == 1) then 52 | gpio.write(pin, 0) 53 | else 54 | gpio.write(pin,1) 55 | end 56 | sendHeader(conn) 57 | conn:send("{\"result\":\"ok\",\"digitalPin\": "..pin..", \"status\": "..gpio.read(pin).."}") 58 | elseif (list[2]=="read") then 59 | local pin = tonumber(list[3]) 60 | sendHeader(conn) 61 | conn:send("{\"result\":\"ok\", \"digitalPins\": [{\"digitalPin\": 4, \"status\": "..gpio.read(4).."},{\"digitalPin\": 5, \"status\": "..gpio.read(5).."}]}") 62 | else 63 | notfound(conn) 64 | end 65 | conn:close() 66 | end) 67 | end) 68 | -------------------------------------------------------------------------------- /htu21df.lua: -------------------------------------------------------------------------------- 1 | htu21df= { 2 | dev_addr = 0x40, 3 | id = 0, 4 | init = function (self, sda, scl) 5 | self.id = 0 6 | i2c.setup(self.id, sda, scl, i2c.SLOW) 7 | end, 8 | read_reg = function(self, dev_addr, reg_addr) 9 | i2c.start(self.id) 10 | i2c.address(self.id, dev_addr ,i2c.TRANSMITTER) 11 | i2c.write(self.id,reg_addr) 12 | i2c.stop(self.id) 13 | i2c.start(self.id) 14 | i2c.address(self.id, dev_addr,i2c.RECEIVER) 15 | c=i2c.read(self.id,2) 16 | i2c.stop(self.id) 17 | return c 18 | end, 19 | 20 | 21 | readTemp = function(self) 22 | h, l = string.byte(self:read_reg(0x40, 0xE3), 1, 2) 23 | h1=bit.lshift(h,8) 24 | rawtemp = bit.band(bit.bor(h1,l),0xfffc) 25 | temp = ((rawtemp/65536)*175.72)-46.85 26 | return temp 27 | end, 28 | readHum = function(self) 29 | h,l = string.byte(self:read_reg(0x40,0xE5),1,2) 30 | h1=bit.lshift(h,8) 31 | rawhum = bit.band(bit.bor(h1,1),0xfffc) 32 | hum = ((rawhum/65536)*125.0)-6.0 33 | return hum 34 | end 35 | } -------------------------------------------------------------------------------- /init.lua: -------------------------------------------------------------------------------- 1 | dofile('main.lc') 2 | -------------------------------------------------------------------------------- /init_bak.lua: -------------------------------------------------------------------------------- 1 | ssid = 'xxxxxxxxxx' 2 | pwd = 'xxxxxxxxxx' 3 | print("set up wifi mode") 4 | wifi.setmode(wifi.STATION) 5 | wifi.sta.config(ssid, pwd) 6 | wifi.sta.connect() 7 | cnt = 0 8 | tmr.alarm(0, 1000, 1, function() 9 | if (wifi.sta.getip() == nil) and (cnt < 10) then 10 | print("IP unavaiable, Waiting...") 11 | cnt = cnt + 1 12 | else 13 | tmr.stop(0) 14 | if (cnt < 10) then 15 | print("Config done, IP is "..wifi.sta.getip()) 16 | tmr.alarm(0, 2000, 1, function() 17 | tmr.stop(0) 18 | print("WIFI connected...") 19 | end) 20 | else 21 | print("Wifi setup time more than 10s, Please verify wifi.sta.config() function. Then re-download the file.") 22 | end 23 | end 24 | end) 25 | -------------------------------------------------------------------------------- /iothink.lua: -------------------------------------------------------------------------------- 1 | local moduleName = "iothink" 2 | local M = {} 3 | _G[moduleName] = M 4 | local func = {} 5 | local var = {} 6 | local conn = nil 7 | local deviceId = 1 8 | local node_name = "Node 1" 9 | local core_Id = node.chipid() 10 | local server = "103.22.180.136" 11 | 12 | local function split(s, delimiter) 13 | result = {}; 14 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 15 | table.insert(result, match); 16 | end 17 | return result; 18 | end 19 | local function receive(conn, payload) 20 | list=split(payload,"|") 21 | if (list[1] == 'V') then 22 | local _valiable = var[list[2]] 23 | if (_valiable ~= nil) then 24 | local _result = _valiable() 25 | conn:send('{ "type": 2, "data": '.._result..' }') 26 | else 27 | conn:send('{ "type": 2, "data": { "result": "error", "msg": "Valiable not found" } }') 28 | end 29 | end 30 | if (list[1] == 'F') then 31 | local _function = func[list[2]] 32 | if (_function ~= nil) then 33 | local param = list[3] 34 | local _result = _function(param) 35 | --print(_result) 36 | conn:send('{ "type": 3, "data": '.._result..' }') 37 | else 38 | conn:send('{ "type": 3, "data": { "result": "error", "msg": "function not found" } }') 39 | end 40 | end 41 | end 42 | function M.init(id, nodeName, coreId) 43 | deviceId = id 44 | if (nodeName ~= nil) then 45 | node_name = nodeName 46 | end 47 | if (coreId ~= nil) then 48 | core_Id = coreId 49 | end 50 | end 51 | function M.connect() 52 | 53 | conn=net.createConnection(net.TCP, 0) 54 | --conn:on("reconnection", function() 55 | -- print("Disconnection..") 56 | --end); 57 | conn:on("connection", function() 58 | print("Connected..") 59 | print("You Core ID: "..core_Id.."") 60 | conn:send('{ "type": 1, "data": "'.. core_Id .. '", "name": "'..node_name..'"}') 61 | end); 62 | conn:on("receive", receive) 63 | conn:on("disconnection", function() 64 | print("Disconnected..") 65 | tmr.alarm(0, 3000, 0, function() 66 | ip = wifi.sta.getip() 67 | if ip=="0.0.0.0" or ip==nil then 68 | node.restart() 69 | else 70 | print("Reconnection..") 71 | conn = nil 72 | M.connect() 73 | end 74 | end) 75 | end); 76 | conn:connect(5683, server) 77 | end 78 | function M.alert(msg, msgType) 79 | conn:send('{ "type": '..msgType..', "deviceID": "'.. core_Id ..'", "data": '..msg..' }') 80 | end 81 | function M.addVariable(name, vr) 82 | var[name] = vr 83 | end 84 | function M.addFunction(name, fn) 85 | func[name] = fn 86 | end 87 | 88 | return M 89 | -------------------------------------------------------------------------------- /iothink.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/iothink.zip -------------------------------------------------------------------------------- /iothink_dht11.lua: -------------------------------------------------------------------------------- 1 | io = require("iothink") 2 | local humidity = 0 3 | local temperature = 0 4 | PIN = 5 5 | DHT= require("dht_lib") 6 | io.init(2, "Temperature-01") 7 | 8 | function readTemp() 9 | DHT.read11(PIN) 10 | temperature = DHT.getTemperature() 11 | humidity = DHT.getHumidity() 12 | end 13 | 14 | io.addVariable("temp", function() 15 | readTemp() 16 | print("Humidity: "..humidity.."%") 17 | print("Temperature: "..temperature.." deg C") 18 | return '{"temperature": '..temperature..', "humidity": '..humidity..' }' 19 | end) 20 | 21 | io.connect() 22 | -------------------------------------------------------------------------------- /iothink_dimmer.lua: -------------------------------------------------------------------------------- 1 | pwmPin = 7 2 | pwm.setup(pwmPin, 1000, 0) 3 | pwm.start(pwmPin) 4 | io = require("iothink") 5 | io.init(2, "Dimmer-04") 6 | io.addFunction("dimmer", function(param) 7 | _, _, pin, value = string.find(param, "([0-9]+),([0-9]+)") 8 | local _pin = tonumber(pin) 9 | local _value = tonumber(value) 10 | if pin ~= nil then 11 | print("Pin: " .. _pin) 12 | print("Value: " .. _value) 13 | pwm.setduty(_pin, _value) 14 | end 15 | return '{ "status": "ok"}' 16 | end) 17 | io.connect() -------------------------------------------------------------------------------- /iothink_ds1820.lua: -------------------------------------------------------------------------------- 1 | io = require("iothink") 2 | io.init(2, "DS18b20-03") 3 | temperature = 0 4 | t = require("ds18b20") 5 | 6 | io.addVariable("temp", function() 7 | temperature = (t.read(4)) 8 | if temperature ~= nil then 9 | print("Temperature: "..temperature.." deg C") 10 | return '{"temperature": '..temperature.. '}' 11 | end 12 | end) 13 | io.connect() 14 | -------------------------------------------------------------------------------- /iothink_relay.lua: -------------------------------------------------------------------------------- 1 | gpio.mode(5,gpio.OUTPUT) 2 | gpio.mode(6,gpio.OUTPUT) 3 | gpio.mode(7,gpio.OUTPUT) 4 | gpio.write(5, 0) 5 | gpio.write(6, 0) 6 | gpio.write(7, 0) 7 | io = require("iothink") 8 | io.init(2, "Relay-88") 9 | 10 | io.addFunction("toggle", function(param) 11 | local pin = tonumber(param) 12 | if pin ~= nil then 13 | print("Pin: " .. pin) 14 | if gpio.read(pin) == 1 then 15 | gpio.write(pin, 0) 16 | else 17 | gpio.write(pin, 1) 18 | end 19 | end 20 | return '{ "status": '..gpio.read(pin)..', "pin": '..pin..' }' 21 | end) 22 | io.connect() 23 | -------------------------------------------------------------------------------- /iothink_relay_lcd.lua: -------------------------------------------------------------------------------- 1 | gpio.mode(5,gpio.OUTPUT) 2 | gpio.mode(6,gpio.OUTPUT) 3 | gpio.mode(7,gpio.OUTPUT) 4 | 5 | gpio.write(5, 0) 6 | gpio.write(6, 0) 7 | gpio.write(7, 0) 8 | io = require("iothink") 9 | io.init(2, "Relay-01") 10 | 11 | io.addFunction("toggle", function(param) 12 | local pin = tonumber(param) 13 | print("Pin: " .. pin) 14 | if gpio.read(pin) == 1 then 15 | gpio.write(pin, 0) 16 | else 17 | gpio.write(pin, 1) 18 | end 19 | 20 | return '{ "status": '..gpio.read(pin)..', "pin": '..pin..' }' 21 | end) 22 | io.connect() 23 | -------------------------------------------------------------------------------- /iothink_rgb.lua: -------------------------------------------------------------------------------- 1 | io = require("iothink") 2 | io.init(2, "RGB-01") 3 | io.addFunction("rgb", function(param) 4 | -- param = pin,r,g,b 5 | _, _, pin, r, g, b = string.find(param, "([0-9]+),([0-9]+),([0-9]+),([0-9]+)") 6 | ws2812.writergb(tonumber(pin), string.char(tonumber(r), tonumber(g), tonumber(b)):rep(8)) 7 | return '{"result":"ok"}' 8 | end) 9 | io.connect() 10 | 11 | 12 | -------------------------------------------------------------------------------- /iothink_test.lua: -------------------------------------------------------------------------------- 1 | gpio.mode(5,gpio.OUTPUT) 2 | gpio.write(5, 0) 3 | 4 | io = require("iothink") 5 | io.init("192.168.1.36", 2) 6 | 7 | local function spl(s, delimiter) 8 | result = {}; 9 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 10 | table.insert(result, match); 11 | end 12 | return result; 13 | end 14 | 15 | io.addVariable("heap", function() 16 | return node.heap() 17 | end) 18 | io.addVariable("temp", function() 19 | temperature = "35.50" 20 | humidity = "75.50" 21 | return '{"id": 2, "temperature": '..temperature..', "humidity": '..humidity..' }' 22 | end) 23 | io.addFunction("rgb", function(param) 24 | list=spl(payload,",") 25 | local pin = tonumber(list[1]) 26 | local r = tonumber(list[2]) 27 | local g = tonumber(list[3]) 28 | local b = tonumber(list[4]) 29 | ws2812.writergb(pin, string.char(r, g, b):rep(8)) 30 | sendHeader(conn) 31 | return '{"result":"ok"}' 32 | end) 33 | io.addFunction("toggle", function(param) 34 | print("toggle") 35 | local pin = tonumber(param) 36 | if gpio.read(pin) == 1 then 37 | gpio.write(pin, 0) 38 | else 39 | gpio.write(pin, 1) 40 | end 41 | return '{ "status": '..gpio.read(pin)..', "coreID": "'.. node.chipid() ..'", "id": '..pin..' }' 42 | end) 43 | 44 | io.on("disconnection", function() print("Disconnected..") end) 45 | io.on("connection", function() print("Connected..") end) 46 | io.connect() -------------------------------------------------------------------------------- /iothink_trmp.lua: -------------------------------------------------------------------------------- 1 | io = require("iothink") 2 | local humidity = 0 3 | local temperature = 0 4 | local bitStream = {} 5 | io.init(2, "Temperature-01") 6 | 7 | function readTemp(pin) 8 | bitStream = {} 9 | for j = 1, 40, 1 do 10 | bitStream[j] = 0 11 | end 12 | bitlength = 0 13 | 14 | gpio.mode(pin, gpio.OUTPUT) 15 | gpio.write(pin, gpio.LOW) 16 | tmr.delay(20000) 17 | --Use Markus Gritsch trick to speed up read/write on GPIO 18 | gpio_read = gpio.read 19 | gpio_write = gpio.write 20 | 21 | gpio.mode(pin, gpio.INPUT) 22 | 23 | while (gpio_read(pin) == 0) do end 24 | 25 | c = 0 26 | while (gpio_read(pin) == 1 and c < 100) do c = c + 1 end 27 | 28 | while (gpio_read(pin) == 0) do end 29 | 30 | c = 0 31 | while (gpio_read(pin) == 1 and c < 100) do c = c + 1 end 32 | 33 | for j = 1, 40, 1 do 34 | while (gpio_read(pin) == 1 and bitlength < 10) do 35 | bitlength = bitlength + 1 36 | end 37 | bitStream[j] = bitlength 38 | bitlength = 0 39 | while (gpio_read(pin) == 0) do end 40 | end 41 | 42 | humidity = 0 43 | temperature = 0 44 | 45 | for i = 1, 8, 1 do 46 | if (bitStream[i + 0] > 2) then 47 | humidity = humidity + 2 ^ (8 - i) 48 | end 49 | end 50 | for i = 1, 8, 1 do 51 | if (bitStream[i + 16] > 2) then 52 | temperature = temperature + 2 ^ (8 - i) 53 | end 54 | end 55 | bitStream = {} 56 | gpio_read = nil 57 | gpio_write = nil 58 | end 59 | 60 | io.addVariable("temp", function() 61 | readTemp(4) 62 | print("Humidity: "..humidity.."%") 63 | print("Temperature: "..temperature.." deg C") 64 | return '{"temperature": '..temperature..', "humidity": '..humidity..' }' 65 | end) 66 | 67 | io.connect() 68 | -------------------------------------------------------------------------------- /iothink_weather.lua: -------------------------------------------------------------------------------- 1 | -- your offset to UTC 2 | local UTC_OFFSET = 7 3 | -- Enter your city, check openweathermap.org 4 | -- Ban Rangsit, TH 5 | local CITY = "Thanyaburi,TH" 6 | -- Get an APP ID on openweathermap.org 7 | local APPID = "4a7401363b53fcc52892b4ece69c8b36" 8 | -- Update interval in minutes 9 | local INTERVAL = 1 10 | DEVICE = "10038268" 11 | local temp = '0.0' 12 | local humidity = '0' 13 | 14 | function init_i2c_display() 15 | -- SDA and SCL can be assigned freely to available GPIOs 16 | sda = 3 -- GPIO14 17 | scl = 4 -- GPIO12 18 | sla = 0x3c 19 | i2c.setup(0, sda, scl, i2c.SLOW) 20 | disp = u8g.ssd1306_128x64_i2c(sla) 21 | end 22 | 23 | function prepare() 24 | disp:setFont(u8g.font_6x10) 25 | disp:setFontRefHeightExtendedText() 26 | disp:setDefaultForegroundColor() 27 | disp:setFontPosTop() 28 | end 29 | 30 | function updateWeather() 31 | print("Updating weather") 32 | local conn=net.createConnection(net.TCP, 0) 33 | conn:on("receive", function(conn, payload) 34 | --print(payload) 35 | local payload = string.match(payload, "{.*}") 36 | print(payload) 37 | if (payload ~= nil) then 38 | weather = nil 39 | icon = "01d" 40 | file.open(icon..".MONO", "r") 41 | xbm_data = file.read() 42 | file.close() 43 | weather = cjson.decode(payload) 44 | drawWeather(xbm_data, weather) 45 | else 46 | if (temp == '0.0') then 47 | drawWeather(nil,nil, nil) 48 | end 49 | end 50 | 51 | payload = nil 52 | conn:close() 53 | conn = nil 54 | 55 | end ) 56 | 57 | conn:connect(8080,"103.22.180.136") 58 | conn:on("connection", function() 59 | print("connection...") 60 | conn:send("GET http://iot.dyndns.org:8080/v1/devices/"..DEVICE.."/temp" 61 | .." HTTP/1.1\r\n" 62 | .."Host: iot.dyndns.org\r\n" 63 | .."Connection: close\r\n" 64 | .."Accept: */*\r\n" 65 | .."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n" 66 | .."\r\n") 67 | conn = nil 68 | end) 69 | end 70 | init_i2c_display() 71 | prepare() 72 | updateWeather() 73 | 74 | 75 | function drawWeather(xbm_data, weather) 76 | disp:firstPage() 77 | repeat 78 | if (xbm_data ~= nil) then 79 | disp:drawXBM( 2, 2, 60, 60, xbm_data ) 80 | end 81 | disp:setScale2x2() 82 | if (weather ~= nil) then 83 | temp = math.floor(weather.result.temperature) 84 | humidity = weather.result.humidity 85 | end 86 | disp:drawStr(35,5, temp.."C") 87 | disp:drawStr(35,15, humidity.."%") 88 | disp:undoScale() 89 | until disp:nextPage() == false 90 | end 91 | 92 | tmr.alarm(1, INTERVAL * 60000, 1, function() 93 | ip = wifi.sta.getip() 94 | if ip=="0.0.0.0" or ip==nil then 95 | print("connecting to AP...") 96 | else 97 | print("Loading weather...") 98 | updateWeather() 99 | end 100 | end ) 101 | -------------------------------------------------------------------------------- /magsw.lua: -------------------------------------------------------------------------------- 1 | SensorID = "1" 2 | status = "CLEAR" 3 | oldstatus = "CLEAR" 4 | server = "192.168.1.37" 5 | port = 8888 6 | 7 | function open() 8 | print("Open connection...") 9 | conn=net.createConnection(net.TCP, 0) 10 | conn:on("receive", function(conn, payload) print(payload) end) 11 | conn:connect(port, server) 12 | conn:send("{ \"Type\":\"MAGSW\",\"SensorID\":\"".. SensorID .. "\"}") 13 | end 14 | 15 | function sendalarm(SensorID,status) 16 | conn:send("{ \"Type\":\"MAGSW\",\"SensorID\":\"".. SensorID .. "\", \"Status\":\"".. status .."\"}") 17 | end 18 | 19 | function close() 20 | conn:close() 21 | end 22 | 23 | open() 24 | sensorPin = 4 25 | gpio.mode(sensorPin,gpio.INPUT,gpio.FLOAT) 26 | tmr.alarm(0, 1000, 1, function() -- Set alarm to one second 27 | if gpio.read(sensorPin)==0 then status="ALARM" else status="CLEAR" end 28 | if status ~= oldstatus then sendalarm (SensorID,status) end 29 | oldstatus = status 30 | end) 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /main.lua: -------------------------------------------------------------------------------- 1 | ssid = 'see_dum' 2 | pwd = '0863219053' 3 | print("set up wifi mode") 4 | wifi.setmode(wifi.STATION) 5 | wifi.sta.config(ssid, pwd) 6 | wifi.sta.connect() 7 | cnt = 0 8 | tmr.alarm(0, 1000, 1, function() 9 | if (wifi.sta.getip() == nil) and (cnt < 10) then 10 | print("IP unavaiable, Waiting...") 11 | cnt = cnt + 1 12 | else 13 | tmr.stop(0) 14 | if (cnt < 10) then 15 | print("Config done, IP is "..wifi.sta.getip()) 16 | tmr.alarm(0, 2000, 1, function() 17 | tmr.stop(0) 18 | print("WIFI connected...") 19 | dofile("tcp_client_temp.lc") 20 | -- dofile("sample_http3.lua") 21 | --dofile("iothink_weather.lc") 22 | end) 23 | else 24 | print("Wifi setup time more than 10s, Please verify wifi.sta.config() function. Then re-download the file.") 25 | end 26 | end 27 | end) 28 | -------------------------------------------------------------------------------- /monDHT11.lua: -------------------------------------------------------------------------------- 1 | 2 | tmr.delay(1000000) 3 | humi="-1" 4 | temp="-1" 5 | fare="-1" 6 | bimb=1 7 | PIN = 4 -- data pin, GPIO2 8 | --load DHT11 module and read sensor 9 | function ReadDHT11() 10 | dht11 = require("dht11") 11 | dht11.read(PIN) 12 | t = dht11.getTemperature() 13 | h = dht11.getHumidity() 14 | humi=(h) 15 | temp=(t) 16 | fare=((t*9/5)+32) 17 | print("Humidity: "..humi.."%") 18 | print("Temperature: "..temp.." deg C") 19 | print("Temperature: "..fare.." deg F") 20 | -- release module 21 | dht11 = nil 22 | package.loaded["dht11"]=nil 23 | end 24 | 25 | function split(s, delimiter) 26 | result = {}; 27 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 28 | table.insert(result, match); 29 | end 30 | return result; 31 | end 32 | 33 | function urldecode(payload) 34 | result = {}; 35 | list=split(payload,"\r\n") 36 | --print(list[1]) 37 | list=split(list[1]," ") 38 | --print(list[2]) 39 | list=split(list[2],"\/") 40 | 41 | table.insert(result, list[1]); 42 | table.insert(result, list[2]); 43 | table.insert(result, list[3]); 44 | 45 | return result; 46 | end 47 | 48 | 49 | tmr.alarm(1,10000, 1, function() ReadDHT11() bimb=bimb+1 if bimb==5 then bimb=0 wifi.sta.connect() print("Reconnect")end end) 50 | 51 | function index(conn) 52 | conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\ 53 | \ 54 | ESP8266\ 55 |

Hygrometer with
DHT11 sensor

\ 56 |

\ 57 |
\ 58 | % of relative humidity

\ 59 |
\ 60 | Temperature grade C
\ 61 | Temperature grade F

\ 62 | ') 63 | end 64 | 65 | function notfound(conn) 66 | conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\ 67 | \ 68 | ESP8266\ 69 |

Page Not Found

\ 70 | ') 71 | end 72 | 73 | function read_temperature(conn) 74 | conn:send('HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nContent-Type: application/json; charset=utf-8\r\nConnection: close\r\nCache-Control: private, no-store\r\n\r\n\ 75 | {\"result\":\"ok\",\"Temperature\": '..temp..', \"Humidity\": '..humi..'}') 76 | end 77 | 78 | srv=net.createServer(net.TCP) srv:listen(80,function(conn) 79 | conn:on("receive",function(conn,payload) 80 | --print(payload) -- for debugging only 81 | --generates HTML web site 82 | list=urldecode(payload) 83 | 84 | if (list[2]=="index.html") then 85 | index(conn) 86 | elseif (list[2]=="temperature") then 87 | read_temperature(conn) 88 | else 89 | notfound(conn) 90 | end 91 | end) 92 | conn:on("sent",function(conn) conn:close() end) 93 | end) 94 | -------------------------------------------------------------------------------- /mqtt_client.lua: -------------------------------------------------------------------------------- 1 | dofile('ssd1306.lc') 2 | STATUS = 6 3 | gpio.mode(STATUS, gpio.OUTPUT) 4 | gpio.write(STATUS, 0) 5 | -- init mqtt client with keepalive timer 120sec 6 | clientId=node.chipid() 7 | m = mqtt.Client(clientId, 120, "user", "password") 8 | -- setup Last Will and Testament (optional) 9 | -- Broker will publish a message with qos = 0, retain = 0, data = "offline" 10 | -- to topic "/lwt" if client don't send keepalive packet 11 | m:lwt("/lwt", "offline", 0, 0) 12 | m:on("connect", function(con) print ("connected") end) 13 | m:on("offline", 14 | function(con) 15 | print ("offline") 16 | --node.restart() 17 | tmr.alarm(0, 2000, 1, function() 18 | print ('Reconnect...') 19 | connect() 20 | end) 21 | end 22 | ) 23 | function drawWeather(xbm_data, weather) 24 | disp:firstPage() 25 | repeat 26 | if (xbm_data ~= nil) then 27 | disp:drawXBM( 0, 0, 60, 60, xbm_data ) 28 | end 29 | disp:setScale2x2() 30 | disp:drawStr(35,5, weather.Temperature.."C") 31 | disp:drawStr(35,15, weather.Humidity.."%") 32 | disp:undoScale() 33 | until disp:nextPage() == false 34 | end 35 | -- on publish message receive event 36 | m:on("message", function(conn, topic, data) 37 | print(topic .. ":" ) 38 | if data ~= nil then 39 | weather = cjson.decode(data) 40 | file.open("temp.MONO", "r") 41 | xbm_data = file.read() 42 | file.close() 43 | drawWeather(xbm_data, weather) 44 | xbm_data = nil 45 | print("Temperature: "..weather.Temperature..", Humidity: "..weather.Humidity) 46 | end 47 | end) 48 | function connect() 49 | -- iot.eclipse.org 50 | -- broker.mqttdashboard.com 51 | -- subscribe topic with qos = 0 52 | m:connect("iot.eclipse.org", 1883, 0, 53 | function(conn) 54 | gpio.write(STATUS, 1) 55 | print("connected") 56 | m:subscribe("/10038268/temperature",0, function(conn) print("subscribe success") end) 57 | end 58 | ) 59 | end 60 | init_i2c_display() 61 | prepare() 62 | connect() 63 | -------------------------------------------------------------------------------- /mqtt_dht11.lua: -------------------------------------------------------------------------------- 1 | PIN=0 2 | STATUS = 7 3 | humi=0 4 | temp=0 5 | DHT= require("dht_lib") 6 | gpio.mode(STATUS, gpio.OUTPUT) 7 | gpio.write(STATUS, 0) 8 | -- init mqtt client with keepalive timer 120sec 9 | clientId=node.chipid() 10 | m = mqtt.Client(clientId, 120, "user", "password") 11 | -- setup Last Will and Testament (optional) 12 | -- Broker will publish a message with qos = 0, retain = 0, data = "offline" 13 | -- to topic "/lwt" if client don't send keepalive packet 14 | m:lwt("/lwt", "offline", 0, 0) 15 | m:on("connect", function(con) print ("connected") end) 16 | m:on("offline", 17 | function(con) 18 | print ("offline") 19 | node.restart() 20 | end 21 | ) 22 | -- on publish message receive event 23 | m:on("message", function(conn, topic, data) 24 | print(topic .. ":" ) 25 | if data ~= nil then 26 | print('Message: '..data) 27 | print('*****************************') 28 | end 29 | end) 30 | function connect() 31 | -- iot.eclipse.org 32 | -- broker.mqttdashboard.com 33 | -- subscribe topic with qos = 0 34 | m:connect("iot.eclipse.org", 1883, 0, 35 | function(conn) 36 | gpio.write(STATUS, 1) 37 | print("connected") 38 | m:subscribe("/"..clientId.."/temperature",0, function(conn) print("subscribe success") end) 39 | publish() 40 | start() 41 | end 42 | ) 43 | end 44 | function ReadDHT11() 45 | DHT.read11(PIN) 46 | temp = DHT.getTemperature() 47 | humi = DHT.getHumidity() 48 | print("Temperature: "..temp.." deg C, Humidity: "..humi.."%") 49 | end 50 | function publish() 51 | ReadDHT11() 52 | msg = '{"Id": 2, "Temperature": '..temp..', "Humidity": '..humi..'}' 53 | m:publish("/"..clientId.."/temperature",msg,0,0, function(conn) print("sent") end) 54 | end 55 | function start() 56 | tmr.alarm(1, 20000, 1, function() 57 | if pcall(publish) then 58 | print("Temp sent OK") 59 | else 60 | print("Temp sent err" ) 61 | end 62 | end) 63 | end 64 | 65 | connect() 66 | -------------------------------------------------------------------------------- /mqtt_ds1820.lua: -------------------------------------------------------------------------------- 1 | DS_PIN = 4 2 | ID = 5 3 | temperature = 0 4 | t = require("ds18b20") 5 | 6 | -- init mqtt client with keepalive timer 120sec 7 | clientId=node.chipid() 8 | m = mqtt.Client(clientId, 120, "user", "password") 9 | -- setup Last Will and Testament (optional) 10 | -- Broker will publish a message with qos = 0, retain = 0, data = "offline" 11 | -- to topic "/lwt" if client don't send keepalive packet 12 | m:lwt("/lwt", "offline", 0, 0) 13 | m:on("connect", function(con) print ("connected") end) 14 | m:on("offline", 15 | function(con) 16 | print ("offline") 17 | node.restart() 18 | end 19 | ) 20 | -- on publish message receive event 21 | m:on("message", function(conn, topic, data) 22 | print(topic .. ":" ) 23 | if data ~= nil then 24 | print(data) 25 | end 26 | end) 27 | function publish() 28 | temperature = (t.read(DS_PIN)) 29 | msg = '{"Id": '..ID..', "Temperature": '..temperature..'}' 30 | print (msg) 31 | m:publish("/"..clientId.."/temperature",msg,0,0, function(conn) print("sent") end) 32 | end 33 | function start() 34 | tmr.alarm(1, 20000, 1, function() 35 | if pcall(publish) then 36 | print("Temp sent OK") 37 | else 38 | print("Temp sent err" ) 39 | end 40 | end) 41 | end 42 | -- iot.eclipse.org 43 | -- broker.mqttdashboard.com 44 | -- subscribe topic with qos = 0 45 | m:connect("192.168.1.36", 1883, 0, 46 | function(conn) 47 | publish() 48 | start() 49 | end 50 | ) 51 | -------------------------------------------------------------------------------- /mqtt_soil.lua: -------------------------------------------------------------------------------- 1 | SOIL1 = 5 2 | SOIL2 = 6 3 | gpio.mode(SOIL1,gpio.INPUT,gpio.FLOAT) 4 | gpio.mode(SOIL2,gpio.INPUT,gpio.FLOAT) 5 | ID = 1 6 | status_soil2 = "HIGH" 7 | oldstatus_soil2 = "HIGH" 8 | status_soil3 = "HIGH" 9 | oldstatus_soil3 = "HIGH" 10 | -- init mqtt client with keepalive timer 120sec 11 | clientId=node.chipid() 12 | m = mqtt.Client(clientId, 120, "user", "password") 13 | -- setup Last Will and Testament (optional) 14 | -- Broker will publish a message with qos = 0, retain = 0, data = "offline" 15 | -- to topic "/lwt" if client don't send keepalive packet 16 | m:lwt("/lwt", "offline", 0, 0) 17 | m:on("connect", function(con) print ("connected") end) 18 | m:on("offline", 19 | function(con) 20 | print ("offline") 21 | node.restart() 22 | end 23 | ) 24 | -- on publish message receive event 25 | m:on("message", function(conn, topic, data) 26 | print(topic .. ":" ) 27 | if data ~= nil then 28 | print(data) 29 | end 30 | end) 31 | function connect() 32 | -- iot.eclipse.org 33 | -- broker.mqttdashboard.com 34 | -- subscribe topic with qos = 0 35 | m:connect("192.168.1.36", 1883, 0, 36 | function(conn) 37 | print("connected") 38 | publish() 39 | start() 40 | end 41 | ) 42 | end 43 | 44 | function publish() 45 | if gpio.read(SOIL1)==1 then status_soil2="LOW" else status_soil2="HIGH" end 46 | --if status_soil2 ~= oldstatus_soil2 then sendalarm (1, status_soil2) end 47 | oldstatus_soil2 = status_soil2 48 | 49 | if gpio.read(SOIL2)==1 then status_soil3="LOW" else status_soil3="HIGH" end 50 | --if status_soil3 ~= oldstatus_soil3 then sendalarm (2, status_soil3) end 51 | oldstatus_soil3 = status_soil3 52 | 53 | msg = '{ "Id": '..ID..', "soil1": "'..status_soil2..'", "soil2": "'..status_soil3..'" }' 54 | print (msg) 55 | m:publish("/"..clientId.."/soil",msg,0,0, function(conn) print("sent") end) 56 | end 57 | function start() 58 | tmr.alarm(1, 20000, 1, function() 59 | if pcall(publish) then 60 | print("Send OK") 61 | else 62 | print("Send err" ) 63 | end 64 | end) 65 | end 66 | 67 | 68 | connect() 69 | -------------------------------------------------------------------------------- /mqtt_test.lua: -------------------------------------------------------------------------------- 1 | STATUS = 5 2 | gpio.mode(STATUS, gpio.OUTPUT) 3 | gpio.write(STATUS, 0) 4 | -- init mqtt client with keepalive timer 120sec 5 | clientId=node.chipid() 6 | m = mqtt.Client(clientId, 120, "user", "password") 7 | -- setup Last Will and Testament (optional) 8 | -- Broker will publish a message with qos = 0, retain = 0, data = "offline" 9 | -- to topic "/lwt" if client don't send keepalive packet 10 | m:lwt("/lwt", "offline", 0, 0) 11 | m:on("connect", function(con) print ("connected") end) 12 | m:on("offline", 13 | function(con) 14 | print ("offline") 15 | --node.restart() 16 | tmr.alarm(0, 2000, 1, function() 17 | print ('Reconnect...') 18 | connect() 19 | end) 20 | end 21 | ) 22 | 23 | -- on publish message receive event 24 | m:on("message", function(conn, topic, data) 25 | print(topic .. ":" ) 26 | weather = cjson.decode(data) 27 | print("Temperature: "..weather.Temperature..", Humidity: "..weather.Humidity) 28 | print("**************************************") 29 | end) 30 | function connect() 31 | -- iot.eclipse.org 32 | -- broker.mqttdashboard.com 33 | -- subscribe topic with qos = 0 34 | m:connect("192.168.43.94", 1883, 0, 35 | function(conn) 36 | gpio.write(STATUS, 1) 37 | print("connected") 38 | m:subscribe("/10040058/temperature",0, function(conn) print("subscribe success") end) 39 | end 40 | ) 41 | end 42 | connect() 43 | -------------------------------------------------------------------------------- /mymodule.lua: -------------------------------------------------------------------------------- 1 | local moduleName = 'mymodule' 2 | local M = {} 3 | _G[moduleName] = M 4 | 5 | local function doPrint(v) 6 | print(v) 7 | end 8 | 9 | function M.MyPrint(v) 10 | doPrint(v) 11 | end 12 | 13 | return M -------------------------------------------------------------------------------- /node_ds1820.lua: -------------------------------------------------------------------------------- 1 | server = "192.168.1.48" 2 | port = 5683 3 | node_name = "Temperature-03" 4 | local nonn = nil 5 | DS_PIN = 4 6 | ID = 3 7 | temperature = 0 8 | t = require("ds18b20") 9 | t.setup(DS_PIN) 10 | 11 | function split(s, delimiter) 12 | result = {}; 13 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 14 | table.insert(result, match); 15 | end 16 | return result; 17 | end 18 | 19 | function open() 20 | print("Open connection...") 21 | conn=net.createConnection(net.TCP, 0) 22 | conn:on("receive", receive) 23 | conn:on("disconnection", function() 24 | print("Disconnection..") 25 | tmr.stop(0) 26 | conn= nil 27 | if (wifi.sta.status() == 5) then 28 | tmr.alarm(0, 5000, 1, function() 29 | tmr.stop(0) 30 | open() 31 | end) 32 | end 33 | end); 34 | conn:on("reconnection", function() print("Disconnection..") end); 35 | conn:on("connection", function() 36 | print("Connected..") 37 | conn:send('{ "type": 1, "data": "'.. node.chipid() .. '", "name": "'..node_name..'"}') 38 | end); 39 | conn:connect(port, server) 40 | end 41 | 42 | --load DHT11 module and read sensor 43 | function ReadDS1820(pin) 44 | addrs = t.addrs() 45 | if (addrs ~= nil) then 46 | print("Total DS18B20 sensors: "..table.getn(addrs)) 47 | end 48 | temperature = (t.read()) 49 | -- Just read temperature 50 | print("Temperature: "..temperature.."'C") 51 | end 52 | 53 | function receive(conn, payload) 54 | list=split(payload,"|") 55 | if (list[1] == 'V') then 56 | if (list[2] == 'heap') then 57 | conn:send('{ "type": 2, "data":"'.. node.heap() .. '"}') 58 | end 59 | if (list[2] == 'temperature') then 60 | ReadDS1820(DS_PIN) 61 | conn:send('{ "type": 2, "data":{ "coreID": '.. node.chipid() ..', "id": '..ID..', "temperature": '..temperature..' }}') 62 | end 63 | end 64 | end 65 | 66 | function close() 67 | conn:close() 68 | end 69 | 70 | open() 71 | 72 | -------------------------------------------------------------------------------- /node_light: -------------------------------------------------------------------------------- 1 | server = "192.168.1.48" 2 | port = 5683 3 | node_name = "Light-01" 4 | conn=net.createConnection(net.TCP, 0) 5 | gpio.mode(4,gpio.INPUT,gpio.FLOAT) 6 | soil = "HIGH" 7 | function split(s, delimiter) 8 | result = {}; 9 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 10 | table.insert(result, match); 11 | end 12 | return result; 13 | end 14 | 15 | function open() 16 | print("Open connection...") 17 | conn:on("receive", receive) 18 | conn:on("disconnection", function() 19 | print("Disconnection..") 20 | end); 21 | conn:on("reconnection", function() print("Disconnection..") end); 22 | conn:on("connection", function() print("Connected..") end); 23 | conn:connect(port, server) 24 | conn:send('{ "type": 1, "data": "'.. node.chipid() .. '", "name": "'..node_name..'"}') 25 | end 26 | 27 | function receive(conn, payload) 28 | list=split(payload,"|") 29 | if (list[1] == 'V') then 30 | if (list[2] == 'heap') then 31 | conn:send('{ "type": 2, "data":"'.. node.heap() .. '"}') 32 | end 33 | 34 | if (list[2] == 'light1') then 35 | soil = "CLEAR" 36 | if gpio.read(4)==1 then 37 | soil = "ALARM" 38 | end 39 | conn:send('{ "type": 5, "data": { "deviceID": "'..node.chipid()..'", "id": 1, "value": "'..soil..'"}}') 40 | end 41 | end 42 | end 43 | 44 | light_status = "CLEAR" 45 | light_oldstatus = "CLEAR" 46 | sensorPin = 4 47 | function sendalarm(id, status) 48 | conn:send('{ "type": 5, "deviceID": "'.. node.chipid() ..'", "data": {"msg": "'..node_name..' status change", "id": '..id..', "value": "'..status..'"}}') 49 | end 50 | 51 | tmr.alarm(0, 3000, 1, function() -- Set alarm to one second 52 | if gpio.read(sensorPin)==0 then light_status="ALARM" else light_status="CLEAR" end 53 | if light_status ~= light_oldstatus then sendalarm(1, light_status) end 54 | light_oldstatus = light_status 55 | end) 56 | 57 | function close() 58 | conn:close() 59 | end 60 | 61 | open() 62 | 63 | -------------------------------------------------------------------------------- /node_light.lua: -------------------------------------------------------------------------------- 1 | server = "192.168.1.48" 2 | port = 5683 3 | node_name = "Light-01" 4 | sensorPin = 4 5 | gpio.mode(sensorPin, gpio.INPUT,gpio.FLOAT) 6 | soil = "HIGH" 7 | local nonn = nil 8 | 9 | function split(s, delimiter) 10 | result = {}; 11 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 12 | table.insert(result, match); 13 | end 14 | return result; 15 | end 16 | 17 | function open() 18 | conn=net.createConnection(net.TCP, 0) 19 | print("Open connection...") 20 | conn:on("receive", receive) 21 | conn:on("disconnection", function() 22 | print("Disconnection..") 23 | tmr.stop(0) 24 | conn= nil 25 | if (wifi.sta.status() == 5) then 26 | tmr.alarm(0, 5000, 1, function() 27 | open() 28 | tmr.stop(0) 29 | end) 30 | end 31 | end); 32 | conn:on("reconnection", function() print("Disconnection..") end); 33 | conn:on("connection", function() 34 | print("Connected..") 35 | start() 36 | conn:send('{ "type": 1, "data": "'.. node.chipid() .. '", "name": "'..node_name..'"}') 37 | end); 38 | conn:connect(port, server) 39 | end 40 | 41 | function receive(conn, payload) 42 | list=split(payload,"|") 43 | if (list[1] == 'V') then 44 | if (list[2] == 'heap') then 45 | conn:send('{ "type": 2, "data":"'.. node.heap() .. '"}') 46 | end 47 | 48 | if (list[2] == 'security1') then 49 | soil = "CLEAR" 50 | if gpio.read(4)==0 then 51 | soil = "ALARM" 52 | end 53 | conn:send('{ "type": 2, "data": { "deviceID": "'..node.chipid()..'", "id": 1, "value": "'..soil..'"}}') 54 | end 55 | end 56 | end 57 | 58 | light_status = "CLEAR" 59 | light_oldstatus = "CLEAR" 60 | 61 | function sendalarm(id, status) 62 | print(status) 63 | conn:send('{ "type": 5, "deviceID": "'.. node.chipid() ..'", "data": {"msg": "'..node_name..' status change", "id": '..id..', "value": "'..status..'"}}') 64 | end 65 | 66 | function start() 67 | tmr.alarm(0, 1000, 1, function() -- Set alarm to one second 68 | if gpio.read(sensorPin)==0 then light_status="ALARM" else light_status="CLEAR" end 69 | if light_status ~= light_oldstatus then sendalarm(1, light_status) end 70 | light_oldstatus = light_status 71 | end) 72 | end 73 | 74 | function close() 75 | conn:close() 76 | end 77 | 78 | tmr.alarm(0, 2000, 1, function() 79 | open() 80 | tmr.stop(0) 81 | end) 82 | 83 | -------------------------------------------------------------------------------- /node_ralay.lua: -------------------------------------------------------------------------------- 1 | 2 | gpio.mode(5,gpio.OUTPUT) 3 | gpio.mode(6,gpio.OUTPUT) 4 | gpio.mode(7,gpio.OUTPUT) 5 | 6 | gpio.write(5, 0) 7 | gpio.write(6, 0) 8 | gpio.write(7, 0) 9 | 10 | server = "192.168.43.94" 11 | port = 5683 12 | node_name = "Lighting-01" 13 | conn=net.createConnection(net.TCP, 0) 14 | 15 | function split(s, delimiter) 16 | result = {}; 17 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 18 | table.insert(result, match); 19 | end 20 | return result; 21 | end 22 | 23 | function open() 24 | print("Open connection...") 25 | conn:on("receive", receive) 26 | conn:on("disconnection", function() print("Disconnection..") end) 27 | conn:on("reconnection", function() print("Reconnection..") end) 28 | conn:on("connection", function() print("Connected..") end) 29 | conn:connect(port, server) 30 | conn:send('{ "type": 1, "data": "'.. node.chipid() .. '", "name": "'..node_name..'"}') 31 | end 32 | 33 | function receive(conn, payload) 34 | --print(payload) 35 | list=split(payload,"|") 36 | if (list[1] == 'V') then 37 | if (list[2] == 'heap') then 38 | conn:send('{ "type": 2, "data":"'.. node.heap() .. '"}') 39 | end 40 | end 41 | if (list[1] == 'P') then 42 | if (list[2] == 'heap') then 43 | conn:send('{ "type": 6, "data": "ok"}') 44 | end 45 | end 46 | if (list[1] == 'F') then 47 | if (list[2] == 'toggle') then 48 | toggle(list[3]) 49 | end 50 | end 51 | end 52 | 53 | function close() 54 | conn:close() 55 | end 56 | 57 | function toggle(param) 58 | --print(param) 59 | local pin = tonumber(param) 60 | print("Pin: " .. pin) 61 | if gpio.read(pin) == 1 then 62 | gpio.write(pin, 0) 63 | else 64 | gpio.write(pin, 1) 65 | end 66 | conn:send('{"type": 3, "data": { "status": '..gpio.read(pin)..', "coreID": "'.. node.chipid() ..'", "id": '..pin..' }}') 67 | end 68 | 69 | 70 | open() 71 | 72 | -------------------------------------------------------------------------------- /node_secur.lua: -------------------------------------------------------------------------------- 1 | server = "192.168.1.48" 2 | port = 5683 3 | node_name = "Door-Switch-01" 4 | local nonn = nil 5 | ID = 2 6 | conn=net.createConnection(net.TCP, 0) 7 | soil = "CLEAR" 8 | function split(s, delimiter) 9 | result = {}; 10 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 11 | table.insert(result, match); 12 | end 13 | return result; 14 | end 15 | 16 | function open() 17 | print("Open connection...") 18 | conn=net.createConnection(net.TCP, 0) 19 | conn:on("receive", receive) 20 | conn:on("disconnection", function() 21 | print("Disconnection..") 22 | tmr.stop(0) 23 | conn= nil 24 | if (wifi.sta.status() == 5) then 25 | tmr.alarm(0, 5000, 1, function() 26 | tmr.stop(0) 27 | open() 28 | end) 29 | end 30 | end); 31 | conn:on("reconnection", function() print("Disconnection..") end); 32 | conn:on("connection", function() 33 | print("Connected..") 34 | start() 35 | conn:send('{ "type": 1, "data": "'.. node.chipid() .. '", "name": "'..node_name..'"}') 36 | end); 37 | conn:connect(port, server) 38 | end 39 | 40 | function receive(conn, payload) 41 | list=split(payload,"|") 42 | if (list[1] == 'V') then 43 | if (list[2] == 'heap') then 44 | conn:send('{ "type": 2, "data":"'.. node.heap() .. '"}') 45 | end 46 | 47 | if (list[2] == 'security1') then 48 | soil = "CLEAR" 49 | if gpio.read(5)==0 then 50 | soil = "ALARM" 51 | end 52 | conn:send('{ "type": 2, "data": { "deviceID": "'..node.chipid()..'", "id": '..ID..', "value": "'..soil..'"}}') 53 | end 54 | end 55 | end 56 | 57 | gpio.mode(5,gpio.INPUT,gpio.FLOAT) 58 | status_soil2 = "ALARM" 59 | oldstatus_soil2 = "ALARM" 60 | function sendalarm(id, status) 61 | print("ID: "..id..", "..status) 62 | conn:send('{ "type": 5, "deviceID": "'.. node.chipid() ..'", "data": {"msg": "'..node_name..' status change", "id": '..id..', "value": "'..status..'"}}') 63 | end 64 | 65 | function start() 66 | tmr.alarm(0, 2000, 1, function() -- Set alarm to one second 67 | if gpio.read(5)==1 then status_soil2="CLEAR" else status_soil2="ALARM" end 68 | if status_soil2 ~= oldstatus_soil2 then sendalarm(ID, status_soil2) end 69 | oldstatus_soil2 = status_soil2 70 | end) 71 | end 72 | 73 | tmr.alarm(0, 2000, 1, function() 74 | tmr.stop(0) 75 | open() 76 | end) 77 | 78 | -------------------------------------------------------------------------------- /node_sensor.lua: -------------------------------------------------------------------------------- 1 | print("\r\n\r\nDevice ID: "..node.chipid()) 2 | server = "192.168.1.48" 3 | port = 5683 4 | node_name = "Test" 5 | conn=net.createConnection(net.TCP, 0) 6 | 7 | function split(s, delimiter) 8 | result = {}; 9 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 10 | table.insert(result, match); 11 | end 12 | return result; 13 | end 14 | 15 | function open() 16 | print("Open connection...") 17 | conn:on("receive", receive) 18 | conn:on("disconnection", function() print("Disconnection..") end) 19 | conn:on("reconnection", function() print("Reconnection..") end) 20 | conn:on("connection", function() print("Connected..") end) 21 | conn:connect(port, server) 22 | conn:send('{ "type": 1, "data": "'.. node.chipid() .. '", "name": "'..node_name..'"}') 23 | end 24 | 25 | function receive(conn, payload) 26 | list=split(payload,"|") 27 | if (list[1] == 'V') then 28 | if (list[2] == 'heap') then 29 | conn:send('{ "type": 2, "data":"'.. node.heap() .. '"}') 30 | end 31 | end 32 | if (list[1] == 'F') then 33 | if (list[2] == 'led') then 34 | led(list[3]) 35 | end 36 | if (list[2] == 'toggle') then 37 | toggle(list[3]) 38 | end 39 | if (list[2] == 'status') then 40 | status(list[3]) 41 | end 42 | end 43 | end 44 | 45 | function close() 46 | conn:close() 47 | end 48 | 49 | function led(param) 50 | print(param) 51 | list=split(param,",") 52 | local pin = tonumber(list[1]) 53 | local state = tonumber(list[2]) 54 | print("Pin: " .. pin) 55 | print("State: " .. state) 56 | gpio.write(pin, state) 57 | conn:send('{"type": 3, "data": '.. gpio.read(pin) ..'}') 58 | end 59 | 60 | function toggle(param) 61 | print(param) 62 | list=split(param,",") 63 | local pin = tonumber(list[1]) 64 | print("Pin: " .. pin) 65 | if gpio.read(pin) == 1 then 66 | gpio.write(pin, 0) 67 | else 68 | gpio.write(pin, 1) 69 | end 70 | conn:send('{"type": 3, "data": '.. gpio.read(pin) ..'}') 71 | end 72 | 73 | function status(param) 74 | local pin = tonumber(param) 75 | conn:send('{"type": 3, "data": '.. gpio.read(pin) ..'}') 76 | end 77 | 78 | open() 79 | 80 | -------------------------------------------------------------------------------- /node_soil.lua: -------------------------------------------------------------------------------- 1 | server = "103.22.180.136" 2 | port = 5683 3 | node_name = "Soil-01" 4 | conn=nil 5 | gpio.mode(5,gpio.INPUT,gpio.FLOAT) 6 | gpio.mode(6,gpio.INPUT,gpio.FLOAT) 7 | soil = "HIGH" 8 | function split(s, delimiter) 9 | result = {}; 10 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 11 | table.insert(result, match); 12 | end 13 | return result; 14 | end 15 | function open() 16 | print("Open connection...") 17 | conn=net.createConnection(net.TCP, 0) 18 | conn:on("receive", receive) 19 | conn:on("disconnection", function() 20 | print("Disconnection..") 21 | tmr.stop(0) 22 | conn= nil 23 | if (wifi.sta.status() == 5) then 24 | tmr.alarm(0, 5000, 1, function() 25 | open() 26 | tmr.stop(0) 27 | end) 28 | end 29 | end); 30 | conn:on("reconnection", function() print("Disconnection..") end); 31 | conn:on("connection", function() 32 | print("Connected..") 33 | start() 34 | end); 35 | conn:connect(port, server) 36 | conn:send('{ "type": 1, "data": "'.. node.chipid() .. '", "name": "'..node_name..'"}') 37 | end 38 | function receive(conn, payload) 39 | list=split(payload,"|") 40 | if (list[1] == 'V') then 41 | if (list[2] == 'heap') then 42 | conn:send('{ "type": 2, "data":"'.. node.heap() .. '"}') 43 | end 44 | if (list[2] == 'soil1') then 45 | soil = "HIGH" 46 | if gpio.read(5)==1 then 47 | soil = "LOW" 48 | end 49 | conn:send('{ "type": 2, "data": { "deviceID": "'..node.chipid()..'", "id": 1, "value": "'..soil..'"}}') 50 | end 51 | if (list[2] == 'soil2') then 52 | soil = "HIGH" 53 | if gpio.read(6)==1 then 54 | soil = "LOW" 55 | end 56 | conn:send('{ "type": 2, "data": { "deviceID": "'..node.chipid()..'", "id": 2, "value": "'..soil..'"}}') 57 | end 58 | end 59 | end 60 | status_soil2 = "HIGH" 61 | oldstatus_soil2 = "HIGH" 62 | status_soil3 = "HIGH" 63 | oldstatus_soil3 = "HIGH" 64 | function sendalarm(id, status) 65 | conn:send('{ "type": 4, "deviceID": "'.. node.chipid() ..'", "data": {"msg": "'..node_name..' status change", "id": '..id..', "value": "'..status..'"}}') 66 | end 67 | function start() 68 | tmr.alarm(0, 3000, 1, function() -- Set alarm to one second 69 | if gpio.read(5)==1 then status_soil2="LOW" else status_soil2="HIGH" end 70 | if status_soil2 ~= oldstatus_soil2 then sendalarm (1, status_soil2) end 71 | oldstatus_soil2 = status_soil2 72 | 73 | if gpio.read(6)==1 then status_soil3="LOW" else status_soil3="HIGH" end 74 | if status_soil3 ~= oldstatus_soil3 then sendalarm (2, status_soil3) end 75 | oldstatus_soil3 = status_soil3 76 | end) 77 | end 78 | function close() 79 | conn:close() 80 | end 81 | tmr.alarm(0, 2000, 1, function() 82 | open() 83 | tmr.stop(0) 84 | end) 85 | 86 | open() 87 | 88 | -------------------------------------------------------------------------------- /node_temperature.lua: -------------------------------------------------------------------------------- 1 | server = "192.168.1.48" 2 | port = 5683 3 | node_name = "Node-03" 4 | conn=net.createConnection(net.TCP, 0) 5 | DHT1_PIN = 4 6 | humidity = 0 7 | temperature = 0 8 | dht11 = require("dht11") 9 | 10 | gpio.mode(5,gpio.INPUT,gpio.FLOAT) 11 | gpio.mode(6,gpio.INPUT,gpio.FLOAT) 12 | function split(s, delimiter) 13 | result = {}; 14 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 15 | table.insert(result, match); 16 | end 17 | return result; 18 | end 19 | 20 | function open() 21 | print("Open connection...") 22 | conn:on("receive", receive) 23 | conn:on("disconnection", function() 24 | print("Disconnection..") 25 | end); 26 | conn:on("reconnection", function() print("Disconnection..") end); 27 | conn:on("connection", function() print("Connected..") end); 28 | conn:connect(port, server) 29 | conn:send('{ "type": 1, "data": "'.. node.chipid() .. '", "name": "'..node_name..'"}') 30 | end 31 | 32 | --load DHT11 module and read sensor 33 | function ReadDHT11(pin) 34 | 35 | dht11.init(pin) 36 | t = dht11.getTemp() 37 | h = dht11.getHumidity() 38 | humidity=(h) 39 | temperature=(t) 40 | print("Humidity: "..humidity.."%") 41 | print("Temperature: "..temperature.." deg C") 42 | end 43 | 44 | function receive(conn, payload) 45 | list=split(payload,"|") 46 | if (list[1] == 'V') then 47 | if (list[2] == 'heap') then 48 | conn:send('{ "type": 2, "data":"'.. node.heap() .. '"}') 49 | end 50 | if (list[2] == 'temperature1') then 51 | ReadDHT11(4) 52 | conn:send('{ "type": 2, "data":{"id": 2, "temperature": '..temperature..', "humidity": '..humidity..' }}') 53 | end 54 | end 55 | end 56 | 57 | function close() 58 | conn:close() 59 | end 60 | 61 | 62 | open() 63 | -------------------------------------------------------------------------------- /oled.lua: -------------------------------------------------------------------------------- 1 | local moduleName = "oled" 2 | local M = {} 3 | _G[moduleName] = M 4 | 5 | oled_id = 0 6 | oled_gpio = {[0]=3,[2]=4,[4]=2,[5]=1,[12]=6,[13]=7,[14]=5} 7 | oled_sda = oled_gpio[0] 8 | oled_scl = oled_gpio[2] 9 | oled_addr = 0x3C 10 | 11 | ascii = {} 12 | 13 | ascii[0] = {} 14 | ascii[0][0] = 0x3e 15 | ascii[0][1] = 0x51 16 | ascii[0][2] = 0x49 17 | ascii[0][3] = 0x45 18 | ascii[0][4] = 0x3e 19 | 20 | ascii[1] = {} 21 | ascii[1][0] = 0x00 22 | ascii[1][1] = 0x42 23 | ascii[1][2] = 0x7f 24 | ascii[1][3] = 0x40 25 | ascii[1][4] = 0x00 26 | 27 | ascii[2] = {} 28 | ascii[2][0] = 0x42 29 | ascii[2][1] = 0x61 30 | ascii[2][2] = 0x51 31 | ascii[2][3] = 0x49 32 | ascii[2][4] = 0x46 33 | 34 | ascii[3] = {} 35 | ascii[3][0] = 0x21 36 | ascii[3][1] = 0x41 37 | ascii[3][2] = 0x45 38 | ascii[3][3] = 0x4b 39 | ascii[3][4] = 0x31 40 | 41 | ascii[4] = {} 42 | ascii[4][0] = 0x18 43 | ascii[4][1] = 0x14 44 | ascii[4][2] = 0x12 45 | ascii[4][3] = 0x7f 46 | ascii[4][4] = 0x10 47 | 48 | ascii[5] = {} 49 | ascii[5][0] = 0x27 50 | ascii[5][1] = 0x45 51 | ascii[5][2] = 0x45 52 | ascii[5][3] = 0x45 53 | ascii[5][4] = 0x39 54 | 55 | ascii[6] = {} 56 | ascii[6][0] = 0x3c 57 | ascii[6][1] = 0x4a 58 | ascii[6][2] = 0x49 59 | ascii[6][3] = 0x49 60 | ascii[6][4] = 0x30 61 | 62 | ascii[7] = {} 63 | ascii[7][0] = 0x01 64 | ascii[7][1] = 0x71 65 | ascii[7][2] = 0x09 66 | ascii[7][3] = 0x05 67 | ascii[7][4] = 0x03 68 | 69 | ascii[8] = {} 70 | ascii[8][0] = 0x36 71 | ascii[8][1] = 0x49 72 | ascii[8][2] = 0x49 73 | ascii[8][3] = 0x49 74 | ascii[8][4] = 0x36 75 | 76 | ascii[9] = {} 77 | ascii[9][0] = 0x06 78 | ascii[9][1] = 0x49 79 | ascii[9][2] = 0x49 80 | ascii[9][3] = 0x29 81 | ascii[9][4] = 0x1e 82 | 83 | function write_reg(dev_addr, reg_addr, reg_val) 84 | i2c.start(oled_id) 85 | i2c.address(oled_id, dev_addr, i2c.TRANSMITTER) 86 | i2c.write(oled_id, reg_addr) 87 | i2c.write(oled_id, reg_val) 88 | i2c.stop(oled_id) 89 | end 90 | 91 | function oled_command(cmd) 92 | write_reg(oled_addr, 0, cmd) 93 | end 94 | 95 | function M.init(sda_n, scl_n) 96 | oled_sda = oled_gpio[sda_n] 97 | oled_scl = oled_gpio[scl_n] 98 | 99 | i2c.setup(oled_id, oled_sda, oled_scl, i2c.SLOW) 100 | 101 | oled_command(0x8d) 102 | oled_command(0x14) 103 | oled_command(0xaf) 104 | 105 | oled_command(0xd3) 106 | oled_command(0x00) 107 | oled_command(0x40) 108 | oled_command(0xa1) 109 | oled_command(0xc8) 110 | oled_command(0xda) 111 | oled_command(0x12) 112 | oled_command(0x81) 113 | oled_command(0xff) 114 | 115 | oled_command(0x20) 116 | oled_command(0x02) 117 | end 118 | 119 | function M.on() 120 | oled_command(0xAF) 121 | end 122 | 123 | function M.off() 124 | oled_command(0xAE) 125 | end 126 | 127 | function M.invert(state) 128 | if state == 1 then 129 | oled_command(0xA7) 130 | else 131 | oled_command(0xA6) 132 | end 133 | end 134 | 135 | function M.scroll(start, stop, left) 136 | if left then 137 | oled_command(0x26) 138 | else 139 | oled_command(0x27) 140 | end 141 | oled_command(0X00) 142 | oled_command(start) 143 | oled_command(0X00) 144 | oled_command(stop) 145 | oled_command(0X00) 146 | oled_command(0XFF) 147 | oled_command(0x2F) 148 | end 149 | 150 | function M.scroll_stop() 151 | oled_command(0x2E) 152 | end 153 | 154 | function M.set_pos(x, y) 155 | xp = bit.band(x, 0xf0) 156 | xp = bit.rshift(xp, 4) 157 | xp = bit.bor(xp, 0x10) 158 | 159 | xr = bit.band(x, 0x0f) 160 | xr = bit.bor(xr, 0x01) 161 | 162 | oled_command(0xB0+y) 163 | oled_command(xp) 164 | oled_command(xr) 165 | end 166 | 167 | function M.clear() 168 | oled_command(0x20) 169 | oled_command(0x01) 170 | for i=0,1024 do 171 | write_reg(oled_addr, 0x40, 0x00) 172 | tmr.wdclr() 173 | end 174 | oled_command(0x20) 175 | oled_command(0x02) 176 | end 177 | 178 | function M.write_char(char) 179 | for ic=0,4 do 180 | write_reg(oled_addr, 0x40, ascii[char][ic]) 181 | tmr.wdclr() 182 | end 183 | end 184 | 185 | function M.write_word(_word) 186 | _wc = 1 187 | while _word[_wc] do 188 | M.write_char(_word[_wc]) 189 | _wc=_wc+1 190 | end 191 | end 192 | 193 | return M -------------------------------------------------------------------------------- /oled_test.lua: -------------------------------------------------------------------------------- 1 | oled = require("oled") 2 | 3 | oled.init(14, 12) 4 | 5 | oled.set_pos(75, 3) -- set cursor to 75, 3 6 | 7 | we = {"1","2","3","4","5","6"} 8 | 9 | oled.write_word(we) -- write Hello(lol) 10 | -------------------------------------------------------------------------------- /palette.lua: -------------------------------------------------------------------------------- 1 | dofile("SSD1306_IP.lua") 2 | display() 3 | function split(s, delimiter) 4 | result = {}; 5 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 6 | table.insert(result, match); 7 | end 8 | return result; 9 | end 10 | function urlencode(payload) 11 | result = {}; 12 | list=split(payload,"\r\n") 13 | --print(list[1]) 14 | list=split(list[1]," ") 15 | --print(list[2]) 16 | list=split(list[2],"\/") 17 | 18 | table.insert(result, list[1]); 19 | table.insert(result, list[2]); 20 | table.insert(result, list[3]); 21 | 22 | return result; 23 | end 24 | 25 | function sendHeader(conn) 26 | conn:send("HTTP/1.1 200 OK\r\n") 27 | conn:send("Access-Control-Allow-Origin: *\r\n") 28 | conn:send("Content-Type: application/json; charset=utf-8\r\n") 29 | conn:send("Server:NodeMCU\r\n") 30 | conn:send("Connection: close\r\n\r\n") 31 | end 32 | 33 | function index(conn) 34 | conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\ 35 | \ 36 | ESP8266\ 37 | \ 38 | \ 39 | \ 40 | \ 41 | \ 48 | \ 49 | \ 50 | ') 51 | end 52 | srv=net.createServer(net.TCP) 53 | srv:listen(80,function(conn) 54 | conn:on("receive",function(conn,payload) 55 | print("Http Request..") 56 | list=urlencode(payload) 57 | if (list[2]=="") then 58 | index(conn) 59 | elseif (list[2]=="rgb") then 60 | local r = tonumber(list[3]) 61 | local g = tonumber(list[4]) 62 | local b = tonumber(list[5]) 63 | print(r) 64 | print(g) 65 | print(b) 66 | ws2812.writergb(5, string.char(r, g, b):rep(8)) 67 | sendHeader(conn) 68 | conn:send("{\"result\":\"ok\"}") 69 | else 70 | sendHeader(conn) 71 | conn:send("{\"result\":\"error\",\"message\": \"command not found\"}") 72 | end 73 | conn:close() 74 | end) 75 | end) 76 | -------------------------------------------------------------------------------- /palette2.lua: -------------------------------------------------------------------------------- 1 | dofile("SSD1306_IP.lua") 2 | display() 3 | function split(s, delimiter) 4 | result = {}; 5 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 6 | table.insert(result, match); 7 | end 8 | return result; 9 | end 10 | function urlencode(payload) 11 | result = {}; 12 | list=split(payload,"\r\n") 13 | --print(list[1]) 14 | list=split(list[1]," ") 15 | --print(list[2]) 16 | list=split(list[2],"\/") 17 | 18 | table.insert(result, list[1]); 19 | table.insert(result, list[2]); 20 | table.insert(result, list[3]); 21 | 22 | return result; 23 | end 24 | 25 | function sendHeader(conn) 26 | conn:send("HTTP/1.1 200 OK\r\n") 27 | conn:send("Access-Control-Allow-Origin: *\r\n") 28 | conn:send("Content-Type: text/html; charset=utf-8\r\n") 29 | conn:send("Server:NodeMCU\r\n") 30 | conn:send("Connection: close\r\n\r\n") 31 | end 32 | 33 | function index(conn) 34 | conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\ 35 | \ 36 | ESP8266\ 37 | \ 38 | \ 39 | \ 40 |

Page Not Found

\ 41 | ') 42 | end 43 | srv=net.createServer(net.TCP) 44 | srv:listen(80,function(conn) 45 | conn:on("receive",function(conn,payload) 46 | print("Http Request..") 47 | list=urlencode(payload) 48 | if (list[2]=="") then 49 | index(conn) 50 | elseif (list[2]=="rgb") then 51 | local r = tonumber(list[3]) 52 | local g = tonumber(list[4]) 53 | local b = tonumber(list[5]) 54 | print(r) 55 | print(g) 56 | print(b) 57 | ws2812.writergb(5, string.char(r, g, b):rep(8)) 58 | sendHeader(conn) 59 | conn:send("{\"result\":\"ok\"}") 60 | else 61 | sendHeader(conn) 62 | conn:send("{\"result\":\"error\",\"message\": \"command not found\"}") 63 | end 64 | conn:close() 65 | end) 66 | end) 67 | -------------------------------------------------------------------------------- /pir.lua: -------------------------------------------------------------------------------- 1 | SensorID = "1" 2 | status = "CLEAR" 3 | oldstatus = "CLEAR" 4 | server = "192.168.1.37" 5 | port = 8888 6 | 7 | function open() 8 | print("Open connection...") 9 | conn=net.createConnection(net.TCP, 0) 10 | conn:on("receive", function(conn, payload) print(payload) end) 11 | conn:connect(port, server) 12 | conn:send("{ \"Type\":\"PIR\",\"SensorID\":\"".. SensorID .. "\"}") 13 | end 14 | 15 | function sendalarm(SensorID,status) 16 | conn:send("{ \"Type\":\"PIR\",\"SensorID\":\"".. SensorID .. "\", \"Status\":\"".. status .."\"}") 17 | end 18 | 19 | function close() 20 | conn:close() 21 | end 22 | 23 | open() 24 | gpio.mode(4,gpio.INPUT,gpio.FLOAT) 25 | tmr.alarm(0, 1000, 1, function() -- Set alarm to one second 26 | if gpio.read(4)==1 then status="ALARM" else status="CLEAR" end 27 | if status ~= oldstatus then sendalarm (SensorID,status) end 28 | oldstatus = status 29 | end) 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /rgb.lua: -------------------------------------------------------------------------------- 1 | pin1=3 2 | pin2=4 3 | gpio.mode(pin1,gpio.OUTPUT) 4 | gpio.mode(pin2,gpio.OUTPUT) 5 | function split(s, delimiter) 6 | result = {}; 7 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 8 | table.insert(result, match); 9 | end 10 | return result; 11 | end 12 | function urlencode(payload) 13 | result = {}; 14 | list=split(payload,"\r\n") 15 | --print(list[1]) 16 | list=split(list[1]," ") 17 | --print(list[2]) 18 | list=split(list[2],"\/") 19 | 20 | table.insert(result, list[1]); 21 | table.insert(result, list[2]); 22 | table.insert(result, list[3]); 23 | 24 | return result; 25 | end 26 | 27 | function sendHeader(conn) 28 | conn:send("HTTP/1.1 200 OK\r\n") 29 | conn:send("Access-Control-Allow-Origin: *\r\n") 30 | conn:send("Content-Type: application/json; charset=utf-8\r\n") 31 | conn:send("Server:NodeMCU\r\n") 32 | conn:send("Connection: close\r\n\r\n") 33 | end 34 | 35 | function index(conn) 36 | conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\ 37 | \ 38 | ESP8266\ 39 | \ 40 | \ 41 | \ 48 | \ 49 |

25

\ 50 |

25

\ 51 |

25

\ 52 | ') 53 | end 54 | srv=net.createServer(net.TCP) 55 | srv:listen(80,function(conn) 56 | conn:on("receive",function(conn,payload) 57 | print("Http Request..") 58 | list=urlencode(payload) 59 | if (list[2]=="") then 60 | index(conn) 61 | elseif (list[2]=="rgb") then 62 | local r = tonumber(list[3]) 63 | local g = tonumber(list[4]) 64 | local b = tonumber(list[5]) 65 | print(r) 66 | print(g) 67 | print(b) 68 | ws2812.writergb(4, string.char(r, g, b):rep(3)) 69 | sendHeader(conn) 70 | conn:send("{\"result\":\"ok\"}") 71 | else 72 | sendHeader(conn) 73 | conn:send("{\"result\":\"error\",\"message\": \"command not found\"}") 74 | end 75 | conn:close() 76 | end) 77 | end) 78 | -------------------------------------------------------------------------------- /run.lua: -------------------------------------------------------------------------------- 1 | 2 | function split(s, delimiter) 3 | result = {}; 4 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 5 | table.insert(result, match); 6 | end 7 | return result; 8 | end 9 | function urlencode(payload) 10 | result = {}; 11 | list=split(payload,"\r\n") 12 | --print(list[1]) 13 | list=split(list[1]," ") 14 | --print(list[2]) 15 | list=split(list[2],"\/") 16 | 17 | table.insert(result, list[1]); 18 | table.insert(result, list[2]); 19 | table.insert(result, list[3]); 20 | 21 | return result; 22 | end 23 | function sendHeader(conn) 24 | conn:send("HTTP/1.1 200 OK\r\n") 25 | conn:send("Access-Control-Allow-Origin: *\r\n") 26 | conn:send("Content-Type: application/json; charset=utf-8\r\n") 27 | conn:send("Server:NodeMCU\r\n") 28 | conn:send("Connection: close\r\n\r\n") 29 | end 30 | function index(conn) 31 | conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\ 32 | \ 33 | ESP8266\ 34 | \ 35 | \ 36 | \ 37 |

ESP8266 GOIP API

\ 38 | ') 39 | end 40 | srv=net.createServer(net.TCP) 41 | srv:listen(80,function(conn) 42 | conn:on("receive",function(conn,payload) 43 | list=urlencode(payload) 44 | if (list[2]=="") then 45 | index(conn) 46 | elseif (list[2]=="write") then 47 | local pin = tonumber(list[3]) 48 | --print("Pin: "..pin) 49 | local status = tonumber(list[4]) 50 | --print("State: "..status) 51 | gpio.write(pin, status) 52 | -- Response Header 53 | sendHeader(conn) 54 | -- Response Content 55 | conn:send('{"result":"ok","digitalPin": '..pin..', "status": '..gpio.read(pin)..'}') 56 | elseif (list[2]=="read") then 57 | -- Response Header 58 | sendHeader(conn) 59 | -- Response Content 60 | conn:send('{"result":"ok", "digitalPins":[\ 61 | {"digitalPin": 4,"status": '..gpio.read(4)..'},\ 62 | {"digitalPin": 5, "status": '..gpio.read(5)..'},\ 63 | {"digitalPin": 6, "status": '..gpio.read(6)..'},\ 64 | {"digitalPin": 7, "status": '..gpio.read(7)..'}\ 65 | ]}') 66 | else 67 | -- Response Header 68 | sendHeader(conn) 69 | -- Response Content 70 | conn:send('{"result":"error","message": "command not found"}') 71 | end 72 | conn:close() 73 | end) 74 | end) 75 | -------------------------------------------------------------------------------- /sample_http.lua: -------------------------------------------------------------------------------- 1 | 2 | function split(s, delimiter) 3 | result = {}; 4 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 5 | table.insert(result, match); 6 | end 7 | return result; 8 | end 9 | function urldecode(payload) 10 | result = {}; 11 | list=split(payload,"\r\n") 12 | list=split(list[1]," ") 13 | list=split(list[2],"\/") 14 | table.insert(result, list[1]); 15 | table.insert(result, list[2]); 16 | table.insert(result, list[3]); 17 | return result; 18 | end 19 | 20 | function index(conn) 21 | conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\ 22 | \ 23 | ESP8266\ 24 | \ 25 | \ 26 |

Page Index


\ 27 | ') 28 | end 29 | 30 | function about(conn) 31 | conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\ 32 | \ 33 | ESP8266\ 34 | \ 35 | \ 36 |

Page About


\ 37 | ') 38 | end 39 | 40 | function notfound(conn) 41 | conn:send('HTTP/1.1 404 Not Found\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\ 42 | \ 43 | ESP8266\ 44 |

Page Not Found

\ 45 | ') 46 | end 47 | 48 | srv=net.createServer(net.TCP) 49 | 50 | 51 | srv:listen(80,function(conn) 52 | conn:on("receive",function(conn,payload) 53 | print("Http Request..\r\n") 54 | print (payload) 55 | list=urldecode(payload) 56 | 57 | if ((list[2]=="") or (list[2]=="index.html")) then 58 | index(conn) 59 | elseif (list[2]=="about.html") then 60 | about(conn) 61 | else 62 | notfound(conn) 63 | end 64 | conn:close() 65 | end) 66 | end) 67 | -------------------------------------------------------------------------------- /sample_http2.lua: -------------------------------------------------------------------------------- 1 | pin2 = 6 2 | pin3 = 7 3 | gpio.mode(pin2,gpio.OUTPUT) 4 | gpio.mode(pin3,gpio.OUTPUT) 5 | function split(s, delimiter) 6 | result = {}; 7 | for match in (s..delimiter):gmatch("(.-)"..delimiter) do 8 | table.insert(result, match); 9 | end 10 | return result; 11 | end 12 | function urldecode(payload) 13 | result = {}; 14 | list=split(payload,"\r\n") 15 | list=split(list[1]," ") 16 | list=split(list[2],"\/") 17 | table.insert(result, list[1]); 18 | table.insert(result, list[2]); 19 | table.insert(result, list[3]); 20 | return result; 21 | end 22 | 23 | function index(conn) 24 | gpio6 = 'ON' 25 | gpio7 = 'ON' 26 | if (gpio.read(pin2) == 1) then 27 | gpio6 = 'OFF' 28 | end 29 | if (gpio.read(pin3) == 1) then 30 | gpio7 = 'OFF' 31 | end 32 | conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\ 33 | \ 34 | ESP8266\ 35 | \ 36 |

ESP8266-01


\ 37 |
\ 38 |

\ 39 |
\ 40 |

\ 41 | ') 42 | end 43 | function notfound(conn) 44 | conn:send('HTTP/1.1 404 Not Found\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\ 45 | \ 46 | ESP8266\ 47 |

Page Not Found

\ 48 | ') 49 | end 50 | 51 | srv=net.createServer(net.TCP) 52 | srv:listen(80,function(conn) 53 | conn:on("receive",function(conn,payload) 54 | print("Http Request..\r\n") 55 | --print (payload) 56 | list=urldecode(payload) 57 | if ((list[2]=="") or (list[2]=="index.html")) then 58 | index(conn) 59 | elseif (list[2]=="digital") then 60 | local pin = tonumber(list[3]) 61 | local status = tonumber(list[4]) 62 | if (status == 1) then 63 | gpio.write(pin, 0) 64 | else 65 | gpio.write(pin, 1) 66 | end 67 | --print(gpio.read(pin)) 68 | index(conn) 69 | else 70 | notfound(conn) 71 | end 72 | conn:close() 73 | end) 74 | end) 75 | -------------------------------------------------------------------------------- /sample_http3.lua: -------------------------------------------------------------------------------- 1 | gpio.mode(5,gpio.OUTPUT) 2 | gpio.mode(6,gpio.OUTPUT) 3 | gpio.mode(7,gpio.OUTPUT) 4 | 5 | gpio.write(5, 0) 6 | gpio.write(6, 0) 7 | gpio.write(7, 0) 8 | --dofile('ssd1306_ip.lua') 9 | function index(conn) 10 | local gpio5 = 'ON' 11 | local gpio6 = 'ON' 12 | local gpio7 = 'ON' 13 | if (gpio.read(5) == 1) then 14 | gpio5 = 'OFF' 15 | end 16 | if (gpio.read(6) == 1) then 17 | gpio6 = 'OFF' 18 | end 19 | if (gpio.read(7) == 1) then 20 | gpio7 = 'OFF' 21 | end 22 | conn:send('HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nAccess-Control-Allow-Origin: *\r\nCache-Control: private, no-store\r\n\r\n\ 23 | \ 24 | ESP8266\ 25 | \ 26 |

ESP8266-12


\ 27 |

\ 28 |

\ 29 |
\ 30 | ') 31 | end 32 | function notfound(conn) 33 | conn:send('HTTP/1.1 404 Not Found\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\n\r\n\ 34 | \ 35 | ESP8266\ 36 |

Page Not Found

\ 37 | ') 38 | end 39 | 40 | srv=net.createServer(net.TCP) 41 | srv:listen(80,function(conn) 42 | conn:on("receive",function(conn,payload) 43 | print("Http Request..\r\n") 44 | -- print (payload) 45 | -- "GET /digital/5/1 HTTP" 46 | _, _, method, action, pin, state = string.find(payload, "([A-Z]+) /(.+)/([0-9]+)/([0-1]+) HTTP") 47 | --print (action) 48 | --print (pin) 49 | --print (state) 50 | --list=urldecode(payload) 51 | if ((action==nil) or (action=="index.html")) then 52 | index(conn) 53 | elseif (action=="digital") then 54 | local pin = tonumber(pin) 55 | local status = tonumber(state) 56 | if (status == 1) then 57 | gpio.write(pin, 0) 58 | else 59 | gpio.write(pin, 1) 60 | end 61 | --print(gpio.read(pin)) 62 | index(conn) 63 | else 64 | notfound(conn) 65 | end 66 | conn:close() 67 | --display() 68 | end) 69 | end) 70 | -------------------------------------------------------------------------------- /set_config.lua: -------------------------------------------------------------------------------- 1 | ss=net.createServer(net.TCP) 2 | ss:listen(80,function(c) 3 | c:on("receive",function(c,pl) 4 | print(pl) 5 | ssidBegin = string.find(pl, "=", 0)+1 6 | ssidEnd = string.find(pl, "&", 0)-1 7 | passBegin = string.find(pl, "=", ssidEnd+2)+1 8 | passEnd = string.find(pl, "&", ssidEnd+2)-1 9 | 10 | ssidName = string.sub(pl, ssidBegin, ssidEnd) 11 | pass = string.sub(pl, passBegin, passEnd) 12 | 13 | print ("Got SSID: " .. ssidName) 14 | print ("key: " .. pass) 15 | 16 | c:send("HTTP/1.1 200 OK\n\n") 17 | c:send("") 18 | c:send("

Your ESP device is now connected to the following SSID.


") 19 | c:send("SSID : " .. ssidName .. "
") 20 | c:send("key : " .. pass .. "
") 21 | c:send("") 22 | 23 | c:send("\nTMR:"..tmr.now().." MEM:"..node.heap()) 24 | 25 | 26 | wifi.sta.config(ssidName,pass) 27 | wifi.sta.connect() 28 | cnt = 0 29 | tmr.alarm(0, 1000, 1, function() 30 | if (wifi.sta.getip() == nil) and (cnt < 10) then 31 | print("IP unavaiable, Waiting...") 32 | cnt = cnt + 1 33 | else 34 | tmr.stop(0) 35 | if cnt < 10 then 36 | file.open("config.lua","w+") 37 | file.writeline(ssidName) 38 | file.writeline(pass) 39 | file.close() 40 | print("Connected to your wifi network!") 41 | c:send("\nConnected to your wifi network!") 42 | else 43 | print("Wifi setup time more than 10s, Please verify wifi.sta.config() function. Then re-download the file.") 44 | c:send("\nWifi setup time more than 10s, Please verify wifi.sta.config() function. Then re-download the file.") 45 | end 46 | 47 | c:close() 48 | ss:close() 49 | end 50 | end) 51 | 52 | end) 53 | end) -------------------------------------------------------------------------------- /soil.lua: -------------------------------------------------------------------------------- 1 | SensorPin = 3 2 | 3 | function open() 4 | print("Open connection...") 5 | conn=net.createConnection(net.TCP, 0) 6 | conn:on("receive", function(conn, payload) print(payload) end) 7 | conn:connect(8888,"192.168.1.35") 8 | conn:send("{ \"Type\":\"SOIL\",\"SensorID\":\"".. SensorID .. "\"}") 9 | end 10 | 11 | function sendalarm(SensorID,status) 12 | conn:send("{ \"Type\": \"SOIL\",\"SensorID\":\"".. SensorID .. "\", \"Status\":\"".. status .."\"}") 13 | end 14 | 15 | function close() 16 | conn:close() 17 | end 18 | 19 | open() 20 | 21 | SensorID = "2" 22 | status = "LOW" 23 | oldstatus = "LOW" 24 | 25 | gpio.mode(SensorPin,gpio.INPUT,gpio.FLOAT) 26 | 27 | tmr.alarm(0, 1000, 1, function() -- Set alarm to one second 28 | if gpio.read(SensorPin)==1 then status="LOW" else status="HIGH" end 29 | if status ~= oldstatus then sendalarm (SensorID,status) end 30 | oldstatus = status 31 | end) 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /ssd1306.lua: -------------------------------------------------------------------------------- 1 | function init_i2c_display() 2 | -- SDA and SCL can be assigned freely to available GPIOs 3 | sda = 3 -- GPIO14 4 | scl = 4 -- GPIO12 5 | sla = 0x3c 6 | i2c.setup(0, sda, scl, i2c.SLOW) 7 | disp = u8g.ssd1306_128x64_i2c(sla) 8 | end 9 | 10 | function prepare() 11 | disp:setFont(u8g.font_6x10) 12 | disp:setFontRefHeightExtendedText() 13 | disp:setDefaultForegroundColor() 14 | disp:setFontPosTop() 15 | end -------------------------------------------------------------------------------- /ssd13061.lua: -------------------------------------------------------------------------------- 1 | function init_i2c_display() 2 | -- SDA and SCL can be assigned freely to available GPIOs 3 | sda = 5 -- GPIO14 4 | scl = 6 -- GPIO12 5 | sla = 0x3c 6 | i2c.setup(0, sda, scl, i2c.SLOW) 7 | disp = u8g.ssd1306_128x64_i2c(sla) 8 | end 9 | 10 | -- graphic test components 11 | function prepare() 12 | disp:setFont(u8g.font_6x10) 13 | disp:setFontRefHeightExtendedText() 14 | disp:setDefaultForegroundColor() 15 | disp:setFontPosTop() 16 | end 17 | 18 | function box_frame(a) 19 | disp:drawStr(0, 0, "drawBox") 20 | disp:drawBox(5, 10, 20, 10) 21 | disp:drawBox(10+a, 15, 30, 7) 22 | disp:drawStr(0, 30, "drawFrame") 23 | disp:drawFrame(5, 10+30, 20, 10) 24 | disp:drawFrame(10+a, 15+30, 30, 7) 25 | end 26 | 27 | function ascii_1() 28 | local x, y, s 29 | disp:drawStr(0, 0, "ASCII page 1") 30 | for y = 0, 5, 1 do 31 | for x = 0, 15, 1 do 32 | s = y*16 + x + 32 33 | disp:drawStr(x*7, y*10+10, string.char(s)) 34 | end 35 | end 36 | end 37 | 38 | init_i2c_display() 39 | prepare() 40 | 41 | disp:firstPage() 42 | repeat 43 | box_frame(6) 44 | until disp:nextPage() == false 45 | tmr.delay(50000) 46 | -- re-trigger Watchdog! 47 | tmr.wdclr() 48 | disp:firstPage() 49 | repeat 50 | ascii_1() 51 | until disp:nextPage() == false 52 | 53 | -------------------------------------------------------------------------------- /tcp_client_temp.lua: -------------------------------------------------------------------------------- 1 | SensorID = node.chipid() 2 | local humidity = 0 3 | local temperature = 0 4 | server = "192.168.1.36" 5 | port = 8888 6 | PIN = 5 7 | DHT= require("dht_lib") 8 | 9 | function init_i2c_display() 10 | -- SDA and SCL can be assigned freely to available GPIOs 11 | sda = 3 -- GPIO14 12 | scl = 4 -- GPIO12 13 | sla = 0x3c 14 | i2c.setup(0, sda, scl, i2c.SLOW) 15 | disp = u8g.ssd1306_128x64_i2c(sla) 16 | end 17 | 18 | function prepare() 19 | disp:setFont(u8g.font_6x10) 20 | disp:setFontRefHeightExtendedText() 21 | disp:setDefaultForegroundColor() 22 | disp:setFontPosTop() 23 | end 24 | 25 | function readTemp() 26 | DHT.read11(PIN) 27 | local t = DHT.getTemperature() 28 | local h = DHT.getHumidity() 29 | 30 | if h == nil then 31 | print("Error reading from DHT11/22") 32 | else 33 | -- temperature in degrees Celsius and Farenheit 34 | humidity = h --((h - (h % 10)) / 10).."."..(h % 10) 35 | temperature = t --((t-(t % 10)) / 10).."."..(t % 10) 36 | print("Temperature: "..temperature.." deg C") 37 | print("Humidity: "..humidity.."%") 38 | end 39 | end 40 | 41 | function open() 42 | print("Open connection...") 43 | conn=net.createConnection(net.TCP, 0) 44 | conn:on("receive", function(conn, payload) print(payload) end) 45 | conn:on("disconnection", function() 46 | print("Disconnection..") 47 | tmr.stop(0) 48 | end) 49 | conn:on("reconnection", function() 50 | print("Reconnection..") 51 | end) 52 | conn:on("connection", function() 53 | print("Connected..") 54 | start() 55 | --conn:send("{ \"Type\":\"TEMP\",\"SensorID\":\"".. SensorID .. "\"}") 56 | end) 57 | conn:connect(port, server) 58 | end 59 | 60 | function send() 61 | readTemp() 62 | if (humidity ~= nil) then 63 | file.open("temp.MONO", "r") 64 | xbm_data = file.read() 65 | file.close() 66 | disp:firstPage() 67 | repeat 68 | disp:drawXBM( 0, 0, 60, 60, xbm_data ) 69 | disp:setScale2x2() 70 | disp:drawStr(35,5, temperature.."C") 71 | disp:drawStr(35,15, humidity.."%") 72 | disp:undoScale() 73 | until disp:nextPage() == false 74 | xbm_data = nil 75 | conn:send('{ "Type": "TEMP", "SensorID":'.. SensorID .. ', "temperature": '..temperature..', "humidity": '..humidity..'}') 76 | end 77 | end 78 | 79 | function close() 80 | conn:close() 81 | end 82 | init_i2c_display() 83 | prepare() 84 | tmr.alarm(0, 2000, 0, function() 85 | open() 86 | end) 87 | function start() 88 | send() 89 | tmr.alarm(0, 5000, 1, function() -- Set alarm to one second 90 | send() 91 | end) 92 | end 93 | --tmr.stop(0) 94 | -------------------------------------------------------------------------------- /tcp_server_temp.lua: -------------------------------------------------------------------------------- 1 | gpio.mode(5, gpio.OUTPUT) 2 | gpio.write(5, 0) 3 | port = 8888 4 | local humidity = 0 5 | local temperature = 0 6 | PIN = 0 7 | DHT= require("dht_lib") 8 | function readTemp() 9 | DHT.read22(PIN) 10 | temperature = DHT.getTemperature() 11 | humidity = DHT.getHumidity() 12 | 13 | print(temperature) 14 | print(humidity) 15 | end 16 | 17 | function open() 18 | print("Open connection...") 19 | srv=net.createServer(net.TCP) 20 | srv:listen(port,function(conn) 21 | conn:on("receive",function(conn,payload) 22 | print(payload) 23 | _, _, method, pin = string.find(payload, "([A-Z]+[0-9]+),([0-9]+)") 24 | print(method) 25 | print(pin) 26 | if (method == 'D5') then 27 | gpio.write(5, tonumber(pin)) 28 | end 29 | if (method == 'READ1') then 30 | readTemp() 31 | if (humidity ~= nil) then 32 | conn:send('{ "Type": "TEMP", "SensorID":'.. node.chipid() .. ', "temperature": '..temperature..', "humidity": '..humidity..'}') 33 | else 34 | conn:send('{"error": 100}') 35 | end 36 | end 37 | end) 38 | end) 39 | end 40 | 41 | open() 42 | -------------------------------------------------------------------------------- /thingspeak_dh11.lua: -------------------------------------------------------------------------------- 1 | dht11 = require("dht11") 2 | Temperature = 0 3 | Humidity = 0 4 | key = "BW2QUW86FJ96DGIG" 5 | 6 | PIN = 4 -- data pin, GPIO2 7 | --load DHT11 module and read sensor 8 | function ReadDHT11() 9 | dht11.init(PIN) 10 | t = dht11.getTemp() 11 | h = dht11.getHumidity() 12 | Humidity=(h) 13 | Temperature=(t) 14 | --fare=((Temperature*9/5)+32) 15 | print("Humidity: "..Humidity.."%") 16 | print("Temperature: "..Temperature.." deg C") 17 | --print("Temperature: "..fare.." deg F") 18 | end 19 | 20 | function sendData() 21 | ReadDHT11() 22 | -- conection to thingspeak.com 23 | print("Sending data to thingspeak.com") 24 | conn=net.createConnection(net.TCP, 0) 25 | conn:on("receive", function(conn, payload) print(payload) end) 26 | -- api.thingspeak.com 184.106.153.149 27 | conn:connect(80,'184.106.153.149') 28 | conn:send("GET /update?key="..key.."&field1="..Temperature.. "&field2="..Humidity.. " HTTP/1.1\r\n") 29 | conn:send("Host: api.thingspeak.com\r\n") 30 | conn:send("Accept: */*\r\n") 31 | conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n") 32 | conn:send("\r\n") 33 | conn:on("sent",function(conn) 34 | print("Closing connection") 35 | conn:close() 36 | end) 37 | conn:on("disconnection", function(conn) 38 | print("Got disconnection...") 39 | end) 40 | end 41 | 42 | tmr.alarm(0, 10000, 1, function() sendData() end ) 43 | -------------------------------------------------------------------------------- /thingspeak_dht11.lua: -------------------------------------------------------------------------------- 1 | dht11 = require("dht11") 2 | Temperature = 0 3 | Humidity = 0 4 | key = "you-key" 5 | 6 | PIN = 4 -- data pin, GPIO2 7 | --load DHT11 module and read sensor 8 | function ReadDHT11() 9 | dht11.init(PIN) 10 | t = dht11.getTemp() 11 | h = dht11.getHumidity() 12 | Humidity=(h) 13 | Temperature=(t) 14 | --fare=((Temperature*9/5)+32) 15 | print("Humidity: "..Humidity.."%") 16 | print("Temperature: "..Temperature.." deg C") 17 | --print("Temperature: "..fare.." deg F") 18 | end 19 | 20 | function sendData() 21 | ReadDHT11() 22 | -- conection to thingspeak.com 23 | print("Sending data to thingspeak.com") 24 | conn=net.createConnection(net.TCP, 0) 25 | conn:on("receive", function(conn, payload) print(payload) end) 26 | -- api.thingspeak.com 184.106.153.149 27 | conn:connect(80,'184.106.153.149') 28 | conn:send("GET /update?key="..key.."&field1="..Temperature.. "&field2="..Humidity.. " HTTP/1.1\r\n") 29 | conn:send("Host: api.thingspeak.com\r\n") 30 | conn:send("Accept: */*\r\n") 31 | conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n") 32 | conn:send("\r\n") 33 | conn:on("sent",function(conn) 34 | print("Closing connection") 35 | conn:close() 36 | end) 37 | conn:on("disconnection", function(conn) 38 | print("Got disconnection...") 39 | end) 40 | end 41 | 42 | tmr.alarm(0, 10000, 1, function() sendData() end ) -------------------------------------------------------------------------------- /thingspeak_ds18B20.lua: -------------------------------------------------------------------------------- 1 | Temperature = 0 2 | t = require("ds18b20") 3 | t.setup(4) 4 | key = "BW2QUW86FJ96DGIG" 5 | 6 | --load DHT11 module and read sensor 7 | function ReadDS1820(pin) 8 | addrs = t.addrs() 9 | if (addrs ~= nil) then 10 | print("Total DS18B20 sensors: "..table.getn(addrs)) 11 | end 12 | Temperature = (t.read()) 13 | -- Just read temperature 14 | print("Temperature: "..Temperature.."'C") 15 | end 16 | 17 | function sendData() 18 | ReadDS1820(4) 19 | -- conection to thingspeak.com 20 | print("Sending data to thingspeak.com") 21 | conn=net.createConnection(net.TCP, 0) 22 | conn:on("receive", function(conn, payload) print(payload) end) 23 | -- api.thingspeak.com 184.106.153.149 24 | conn:connect(80,'184.106.153.149') 25 | conn:send("GET /update?key="..key.."&field3="..Temperature.." HTTP/1.1\r\n") 26 | conn:send("Host: api.thingspeak.com\r\n") 27 | conn:send("Accept: */*\r\n") 28 | conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n") 29 | conn:send("\r\n") 30 | conn:on("sent",function(conn) 31 | print("Closing connection") 32 | conn:close() 33 | end) 34 | conn:on("disconnection", function(conn) 35 | print("Got disconnection...") 36 | end) 37 | end 38 | 39 | tmr.alarm(0, 10000, 1, function() sendData() end ) -------------------------------------------------------------------------------- /tinyweb.lua: -------------------------------------------------------------------------------- 1 | local moduleName = "tinyweb" 2 | local M = {} 3 | _G[moduleName] = M 4 | 5 | local handlers = {} 6 | 7 | local function response(status, body) 8 | if body == nil then 9 | body = '' 10 | end 11 | local length = string.len(body) 12 | return 'HTTP/1.0 '..status..' OK\r\nServer: esphttpd/0.9\r\nContent-Type: text/html\r\nContent-Length: '..length..'\r\nConnection: close\r\n\r\n'..body 13 | end 14 | 15 | local function urlDecode(str) 16 | if str == nil then 17 | return nil 18 | end 19 | str = string.gsub(str, '+', ' ') 20 | str = string.gsub(str, '%%(%x%x)', function(h) return string.char(tonumber(h, 16)) end) 21 | return str 22 | end 23 | 24 | local function render(filename, subs) 25 | file.open(filename, 'r') 26 | content = '' 27 | while true do 28 | local line = file.readline() 29 | if line == nil then 30 | break 31 | end 32 | for k, v in pairs(subs) do 33 | line = string.gsub(line, '{{'..k..'}}', v) 34 | end 35 | content = content .. line 36 | end 37 | return content 38 | end 39 | 40 | local function hello(conn, path, method, data) 41 | conn:send(response(200, '

hello world

')) 42 | end 43 | 44 | local function calculator(conn, path, method, data) 45 | local result = '

result: 1 + 1 = 2

' 46 | if method == 'POST' then 47 | local num1 = tonumber(string.gmatch(data, 'num1=([0-9]+)')()) 48 | local sign = urlDecode(string.gmatch(data, 'sign=([^&]+)')()) 49 | local num2 = tonumber(string.gmatch(data, 'num2=([0-9]+)')()) 50 | local resultNum = 'NaN' 51 | if sign == '+' then 52 | resultNum = num1 + num2 53 | end 54 | if sign == '-' then 55 | resultNum = num1 - num2 56 | end 57 | if sign == '*' then 58 | resultNum = num1 * num2 59 | end 60 | if sign == '/' then 61 | resultNum = num1 / num2 62 | end 63 | result = '

result: '..num1..' '..sign..' '..num2..' = '..resultNum..'

' 64 | end 65 | local body = render('calculator.html', {result=result}) 66 | conn:send(response(200, body)) 67 | end 68 | 69 | local function receive(conn, data) 70 | local i, j = string.find(data, '\r\n\r\n') 71 | if i == nil then 72 | return false 73 | end 74 | local header = string.sub(data, 1, i-1) 75 | local body = string.sub(data, i+4, -1) 76 | local data = nil 77 | local method, path = 'GET', string.gmatch(header, 'GET ([0-9a-zA-Z.-_/]+) HTTP/1.+')() 78 | if path == nil then 79 | method, path = 'POST', string.gmatch(header, 'POST ([0-9a-zA-Z.-_/]+) HTTP/1.+')() 80 | end 81 | if path == nil then 82 | return 83 | end 84 | func = handlers[path] 85 | if func == nil then 86 | conn:send(response(404, '404 Not Found')) 87 | return 88 | end 89 | func(conn, path, method, body) 90 | end 91 | 92 | local function route(conn) 93 | conn:on('receive', receive) 94 | end 95 | ---- 96 | function M.handle(path, func) 97 | handlers[path] = func 98 | end 99 | 100 | function M.run() 101 | srv = net.createServer(net.TCP) 102 | srv:listen(80, route) 103 | M.handle('/', calculator) 104 | M.handle('/hello/', hello) 105 | end 106 | 107 | return M 108 | -------------------------------------------------------------------------------- /training/blinking.lua: -------------------------------------------------------------------------------- 1 | LED = 7 2 | gpio.mode(LED, gpio.OUTPUT) 3 | tmr.alarm(0, 1000, 1, function() 4 | if (gpio.read(LED) == 1) then 5 | gpio.write(LED, 0) 6 | else 7 | gpio.write(LED, 1) 8 | end 9 | end) -------------------------------------------------------------------------------- /training/dht.lua: -------------------------------------------------------------------------------- 1 | PIN = 0 -- data pin, GPIO2 2 | 3 | DHT= require("dht_lib") 4 | 5 | function read() 6 | DHT.read11(PIN) 7 | --DHT.read22(PIN) 8 | t = DHT.getTemperature() 9 | h = DHT.getHumidity() 10 | if h == nil then 11 | print("Error reading from DHT11/22") 12 | 13 | else 14 | -- *********** DHT11 ******************** 15 | print("Temperature: "..t.." deg C") 16 | print("Humidity: "..h.."%") 17 | -- *********** DHT22 ******************** 18 | -- temperature in degrees Celsius and Farenheit 19 | --print("Temperature: "..((t-(t % 10)) / 10).."."..(t % 10).." deg C") 20 | --print("Temperature: "..(9 * t / 50 + 32).."."..(9 * t / 5 % 10).." deg F") 21 | -- humidity 22 | --print("Humidity: "..((h - (h % 10)) / 10).."."..(h % 10).."%") 23 | end 24 | end 25 | 26 | read() 27 | 28 | -- release module 29 | --DHT = nil 30 | --package.loaded["dht_lib"]=nil -------------------------------------------------------------------------------- /training/for_1.lua: -------------------------------------------------------------------------------- 1 | color = {} 2 | color["1"] = "red" 3 | color["2"] = "green" 4 | color["3"] = "blue" 5 | for k,v in pairs(color) do 6 | print(k.." : "..v) 7 | end -------------------------------------------------------------------------------- /training/for_2.lua: -------------------------------------------------------------------------------- 1 | wifi.setmode(wifi.STATION) 2 | function listap(t) 3 | for k,v in pairs(t) do 4 | print(k.." : "..v) 5 | end 6 | end 7 | wifi.sta.getap(listap) -------------------------------------------------------------------------------- /training/for_3.lua: -------------------------------------------------------------------------------- 1 | wifi.setmode(wifi.STATION) 2 | function listap(t) 3 | for ssid,v in pairs(t) do 4 | authmode, rssi, bssid, channel = string.match(v, "(%d),(-?%d+),(%x%x:%x%x:%x%x:%x%x:%x%x:%x%x),(%d+)") 5 | print(ssid,authmode,rssi,bssid,channel) 6 | end 7 | end 8 | 9 | wifi.sta.getap(listap) -------------------------------------------------------------------------------- /training/http_server.lua: -------------------------------------------------------------------------------- 1 | srv=net.createServer(net.TCP) 2 | srv:listen(80,function(conn) 3 | conn:on("receive",function(conn,payload) 4 | print(payload) 5 | conn:send("

Hello, NodeMCU.

") 6 | end) 7 | end) -------------------------------------------------------------------------------- /training/if.lua: -------------------------------------------------------------------------------- 1 | i = 100 2 | if (i > 100) then 3 | print('Value: True') 4 | else 5 | print('Value: False') 6 | end -------------------------------------------------------------------------------- /training/read_dht.lua: -------------------------------------------------------------------------------- 1 | tmr.alarm(0, 5000, 1, function() 2 | read() 3 | print("***************************") 4 | end) -------------------------------------------------------------------------------- /training/tcp_server.lua: -------------------------------------------------------------------------------- 1 | sv=net.createServer(net.TCP, 30) -- 30s time out for a inactive client 2 | -- server listen on 80, if data received, print data to console, and send "hello world" to remote. 3 | sv:listen(80,function(c) 4 | c:on("receive", function(c, pl) print(pl) end) 5 | c:send("hello world") 6 | end) -------------------------------------------------------------------------------- /training/timer.lua: -------------------------------------------------------------------------------- 1 | tmr.alarm(0,1000,1,function() 2 | print("alarm 0") 3 | end) 4 | --tmr.stop(0) 5 | -------------------------------------------------------------------------------- /training/while.lua: -------------------------------------------------------------------------------- 1 | gpio.mode(3, gpio.OUTPUT) 2 | while 1 do 3 | gpio.write(3, gpio.HIGH) 4 | tmr.delay(1000000) -- wait 1,000,000 us = 1 second 5 | gpio.write(3, gpio.LOW) 6 | tmr.delay(1000000) -- wait 1,000,000 us = 1 second 7 | end -------------------------------------------------------------------------------- /training/wifi.lua: -------------------------------------------------------------------------------- 1 | wifi.sta.config("accesspointname","yourpassword") 2 | wifi.sta.connect() 3 | tmr.delay(1000000) -- wait 1,000,000 us = 1 second 4 | print(wifi.sta.status()) 5 | print(wifi.sta.getip()) -------------------------------------------------------------------------------- /u8g_graphics_test.lua: -------------------------------------------------------------------------------- 1 | 2 | -- setup I2c and connect display 3 | function init_i2c_display() 4 | -- SDA and SCL can be assigned freely to available GPIOs 5 | sda = 3 -- GPIO14 6 | scl = 4 -- GPIO12 7 | sla = 0x3c 8 | i2c.setup(0, sda, scl, i2c.SLOW) 9 | disp = u8g.ssd1306_128x64_i2c(sla) 10 | end 11 | 12 | -- setup SPI and connect display 13 | function init_spi_display() 14 | -- Hardware SPI CLK = GPIO14 15 | -- Hardware SPI MOSI = GPIO13 16 | -- Hardware SPI MISO = GPIO12 (not used) 17 | -- CS, D/C, and RES can be assigned freely to available GPIOs 18 | cs = 8 -- GPIO15, pull-down 10k to GND 19 | dc = 4 -- GPIO2 20 | res = 0 -- GPIO16 21 | 22 | spi.setup(1, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, spi.DATABITS_8, 0) 23 | disp = u8g.ssd1306_128x64_spi(cs, dc, res) 24 | end 25 | 26 | 27 | -- graphic test components 28 | function prepare() 29 | disp:setFont(u8g.font_6x10) 30 | disp:setFontRefHeightExtendedText() 31 | disp:setDefaultForegroundColor() 32 | disp:setFontPosTop() 33 | end 34 | 35 | function box_frame(a) 36 | disp:drawStr(0, 0, "drawBox") 37 | disp:drawBox(5, 10, 20, 10) 38 | disp:drawBox(10+a, 15, 30, 7) 39 | disp:drawStr(0, 30, "drawFrame") 40 | disp:drawFrame(5, 10+30, 20, 10) 41 | disp:drawFrame(10+a, 15+30, 30, 7) 42 | end 43 | 44 | function disc_circle(a) 45 | disp:drawStr(0, 0, "drawDisc") 46 | disp:drawDisc(10, 18, 9) 47 | disp:drawDisc(24+a, 16, 7) 48 | disp:drawStr(0, 30, "drawCircle") 49 | disp:drawCircle(10, 18+30, 9) 50 | disp:drawCircle(24+a, 16+30, 7) 51 | end 52 | 53 | function r_frame(a) 54 | disp:drawStr(0, 0, "drawRFrame/Box") 55 | disp:drawRFrame(5, 10, 40, 30, a+1) 56 | disp:drawRBox(50, 10, 25, 40, a+1) 57 | end 58 | 59 | function stringtest(a) 60 | disp:drawStr(30+a, 31, " 0") 61 | disp:drawStr90(30, 31+a, " 90") 62 | disp:drawStr180(30-a, 31, " 180") 63 | disp:drawStr270(30, 31-a, " 270") 64 | end 65 | 66 | function line(a) 67 | disp:drawStr(0, 0, "drawLine") 68 | disp:drawLine(7+a, 10, 40, 55) 69 | disp:drawLine(7+a*2, 10, 60, 55) 70 | disp:drawLine(7+a*3, 10, 80, 55) 71 | disp:drawLine(7+a*4, 10, 100, 55) 72 | end 73 | 74 | function triangle(a) 75 | local offset = a 76 | disp:drawStr(0, 0, "drawTriangle") 77 | disp:drawTriangle(14,7, 45,30, 10,40) 78 | disp:drawTriangle(14+offset,7-offset, 45+offset,30-offset, 57+offset,10-offset) 79 | disp:drawTriangle(57+offset*2,10, 45+offset*2,30, 86+offset*2,53) 80 | disp:drawTriangle(10+offset,40+offset, 45+offset,30+offset, 86+offset,53+offset) 81 | end 82 | 83 | function ascii_1() 84 | local x, y, s 85 | disp:drawStr(0, 0, "ASCII page 1") 86 | for y = 0, 5, 1 do 87 | for x = 0, 15, 1 do 88 | s = y*16 + x + 32 89 | disp:drawStr(x*7, y*10+10, string.char(s)) 90 | end 91 | end 92 | end 93 | 94 | function ascii_2() 95 | local x, y, s 96 | disp:drawStr(0, 0, "ASCII page 2") 97 | for y = 0, 5, 1 do 98 | for x = 0, 15, 1 do 99 | s = y*16 + x + 160 100 | disp:drawStr(x*7, y*10+10, string.char(s)) 101 | end 102 | end 103 | end 104 | 105 | function extra_page(a) 106 | disp:drawStr(0, 12, "setScale2x2") 107 | disp:setScale2x2() 108 | disp:drawStr(0, 6+a, "setScale2x2") 109 | disp:undoScale() 110 | end 111 | 112 | 113 | -- the draw() routine 114 | function draw(draw_state) 115 | local component = bit.rshift(draw_state, 3) 116 | 117 | prepare() 118 | 119 | if (component == 0) then 120 | box_frame(bit.band(draw_state, 7)) 121 | elseif (component == 1) then 122 | disc_circle(bit.band(draw_state, 7)) 123 | elseif (component == 2) then 124 | r_frame(bit.band(draw_state, 7)) 125 | elseif (component == 3) then 126 | stringtest(bit.band(draw_state, 7)) 127 | elseif (component == 4) then 128 | line(bit.band(draw_state, 7)) 129 | elseif (component == 5) then 130 | triangle(bit.band(draw_state, 7)) 131 | elseif (component == 6) then 132 | ascii_1() 133 | elseif (component == 7) then 134 | ascii_2() 135 | elseif (component == 8) then 136 | extra_page(bit.band(draw_state, 7)) 137 | end 138 | end 139 | 140 | function graphics_test(delay) 141 | print("--- Starting Graphics Test ---") 142 | 143 | -- cycle through all components 144 | local draw_state 145 | for draw_state = 0, 7 + 8*8, 1 do 146 | disp:firstPage() 147 | repeat 148 | draw(draw_state) 149 | until disp:nextPage() == false 150 | --print(node.heap()) 151 | tmr.delay(delay) 152 | -- re-trigger Watchdog! 153 | tmr.wdclr() 154 | end 155 | 156 | print("--- Graphics Test done ---") 157 | end 158 | 159 | init_i2c_display() 160 | --init_spi_display() 161 | graphics_test(50000) 162 | -------------------------------------------------------------------------------- /uart.lua: -------------------------------------------------------------------------------- 1 | server = "192.168.1.49" 2 | port = 5683 3 | node_name = "Test" 4 | uart.setup( 0, 9600, 8, 0, 1, 0 ) 5 | conn=net.createConnection(net.TCP, 0) 6 | function print_ip() 7 | uart.write(0,0x02) 8 | uart.write(0,"I"..wifi.sta.getip()) 9 | uart.write(0,0x03) 10 | end 11 | conn:on("receive", function(conn, payload) 12 | uart.write(0,0x02) 13 | uart.write(0,payload) 14 | uart.write(0,0x03) 15 | end) 16 | conn:on("disconnection", function() 17 | uart.write(0,0x02) 18 | uart.write(0,"C") 19 | uart.write(0,0x03) 20 | end) 21 | conn:on("connection", function() 22 | print_ip() 23 | function s_output(str) 24 | if (str.find(str, "GETIP")~=nil) then 25 | print_ip() 26 | else 27 | if(c~=nil) then c:send(str) end 28 | end 29 | end 30 | uart.on("data",s_output, 0) 31 | conn:send('{ "type": 1, "data": "'.. node.chipid() .. '", "name": "'..node_name..'"}') 32 | end) 33 | conn:connect(port, server) 34 | -------------------------------------------------------------------------------- /uart_server.lua: -------------------------------------------------------------------------------- 1 | uart.setup( 0, 9600, 8, 0, 1, 0 ) 2 | function print_ip() 3 | uart.write(0,0x02) 4 | uart.write(0,"I"..wifi.sta.getip()) 5 | uart.write(0,0x03) 6 | end 7 | print_ip() 8 | s=net.createServer(net.TCP,180) 9 | s:listen(1001,function(c) 10 | c:on("receive",function(c,l) 11 | uart.write(0,0x02) 12 | uart.write(0,l) 13 | uart.write(0,0x03) 14 | end) 15 | function s_output(str) 16 | if (str.find(str, "GETIP")~=nil) then 17 | print_ip() 18 | else 19 | if(c~=nil) then c:send(str) end 20 | end 21 | end 22 | uart.on("data",s_output, 0) 23 | print("Connected\r\n") 24 | end) 25 | -------------------------------------------------------------------------------- /weatherStation.lua: -------------------------------------------------------------------------------- 1 | -- your offset to UTC 2 | local UTC_OFFSET = 7 3 | -- Enter your city, check openweathermap.org 4 | -- Ban Rangsit, TH 5 | local CITY = "Thanyaburi,TH" 6 | -- Get an APP ID on openweathermap.org 7 | local APPID = "4a7401363b53fcc52892b4ece69c8b36" 8 | -- Update interval in minutes 9 | local INTERVAL = 1 10 | 11 | local temp = '0.0' 12 | local humidity = '0' 13 | 14 | function init_i2c_display() 15 | -- SDA and SCL can be assigned freely to available GPIOs 16 | sda = 3 -- GPIO14 17 | scl = 4 -- GPIO12 18 | sla = 0x3c 19 | i2c.setup(0, sda, scl, i2c.SLOW) 20 | disp = u8g.ssd1306_128x64_i2c(sla) 21 | end 22 | 23 | function prepare() 24 | disp:setFont(u8g.font_6x10) 25 | disp:setFontRefHeightExtendedText() 26 | disp:setDefaultForegroundColor() 27 | disp:setFontPosTop() 28 | end 29 | 30 | function updateWeather() 31 | print("Updating weather") 32 | local conn=net.createConnection(net.TCP, 0) 33 | conn:on("receive", function(conn, payload) 34 | --print(payload) 35 | local lastUpdate = string.sub(payload,string.find(payload,"Date: ")+23,string.find(payload,"Date: ")+31) 36 | local hour = string.sub(lastUpdate, 0, 2) + UTC_OFFSET 37 | lastUpdate = hour..string.sub(lastUpdate, 3, 9) 38 | local payload = string.match(payload, "{.*}") 39 | print(payload) 40 | if (payload ~= nil) then 41 | weather = nil 42 | weather = cjson.decode(payload) 43 | local icon = weather.weather[1].icon 44 | icon = string.gsub(icon, "n", "d") 45 | file.open(icon..".MONO", "r") 46 | xbm_data = file.read() 47 | file.close() 48 | drawWeather(xbm_data, weather, lastUpdate) 49 | else 50 | if (temp == '0.0') then 51 | drawWeather(nil,nil, nil) 52 | end 53 | end 54 | 55 | payload = nil 56 | conn:close() 57 | conn = nil 58 | 59 | end ) 60 | 61 | conn:connect(80,"128.199.103.33") 62 | conn:on("connection", function() 63 | print("connection...") 64 | conn:send("GET /data/2.5/weather?q="..CITY.."&units=metric&APPID="..APPID 65 | .." HTTP/1.1\r\n" 66 | .."Host: api.openweathermap.org\r\n" 67 | .."Connection: close\r\n" 68 | .."Accept: */*\r\n" 69 | .."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n" 70 | .."\r\n") 71 | conn = nil 72 | end) 73 | end 74 | init_i2c_display() 75 | prepare() 76 | updateWeather() 77 | 78 | 79 | function drawWeather(xbm_data, weather, lastUpdate) 80 | disp:firstPage() 81 | repeat 82 | if (xbm_data ~= nil) then 83 | disp:drawXBM( 0, 0, 60, 60, xbm_data ) 84 | end 85 | disp:setScale2x2() 86 | 87 | if (weather ~= nil) then 88 | local mult = 10^(idp or 0) 89 | temp = math.floor(weather.main.temp + 1) 90 | humidity = weather.main.humidity 91 | --disp:drawStr(2,16, weather.weather[1].main) 92 | end 93 | disp:drawStr(35,5, temp.."C") 94 | disp:drawStr(35,15, humidity.."%") 95 | --disp:drawStr(10,5, "TH") 96 | disp:undoScale() 97 | --disp:drawStr(5,17, "Thanyaburi") 98 | if (lastUpdate ~= nil) then 99 | --disp:drawStr(10,52, weather.wind.speed..'m/s') 100 | disp:drawStr(70,52, lastUpdate) 101 | end 102 | 103 | until disp:nextPage() == false 104 | end 105 | 106 | tmr.alarm(1, INTERVAL * 60000, 1, function() 107 | ip = wifi.sta.getip() 108 | if ip=="0.0.0.0" or ip==nil then 109 | print("connecting to AP...") 110 | else 111 | print("Loading weather...") 112 | updateWeather() 113 | end 114 | end ) 115 | -------------------------------------------------------------------------------- /weathericon/01d.MONO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/weathericon/01d.MONO -------------------------------------------------------------------------------- /weathericon/02d.MONO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/weathericon/02d.MONO -------------------------------------------------------------------------------- /weathericon/03d.MONO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/weathericon/03d.MONO -------------------------------------------------------------------------------- /weathericon/04d.MONO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/weathericon/04d.MONO -------------------------------------------------------------------------------- /weathericon/09d.MONO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/weathericon/09d.MONO -------------------------------------------------------------------------------- /weathericon/10d.MONO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/weathericon/10d.MONO -------------------------------------------------------------------------------- /weathericon/11d.MONO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/weathericon/11d.MONO -------------------------------------------------------------------------------- /weathericon/13d.MONO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/weathericon/13d.MONO -------------------------------------------------------------------------------- /weathericon/50d.MONO: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/db4linq/lua/031e30e81ab53c871f6e7ee12d810258809fc9cb/weathericon/50d.MONO -------------------------------------------------------------------------------- /wifi_sensor.lua: -------------------------------------------------------------------------------- 1 | SensorPin1 = 5 2 | status1 = "LOW" 3 | oldstatus1 = "LOW" 4 | gpio.mode(SensorPin1,gpio.INPUT,gpio.FLOAT) 5 | 6 | SensorPin2 = 6 7 | status2 = "LOW" 8 | oldstatus2 = "LOW" 9 | gpio.mode(SensorPin2,gpio.INPUT,gpio.FLOAT) 10 | 11 | 12 | SensorPin3 = 4 13 | 14 | function ReadDHT11() 15 | dht11 = require("dht11") 16 | dht11.init(SensorPin3) 17 | t = dht11.getTemp() 18 | h = dht11.getHumidity() 19 | humi=(h) 20 | temp=(t) 21 | fare=((t*9/5)+32) 22 | print("Humidity: "..humi.."%") 23 | print("Temperature: "..temp.." deg C") 24 | print("Temperature: "..fare.." deg F") 25 | print("==================================") 26 | dht11 = nil 27 | package.loaded["dht11"]=nil 28 | end 29 | 30 | function run() 31 | tmr.alarm(0, 500, 1, function() -- Set alarm to one second 32 | if gpio.read(SensorPin1)==1 then status1="LOW" else status1="HIGH" end 33 | if status1 ~= oldstatus1 then 34 | print("Soil 1: "..status1) 35 | print("==================================") 36 | end 37 | oldstatus1 = status1 38 | end) 39 | 40 | tmr.alarm(1, 500, 1, function() -- Set alarm to one second 41 | if gpio.read(SensorPin2)==1 then status2="LOW" else status2="HIGH" end 42 | if status2 ~= oldstatus2 then 43 | print("Soil 2: "..status2) 44 | print("==================================") 45 | end 46 | oldstatus2 = status2 47 | end) 48 | 49 | tmr.alarm(2, 5000, 1, function() -- Set alarm to one second 50 | ReadDHT11() 51 | end) 52 | end 53 | 54 | run() -------------------------------------------------------------------------------- /www.lua: -------------------------------------------------------------------------------- 1 | handlers = {} 2 | length = 0 3 | function response(status, body) 4 | if body == nil then 5 | body = '' 6 | end 7 | length = string.len(body) 8 | return 'HTTP/1.0 '..status..' OK\r\nServer: esphttpd/0.9\r\nContent-Type: text/html\r\nContent-Length: '..length..'\r\nConnection: close\r\n\r\n'..body 9 | end 10 | function urlDecode(str) 11 | if str == nil then 12 | return nil 13 | end 14 | str = string.gsub(str, '+', ' ') 15 | str = string.gsub(str, '%%(%x%x)', function(h) return string.char(tonumber(h, 16)) end) 16 | return str 17 | end 18 | function render(filename, subs) 19 | file.open(filename, 'r') 20 | content = '' 21 | while true do 22 | local line = file.readline() 23 | if line == nil then 24 | break 25 | end 26 | for k, v in pairs(subs) do 27 | line = string.gsub(line, '{{'..k..'}}', v) 28 | end 29 | content = content .. line 30 | line = nil 31 | end 32 | return content 33 | end 34 | function hello(conn, path, method, data) 35 | conn:send(response(200, '

hello world

')) 36 | end 37 | function calculator(conn, path, method, data) 38 | local result = '

result: 1 + 1 = 2

' 39 | if method == 'POST' then 40 | local num1 = tonumber(string.gmatch(data, 'num1=([0-9]+)')()) 41 | local sign = urlDecode(string.gmatch(data, 'sign=([^&]+)')()) 42 | local num2 = tonumber(string.gmatch(data, 'num2=([0-9]+)')()) 43 | local resultNum = 'NaN' 44 | if sign == '+' then 45 | resultNum = num1 + num2 46 | end 47 | if sign == '-' then 48 | resultNum = num1 - num2 49 | end 50 | if sign == '*' then 51 | resultNum = num1 * num2 52 | end 53 | if sign == '/' then 54 | resultNum = num1 / num2 55 | end 56 | result = '

result: '..num1..' '..sign..' '..num2..' = '..resultNum..'

' 57 | end 58 | local body = render('calculator.html', {result=result}) 59 | conn:send(response(200, body)) 60 | end 61 | function receive(conn, data) 62 | local i, j = string.find(data, '\r\n\r\n') 63 | if i == nil then 64 | return false 65 | end 66 | local header = string.sub(data, 1, i-1) 67 | local body = string.sub(data, i+4, -1) 68 | local data = nil 69 | local method, path = 'GET', string.gmatch(header, 'GET ([0-9a-zA-Z.-_/]+) HTTP/1.+')() 70 | if path == nil then 71 | method, path = 'POST', string.gmatch(header, 'POST ([0-9a-zA-Z.-_/]+) HTTP/1.+')() 72 | end 73 | if path == nil then 74 | return 75 | end 76 | func = handlers[path] 77 | if func == nil then 78 | conn:send(response(404, '404 Not Found')) 79 | return 80 | end 81 | func(conn, path, method, body) 82 | i = nil 83 | j = nil 84 | header = nil 85 | body = nil 86 | data = nil 87 | method = nil 88 | path = nil 89 | 90 | end 91 | function handle(path, func) 92 | handlers[path] = func 93 | end 94 | function run() 95 | srv = net.createServer(net.TCP) 96 | srv:listen(80, function(conn) 97 | conn:on('receive', receive) 98 | end) 99 | handle('/', calculator) 100 | handle('/hello/', hello) 101 | end 102 | run() 103 | 104 | -------------------------------------------------------------------------------- /xperia.lua: -------------------------------------------------------------------------------- 1 | ssid = 'Xperia_Z2' 2 | pwd = '1234567890' 3 | print("set up wifi mode") 4 | wifi.setmode(wifi.STATION) 5 | wifi.sta.config(ssid, pwd) 6 | wifi.sta.connect() 7 | cnt = 0 8 | tmr.alarm(0, 1000, 1, function() 9 | if (wifi.sta.getip() == nil) and (cnt < 10) then 10 | print("IP unavaiable, Waiting...") 11 | cnt = cnt + 1 12 | else 13 | tmr.stop(0) 14 | if (cnt < 10) then 15 | print("Config done, IP is "..wifi.sta.getip()) 16 | tmr.alarm(0, 2000, 1, function() 17 | tmr.stop(0) 18 | print("WIFI connected...") 19 | --dofile('sample_http.lua') 20 | --dofile("iothink_relay.lc") 21 | --dofile("sample_http3.lua") 22 | --dofile("iothink_weather.lc") 23 | end) 24 | else 25 | print("Wifi setup time more than 10s, Please verify wifi.sta.config() function. Then re-download the file.") 26 | end 27 | end 28 | end) 29 | --------------------------------------------------------------------------------