How can I use Process class in Arduino Yun to read some stream-data? - arduino-yun

Running shell command through Bridge in Arduino Yun through a Process let's say, proc, it gives result and we can read the result bytes using following piece of code.
#include <Bridge.h>
#include <Process.h>
void setup() {
Bridge.begin();
Serial.begin(9600);
while (!Serial);
}
void loop() {
Process proc;
proc.runShellCommand("ls /root/");
while (proc.available() > 0)
Serial.print((char)proc.read());
Serial.println();
}
What if I have to access data from a blocking shell command as it gets updated like and event? Such as, some consumer that listens to Kafka or Mosquitto subscribed topic. Whenever that topic is updated/published with new data, the listener gets it.
How can I model such structure using Arduino Yun program using Bridge.

You can do it quite easily.
Run that particular command with nohup, in that case, file with the name nohup.out would be created. then after that run a script that continuously monitors that nohup.out file in case of any change in the size of nohup.out, get the latest data and push that data to anywhere you want.

Related

ESP32 stucks in HardwareSerial initialization code after software restart

I am working in ArduinoIDE with ESP32 and TFP625A fingerprint sensor. The sensor is connected to UART2 and served by the FPM library using HardwareSerial. Everything works fine until a programmatically reboot is performed - ESP.restart(). After this command, the microcontroller reboots and stucks at the place of sensor initialization. I inserted some Serial.print() into the HardwareSerial source code file and saw that in the HardwareSerial::begin() function, the code does not go further than this place
_uart = uartBegin(_uart_nr, baud ? baud : 9600, config, rxPin, txPin, 256, invert);
As I understand it, after rebooting the microcontroller, information about the previously configured UART2 remains in its registers. Attempts to zero the UART before rebooting the microcontroller do not correct the situation. If I make hardware restart the microcontroller by the reset button, then everything works fine.
How to overcome ESP32 stuck?
Snippets of code:
#include <HardwareSerial.h>
#include <FPM.h>
HardwareSerial mySerial(2);
FPM fpm(&mySerial);
...
void freeFPM()
{
fpm = 0;
mySerial.end();
mySerial = 0;
}
bool fpm_setup()
{
freeFPM();
mySerial = HardwareSerial(2);
finger = FPM(&mySerial);
mySerial.begin(57600);
fpm.begin(0);
}
void restartESP()
{
freeFPM();
ESP.restart();
}
I think you need to include a hard reset of the sensor when the ESP32 restarts. I am not familiar with that particular sensor but I've had similar issues.
What I did was to wire the reset input of the sensor to a GPIO output on the ESP32 and then one of the first steps in the code is to pull down that pin for a short while to reset it.
I think what is happening is that when you first switch on the project the sensor is reset by its own power on reset mechanism but when you do the software reset of the ESP32 the sensor is not also reset, hence why I think you need to do it as I described above.

Delay in Serial Communication when talking between Arduino and Python

I have an Arduino-controlled robot with a gyroscope, and I'm trying to send data from it over to a Python program run on a Raspberry Pi. However, there's a 1-2 second delay between me moving the robot, and info being printed out the python program. I've tried restarting my computer, as well as replugging the wires. Is there anything I'm doing in my code that is causing this or is it a hardware thing?
The robot is connected to a Raspberry Pi, with which I have a headless setup. The python program is being run on the Pi but can be viewed on my desktop over SSH. The program has no delay when I connect the robot to my desktop and run the python program on it as well.
Arduino Program:
#include <robot's file>
Gyro gyro; //Object using an imported class
void setup() {
Serial.begin(115200);
while (!Serial) {}
}
void loop() {
gyro.read(); //Reads the robot's yaw and puts it into variable "z"
Serial.println(gyro.z);
}
Python Program:
import serial
from serial.serialutil import SerialException
ser = serial.Serial("/dev/ttyACM0", 115200)
try:
ser.open()
except SerialException:
print("Port already opened")
while True: print(ser.readline())
looks to me that everything is good in your code.
Most of the delay is related to your PC computing time.
If you want to speed up the process I would suggest to interface an Oled display (or an LCD) to display directly your data: using your PC is good for the initial testing, but if you need immediate response it is better to manage the data interfacing directly.
This link may support you: https://www.waveshare.com/wiki/1.3inch_OLED_HAT All the best
After some testing, I think I've figured it out.
Printing to terminal takes a long time, which made the Arduino program faster than the Python program, creating a buildup of data, causing the delay. I've fixed the problem by only printing out data every 10 iterations. The problem would also be fixed if I stored the data into a variable, and did not call the print() function.

Arduino with SIM 900a- How can I store all incoming messages into a text file?

I have connected my Arduino Uno with SIM 900a GSM module. I want to store all my text messages that I receive on the SIM inside the GSM module to a text file continuously.
I can send SMS through the code shown below but I cannot receive and save my messages to a file. What is the correct way to do this?
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9, 10);
void setup() {
mySerial.begin(9600); // Setting the baud rate of GSM Module
Serial.begin(9600); // Setting the baud rate of Serial Monitor (Arduino)
delay(100);
}
void loop() {
if (Serial.available()>0)
switch(Serial.read()) {
case 's':
SendMessage();
break;
case 'r':
RecieveMessage();
break;
}
if (mySerial.available()>0)
Serial.write(mySerial.read());
}
void SendMessage() {
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
mySerial.println("AT+CMGS=\"+9779813546162\"\r"); // Replace x with mobile number
delay(1000);
mySerial.println("I am SMS from GSM Module");// The SMS text you want to send
delay(100);
mySerial.println((char)26);// ASCII code of CTRL+Z
delay(1000);
}
void RecieveMessage() {
mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
delay(1000);
}
The Arduino cannot naturally create files on your host system, so you will need to
run an independent program on the host system which watches the serial connection and logs the messages produced by the Arduino (my recommendation)
attach some storage to the Arduino (such as an SD card shield)
have the Arduino pretend to be a keyboard and output as if it were being typed upon
Independent Program
This is the route I would recommend
easy to use and test (just check your file has the contents you want)
will not "mess up" your Arduino
can do other work and tests on system
do not need to deal with storage problems
This simple Python script may work for your needs: Serial data logging with python
This post suggests you can do it with a 1-liner under both Linux (may be the same for Mac) and Windows, but you may run into trouble with the Serial Port's baud rate. If this answer is dated or only gets some of the output (ie. a single line and then exits), you could run it in a loop or search further. You'll need to pick the right serial port as there may be a few (or just one with a different name).
Attached Storage
Many vendors will sell you a Shield for this
Adafruit Assembled Data Logging shield for Arduino (deals with filesystem automatically, nice)
SparkFun microSD Shield
generally searching "Arduino SD Card Shield" (will probably turn up cheaper versions, but they may not be of good quality, have nice drivers, tutorials, etc.)
Beware that Flash Storage can be annoying to deal with
need to eject (perhaps swap out) the card and look at it periodically to both see the results and if the results are correct
filesystems hoopla (should I use FAT, exFAT, ext2..)
ensuring the Arduino can write the filesystem (though modern shields probably do this for you, at least the Adafruit one suggested above does)
Keyboard Emulation
To start, I do not recommend doing this for the following reasons, though it's pretty neat and doesn't require any more hardware than you have.
BEWARE may make your Arduino unusable without some start gate such as waiting for a switch to be enabled (haywire inputs to computer: cannot program it)
requires total access to the computer while running (computer cannot be otherwise used)
not easier to configure than an independent logger (annoying trial and error / waiting / haywire inputs)
Official docs: https://www.arduino.cc/reference/en/language/functions/usb/keyboard/
They have the same warning I would give
A word of caution on using the Mouse and Keyboard libraries: if the Mouse or Keyboard library is constantly running, it will be difficult to program your board. Functions such as Mouse.move() and Keyboard.print() will move your cursor or send keystrokes to a connected computer and should only be called when you are ready to handle them. It is recommended to use a control system to turn this functionality on, like a physical switch or only responding to specific input you can control. Refer to the Mouse and Keyboard examples for some ways to handle this.

Arduino taking forever to upload sketch

I am using Arduino Uno board and programming it with a Windows 10 system. From the tutorials on Arduino website, I am trying to upload the following code:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(1);
}
It had undergone a series of issues:
First,
avrdude: ser_open(): can't open device "\\.\com3"
I searched for his issue, and found that most common solution is to go to device manager and change the name of the port, then unplug and plug again the arduino board. I did that
Second,
C:\Program Files (x86)\Arduino\hardware\tools\axr\bin\avr-ar:
unable to rename 'core.a'; reason: file exists\\
I thought it could be a memory problem. I used the reset button on the board, and repeated steps from the solution to the first problem. I tried to upload the code again.
But now, I am not getting any error message. But the problem is that it is taking forever to upload the code. I see Uploading... status since a long while. The green progress bar is also complete, but no uploading completed till now.
Any help to understand and solve this will be appreciated.
The solution for me was to disable Apple's Bonjour service with the task manager.

How do i run a system() command using arduino sketch program?

So, here is the thing. I have written an arduino sketch program and this is what i want say if the LED blinks then say the following code is executed such as
system("c:\temp\capture.txt") something like that. Is there a way to do that?
void loop()
{
val = digitalRead(2);
if(val == HIGH)
{
digitalWrite(LED,HIGH)
//system command happens here
}
}
How do i do that? Thanks
Arduino won't be able to execute programs in PC directly because its system is isolated from PC. That's why Arduino can run programs without PC (standalone).
Consider developing a host program that run on your PC and accept comnands via serial communication, and send comnand to execute something from Arduino to the host program. Then, have the host program execute the program to run.

Resources