1

When compiling the following lua script in Arduino 1.8.16 to be loaded to NodeMCU 8266 MOD 12-F I get a failure message (attached after the script). I am new in the whole Arduino (and SW overall..) topics and just trying to build some DYI based on the NodeMCU. I would appreciate an advice on what is wrong or missing here.. Thanks a lot in advance!

wifi.setmode (wifi.STATION) 
wifi.sta.config ("BudSpencer", "PaPAT2xx1", 1) 
wifi.sta.connect () 
print (wifi.sta.getip ()) 
bell1 = 2 
pin1 = 3 
led1 = 4 
gpio.mode (pin1, gpio.OUTPUT) 
gpio.mode (led1, gpio.OUTPUT) 
gpio.mode (bell1, gpio.INPUT, gpio.PULLUP); 
gpio.write (pin1, gpio.LOW) 
gpio.write (led1, gpio.HIGH) 
srv = net.createServer (net.TCP) srv: listen (80, function (conn) 
     conn: on ("receive", function ( client, request) 
         local buf = ""; 
         local , , method, path, vars = string.find (request, "([AZ] +) HTTP"). (+)? (+.); 
         if (method == nil) then 
             ,, method, path = string.find (request, "([AZ] +) (. +) HTTP"); 
         end 
         local _GET = {} 
         if (vars ~ = nil) then 
             for k, v in string.gmatch (vars, "(% w +) = (% w +) & *") do 
                 _GET [k] = v 
             end 
         end 
         buf = buf .. " 
NodeMCU Door Opener 
"; 
         buf = buf .. "Door OPEN
 "; 
         local _on, _off = "", "" 
         if (_GET.pin == "OPEN") then 
             gpio.write (led1, gpio.LOW) 
             gpio.write (pin1, gpio.HIGH) 
             tmr.delay (1000 * 2000) 
             gpio.write (led1, gpio.

         end 
         client: send (buf); 
         client: close (); 
         collectgarbage (); 
     end) 
end) 
lock = 0 
function callback () 
     if lock == 0 then 
         lock = 1 
         print ("called") 
         print ("notify ()") 
         http.post ('http://192.168.178.5:9999/bell ', 
             ' Content-Type: application / json \ r \ n ', 
             ' {"hello": "world"} ', 
             nil) 
         lock = 0 
     end 
end 
gpio.trig (bell1, "up", callback)

The failure message - Arduino: 1.8.16 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Disabled (new aborts on oom), Disabled, All SSL ciphers (most compatible), 32KB cache + 32KB IRAM (balanced), Use pgm_read macros for IRAM/PROGMEM, 4MB (FS:2MB OTA:~1019KB), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"

sketch_oct05a:26:21: error: missing terminating " character

26 | buf = buf.."

  |                     ^ 

sketch_oct05a:28:2: error: missing terminating " character

28 | ";

  |  ^~

sketch_oct05a:29:21: error: missing terminating " character

29 | buf = buf.."Door OPEN

  |                     ^~~~~~~~~~

sketch_oct05a:30:1: error: missing terminating " character

30 | ";

  | ^~

sketch_oct05a:1:1: error: 'wifi' does not name a type

1 | wifi.setmode(wifi.STATION)

  | ^~~~

sketch_oct05a:11:2: error: 'gpio' does not name a type

11 | gpio.write(pin1, gpio.LOW)

  |  ^~~~

sketch_oct05a:16:10: error: 'local' does not name a type

16 | local , , method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");

  |          ^~~~~

sketch_oct05a:17:10: error: expected unqualified-id before 'if'

17 | if(method == nil) then

  |          ^~

sketch_oct05a:19:10: error: 'end' does not name a type

19 | end

  |          ^~~

sketch_oct05a:21:10: error: expected unqualified-id before 'if'

21 | if (vars ~= nil)then

  |          ^~

sketch_oct05a:40:16: error: found ':' in nested-name-specifier, expected '::'

40 | client:close();

  |                ^

  |                ::

sketch_oct05a:40:10: error: 'client' does not name a type

40 | client:close();

  |          ^~~~~~

sketch_oct05a:41:26: error: expected constructor, destructor, or type conversion before ';' token

41 | collectgarbage();

  |                          ^

sketch_oct05a:42:6: error: 'end' does not name a type

42 | end)

  |      ^~~

exit status 1

missing terminating " character

This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.

Igor
  • 11
  • 1
  • 1
    "The failure message - Arduino: 1.8.16 (Windows 10)" - are you sure you are using the right tools? To my knowledge the Arduino IDE is not able to process Lua scripts. – Marcel Stör Oct 08 '21 at 06:23
  • in addition to my answer: if you want to use your NodeMCU hardware (just a ESP development board), one option is to use the Arduino Software. Then you program your board in C++. https://create.arduino.cc/projecthub/electropeak/getting-started-w-nodemcu-esp8266-on-arduino-ide-28184f You can also flash the NodeMCU firmware, then you can upload Lua scripts that the firmware will execute. https://nodemcu.readthedocs.io/en/latest/flash/ – Piglet Oct 08 '21 at 06:45

2 Answers2

1

I think you have to use a more specialized defining for Datatype string here.
I mean that...

buf = buf .. " 
NodeMCU Door Opener 
";

...has to be...

buf = buf .. [[ 
NodeMCU Door Opener 
]];

...here.

It ([[ and ]]) for a string is like the HTML <pre></pre> Element.
Because you can put in what you want.
Like Source Code of other Programming Languages for example.
Also the \n and similar (Whitespace) will not be interpreted and will putted out as is.
Especially in your Case there are Newlines in it what is probably what the Errormessage moans.

Or simply define...

buf = buf .. "\nNodeMCU Door Opener\n";

Same for: buf = buf .. "Door OPEN\n";

koyaanisqatsi
  • 2,585
  • 2
  • 8
  • 15
0

I'm not sure what you're doing here. Arduino is not a Lua IDE. While you can program NodeMCU hardware with the Arduino IDE in C++, the NodeMCU firmware runs Lua scripts. You cannot compile Lua code in Arduino software. That doesn't make sense.

I think you have some big misconception here. That's why you get all these type and name errors btw.

Just to name a few issues in Lua your code:

buf = buf .. " 
NodeMCU Door Opener 
"; 
         buf = buf .. "Door OPEN
 "; 

Here you violate the following rule from Lua 5.4 Reference Manual 3.1 Lexical Conventions:

A short literal string cannot contain unescaped line breaks

Use escaped line breaks \n or long literal strings

Literal strings can also be defined using a long format enclosed by long brackets. We define an opening long bracket of level n as an opening square bracket followed by n equal signs followed by another opening square bracket. So, an opening long bracket of level 0 is written as [[, an opening long bracket of level 1 is written as [=[, and so on. A closing long bracket is defined similarly; for instance, a closing long bracket of level 4 is written as ]====]. A long literal starts with an opening long bracket of any level and ends at the first closing long bracket of the same level. It can contain any text except a closing bracket of the same level. Literals in this bracketed form can run for several lines, do not interpret any escape sequences, and ignore long brackets of any other level. Any kind of end-of-line sequence (carriage return, newline, carriage return followed by newline, or newline followed by carriage return) is converted to a simple newline. When the opening long bracket is immediately followed by a newline, the newline is not included in the string.

local , , method, path, vars = string.find (request, "([AZ] +) HTTP"). (+)? (+.)

Remove the commas between local and method.

Also your string pattern is not ok. You need to escape magic characters with %

gpio.write (led1, gpio. is incomplete.

Piglet
  • 27,501
  • 3
  • 20
  • 43