diff --git a/lua_examples/http_server b/lua_examples/http_server new file mode 100644 index 00000000..c9ad275a --- /dev/null +++ b/lua_examples/http_server @@ -0,0 +1,121 @@ +-- +-- Simple NodeMCU web server (done is a not so nodeie fashion :-) +-- +-- Highly modified by Bruce Meacham, based on work by Scott Beasley 2015 +-- Open and free to change and use. Enjoy. [Beasley/Meacham 2015] +-- +-- Meacham Update: I streamlined/improved the parsing to focus on simple HTTP GET request and their simple parameters +-- Also added the code to drive a servo/light. Comment out as you see fit. +-- +-- Usage: +-- Change SSID and SSID_PASSPHRASE for your wifi network +-- Download to NodeMCU +-- node.compile("http_server.lua") +-- dofile("http_server.lc") +-- When the server is esablished it will output the IP address. +-- http://{ip address}/?s0=1200&light=1 +-- s0 is the servo position (actually the PWM hertz), 500 - 2000 are all good values +-- light chanel high(1)/low(0), some evaluation boards have LEDs pre-wired in a "pulled high" confguration, so '0' ground the emitter and turns it on backwards. +-- +-- Add to init.lua if you want it to autoboot. +-- + +-- Your Wifi connection data +local SSID = "YOUR WIFI SSID" +local SSID_PASSWORD = "YOUR SSID PASSPHRASE" + +-- General setup +local pinLight = 2 -- this is GPIO4 +gpio.mode(pinLight,gpio.OUTPUT) +gpio.write(pinLight,gpio.HIGH) + +servo = {} +servo.pin = 4 --this is GPIO2 +servo.value = 1500 +servo.id = "servo" +gpio.mode(servo.pin, gpio.OUTPUT) +gpio.write(servo.pin, gpio.LOW) + +-- This alarm drives the servo +tmr.alarm(0,10,1,function() -- 50Hz + if servo.value then -- generate pulse + gpio.write(servo.pin, gpio.HIGH) + tmr.delay(servo.value) + gpio.write(servo.pin, gpio.LOW) + end +end) + +local function connect (conn, data) + local query_data + + conn:on ("receive", + function (cn, req_data) + params = get_http_req (req_data) + cn:send("HTTP/1.1 200/OK\r\nServer: NodeLuau\r\nContent-Type: text/html\r\n\r\n") + cn:send ("