├── LuaLoader.zip ├── README.md ├── Source ├── LuaLoader.dpr ├── LuaLoader.res ├── LuaLoaderMain.dfm ├── LuaLoaderMain.pas ├── SerialNG.pas ├── SerialNGAdv.dfm ├── SerialNGAdv.pas └── supertim.zip ├── examples ├── AutoConnect.lua ├── Client with DNS.lua ├── Count Interrupts.lua ├── DS18B20 One Wire Temperature server.lua ├── Find Hot Spots │ ├── autoconnect.lua │ ├── httpget.lua │ └── init.lua ├── Kankun WiFi Plug │ ├── README.md │ ├── click.mp3 │ ├── httpget.lua │ ├── index.html │ ├── init.lua │ └── relay.cgi ├── README.md ├── Scan for I2C devices.lua ├── Servo Control by UDP.lua ├── googleTime.lua ├── gpio monitor.lua ├── httpget.lua └── thebutton │ ├── autoconnect.lua │ ├── httpget.lua │ ├── init.lua │ ├── reconnect.lua │ └── thebutton.php ├── version 0.87 ├── LLbin.lua ├── LuaLoader.exe ├── ReadMe.txt ├── RecentFiles.txt └── xxd.lua └── version 0.90 ├── LLbin.lua ├── LuaLoader.exe ├── ReadMe.txt ├── RecentFiles.txt └── xxd.lua /LuaLoader.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeoNomad/LuaLoader/e42164e7285a0fdcb3ee124e8a680eaf9518d701/LuaLoader.zip -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | LuaLoader for Windows 2 | ===================== 3 | 4 | More documentation and FAQ are at http://benlo.com/esp8266 5 | 6 | LuaLoader is a Windows program for uploading files to the ESP8266 and working with the Lua serial interface. It is compatible with all versions of Windows from Windows 95 to Windows 10. 7 | 8 | The terminal window shows the output from the ESP8266 UART and lets you type or paste commands for immediate interpretation and execution. 9 | 10 | A selection of buttons are available to automatically type often used commands and to select files for uploading to the ESP8266 file system. 11 | 12 | The Buttons 13 | 14 | All buttons have popup help information on mouse over. A few explanations follow: 15 | 16 |
17 | GPIO Set the IO pins to read or write. Change their values. Read values once or multiple times 18 | on a polling schedule. Read the ADC value. 19 |
20 |21 | Heap. Print the current Heap (RAM) available. A common cause of restarts is running out of RAM. 22 |
23 |24 | Restart. Perform a soft restart. The init.lua file is run automatically on restart. 25 |
26 |27 | chipID. Each chip has a unique ID that can be used in a multiple IoT environment. 28 |
29 |30 | tmr.stop. Stops one or more of the 7 timers. Right click to set which ones. 31 |
32 |33 | Set AP. Set the chip to STATION mode and set the Access Point SSID and Password. The ESP8266 will automatically connect to the access point. The information is saved and after a restart 34 | the chip will automatically reconnect within 2 seconds, normally. 35 |
36 |37 | Get IP. Get the currently assigned IP address, if any. 38 |
39 |40 | Wifi Status. Show the current status of the Wifi connection. 41 |
42 |43 | Disconnect. Disconnect from the access point. 44 |
45 |46 | Upload File... Upload a file from your hard drive to the ESP8266 in text mode or binary mode. 47 | The folder used is now designated as the current workspace and the folder name is added to the File - 48 | Workspace menu. 49 |
50 |51 | Upload Bin (or Text). Uploads the file named in the edit box below. Right click to select 52 | text or binary uploads. Binary uploads test each block with a checksum for data integrity 53 | and are faster. However, the LLbin() function in the LLbin.lua file must be loaded first. 54 |
55 |56 | File Selection. A dropdown list of files in the current workspace and on the ESP8266. 57 | Files on the ESP8266 are marked with a < character. 58 |
59 |60 | dofile. Executes the dofile() command to execute the file named in the text box above. 61 |
62 |63 | remove. Remove the file named in the text box from the ESP8266 file system. 64 |
65 |66 | cat. List the contents of the file named in the text box. 67 |
68 |69 | Format. Format the flash file system on the ESP8266. Deletes all the files. Format only works 70 | if you are running firmware built after 2015.01.05. 71 |
72 |73 | file.list. Lists the files in the ESP8266 flash file system. This list populates the drop 74 | down menu in the file name text box above. 75 |
76 | 77 | 78 | LuaLoader is freeware. 79 | 80 | The MIT License (MIT) 81 | 82 | Copyright (c) 2015 Peter Jennings benlo.com 83 | 84 | Permission is hereby granted, free of charge, to any person obtaining a copy 85 | of this software and associated documentation files (the "Software"), to deal 86 | in the Software without restriction, including without limitation the rights 87 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 88 | copies of the Software, and to permit persons to whom the Software is 89 | furnished to do so, subject to the following conditions: 90 | 91 | The above copyright notice and this permission notice shall be included in all 92 | copies or substantial portions of the Software. 93 | 94 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 95 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 96 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 97 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 98 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 99 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 100 | SOFTWARE. 101 | -------------------------------------------------------------------------------- /Source/LuaLoader.dpr: -------------------------------------------------------------------------------- 1 | program LuaLoader; 2 | 3 | uses 4 | Forms, 5 | LuaLoaderMain in 'LuaLoaderMain.pas' {Form1}, 6 | netstat in 'netstat.pas' {net}, 7 | SerialNG in 'SerialNG.pas', 8 | SerialNGAdv in 'SerialNGAdv.pas' {SerialNGAdvDLG}; 9 | {$R *.RES} 10 | 11 | 12 | begin 13 | Application.Initialize; 14 | Application.Title := 'LuaLoader'; 15 | Application.CreateForm(TForm1, Form1); 16 | Application.CreateForm(TSerialNGAdvDLG, SerialNGAdvDLG); 17 | Application.CreateForm(Tnet, net); 18 | Application.Run; 19 | net.Free; 20 | SerialNGAdvDLG.Free; 21 | Halt; 22 | Form1.Free; 23 | end. 24 | 25 | 26 | -------------------------------------------------------------------------------- /Source/LuaLoader.res: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeoNomad/LuaLoader/e42164e7285a0fdcb3ee124e8a680eaf9518d701/Source/LuaLoader.res -------------------------------------------------------------------------------- /Source/LuaLoaderMain.dfm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeoNomad/LuaLoader/e42164e7285a0fdcb3ee124e8a680eaf9518d701/Source/LuaLoaderMain.dfm -------------------------------------------------------------------------------- /Source/LuaLoaderMain.pas: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeoNomad/LuaLoader/e42164e7285a0fdcb3ee124e8a680eaf9518d701/Source/LuaLoaderMain.pas -------------------------------------------------------------------------------- /Source/SerialNG.pas: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeoNomad/LuaLoader/e42164e7285a0fdcb3ee124e8a680eaf9518d701/Source/SerialNG.pas -------------------------------------------------------------------------------- /Source/SerialNGAdv.dfm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeoNomad/LuaLoader/e42164e7285a0fdcb3ee124e8a680eaf9518d701/Source/SerialNGAdv.dfm -------------------------------------------------------------------------------- /Source/SerialNGAdv.pas: -------------------------------------------------------------------------------- 1 | unit SerialNGAdv; 2 | // Helper Unit for SerialNG Advanced Demo 3 | // Includes a Dialog for Advanced Settings 4 | // To use this Dialog, simply include this Unit to Your Project 5 | // Version 1.0.1 18.9.2001 Create the Comportlist from the System Registry 6 | 7 | interface 8 | 9 | uses Windows, SysUtils, Classes, Graphics, Forms, Controls, StdCtrls, 10 | Buttons, ExtCtrls, ComCtrls, SerialNG; 11 | 12 | type 13 | TSerialNGAdvDLG = class(TForm) 14 | Panel1: TPanel; 15 | OKBtn: TButton; 16 | CancelBtn: TButton; 17 | DLGTabSheet: TPageControl; 18 | BasicSheet: TTabSheet; 19 | TimingSheet: TTabSheet; 20 | AdvancedSheet: TTabSheet; 21 | CBPort : TComboBox; 22 | CBBaud: TComboBox; 23 | CBData: TComboBox; 24 | CBStop: TComboBox; 25 | CBParity: TComboBox; 26 | CBFlow: TComboBox; 27 | Label3: TLabel; 28 | Label4: TLabel; 29 | Label5: TLabel; 30 | Label6: TLabel; 31 | Label7: TLabel; 32 | Label8: TLabel; 33 | Label1: TLabel; 34 | Label2: TLabel; 35 | Label9: TLabel; 36 | Label10: TLabel; 37 | WTOExtraDelayEdit: TEdit; 38 | RTOCharDelayEdit: TEdit; 39 | WTOCharDelayEdit: TEdit; 40 | RTOExtraDelayEdit: TEdit; 41 | CBXTOAuto: TCheckBox; 42 | GroupBox1: TGroupBox; 43 | Label11: TLabel; 44 | Label12: TLabel; 45 | XOffCharEdit: TEdit; 46 | XOnCharEdit: TEdit; 47 | Label13: TLabel; 48 | Label14: TLabel; 49 | XOnLimitEdit: TEdit; 50 | XOffLimitEdit: TEdit; 51 | GroupBox2: TGroupBox; 52 | CBParityErrorReplacement: TCheckBox; 53 | Label15: TLabel; 54 | ParityErrorCharEdit: TEdit; 55 | CBStripNullChars: TCheckBox; 56 | Label16: TLabel; 57 | CBReport: TComboBox; 58 | Label17: TLabel; 59 | EventCharEdit: TEdit; 60 | PortBtn: TButton; 61 | ManualPort: TEdit; 62 | Label18: TLabel; 63 | procedure ValidateCharInput(Sender: TObject); 64 | procedure ValidateInteger(Sender: TObject); 65 | procedure FormCreate(Sender: TObject); 66 | procedure FormShow(Sender: TObject); 67 | procedure PortBtnClick(Sender: TObject); 68 | procedure ManualPortExit(Sender: TObject); 69 | private 70 | { Private declarations } 71 | public 72 | { Public declarations } 73 | procedure SetDLGData(SerialPortNG : TSerialPortNG); 74 | procedure GetDLGData(SerialPortNG : TSerialPortNG); 75 | end; 76 | 77 | var 78 | SerialNGAdvDLG: TSerialNGAdvDLG; 79 | 80 | function CharToEdit(C : Char):String; 81 | function EditToChar(S : String; DefaultC : Char):Char; 82 | 83 | implementation 84 | 85 | uses netstat; 86 | 87 | {$R *.DFM} 88 | 89 | procedure TSerialNGAdvDLG.ValidateCharInput(Sender: TObject); 90 | var S : String; 91 | N : LongInt; 92 | begin 93 | S := TEdit(Sender).Text; 94 | if Length(S) > 1 then 95 | begin 96 | try 97 | EditToChar(S,#0); 98 | except 99 | Application.MessageBox('Input allowed single Char or 0..255. Leave empty to hold previous Value.','Invalid Input',mb_OK); 100 | end; 101 | end 102 | else if Length(S) = 0 then 103 | Application.MessageBox('Input allowed single Char or 0..255. Leave empty to hold previous Value.','Invalid Input',mb_OK); 104 | end; 105 | 106 | procedure TSerialNGAdvDLG.ValidateInteger(Sender: TObject); 107 | var S : String; 108 | N : Cardinal; 109 | begin 110 | S := TEdit(Sender).Text; 111 | if Length(S) > 1 then 112 | begin 113 | try 114 | N := StrToInt(S); 115 | except 116 | Application.MessageBox('Invalid Input. Leave empty to hold previous Value.','Invalid Input',mb_OK); 117 | end; 118 | end; 119 | end; 120 | 121 | function CharToEdit(C : Char):String; 122 | var S : String; 123 | begin 124 | case C of 125 | #0..#31,#127,#255 : S := Format('0x%2.2x',[Byte(C)]); 126 | else 127 | S := C; 128 | end; 129 | CharToEdit := S; 130 | end; 131 | 132 | function EditToChar(S : String; DefaultC : Char):Char; 133 | var N : LongInt; 134 | C : Char; 135 | begin 136 | if Length(S) > 1 then 137 | begin 138 | if Copy(S,1,2) = '0x' then 139 | S := '$'+Copy(S,3,Length(S)); 140 | N := StrToInt(S); 141 | if (N < 0) or (N > 255) then 142 | raise ERangeError.CreateFmt( 143 | '%d is not within the valid range of %d..%d', 144 | [N, 0, 255]) 145 | else 146 | C := Char(N); 147 | end 148 | else if Length(S) = 1 then 149 | C := S[1] 150 | else 151 | C := DefaultC; 152 | EditToChar := C; 153 | end; 154 | 155 | procedure TSerialNGAdvDLG.SetDLGData(SerialPortNG : TSerialPortNG); 156 | var i : Integer; 157 | begin 158 | // Page 1 (Basic) 159 | try 160 | i := CBPort.Items.IndexOf(SerialPortNG.CommPort); 161 | except 162 | SerialPortNG.CommPort := ''; 163 | i := 0; 164 | end; 165 | if i >= 0 then 166 | CBPort.ItemIndex := i 167 | else 168 | CBPort.ItemIndex := 1; //COM2 169 | i := CBBaud.Items.IndexOf(IntToStr(SerialPortNG.BaudRate)); 170 | if i >= 0 then 171 | CBBaud.ItemIndex := i 172 | else 173 | CBBaud.Text := IntToStr(SerialPortNG.BaudRate); 174 | // CBBaud.ItemIndex := 6; // 9600 175 | i := CBData.Items.IndexOf(IntToStr(SerialPortNG.DataBits)+' Bit'); 176 | if i >= 0 then 177 | CBData.ItemIndex := i 178 | else 179 | CBData.ItemIndex := 4; // 8 Bit 180 | CBStop.ItemIndex := SerialPortNG.StopBits; 181 | CBParity.ItemIndex := SerialPortNG.ParityType; 182 | case SerialPortNG.FlowControl of 183 | fcNone : CBFlow.ItemIndex := 0; 184 | fcXON_XOFF : CBFlow.ItemIndex := 1; 185 | fcRTS_CTS : CBFlow.ItemIndex := 2; 186 | fcDSR_DTR : CBFlow.ItemIndex := 3; 187 | else 188 | CBFlow.ItemIndex := 0; 189 | end; 190 | // CBOpenPort.Checked := SerialPortNG.Active; 191 | // Page 2 Timing 192 | WTOCharDelayEdit.Text := IntToStr(SerialPortNG.WTOCharDelayTime); 193 | RTOCharDelayEdit.Text := IntToStr(SerialPortNG.RTOCharDelayTime); 194 | WTOExtraDelayEdit.Text := IntToStr(SerialPortNG.WTOExtraDelayTime); 195 | RTOExtraDelayEdit.Text := IntToStr(SerialPortNG.RTOExtraDelayTime); 196 | CBXTOAuto.Checked := SerialPortNG.XTOAuto; 197 | // Page 3 Advanced 198 | XOnCharEdit.Text := CharToEdit(SerialPortNG.XOnChar); 199 | XOffCharEdit.Text := CharToEdit(SerialPortNG.XOffChar); 200 | XOnLimitEdit.Text := IntToStr(SerialPortNG.XOnLimDiv); 201 | XOffLimitEdit.Text := IntToStr(SerialPortNG.XOffLimDiv); 202 | CBParityErrorReplacement.Checked := SerialPortNG.ParityErrorReplacement; 203 | ParityErrorCharEdit.Text := CharToEdit(SerialPortNG.ParityErrorChar); 204 | EventCharEdit.Text := CharToEdit(SerialPortNG.EventChar); 205 | CBStripNullChars.Checked := SerialPortNG.StripNullChars; 206 | if SerialPortNG.ErrorNoise > CBReport.Items.Count then 207 | CBReport.ItemIndex := CBReport.Items.Count-1 208 | else 209 | CBReport.ItemIndex := SerialPortNG.ErrorNoise; 210 | end; 211 | 212 | procedure TSerialNGAdvDLG.GetDLGData(SerialPortNG : TSerialPortNG); 213 | begin 214 | SerialPortNG.CommPort := CBPort.Items[CBPort.ItemIndex]; 215 | // SerialPortNG.BaudRate := StrToIntDef(CBBaud.Items[CBBaud.ItemIndex],9600); 216 | SerialPortNG.BaudRate := StrToIntDef(CBBaud.Text,9600); 217 | SerialPortNG.DataBits := StrToIntDef(Copy(CBData.Items[CBData.ItemIndex],1,1),8); 218 | SerialPortNG.StopBits := CBStop.ItemIndex; 219 | SerialPortNG.ParityType := CBParity.ItemIndex; 220 | SerialPortNG.FlowControl := BasicFlowModes[CBFlow.ItemIndex]; 221 | 222 | // Page 2 Timing 223 | SerialPortNG.XTOAuto := CBXTOAuto.Checked; 224 | if not CBXTOAuto.Checked then 225 | begin 226 | SerialPortNG.WTOCharDelayTime := StrToInt(WTOCharDelayEdit.Text); 227 | SerialPortNG.RTOCharDelayTime := StrToInt(RTOCharDelayEdit.Text); 228 | SerialPortNG.WTOExtraDelayTime := StrToInt(WTOExtraDelayEdit.Text); 229 | SerialPortNG.RTOExtraDelayTime := StrToInt(RTOExtraDelayEdit.Text); 230 | end; 231 | 232 | // Page 3 Advanced 233 | SerialPortNG.XOnChar := EditToChar(XOnCharEdit.Text,SerialPortNG.XOnChar); 234 | SerialPortNG.XOffChar := EditToChar(XOffCharEdit.Text,SerialPortNG.XOffChar); 235 | SerialPortNG.XOnLimDiv := StrToInt(XOnLimitEdit.Text); 236 | SerialPortNG.XOffLimDiv := StrToInt(XOffLimitEdit.Text); 237 | SerialPortNG.ParityErrorReplacement := CBParityErrorReplacement.Checked; 238 | SerialPortNG.ParityErrorChar := EditToChar(ParityErrorCharEdit.Text,SerialPortNG.ParityErrorChar); 239 | SerialPortNG.EventChar := EditToChar(EventCharEdit.Text,SerialPortNG.EventChar); 240 | SerialPortNG.StripNullChars := CBStripNullChars.Checked; 241 | SerialPortNG.ErrorNoise := CBReport.Itemindex; 242 | // Try to Open the Port if wished 243 | // SerialPortNG.Active := CBOpenPort.Checked; 244 | end; 245 | 246 | procedure TSerialNGAdvDLG.FormCreate(Sender: TObject); 247 | begin 248 | GetCommNames(CBPort.Items); 249 | end; 250 | 251 | procedure TSerialNGAdvDLG.FormShow(Sender: TObject); 252 | begin 253 | GetCommNames(CBPort.Items); 254 | end; 255 | 256 | procedure TSerialNGAdvDLG.PortBtnClick(Sender: TObject); 257 | begin 258 | net.ShowURL('http://benlo.com/esp8266/index.html#faq'); 259 | end; 260 | 261 | procedure TSerialNGAdvDLG.ManualPortExit(Sender: TObject); 262 | begin 263 | ManualPort.Text := UpperCase(ManualPort.Text); 264 | if (ManualPort.Text <> '') and (CBPort.Items.IndexOf(ManualPort.Text) < 0) then 265 | begin 266 | CBPort.Items.Add(ManualPort.Text); 267 | end; 268 | end; 269 | 270 | end. 271 | -------------------------------------------------------------------------------- /Source/supertim.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GeoNomad/LuaLoader/e42164e7285a0fdcb3ee124e8a680eaf9518d701/Source/supertim.zip -------------------------------------------------------------------------------- /examples/AutoConnect.lua: -------------------------------------------------------------------------------- 1 | -- choose the strongest open AP available and connect to it 2 | -- Autoconnect to strongest AP 3 | -- tested with NodeMcu 0.9.2 build 20141212 4 | 5 | ConnStatus = nil 6 | function ConnStatus(n) 7 | 8 | status = wifi.sta.status() 9 | uart.write(0,' '..status) 10 | local x = n+1 11 | if (x < 50) and ( status < 5 ) then 12 | tmr.alarm(0,100,0,function() ConnStatus(x) end) 13 | else 14 | if status == 5 then 15 | print('\nConnected as '..wifi.sta.getip()) 16 | else 17 | print("\nConnection failed") 18 | end 19 | end 20 | end 21 | 22 | best_ssid = nil 23 | function best_ssid(ap_db) 24 | 25 | local min = 100 26 | for k,v in pairs(ap_db) do 27 | if tonumber(v) < min then 28 | min = tonumber(v) 29 | ssid = k 30 | end 31 | end 32 | end 33 | 34 | best = nil 35 | function best(aplist) 36 | 37 | ssid = nil 38 | print("\nAvailable Open Access Points:\n") 39 | for k,v in pairs(aplist) do print(k..' '..v) end 40 | 41 | ap_db = nil 42 | ap_db = {} 43 | if nil ~= next(aplist) then 44 | 45 | for k,v in pairs(aplist) do 46 | if '0' == string.sub(v,1,1) then 47 | ap_db[k] = string.match(v, '-(%d+),') 48 | end 49 | end 50 | 51 | if nil ~= next(ap_db) then 52 | best_ssid(ap_db) 53 | end 54 | end 55 | if nil ~= ssid then 56 | print("\nBest SSID: ".. ssid) 57 | wifi.sta.config(ssid,"") 58 | print("\nConnecting to "..ssid) 59 | ConnStatus(0) 60 | else 61 | print("\nNo available open APs") 62 | end 63 | end 64 | 65 | 66 | wifi.setmode(wifi.STATION) 67 | wifi.sta.getap(function(t) best(t) end) 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /examples/Client with DNS.lua: -------------------------------------------------------------------------------- 1 | -- Simple webclient with DNS 2 | 3 | -- http://www.esp8266.com/viewtopic.php?f=19&t=691 4 | 5 | -- untested 6 | 7 | 8 | 9 | file.remove("client.lua") 10 | file.remove("client.lua") 11 | file.open("client.lua","w+") 12 | host="www.ernstc.dk" 13 | ipnr=0 14 | file.writeline([[tmr.alarm(30000, 1, function()]]) 15 | file.writeline([[sk=net.createConnection(net.TCP, 0)]]) 16 | file.writeline([[sk:dns(host,function(conn,ip) ]]) 17 | file.writeline([[ipnr=ip]]) 18 | file.writeline([[end)]]) 19 | file.writeline([[conn=net.createConnection(net.TCP, 0) ]]) 20 | file.writeline([[ conn:on("receive", function(conn, payload) print(payload) end )]]) 21 | file.writeline([[ conn:connect(80,ipnr)]]) 22 | file.writeline([[ conn:send("GET /esp.php?tekst="..adc.read(0) .."&heap="..node.heap().." HTTP/1.1\r\nHost: "..host.."\r\n"]]) 23 | file.writeline([[ .."Connection: keep-alive\r\nAccept: */*\r\n\r\n")]]) 24 | file.writeline([[end)]]) 25 | file.close() 26 | 27 | -------------------------------------------------------------------------------- /examples/Count Interrupts.lua: -------------------------------------------------------------------------------- 1 | -- Count pulses on GPIO2 2 | 3 | count = 0 4 | delay = 0 5 | gpio.mode(9,gpio.INT,gpio.PULLUP) 6 | 7 | function counter(level) 8 | x = tmr.now() 9 | if x > delay then 10 | delay = tmr.now()+250000 11 | count = count + 1 12 | print(count) 13 | end 14 | end 15 | gpio.trig(9, "down",counter) 16 | 17 | -------------------------------------------------------------------------------- /examples/DS18B20 One Wire Temperature server.lua: -------------------------------------------------------------------------------- 1 | -- 1 wire to server for DS18B20 temperature sensor 2 | 3 | -- http://www.esp8266.com/viewtopic.php?f=19&t=752 4 | -- http://www.esp8266.com/viewtopic.php?f=19&t=757 5 | 6 | -- untested 7 | 8 | pin = 9 9 | ow.setup(pin) 10 | 11 | counter=0 12 | lasttemp=-999 13 | 14 | function bxor(a,b) 15 | local r = 0 16 | for i = 0, 31 do 17 | if ( a % 2 + b % 2 == 1 ) then 18 | r = r + 2^i 19 | end 20 | a = a / 2 21 | b = b / 2 22 | end 23 | return r 24 | end 25 | 26 | function getTemp() 27 | addr = ow.reset_search(pin) 28 | repeat 29 | tmr.wdclr() 30 | 31 | if (addr ~= nil) then 32 | crc = ow.crc8(string.sub(addr,1,7)) 33 | if (crc == addr:byte(8)) then 34 | if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then 35 | ow.reset(pin) 36 | ow.select(pin, addr) 37 | ow.write(pin, 0x44, 1) 38 | tmr.delay(1000000) 39 | present = ow.reset(pin) 40 | ow.select(pin, addr) 41 | ow.write(pin,0xBE, 1) 42 | data = nil 43 | data = string.char(ow.read(pin)) 44 | for i = 1, 8 do 45 | data = data .. string.char(ow.read(pin)) 46 | end 47 | crc = ow.crc8(string.sub(data,1,8)) 48 | if (crc == data:byte(9)) then 49 | t = (data:byte(1) + data:byte(2) * 256) 50 | if (t > 32768) then 51 | t = (bxor(t, 0xffff)) + 1 52 | t = (-1) * t 53 | end 54 | t = t * 625 55 | lasttemp = t 56 | print("Last temp: " .. lasttemp) 57 | end 58 | tmr.wdclr() 59 | end 60 | end 61 | end 62 | addr = ow.search(pin) 63 | until(addr == nil) 64 | end 65 | 66 | srv=net.createServer(net.TCP) 67 | srv:listen(8080,function(conn) 68 | getTemp() 69 | t1 = lasttemp / 10000 70 | t2 = (lasttemp >= 0 and lasttemp % 10000) or (10000 - lasttemp % 10000) 71 | conn:send("Temperature: " .. t1 .. "." .. string.format("%04d", t2) .. "\n\n") 72 | conn:on("sent",function(conn) conn:close() end) 73 | end) 74 | 75 | -------------------------------------------------------------------------------- /examples/Find Hot Spots/autoconnect.lua: -------------------------------------------------------------------------------- 1 | -- choose the strongest open AP available and connect to it 2 | -- tested with NodeMcu 0.9.2 build 20150123 3 | 4 | Tstart = tmr.now() 5 | 6 | ConnStatus = nil 7 | function ConnStatus(n) 8 | status = wifi.sta.status() 9 | uart.write(0,' '..status) 10 | local x = n+1 11 | if (x < 50) and ( status < 5 ) then 12 | tmr.alarm(0,100,0,function() ConnStatus(x) end) 13 | else 14 | if status == 5 then 15 | print('\nConnected as '..wifi.sta.getip()) 16 | dofile('httpget.lua') 17 | else 18 | print("\nConnection failed") 19 | end 20 | end 21 | end 22 | 23 | best_ssid = nil 24 | function best_ssid(ap_db) 25 | local min = 100 26 | ssid = nil 27 | for k,v in pairs(ap_db) do 28 | if tonumber(v) < min then 29 | min = tonumber(v) 30 | ssid = k 31 | end 32 | end 33 | 34 | gpio.write(red,gpio.LOW) 35 | gpio.write(blue,gpio.HIGH) 36 | return min 37 | end 38 | 39 | 40 | strongest = nil 41 | function strongest(aplist) 42 | print("\nAvailable Open Access Points:\n") 43 | for k,v in pairs(aplist) do print(k..' '..v) end 44 | 45 | ap_db = {} 46 | if next(aplist) then 47 | for k,v in pairs(aplist) do 48 | if '0' == string.sub(v,1,1) then 49 | ap_db[k] = string.match(v, '-(%d+),') 50 | end 51 | end 52 | signal = -best_ssid(ap_db) 53 | end 54 | if ssid then 55 | print("\nBest SSID: ".. ssid) 56 | wifi.sta.config(ssid,"") 57 | print("\nConnecting to "..ssid) 58 | ConnStatus(0) 59 | else 60 | print("\nNo available open APs") 61 | ssid = '' 62 | end 63 | end 64 | 65 | 66 | wifi.setmode(wifi.STATION) 67 | wifi.sta.getap(function(t) strongest(t) end) 68 | -------------------------------------------------------------------------------- /examples/Find Hot Spots/httpget.lua: -------------------------------------------------------------------------------- 1 | -- tested on NodeMCU 0.9.5 build 20150123 2 | -- sends info to http://benlo.com/esp8266/test.php 3 | 4 | print('httpget.lua started') 5 | 6 | conn = nil 7 | conn=net.createConnection(net.TCP, 0) 8 | 9 | -- show the retrieved web page 10 | 11 | conn:on("receive", function(conn, payload) 12 | print(payload) 13 | if string.match(payload, 'Test Number', 0) then 14 | gpio.write(blue,gpio.LOW) 15 | gpio.write(green,gpio.HIGH) 16 | else 17 | gpio.write(red,gpio.HIGH) 18 | end 19 | end) 20 | 21 | -- when connected, request page (send parameters to a script) 22 | conn:on("connection", function(conn, payload) 23 | print('\nConnected') 24 | conn:send("GET /esp8266/test.php?" 25 | .."TIME="..(tmr.now()-Tstart) 26 | .."&HEAP="..node.heap() 27 | .."&SSID="..ssid 28 | .."&SIG="..signal 29 | .."&ID="..node.chipid()..'.'..node.flashid() 30 | .." HTTP/1.1\r\n" 31 | .."Host: benlo.com\r\n" 32 | .."Connection: close\r\n" 33 | .."Accept: */*\r\n" 34 | .."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n" 35 | .."\r\n") 36 | end) 37 | -- when disconnected, let it be known 38 | conn:on("disconnection", function(conn, payload) 39 | print('\nDisconnected') 40 | tmr.alarm(0,5000,0,function() 41 | gpio.write(red,gpio.LOW) 42 | gpio.write(green,gpio.LOW) 43 | gpio.write(blue,gpio.LOW) 44 | end) 45 | end) 46 | 47 | conn:connect(80,'benlo.com') 48 | -------------------------------------------------------------------------------- /examples/Find Hot Spots/init.lua: -------------------------------------------------------------------------------- 1 | tmr.alarm(3,30000,1,function() dofile('init.lua') end) 2 | 3 | red = 8 4 | blue = 7 5 | green = 6 6 | 7 | 8 | gpio.mode(red,gpio.OUTPUT) 9 | gpio.mode(green,gpio.OUTPUT) 10 | gpio.mode(blue,gpio.OUTPUT) 11 | gpio.write(red,gpio.HIGH) 12 | gpio.write(green,gpio.LOW) 13 | gpio.write(blue,gpio.LOW) 14 | 15 | tmr.alarm(0,1000,0,function() dofile('autoconnect.lua') end) 16 | 17 | -------------------------------------------------------------------------------- /examples/Kankun WiFi Plug/README.md: -------------------------------------------------------------------------------- 1 | Kankun smart plug outlet controller 2 | 3 | Full tutorial is at http://benlo.com/esp8266/KankunSmartPlug.html 4 | 5 |