-
Notifications
You must be signed in to change notification settings - Fork 10
/
aREST.lua
151 lines (117 loc) · 3 KB
/
aREST.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
--[[
aREST Library for the ESP8266 chip using the NodeMCU firmware
See the README file for more details.
Written in 2015 by Marco Schwartz under a GPL license.
Version 0.1
Changelog:
Version 0.1: First working version of the library
--]]
-- Module declaration
local aREST = {}
-- Attributes
local _name
local _id
function aREST.set_id(id)
_id = id
end
function aREST.set_name(name)
_name = name
end
function aREST.handle(conn, request)
-- Variables
local pin
local command
local value
local answer = {}
local mode
local variables = {}
-- ID and name
answer['id'] = _id
answer["name"] = _name
-- Variables
variables["temperature"] = 30
-- Find start
local e = string.find(request, "/")
local request_handle = string.sub(request, e + 1)
-- Cut end
e = string.find(request_handle, "HTTP")
request_handle = string.sub(request_handle, 0, (e-2))
-- Find mode
e = string.find(request_handle, "/")
if e == nil then
mode = request_handle
else
mode = string.sub(request_handle, 0, (e-1))
-- Find pin & command
request_handle = string.sub(request_handle, (e+1))
e = string.find(request_handle, "/")
if e == nil then
pin = request_handle
pin = tonumber(pin)
else
pin = string.sub(request_handle, 0, (e-1))
pin = tonumber(pin)
request_handle = string.sub(request_handle, (e+1))
command = request_handle
end
end
-- Debug output
print('Mode: ', mode)
print('Pin: ', pin)
print('Command: ', command)
-- Apply command
if pin == nil then
for key,value in pairs(variables) do
if key == mode then answer[key] = value end
end
end
if mode == "mode" then
if command == "o" then
gpio.mode(pin, gpio.OUTPUT)
answer['message'] = "Pin D" .. pin .. " set to output"
elseif command == "i" then
gpio.mode(pin, gpio.INPUT)
answer['message'] = "Pin D" .. pin .. " set to input"
elseif command == "p" then
pwm.setup(pin, 100, 0)
pwm.start(pin)
answer['message'] = "Pin D" .. pin .. " set to PWM"
end
end
if mode == "digital" then
if command == "0" then
gpio.write(pin, gpio.LOW)
answer['message'] = "Pin D" .. pin .. " set to 0"
elseif command == "1" then
gpio.write(pin, gpio.HIGH)
answer['message'] = "Pin D" .. pin .. " set to 1"
elseif command == "r" then
value = gpio.read(pin)
answer['return_value'] = value
elseif command == nil then
value = gpio.read(pin)
answer['return_value'] = value
end
end
if mode == "analog" then
if command == nil then
value = adc.read(pin)
answer['return_value'] = value
else
pwm.setduty(pin, command)
answer['message'] = "Pin D" .. pin .. " set to " .. command
end
end
conn:send("HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n" .. table_to_json(answer) .. "\r\n")
end
function table_to_json(json_table)
local json = ""
json = json .. "{"
for key,value in pairs(json_table) do
json = json .. "\"" .. key .. "\": \"" .. value .. "\", "
end
json = string.sub(json, 0, -3)
json = json .. "}"
return json
end
return aREST