Running a file on my computer from an arduino - python

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.

Related

Is it possible to read a sensor via ethernetip without a PLC?

I have a keyence sensor FD-XA2 which is mounted on the communication unit NU-EP1. I connected it to my PC with an Ethernet cable. I thought it would be rather easy to read the values with python by just receiving them from the Ethernet-connection. But I have no idea how to set up the communication between the sensor and PC. I was able to create a client and server with the socket package on my PC, which is working fine. But I have literally no idea if the sensor needs to be a client or server. How do I set up the whole thing correctly?
The manufacturer support told me they just have experience with reading the sensor when it is connected to a PLC. This would be my second option. But I thought maybe I can just read the sensor without a PLC.
Thanks!

i am sending commands through serial port in python but they are sent multiple times instead of one

i am sending some commands having particular response serially using com port..the commands are kept in a file..i am reading each command through the file line by line and sending it serially over the com port..but when i am seeing it from the receiver end using Magic Terminal(Software)..i found that each command is going multiple times..which i am sending only one time..i have made a code in pycharm..and in the console i am seeing that command is going only once but from the uart receiving end the story is something else..i am stuck with this problem..i have maintain the same baudrate and everything but not able to diagnose the issue..
github link for the code is: https://github.com/AkshatPant06/Akshat-Pant/blob/master/cmd%20list
def recvResponse():
ser.write(serial.to_bytes(intCmd))
time.sleep(1)
data_recv=ser.read(2)
return data_recv
this i have used to receive the 2 byte response..
There seems to be nothing wrong with your code. At least to the extent I could reproduce, it only sends the command once (I tried your function after setting up my serial port in loopback).
I cannot say for sure but it might be that the terminal you're using has two windows, one for input and another one for output and somehow you're getting confused with what is in and out of your port.
One easy way to deal with this kind of issue is to use a sniffer on your port. You can do that combining com0com and Termite on Windows, as I recently explained here.
As you can see there is only one window on this terminal, and after setting up the forwarding you'll everything that comes in and out of your port. That should make it easier to see what your code is writing and reading.
To give you a conventional scenario to apply the sniffer trick you can refer to the following screenshot:
In this case, we have two real serial ports on a computer. On the first (COM9) we are running a Modbus server (you can imagine it as a bunch of memory addresses, each of one storing a 16-bit number). On COM10 we have a client that is sending queries asking for the contents of the first 10 addresses (called registers using the Modbus terminology). In a general use case, we have those ports linked with a cable, so we know (theoretically) that the client on COM10 is sending a data frame asking for those ten registers and the server on COM9 is answering with the numbers stored on those registers. But we are only able to see the contents on the server (left side of the picture) and what the client is receiving (right). What we don't see is what is traveling on the bus (yeah, we know what it is, but we don't know exactly how the Modbus protocol looks like on the inside).
If we want to tap on the bus to see what is being sent and received on each side we can create a couple of virtual ports with com0com and a port forwarding connection with Termite, something like the following screenshot:
Now we have moved our Modbus server to one of the virtual serial ports (COM4 in this case). After installing com0com we got (by default, but you can change names or add more port pairs, of course) a pair of forwarded ports (COM4<-->COM5). Now, if we want to see what is circulating through the ports we open Termite (bottom-right side of the picture) and set up another port forwarding scheme, in this case from virtual port COM5 to the real port COM9.
Finally (and exactly the same as before we were sniffing), we have COM9 connected together with COM10 with a cable. But now we are able to see all data going to and fro on the bus (all those HEX values you see on Termite displayed with the green/blue font).
As you can see, this will offer something similar to what you can do with more professional tools.

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.

python sockets and a serial to IP device

Using a Lantronix UDS-1100 serial to IP converter. The goal is to write a small proof of concept piece in Python to capture serial data output by this device over IP.
I've done a couple test projects using sockets in python, but they were all done between python processes (python > python): listen() on one end, and connect(), sendall() etc on the other.
I think I can use sockets for this project, but before I invest a bunch of time into it, wanted to make sure it is a viable solution.
Can python sockets be used to capture IP traffic when the traffic is originating from a non-python source? I have full control over the IP and port that the device sends the serial data to, but there will be no python connect() initiated by the client. I can pre-pend then serial data with some connect() string if needed.
If sockets won't work, please recommend another solution...guessing it will be REST or similar.
Of course. TCP/IP is supposed to be cross-platform and cross-language, so in theory you should be able to communicate with every kind of device as long as you manage to process and send the expected protocol.

Writing web app to program Arduino

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.

Categories