2020JAN171530030.221
Building An Alert System
Objective:
Read across a serial connection a message sent and received
from a Ardrino Diecimila to a software on another machine.
Setup
I spent half an hour yesterday pulling apart my USB Optical Mouse. I'd noticed it had stopped working and wanted to check for a possible loose connection. In the end I pulled apart three mice. All broken in some way. It was great to see the evolution of each itteration of Mouse.
By accident I ran across my unused Diecimila. I picked it up way back in 2008,
played with it a bit and put it down. A bit of hardware looking for a use. This time I have a very specific purpose in mind.
A Warning System
I have an online service I want to check is running. I could do this visually but I want something more timely. I want something that is noisy and flashes lights when things are not working.
I intend to use a RaspberryPi to talk to the Internet then process the service status and if the status is negative talk to the Ardrino to fire off a warning. This is a great architectural match. I can divide code on the RaspberryPi to interrigate the Internet service then deligate the Arduino to run the hardware for the warning system.
Query The Internet
I need a networked device to query the external service. This is the job of the RaspberryPi. Depending on the status of the queried service. I then want the RPi software to message the Ardino hardware. The Diecimila is connected to a RaspberryPi via a USB cable.
Query The Hardware
The Diecimila runs a piece of software that checks if commands sent by the RPi require a warning. If it does, the LED light and simple class C amplifier driving a piezeo-electric speaker are activated. If not, the hardware is dormant.
RaspberryPi Reciving Message through Code
The key concept to making this idea work is communication between the RPi and the Arduino. At the RPi end I decided to write some code to talk to the Arduino via the serial communication protocol.
First I had to upgrade my Python version from 3.5 to 3.8, done. Next install PySerial. I thought the Serial library was standard with Python, not now. So dowload I try. Then I find Pip is complaining. Upgrade Pip. Still complaining. It't the SSL problem so I locate the source code and re-build.
The simple serial example on PySerial got me started and here's the first hack
of the RPi talking (listening) to the Arduino:
# name: pyard_serial.py
# date: 2020JAN17
# prog: pr
# desc: basic receive from ardino using serial protocol
#!/usr/bin/env python
import serial
import config
print("open connection")
ser = serial.Serial(config.SERIAL_PATH)
ser.baudrate = config.BAUD
ser.timeout = config.TIMEOUT
ser.parity = serial.PARITY_EVEN
ser.rtscts = 1
print("talking to serial port")
print("status: [{}]".format(is_open, ser.name))
start = 0
# query the device
with ser as s:
while s.is_open:
value = s.read()
if value:
m = value.decode()
print("[{}]".format(m))
else:
print(".", end='', flush=True)
print("close connection")
ser.close()
Listening but not sending, now we need some code at the Arduino end.
Arduino Sending Message through Code
The setup for the Arduino involved downloading the gui application. It's well worth the effort because it seamlessly works out the tiny details that could stump you.
Opening the editor, connecting the Arduino to the laptop via the USB cable. The Arduino lights up. Below is the simpliest example possible. The setup executes once, the loop continuously.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("");
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print("");
}
So the message being sent over the USB via the serial protocol should look like:
...
This threw me at first. Even though I knew the protocol was serial (one at a time) I had expected the protocol to buffer the result and return everything at once. Not so. This has implications writing the code at both the RPi and Arduino end.
The Next Hack
Next I want to do the following. At the RPi end I want to both send and receive messages from the Arduino. At the Arduino end I want to be able to send and receive messages. It is also some time to start thinking about a container and some light.
Software
A more ambitious step might be to simulate a warning message being sent. Then think of a way to turn the warning off. Could I use a button for instance? An even bigger jump might be to create a very simple protocol to send, receive. This of course requires the ability in code at both ends to capture then interpret the result and respond.
Hardware
I also want to connect a LED to the Arduino. At the moment I'm just using the in-built light on the board on pin 13. This will require a bread-board, resister and LED all of which I have.
References
* docs.python.org/3/py-modindex.html#cap-s
* pyserial.readthedocs.io/en/latest/index.html
* pythonhosted.org/pyserial/shortintro.html
* forum.arduino.cc/index.php?topic=633792.0
* www.arduino.cc/en/Main/Software
* www.arduino.cc/download_handler.php?f=/arduino-1.8.10-mac...
* github.com/mkals/Arduino-Python3-Command-API
* flickr.com/photos/bootload/albums/72157607215259829
* docs.python.org/3/howto/unicode.html
2020JAN171530030.221
Building An Alert System
Objective:
Read across a serial connection a message sent and received
from a Ardrino Diecimila to a software on another machine.
Setup
I spent half an hour yesterday pulling apart my USB Optical Mouse. I'd noticed it had stopped working and wanted to check for a possible loose connection. In the end I pulled apart three mice. All broken in some way. It was great to see the evolution of each itteration of Mouse.
By accident I ran across my unused Diecimila. I picked it up way back in 2008,
played with it a bit and put it down. A bit of hardware looking for a use. This time I have a very specific purpose in mind.
A Warning System
I have an online service I want to check is running. I could do this visually but I want something more timely. I want something that is noisy and flashes lights when things are not working.
I intend to use a RaspberryPi to talk to the Internet then process the service status and if the status is negative talk to the Ardrino to fire off a warning. This is a great architectural match. I can divide code on the RaspberryPi to interrigate the Internet service then deligate the Arduino to run the hardware for the warning system.
Query The Internet
I need a networked device to query the external service. This is the job of the RaspberryPi. Depending on the status of the queried service. I then want the RPi software to message the Ardino hardware. The Diecimila is connected to a RaspberryPi via a USB cable.
Query The Hardware
The Diecimila runs a piece of software that checks if commands sent by the RPi require a warning. If it does, the LED light and simple class C amplifier driving a piezeo-electric speaker are activated. If not, the hardware is dormant.
RaspberryPi Reciving Message through Code
The key concept to making this idea work is communication between the RPi and the Arduino. At the RPi end I decided to write some code to talk to the Arduino via the serial communication protocol.
First I had to upgrade my Python version from 3.5 to 3.8, done. Next install PySerial. I thought the Serial library was standard with Python, not now. So dowload I try. Then I find Pip is complaining. Upgrade Pip. Still complaining. It't the SSL problem so I locate the source code and re-build.
The simple serial example on PySerial got me started and here's the first hack
of the RPi talking (listening) to the Arduino:
# name: pyard_serial.py
# date: 2020JAN17
# prog: pr
# desc: basic receive from ardino using serial protocol
#!/usr/bin/env python
import serial
import config
print("open connection")
ser = serial.Serial(config.SERIAL_PATH)
ser.baudrate = config.BAUD
ser.timeout = config.TIMEOUT
ser.parity = serial.PARITY_EVEN
ser.rtscts = 1
print("talking to serial port")
print("status: [{}]".format(is_open, ser.name))
start = 0
# query the device
with ser as s:
while s.is_open:
value = s.read()
if value:
m = value.decode()
print("[{}]".format(m))
else:
print(".", end='', flush=True)
print("close connection")
ser.close()
Listening but not sending, now we need some code at the Arduino end.
Arduino Sending Message through Code
The setup for the Arduino involved downloading the gui application. It's well worth the effort because it seamlessly works out the tiny details that could stump you.
Opening the editor, connecting the Arduino to the laptop via the USB cable. The Arduino lights up. Below is the simpliest example possible. The setup executes once, the loop continuously.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("");
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print("");
}
So the message being sent over the USB via the serial protocol should look like:
...
This threw me at first. Even though I knew the protocol was serial (one at a time) I had expected the protocol to buffer the result and return everything at once. Not so. This has implications writing the code at both the RPi and Arduino end.
The Next Hack
Next I want to do the following. At the RPi end I want to both send and receive messages from the Arduino. At the Arduino end I want to be able to send and receive messages. It is also some time to start thinking about a container and some light.
Software
A more ambitious step might be to simulate a warning message being sent. Then think of a way to turn the warning off. Could I use a button for instance? An even bigger jump might be to create a very simple protocol to send, receive. This of course requires the ability in code at both ends to capture then interpret the result and respond.
Hardware
I also want to connect a LED to the Arduino. At the moment I'm just using the in-built light on the board on pin 13. This will require a bread-board, resister and LED all of which I have.
References
* docs.python.org/3/py-modindex.html#cap-s
* pyserial.readthedocs.io/en/latest/index.html
* pythonhosted.org/pyserial/shortintro.html
* forum.arduino.cc/index.php?topic=633792.0
* www.arduino.cc/en/Main/Software
* www.arduino.cc/download_handler.php?f=/arduino-1.8.10-mac...
* github.com/mkals/Arduino-Python3-Command-API
* flickr.com/photos/bootload/albums/72157607215259829
* docs.python.org/3/howto/unicode.html