Arduino: Detect code info - arduino

Is there any way to detect the code version or any info like last updated or size of code or size of binary code?
Here is example:
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
// turn the LED on (HIGH is the voltage level)
delay(1000);
// wait for a second
digitalWrite(LED_BUILTIN, LOW);
// turn the LED off by making the voltage LOW
delay(1000);
// wait for a second
}
If I upload code to Arduino board, can I detect any info inside Arduino regarding the size of the code or the last modified time or upload time etc?
Thanks.

Compilation date is stored in the __DATE__ macro, the current system time at compilation is stored in the __TIME__ macro. Also useful may be the __FILE__ macro, which stores the filename. Reference (and other relevant macros): http://www.atmel.com/webdoc/avrassembler/avrassembler.wb_preprocessor.Pre-defined_macros.html
There is some interesting discussion about something along these lines here:
https://github.com/arduino/Arduino/issues/5618
In that thread they're discussing ways to store the Git hash of the code at the time of the compilation/upload. If you're using Git version control that information would probably be more useful than last modified time or code size for determining which version is running on your microcontroller and being able to easily retrieve that version. I'm not sure how crazy I am about automatically doing a commit on every upload by you could always squash them later once testing is finalized. If you don't use Git, the same techniques could be adapted to adding any other version information you like and even doing automatic backups of the code at that version.
As explained by facchinm in that thread, recent versions of the Arduino IDE provide hooks throughout the build process that you can use to add additional actions. See:
https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5-3rd-party-Hardware-specification#pre-and-post-build-hooks-since-ide-165
The original proposal is to store the version information in EEPROM. I believe the most common way to do this is via an .eep file. A problem with this idea is that the popular Optiboot bootloader used by the Arduino Uno as well as popular 3rd party hardware packages does not support writing to EEPROM during uploading (in order to fit the bootloader in a 0.5 kB boot section). You would also need to avoid conflicts caused by use of the same EEPROM addresses by the sketch.
The other thought was to instead store it in the code itself. I guess this would be done by using a hook to automatically update a header file "library" that is included by the sketch. e.g.:
#include <VersionTracker.h>
Now how do you access that information? You could have code in the sketch that sends out that information on request or startup. A simple example:
void setup() {
Serial.begin(9600);
Serial.print(F("Filename: "));
Serial.println(F(__FILE__));
Serial.print(F("Compilation timestamp: "));
Serial.println(F(__DATE__ " " __TIME__));
}
void loop() {}
Will print this information to the Serial Monitor on startup. Of course you could use any other method of communication. This sort of thing will add some significant overhead so it would perhaps be preferable (if less beginner friendly) to download the firmware from the microcontroller and then figure out a way to find the version information from the disassembly.

The time you cannot get. The Arduino doesn't have any clock or any way to know the time.
The code size you could get if you use AvrDude to read the hex code back off the chip and then just look at the size of what you read back.

Related

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.

Arduino: Using the watchdog for both preventing failures and energy savings

I came across this website when trying to find a reliable way to use the watchdog for preventing failures (code lock ups) and saving battery using an Arduino.
I tried the code and it worked fine. However, i would also like to use the serial monitor. I tried adding Serial.begin(9600); in the setup, however, most of what is shown in the serial monitor (from the code within the main loop) are strange characters (the baud rate is set to 9600). Is this something to do with the function to configure the wdt and the placement of Serial.begin(9600) in the code?
I'd also like to use an external interrupt (via a button on Digital pin 3) to wake the board from sleep. How can be achieved based on the current code? I know how to implement using a different method of making the board go to sleep without using any watchdog at all, however, i have been unsuccessful in making it work with this code.
Many thanks for any help.
As said above, the serial communication should work fine. Are you sure you are able to make your serial communication work fine without the watchdog part of the code? I have used the watchdog tips given on the website along with serial communications without any problem on Arduino Uno, so I would guess the serial communication problem lies somewhere else in your code.
Can you write a bare bone example of your code with the watchdog management part, a serial communication or two somewhere in your loop(), and if you want some delays / infinite loops to test the watchdog firing, post it here, test it on your board and indicate if / where you have problems?
I have never used a pin interrupt, but it seems that google gives some nice results with example code. Have you tried the results given by google?
https://www.arduino.cc/en/Reference/AttachInterrupt
http://www.allaboutcircuits.com/technical-articles/using-interrupts-on-arduino/
You will have to be careful regarding the choice of the pin on which you put the interrupt, as explained in the Arduino Reference only pins 2 and 3 support interrupt on the Uno.

Why does not work library dht11 for Arduino in Launchpad msp430 (g2553)?

Why does not work library, if the Launchpad is compatible with Arduino?
The sensor data is sufficient to derive to serial port.
What is the difference between dht11 and dht22 libraries?
There is a project called Energia which says it's a fork of Arduino for the MSP430.
Alternatively there's a post here with code to read the DHT11 on the MSP430 (native code, not using Energia, as far as I can tell), and another one here with code to read the similar DHT22 on MSP430/Energia.
I understand from discussion about these sensors on the Picaxe forum that they're a bit picky about timing, so you may need some tinkering to get this stuff to work.
I came accross this while searching for DHT11 and MSP430, if the question is still valid:
Please check the following code(s) that I've written for interfacing DHT11 sensor and MSP430, the codes do not require any library, you can just paste it in your main.c file in code composer studio and debug+resume. 1st file below(dht11_MSP430G2553) does a one time read and you can see the values in the expressions window, the second file (MSP430_DHT11_Sensor) is more mature and it reads the values in a loop by resetting the microcontroller with watchdog timer and it outputs the values to the serial monitor in 19200 baud
https://github.com/selimg76/microcontroller/blob/master/dht11_MSP430G2553
https://github.com/selimg76/microcontroller/blob/master/MSP430_DHT11_Sensor
Detailed explanations are in the following videos:
https://youtu.be/Hid_jB_Dy-A
https://youtu.be/Fzf8q6fgxfQ

Resources