mirror of
https://github.com/thearn/Python-Arduino-Command-API.git
synced 2026-01-09 22:48:03 -05:00
Updates to fix connection issue on startup and added support for DHT sensors
This commit is contained in:
@@ -108,6 +108,7 @@ class Arduino(object):
|
||||
raise ValueError("Could not find port.")
|
||||
else:
|
||||
sr = serial.Serial(port, baud, timeout=timeout)
|
||||
sr.readline()
|
||||
sr.flush()
|
||||
self.sr = sr
|
||||
self.SoftwareSerial = SoftwareSerial(self)
|
||||
@@ -126,7 +127,7 @@ class Arduino(object):
|
||||
pin : digital pin number
|
||||
val : either "HIGH" or "LOW"
|
||||
"""
|
||||
if val == "LOW":
|
||||
if val.upper() == "LOW":
|
||||
pin_ = -pin
|
||||
else:
|
||||
pin_ = pin
|
||||
@@ -205,7 +206,7 @@ class Arduino(object):
|
||||
returns:
|
||||
duration : pulse length measurement
|
||||
"""
|
||||
if val == "LOW":
|
||||
if val.upper() == "LOW":
|
||||
pin_ = -pin
|
||||
else:
|
||||
pin_ = pin
|
||||
@@ -250,7 +251,7 @@ class Arduino(object):
|
||||
pinMode(pin, INPUT);
|
||||
long duration = pulseIn(pin, HIGH);
|
||||
"""
|
||||
if val == "LOW":
|
||||
if val.upper() == "LOW":
|
||||
pin_ = -pin
|
||||
else:
|
||||
pin_ = pin
|
||||
@@ -410,6 +411,57 @@ class Arduino(object):
|
||||
return int(rd)
|
||||
|
||||
|
||||
def dht(self, pin, module = 0):
|
||||
"""
|
||||
Read data from dht temperature and humidity sensors based on the
|
||||
Adafruit DHT sensor library.
|
||||
https://github.com/adafruit/DHT-sensor-library
|
||||
|
||||
Guide for using library:
|
||||
https://learn.adafruit.com/dht/using-a-dhtxx-sensor
|
||||
|
||||
There are five sensors that work with this library:
|
||||
- DHT 11: blue cage, less accurate
|
||||
- DHT 12:
|
||||
- DHT 21:
|
||||
- DHT 22: white cage
|
||||
- AM2301:
|
||||
Input:
|
||||
pin (int): pin for data
|
||||
module (int): 0 = DHT 11 (default),
|
||||
1 = DHT 12,
|
||||
2 = DHT 21,
|
||||
3 = DHT 22,
|
||||
4 = AM2301
|
||||
Output:
|
||||
[float, float, float] in the format:
|
||||
[ humidity in %,
|
||||
temperature in celcius,
|
||||
heat index in celcius ]
|
||||
"""
|
||||
try:
|
||||
if not (0 <= module <= 4):
|
||||
print("unknown module, must be in range 0 to 4. Using 0 (DHT 11).") # raise exception
|
||||
except:
|
||||
module = 0
|
||||
print("module must be spesified using an integer. Using 0 (DHT 11).")
|
||||
|
||||
cmd_str = build_cmd_str("dht", (pin, module,))
|
||||
try:
|
||||
self.sr.write(str.encode(cmd_str))
|
||||
self.sr.flush()
|
||||
except:
|
||||
pass
|
||||
rd = self.sr.readline().decode("utf-8").replace("\r\n", "")
|
||||
try:
|
||||
strings = rd.split("&")
|
||||
return [float(s) for s in strings]
|
||||
except:
|
||||
return None
|
||||
|
||||
|
||||
|
||||
|
||||
class Shrimp(Arduino):
|
||||
|
||||
def __init__(self):
|
||||
@@ -497,6 +549,7 @@ class Servos(object):
|
||||
return None
|
||||
|
||||
|
||||
|
||||
class SoftwareSerial(object):
|
||||
|
||||
"""
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <Wire.h>
|
||||
#include <Servo.h>
|
||||
#include <EEPROM.h>
|
||||
#include <DHT.h>
|
||||
|
||||
SoftwareSerial *sserial = NULL;
|
||||
Servo servos[8];
|
||||
@@ -316,6 +317,49 @@ void EEPROMHandler(int mode, String data) {
|
||||
}
|
||||
}
|
||||
|
||||
int dhtSensorPin = -1;
|
||||
DHT dhtSensor(dhtSensorPin, DHT11);
|
||||
|
||||
void dht(String data) {
|
||||
|
||||
String sdata[2];
|
||||
split(sdata, 2, data, '%');
|
||||
int dataPin = sdata[0].toInt();
|
||||
int sensorNumber = sdata[1].toInt();
|
||||
|
||||
int sensorType = DHT11; // assume DHT11 as default
|
||||
if (sensorNumber == 1) {
|
||||
sensorType = DHT12;
|
||||
} else if (sensorNumber == 2) {
|
||||
sensorType = DHT21;
|
||||
} else if (sensorNumber == 2) {
|
||||
sensorType = DHT22;
|
||||
} else if (sensorNumber == 2) {
|
||||
sensorType = AM2301;
|
||||
}
|
||||
|
||||
// do not initialize new sensor if we are reading repeatedly from same sensor
|
||||
if (dataPin != dhtSensorPin) {
|
||||
dhtSensorPin = dataPin;
|
||||
dhtSensor = DHT(dataPin, sensorType);
|
||||
dhtSensor.begin();
|
||||
}
|
||||
|
||||
// Reading temperature or humidity takes about 250 milliseconds!
|
||||
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
|
||||
float h = dhtSensor.readHumidity();
|
||||
// Read temperature as Celsius (the default)
|
||||
float t = dhtSensor.readTemperature();
|
||||
|
||||
if (isnan(h) || isnan(t)) {
|
||||
Serial.println("0&0&0");
|
||||
return;
|
||||
}
|
||||
|
||||
float hic = dhtSensor.computeHeatIndex(t, h, false);
|
||||
Serial.println(String(h) + "&" + String(t) + "&" + String(hic));
|
||||
}
|
||||
|
||||
void SerialParser(void) {
|
||||
char readChar[64];
|
||||
Serial.readBytesUntil(33,readChar,64);
|
||||
@@ -399,16 +443,20 @@ void SerialParser(void) {
|
||||
}
|
||||
else if (cmd == "sz") {
|
||||
sizeEEPROM();
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while (!Serial) {
|
||||
; // wait for serial port to connect. Needed for Leonardo only
|
||||
}
|
||||
else if (cmd == "dht") {
|
||||
dht(data);
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial) {
|
||||
; // wait for serial port to connect. Needed for Leonardo only
|
||||
}
|
||||
Serial.println("connected");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
SerialParser();
|
||||
}
|
||||
SerialParser();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user