Writing web app to program Arduino - python

I want to write a web app allowing people to program Arduino remotely. There will be a live video stream of the Arduino with lolShield.
What's the best way to flash the Arduino within Python?

inotool is a commandline wrapper for the sketch environment and is written in python.
Or you could use exec() to run one of the commandline solutions in Command line Arduino compiling and downloading?

avrdude is the tool to program an Arduino over a serial interface. So basically you need a serial interface from your web server to the Arduino in question. Here are two approaches that may help you get there (I had tried the first one about 1.5 years ago, it worked like a breeze):
Xbee RF modules can transparently be operated in serial mode, perfect for this use case. The downside of this is that you need a local in-between machine that is connected to the internet and acts as the gateway to the Xbee/Zigbee network. Programming an Arduino over Xbee has been described in detail at ladyada.net.
If you need direct WiFi connectivity this should work the same way. XBee comes in two parts: 1) the adapter, which connects to the Arduino over a serial interface, 2) the actual RF module. So if you can get a WiFi module that sits on top of a XBee adapter, the same approach should work. I haven't tried this myself, but I guess combining the first approach with the following from homebrew-tech.com should work.

Inotool is a good option. In addition to that you can also try the following two options as well.
Arduino 1.5.x only
If you are using Arduino 1.5.x then you can use the arduino executable can accept commandline parameters.
Note that Arduino 1.5.x is still in beta, so you may face some issues.
Arduino 1.0.x
If you are using Arduino 1.0.x then you can use my makefile for Arduino which can be used to compile and upload Arduino (or plain AVR C) programs to Arduino from the commandline.

Related

How to program a atMega328p MCU with a Raspberry Pi 4 (Python)

I been looking everywhere online for this exact configuration but can't find much.
I want to program my AtMega328p MCU (its on a breadboard) using Python from my Raspberry Pi 4 but I am not sure how to check if communication is going on between them? I have the MISO, MOSI, SCLK, and CE0 pins from the Pi connected to the MISO, MOSI, SCK, SS pins on the AtMega328p respectively.
I understand I have to use SPI communication, however how can I exactly send data from the Raspberry Pi to the MCU to ensure there is communication between the two? Maybe some code to send to the MCU and receive it back? I been using the SPI Dev Python libraries but can't find much info on it. Thank you in advance!!
To load code onto the AVR (program it), you want to use existing software like avrdude that already speaks the AVR ISP protocol. avrdude already has support for using the RPi SPI headers - just use the linuxspi programmer type.
Here's an article by the author that explains it (although there are probably more recent articles if you search around) http://kevincuzner.com/2013/05/27/raspberry-pi-as-an-avr-programmer/
Is there a reason you want to do this from Python specifically? Or are you referring to communicating between the uC firmware and some Python code on the Pi?

Sending data from PC to Raspberry Pi using Python

I am working on a project where I need to detect objects from IP camera on PC using TensorFlow and the data of the detected object to raspberry pi and control its GPIO pins accordingly.
I am using python on both ends.
I read somewhere to form a client-server network between the two.
I need some ideas on how to approach this problem. Thank you. I hope my question was clear.
Extra Information: I am currently using Raspberry pi on PC via ethernet cable. Also, both raspberry pi and my PC are connected to the same Wifi router. I hope this information helps.
You can use sockets to make the Raspberry Pi and your PC communicate. You can use socket.accept(conn, address) to accept a connection and conn.recv(buffer) to receive data.
I'm a big fan of using Redis for this type of thing. It has a small footprint, is simple to set up and is lightning fast. It is a "networked data structure server". "Networked" means that its clients can be on the same machine or anywhere on the network. The data structures it serves include:
strings
integers
atomic integers
lists
queues
sets
sorted sets
hashes
There are bindings for Python, C, C++, PHP, Ruby and rather brilliantly, the shell. That means you can inject values into queues, lists or sets directly from any Terminal on your network whilst your app is running, in order to test it or monitor it.
Example here.

Modbus sniffing using python

I have 2 devices communicating with each other using Modbus and I would like to sniff this communication line using Python for logging purposes. I have connected my computer to the communication bus via a Modbus dongle, but I don't know how I can actually read the data.
I have used Pymodbus before, but I don't think that would work in this case since it only allows for master or slave devices (as far as I know)
Are there any other libraries that I can use for my purpose? Would it be possible for me to implement something like a read only socket on the COM port that doesn't interfere with existing communication on the bus?
Thank you!
You can try a MODBUS simulator like this one: It has an option to see the activity on the bus.
If you want to use another python MODBUS I recommend using modbus_tk. It has an example on how to create an MODBUS simulator. But to be able sniff the packets sent to other devices you will need to do some modifications to disable the automatic response.
If the only thing you want to do is to see the activity on the bus I recommend the first option.
I hope this helps you.
[Edit]:To be more specific you will need to download the following software : Modbus Poll - MODBUS slave simulator(which works on Windows) and plug your MODBUS dongle in the port you intend to use. After you do all the settings for the serial communications go to the Display tab and click on Communication. You will be able to see the traffic on the line.

Reading Analog Data from sensor connected to USB (Python)

I may be confusing a few concepts here so any help is appreciated.
Q1: Is it possible to attach any sensor in the world to the USB on my computer as long as it gives me analog data, and read its output? (e.g. pH, temperature, oxygen sensor etc as long as it gives me analog data)
Q2: If so, then what is the simplest way in python for me read such data.
Comment: I am trying to bypass using PLC's, and trying to see if I can get the output from the sensor directly to the PC. (I do not have drivers for these sensors)
Actual Need: I have an oxygen sensor connected to my computer via a USB. The oxygen sensor is able to send out analog data. The obvious way is to go through a PLC. However, I would like a solution which by-passes PLC's so I can connect the sensor directly to my PC via USB.
The USB port on your computer cannot read analog data because USBs work with digital signals. You would need an analog-to-digital converter (ADC).
as #digitaLink answered, it is not possible directly via USB and yes, the obvious way is to use a PLC.
I would go the PLC way - in fact, I did it a few times in the past - and start with an Arduino and later develop a custom PCB, put it in a box and done.
Another possibility is to use a raspberry pi (or similar SBC), which has the GPIOs you can use for analog read.
Edit: there is another possibility.
The sensor you use now is _very_likely_ a PLC in itself, that is, the sensor is attached to a microcontroller that uses the USB port for serial communication. Now, the drivers you are missing do nothing else but decode the data coming through the serial port. Take a look inside your harware and try to find out what components there are in.
So what you could do is to try to find out how to communicate with the sensor via a serial terminal. It is probably possible to monitor serial communication (although I must admit, I don't know how to do that), reverse engineer the code and write your own driver in python. You could learn a lot from this, even if you don't succeed.

Running a file on my computer from an arduino

I have an idea for building my own auto-rotating monitor, as in the orientation of the monitor changes when it rotates. Creating the arduino (or raspberry pi or something) to do it would be easy, just getting input from an accelerometer and doing something when it rotates 90 degrees, and creating a python file on my computer to change the orientation of my monitor is fairly easy as well. What I can't figure out is how to get the arduino to call a file on my computer when it receives a signal. Is this even possible, and if so, how do I do it?
Arduino won't call a file on your computer directly. It communicates through serial port, and can only send/receive bytes you will have to parse and determine an action.
You will have to make a really basic protocol to send your accelerometer data to your computer (raw data only is enough) through serial port. There, with the serial communication open, you translate it into actions and call the respective script you want.
Making Arduino call a file would require making a library in your Arduino, and an interpreter in your host computer.
As a comment, do not close the serial communication with Arduino until you finish using it, since Arduino resets every time it establishes a new serial connection, generating unwanted delays.
You can't "get the arduino to call a file on [your] computer".
Instead you need to have one program running on each system (ie, both the Arduino and your computer) and have these programs talk to each other over some channel, like the serial port. A simple example for your case is to have the program on your computer, say, a Python program that uses PySerial, occasionally check the port to see whether a message has been sent from the Arduino about the accelerometer.
There are though, probably easier ways to find the screen orientation. For example, just getting the screen size, will usually tell you the orientation. You don't say you platform, but a cross-platform solution would be to use wxPython, and wx.DisplaySize(). There are many other ways as well.
I remember seeing a http library in processing. Maybe you can host a web server on your computer and request the content of the file from the the arduino.
Or if you want to execute a file in the computer, again you can write a tiny webapplication that executes a script when it receives a request from the arduino.

Categories