Lua ESP8266 脚本期望有额外的 =

我正在尝试用我的 ESP8266 测量接近传感器,但是我正在使用的测试代码持续失败。 每当我运行代码时,我会收到一个错误:motion sensor.lua:1: '=' expected near 'int'

我还应该提到我正在使用 ESPlorer v0.2.0

const int PIRSensorOutPin = 2;    //PIR Sensor OUT Pin
void setup() {
   Serial.begin(9600);
  pinMode(PIRSensorOutPin, INPUT);
}
void loop()
{
    if (digitalRead(PIRSensorOutPin) == LOW)
    {
       Serial.println("Person detected!");    //Print to serial monitor
    }
    else {;}
 }

我做错了什么?

点赞
用户2858170
用户2858170
The Lua interpreter doesn't understand C++.

你正在运行 NodeMCU 固件,该固件运行 Lua 文件。但是你试图运行 Arduino C++ 代码。这不会起作用。要运行此代码,需要在 Arduino IDE 中添加 ESP8266 支持,编译代码并将其闪存到 ESP 上。

或者,使用 Lua 编写你的代码。

[https://github.com/esp8266/Arduino](https://github.com/esp8266/Arduino)

[https://www.nodemcu.com/index\_en.html](https://www.nodemcu.com/index_en.html)
2020-01-27 07:10:17
用户4984564
用户4984564

我哪里出了问题?

使用了错误的编程语言。

NodeMCU想要运行 Lua 代码,而您却提供了 C 代码,这是行不通的。

我该如何解决?_(暗示)_

您可以使用 Arduino IDE 写 ESP8266 的 C++ 代码,但由于您已经准备好了运行 Lua 代码的一切,我建议您直接使用 Lua。

您提供的 C 代码可以使用 NodeMCU api 重写为 Lua,如下所示:

local pin = 2 -- I/O 引脚号
local type = "down" -- 下降沿触发

-- https://nodemcu.readthedocs.io/en/master/modules/gpio/#gpiotrig
gpio.trig(pin, type, function()
   print("检测到运动,开始消灭!")
end)
2020-01-27 12:23:52