projets:2019:stagefevrierubo
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédenteProchaine révisionLes deux révisions suivantes | ||
projets:2019:stagefevrierubo [2019/02/20 08:27] – christian.jacolot | projets:2019:stagefevrierubo [2019/02/20 14:59] – christian.jacolot | ||
---|---|---|---|
Ligne 216: | Ligne 216: | ||
VarSpeedServo myservo; | VarSpeedServo myservo; | ||
+ | // twelve servo objects can be created on most boards | ||
int pos = 0; // variable to store the servo position | int pos = 0; // variable to store the servo position | ||
int positionServo = 0; | int positionServo = 0; | ||
int speedServo = 100; | int speedServo = 100; | ||
+ | |||
+ | // Serial reading buffer | ||
+ | const byte numChars = 60; | ||
+ | char receivedChars[numChars]; | ||
+ | boolean newData = false; | ||
StaticJsonBuffer< | StaticJsonBuffer< | ||
- | String strJSON; | + | |
+ | void recvWithEndMarker() { | ||
+ | static byte ndx = 0; | ||
+ | char endMarker = ' | ||
+ | char rc; | ||
+ | |||
+ | while (Serial.available() > 0 && newData == false) { | ||
+ | rc = Serial.read(); | ||
+ | |||
+ | if (rc != endMarker) { | ||
+ | receivedChars[ndx] = rc; | ||
+ | ndx++; | ||
+ | if (ndx >= numChars) { | ||
+ | ndx = numChars - 1; | ||
+ | } | ||
+ | } | ||
+ | else { | ||
+ | receivedChars[ndx] = ' | ||
+ | ndx = 0; | ||
+ | newData = true; | ||
+ | } | ||
+ | } | ||
+ | } | ||
void setup() { | void setup() { | ||
+ | pinMode(9, OUTPUT); | ||
myservo.attach(9); | myservo.attach(9); | ||
Serial.begin(9600); | Serial.begin(9600); | ||
- | myservo.write(positionServo, | + | |
+ | newData = false; | ||
} | } | ||
void loop() { | void loop() { | ||
- | | + | |
- | | + | if (newData |
- | JsonObject& | + | JsonObject& |
if (!root.success()) { | if (!root.success()) { | ||
Ligne 249: | Ligne 280: | ||
// Print values. | // Print values. | ||
- | | + | Serial.println(positionServo); |
- | | + | Serial.println(speedServo); |
// myservo.write(positionServo, | // myservo.write(positionServo, | ||
} else { | } else { | ||
Serial.println(" | Serial.println(" | ||
} | } | ||
+ | | ||
+ | newData = false; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | Arduino: Programme complet (JSON, relai, ventilo, LDR, DHT11, servo-moteur): | ||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include " | ||
+ | |||
+ | #define LDRPIN 0 | ||
+ | #define DHTPIN 2 // Digital pin connected to the DHT sensor | ||
+ | #define VENTILOPIN 7 | ||
+ | #define RELAIPIN 8 | ||
+ | #define SERVOPIN 9 | ||
+ | |||
+ | #define DHTTYPE DHT11 // DHT 11 | ||
+ | DHT dht(DHTPIN, DHTTYPE); | ||
+ | VarSpeedServo myservo; | ||
+ | |||
+ | unsigned long time; | ||
+ | unsigned long diff; | ||
+ | int ldrValue = 0; | ||
+ | |||
+ | int relayState = LOW; | ||
+ | int ventiloState = LOW; | ||
+ | |||
+ | int positionServo = 0; | ||
+ | int speedServo = 100; | ||
+ | |||
+ | long checkDHT = 2000; | ||
+ | |||
+ | // Serial reading buffer | ||
+ | const byte numChars = 60; | ||
+ | char receivedChars[numChars]; | ||
+ | boolean newData = false; | ||
+ | |||
+ | void recvWithEndMarker() { | ||
+ | static byte ndx = 0; | ||
+ | char endMarker = ' | ||
+ | char rc; | ||
+ | |||
+ | while (Serial.available() > 0 && newData == false) { | ||
+ | rc = Serial.read(); | ||
+ | |||
+ | if (rc != endMarker) { | ||
+ | receivedChars[ndx] = rc; | ||
+ | ndx++; | ||
+ | if (ndx >= numChars) { | ||
+ | ndx = numChars - 1; | ||
+ | } | ||
+ | } | ||
+ | else { | ||
+ | receivedChars[ndx] = ' | ||
+ | ndx = 0; | ||
+ | newData = true; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | void setup() { | ||
+ | pinMode(VENTILOPIN, | ||
+ | pinMode(RELAIPIN, | ||
+ | pinMode(SERVOPIN, | ||
+ | |||
+ | time = millis(); | ||
+ | dht.begin(); | ||
+ | myservo.attach(9); | ||
+ | Serial.begin(9600); | ||
+ | newData = false; | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | StaticJsonBuffer< | ||
+ | recvWithEndMarker(); | ||
+ | if (newData == true) { | ||
+ | JsonObject& | ||
+ | |||
+ | if (!root.success()) { | ||
+ | Serial.println(" | ||
+ | return; | ||
+ | } | ||
+ | if(root.containsKey(" | ||
+ | positionServo = root[" | ||
+ | speedServo = root[" | ||
+ | myservo.write(positionServo, | ||
+ | } | ||
+ | | ||
+ | if (root.containsKey(" | ||
+ | int ventiloValue = root[" | ||
+ | if (ventiloValue == 1) { | ||
+ | ventiloState = HIGH; | ||
+ | } else { | ||
+ | ventiloState = LOW; | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | if (root.containsKey(" | ||
+ | int relaiValue = root[" | ||
+ | if (relaiValue == 1) { | ||
+ | relayState = HIGH; | ||
+ | } else { | ||
+ | relayState = LOW; | ||
+ | } | ||
+ | } else { | ||
+ | Serial.println(" | ||
+ | } | ||
+ | | ||
+ | newData = false; | ||
} | } | ||
- | | + | |
+ | if (diff >= checkDHT) { | ||
+ | time = millis(); | ||
+ | float h = dht.readHumidity(); | ||
+ | float t = dht.readTemperature(); | ||
+ | // read the value from the sensor: | ||
+ | ldrValue = analogRead(LDRPIN); | ||
+ | |||
+ | Serial.print(" | ||
+ | Serial.print(h); | ||
+ | Serial.print(" | ||
+ | Serial.print(t); | ||
+ | Serial.print(" | ||
+ | Serial.print(ldrValue); | ||
+ | Serial.println(" | ||
+ | } | ||
+ | // set the relay: | ||
+ | digitalWrite(RELAIPIN, | ||
+ | |||
+ | // set the ventilo: | ||
+ | digitalWrite(VENTILOPIN, | ||
} | } | ||
</ | </ | ||
projets/2019/stagefevrierubo.txt · Dernière modification : 2024/04/16 22:26 de 127.0.0.1