Lua ESP8266 script expecting extra = - lua

I am trying to test a proximity sensor with my ESP8266, however the test code I am using keeps failing.
Whenever I run the code, I get an error: motion sensor.lua:1: '=' expected near 'int'
I should also mention I am using 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 {;}
}
What am I doing wrong?

The Lua interpreter doesn't understand C++.
You're running NodeMCU firmware which runs Lua files. But you're trying to run Arduino C++ code. That's not going to work. To run this code you would have to add ESP8266 support to your Arduino IDE, compile your code and flash it onto the ESP.
Alternatively write your code in Lua.
https://github.com/esp8266/Arduino
https://www.nodemcu.com/index_en.html

What am I doing wrong?
Using the wrong programming language.
NodeMCU wants to run Lua code and you're giving it C code instead, which just can't work.
How do I fix it? (implied)
You can use the arduino IDE to write C++ code for ESP8266, but since you already seem to have everything set up to run Lua code, I suggest just using that instead.
The C code you provided could be rewritten into Lua using the NodeMCU api like this:
local pin = 2 -- The number of the I/O Pin
local type = "down" -- Trigger on falling edge
-- https://nodemcu.readthedocs.io/en/master/modules/gpio/#gpiotrig
gpio.trig(pin, type, function()
print("Movement detected, proceding to exterminate!")
end)

Related

How to connect a thermal printer to an ESP32?

I want to attach my GOOJPRT Thermal Printer (I believe model QR701, communication RS232) to my ESP32 but I cannot seem to get them working.
I tried all the Adafruit Thermal Printer library examples but get the same error each and every time:
"Error compiling for board ESP32 Dev Module."
I guess the libraries are not meant for the ESP32.
I also tried the "Thermal Printer Library" by Larry Bank (which should be compatible with the ESP32 according to its github docs) but there I cannot figure out how to connect the wires of thermal printer to the ESP32 correctly.
Of course, I do not ask for a specific solution, I am just looking for someone to point me in the right direction!
This is an image of the exact thermal printer I have
Full error message from Adafruit Thermal Printer examples:
C:\Users\Thomas\Documents\Arduino\libraries\SoftwareSerial-master\SoftwareSerial.cpp:41:27: fatal error: avr/interrupt.h: No such file or directory
compilation terminated.
Multiple libraries were found for "Adafruit_Thermal.h"
Used: C:\Users\Thomas\Documents\Arduino\libraries\Adafruit_Thermal_Printer_Library
Not used: C:\Users\Thomas\Documents\Arduino\libraries\Adafruit-Thermal-Printer-Library-master
Multiple libraries were found for "SoftwareSerial.h"
Used: C:\Users\Thomas\Documents\Arduino\libraries\SoftwareSerial-master
Not used: C:\Users\Thomas\Documents\Arduino\libraries\EspSoftwareSerial
exit status 1
Error compiling for board ESP32 Dev Module
You need to use the <HardwareSerial.h> library. SoftwareSerial is for Arduino boards.
"Thermal Printer Library" by Larry Bank is for GOOJPRT PT-210 and use Bluetooth. Won't work for qr-701.
Instead of using Adafruit Library, you can try with this:
ThermalPrinter
Quick start:
Import libraries:
#include "TPrinter.h"
#include <HardwareSerial.h>
Set baudrate and pins.
const int printerBaudrate = 9600; // or 19200 usually
const byte rxPin = 16; // check datasheet of your board
const byte txPin = 17; // check datasheet of your board
const byte dtrPin = 27; // optional
const byte rsePin = 4; // direction of transmission, max3485
You need to use boad with max3485(for 3V3 logic lvl) or similar, if you have printer with rs232.
Necessary in my case. I use board UART - RS485 3,3V - ARK/RJ11 - Waveshare 4777
Init
HardwareSerial mySerial(1);
Tprinter myPrinter(&mySerial, printerBaudrate);
void setup() {
micros();
mySerial.begin(printerBaudrate, SERIAL_8N1, rxPin, txPin); // must be 8N1 mode
pinMode(rsePin, OUTPUT); // optional
digitalWrite(rsePin, HIGH); // optional
// myPrinter.enableDtr(dtrPin, LOW); // optional
myPrinter.begin();
}

ZeroBrane remote debugging embedded script

I work on image processing application in Embarcadero C++ Builder XE10.2 that executes Lua scripts. I use LuaJIT with FFI to share image data. Everything works fine. I've downloaded ZeroBrane studio and tried to see if I can debug scripts executed from "host" C++ application, so I've included
package.path = package.path .. ";C:/Portable_App/ZeroBraneStudio/lualibs/mobdebug/?.lua"
package.cpath = package.cpath .. ";C:/Portable_App/ZeroBraneStudio/bin/clibs/?.dll"
require("mobdebug").start()
before any function in the script is called. However, when the script is loaded and executed (on C++ side):
FResult = lua_pcall(FLs, 0, 0, 0);
host program crashes with "floating point division by zero" exception. It crashes on
require("mobdebug").start()
Without this line script works OK. Any clue?
It's not possible to tell what may be going wrong based on the provided information, but you can try to get the stack trace (using this SO answers), which should provide more information about what's leading to the error.
The only division that I'm aware of is in the serialization code that uses tostring(1/0) code to generate platform-independent NaN values. Would this lead to "floating point division by zero" error in your Lua configuration?
(Update to include the solution mentioned in comments) The issue was related to BCC compiler settings on how to handle FPU exceptions. One way is to manipulate FP control: _clear87(); _control87(MCW_EM, MCW_EM); or to set arithmetic exception mask: SetExceptionMask(exAllArithmeticExceptions);.

ROBLOX Studio: How do I run this Lua Bytecode VM?

For me, I want to learn how VM's work and if they can only be ran on Mac. I've found a bytecode vm (from lua) in roblox studio as one of the scripts. I'm very confused on how to use it, its nothing like i've used before. Here it is:
https://web.roblox.com/library/117513593/EpicLua-Lua-5-1-VM
Also if you could tell me more about VM's that would also help me during this process.
Looks like it's plug-and-play.
Require the module in a script/command line on Roblox. It will return a function which you can call with a string and desired environment (optional), which again returns a function you can run to run the string-code.
Example:
local LoadString = require(module)
local func = LoadString("print('Hello world!')")
func() -- prints "Hello world!"

ZBar processor and delphi

Ok so I have been trying to get bar code scanning to work in a delphi application for the last 3 weeks now. Ive been directed to this example but that example uses other librarys like imagemagika and is a console application. I am looking for a vcl forms application.
Here is some code I have written to try and see if I can get the ZBar processor to work in delphi :
// Create Processor
processor := zbar_processor_create(0);
zbar_processor_set_config(processor, ZBAR_NONE, ZBAR_CFG_ENABLE, 1);
// Initialize processor
zbar_processor_init(processor, {what do I put here ?}, 1);
// Setup a callback
{I dont know what do here}
// Enable preview window
zbar_processor_set_visible(processor, 1);
zbar_processor_set_active(processor, 1);
This code is based on a example in C that I found here : https://github.com/ZBar/ZBar/blob/master/examples/processor.c
as well as the documentation over here :
http://zbar.sourceforge.net/api/zbar_8h.html#c-processor
The zbar window opens but it does not show the video feed because I parsed nil as a paramater in the initialize step. In the example they have this C code but I have no idea what it means :
const char *device = "/dev/video0";
/* initialize the Processor */
if(argc > 1)
device = argv[1];
zbar_processor_init(proc, device, 1);
If I parse '/dev/video0' instead of nil the video feed still doesn't show. So I guess my question is what do I need to parse in zbar_processor_init() function ?
I also dont know how to set up a callback function that will be called once a result is found. How would I go about doing this ?
Thanks in advance,
Kobus
argc is the number of parameters passed in the command line and argv fetches them. dev/video is linux style device. Try con:
zbar_processor_init(processor, 'con:', 1)
Con: is the console. Com1: serial port 1, Aux: auxiliary port - probably usb, Prn: the printer Lpt: the line printer.

"attempt to call global 'tonumber' (a nil value)" in Lua, embedded (in VLC)

I use VLC media player 1.1.9 on Ubuntu 11.04. I'm trying to experiment with lua extensions for VLC; so I've added the file test.lua in ~/.local/share/vlc/lua/extensions/, which has only these two lines:
fps="25.000"
frame_duration=1/tonumber(fps)
When I run vlc with verbose output for debugging, I get (edited to split on multiple lines:):
$ vlc --verbose 2
...
[0xa213874] lua generic warning: Error loading script
~/.local/share/vlc/lua/extensions/test.lua:
.../.local/share/vlc/lua/extensions/test.lua:2:
attempt to call global 'tonumber' (a nil value)
...
Now, as far as I know, tonumber as function is part of Lua5.1 proper (Lua 5.1 Reference Manual: tonumber) - and on my system:
$ locate --regex 'lua.*so.*' | head -4
/usr/lib/libipelua.so.7.0.10
/usr/lib/liblua5.1.so
/usr/lib/liblua5.1.so.0
/usr/lib/liblua5.1.so.0.0.0
... apparently I do have Lua 5.1 installed.
So, why do I get an error on using tonumber here - and how can I use this (and other) standard functions in a VLC lua extension properly?
Documentation is sparse for VLC Lua extensions to say the least but I did find an example in the github vlc repository here: https://github.com/videolan/vlc/blob/master/share/lua/extensions/VLSub.lua
Judging from that example it appears you need to supply some basic event functions for your addon for VLC to call into when certain events happen. Some of the obvious callback handlers I've noticed:
descriptor, this should return a table that contains fields describing your addon.
activate, this seems to get called when you activate it from view menubar.
deactivate, called when you deactivate the addon from view menubar.
plus a couple of other functions like close and input_change which you can guess what they're for.
From my brief testing done on VLC 2.0.8 under Win7 it appears VLC loads the lua extension using an empty sandbox environment. This is likely the reason you're getting nil for tonumber and I'm betting none of the other standard lua functions are accessible either when you try to perform computation at this global scope.
However, if I move that code into one of the event handling functions then all those standard functions are accessible again. For example:
function descriptor()
return
{
title = "Test Ext";
version = "0.1";
author = "";
shortdesc = "Testing Lua Extension";
capabilities = {};
description = "VLC Hello Test Addon";
}
end
function activate()
print "test activating"
local fps = tonumber "25.000"
local frame_duration = 1 / fps
print(frame_duration)
return true
end
-- ...
That prints out what you would expect in the console debug log. Now the documentation (what little there is) doesn't mention any of this but what's probably happening here is VLC is injecting the standard lua functions and vlc api table into the sandboxed environment when any of these event handlers get called. But during the extension loading phase, it is done in an empty sandbox environment which explains why all those lua function calls end up being nil when you try to use it at the outter most scope.
I recommend cloning the VLC source tree from github and then performing a grep on the C source that's embedding lua to see what VLC is really doing behind the scenes. Most of the relevant code will likely be here: https://github.com/videolan/vlc/tree/master/modules/lua
Probably some extension script installed in your system overwrites the function and the Lua interpreter instance is shared between all extension scripts, so you end up not being able to call the function if that script is called before yours.
As a quick workaround, Lua being dynamically typed, you can still do things like:
1 / "25.000"
and the string will be coerced to a number.
Alternatively, you can define a tonumber equivalent like:
string_to_num = function(s) return s + 0 end
This again relies on dynamic typing.

Resources