Klingel-Sensor mit Arduino Nano und Python
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
# https://www.instructables.com/id/Arduino-Python-Communication-via-USB/
|
||||
import serial
|
||||
import urllib.request
|
||||
import logging
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s - %(message)s')
|
||||
logging.info('Initializing')
|
||||
|
||||
|
||||
arduino = serial.Serial('/dev/ttyUSB0', 9600, timeout=.1)
|
||||
|
||||
while True:
|
||||
data = arduino.readline()[:-2] #the last bit gets rid of the new-line chars
|
||||
if data:
|
||||
data = data.decode('utf8')
|
||||
if (data == 'RING'):
|
||||
logging.info(data)
|
||||
urllib.request.urlopen('http://192.168.178.51/bell/on')
|
||||
@@ -0,0 +1,35 @@
|
||||
int ledPin = LED_BUILTIN;
|
||||
int buttonPin = 2; // Button connected to D2 and GND
|
||||
bool active = false;
|
||||
|
||||
void setup() {
|
||||
pinMode(ledPin, OUTPUT);
|
||||
pinMode(buttonPin, INPUT_PULLUP);
|
||||
Serial.begin(9600);
|
||||
Serial.println("Starting doorbell sensor");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
int val = digitalRead(buttonPin);
|
||||
|
||||
if (val == LOW) {
|
||||
delay(2); // catch flares
|
||||
if (val == LOW && active == false) {
|
||||
Serial.println("RING");
|
||||
digitalWrite(ledPin, HIGH);
|
||||
active = true;
|
||||
delay(250);
|
||||
}
|
||||
else {
|
||||
// Serial.println("flare");
|
||||
}
|
||||
}
|
||||
else {
|
||||
delay(2); // catch flares
|
||||
if (val == HIGH) {
|
||||
active = false;
|
||||
digitalWrite(ledPin, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user