Outils pour utilisateurs

Outils du site


projets:2019:stagefevrierubo

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
Prochaine révisionLes deux révisions suivantes
projets:2019:stagefevrierubo [2019/02/20 08:27] christian.jacolotprojets:2019:stagefevrierubo [2019/02/20 15:09] christian.jacolot
Ligne 216: Ligne 216:
  
 VarSpeedServo myservo;    // create servo object to control a servo  VarSpeedServo myservo;    // create servo object to control a servo 
 +// 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];   // an array to store the received data
 +boolean newData = false;
  
 StaticJsonBuffer<120> jsonBuffer; StaticJsonBuffer<120> jsonBuffer;
-String strJSON;+ 
 +void recvWithEndMarker() { 
 +    static byte ndx = 0; 
 +    char endMarker = '\n'; 
 +    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] = '\0'; // terminate the string 
 +            ndx = 0; 
 +            newData = true; 
 +        } 
 +    } 
 +
  
 void setup() { void setup() {
 +  pinMode(9, OUTPUT);
   myservo.attach(9);  // attaches the servo on pin 9 to the servo object   myservo.attach(9);  // attaches the servo on pin 9 to the servo object
   Serial.begin(9600);   Serial.begin(9600);
-  myservo.write(positionServo, speedServo, true);+  // myservo.write(positionServo, speedServo, true)
 +  newData = false;
 } }
  
 void loop() { void loop() {
-  if(Serial.available() > 0) { +  recvWithEndMarker(); 
-    strJSON Serial.readStringUntil('\n'); +  if (newData == true{ 
-    JsonObject& root = jsonBuffer.parseObject(strJSON);+    JsonObject& root = jsonBuffer.parseObject(receivedChars);
  
     if (!root.success()) {     if (!root.success()) {
Ligne 249: Ligne 280:
  
       // Print values.       // Print values.
-      // Serial.println(positionServo); +      Serial.println(positionServo); 
-      // Serial.println(speedServo);+      Serial.println(speedServo);
       // myservo.write(positionServo, speedServo); //, true);        // move to 180 degrees, use a speed of 30, wait until move is complete       // myservo.write(positionServo, speedServo); //, true);        // move to 180 degrees, use a speed of 30, wait until move is complete
     } else {     } else {
       Serial.println("parseObject() servo key not found");             Serial.println("parseObject() servo key not found");      
     }     }
 +  
 +  newData = false;
 +  }  
 +}
 +</code>
 +
 +
 +Arduino: Programme complet (JSON, relai, ventilo, LDR, DHT11, servo-moteur):
 +<code>
 +#include <VarSpeedServo.h> 
 +#include <ArduinoJson.h>
 +#include "DHT.h"
 +
 +#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];   // an array to store the received data
 +boolean newData = false;
 +
 +void recvWithEndMarker() {
 +    static byte ndx = 0;
 +    char endMarker = '\n';
 +    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] = '\0'; // terminate the string
 +            ndx = 0;
 +            newData = true;
 +        }
 +    }
 +}
 +
 +
 +void setup() {
 +  pinMode(VENTILOPIN, OUTPUT);
 +  pinMode(RELAIPIN, OUTPUT); // relai, broche en mode sortie
 +  pinMode(SERVOPIN, OUTPUT);
 +
 +  time = millis();
 +  dht.begin();
 +  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
 +  Serial.begin(9600);
 +  newData = false;
 +}
 +
 +void loop() {
 +  StaticJsonBuffer<120> jsonBuffer;
 +  recvWithEndMarker();
 +  if (newData == true) {
 +    JsonObject& root = jsonBuffer.parseObject(receivedChars);
 +
 +    if (!root.success()) {
 +      Serial.println("parseObject() failed");
 +      receivedChars[0] = '\0';
 +      return;
 +    }
 +    if(root.containsKey("servo")) {
 +      positionServo = root["servo"][0];
 +      speedServo = root["servo"][1];
 +      myservo.write(positionServo, speedServo, true);
 +    }
 +    
 +    if (root.containsKey("ventilo")) {
 +      int ventiloValue = root["ventilo"];
 +      if (ventiloValue == 1) {
 +        ventiloState = HIGH;
 +      } else {
 +        ventiloState = LOW;
 +      }            
 +    }
 +    
 +    if (root.containsKey("relai")) {
 +      int relaiValue = root["relai"];
 +      if (relaiValue == 1) {
 +        relayState = HIGH;
 +      } else {
 +        relayState = LOW;
 +      }
 +    } else {
 +      Serial.println("parseObject() servo key not found");
 +    }
 +  
 +  newData = false;
   }   }
-  myservo.write(positionServospeedServotrue);  +  diff = millis() - time; 
 +  if (diff >= checkDHT) { 
 +    time = millis(); 
 +    float h = dht.readHumidity(); 
 +    float t = dht.readTemperature(); 
 +    // read the value from the sensor: 
 +    ldrValue = analogRead(LDRPIN); 
 + 
 +    Serial.print("{\"hum\":"); 
 +    Serial.print(h); 
 +    Serial.print(",\"temp\":"); 
 +    Serial.print(t); 
 +    Serial.print(",\"ldr\":"); 
 +    Serial.print(ldrValue); 
 +    Serial.println("}"); 
 +  }  
 +  // set the relay:  
 +  digitalWrite(RELAIPIN, relayState); 
 + 
 +  // set the ventilo:  
 +  digitalWrite(VENTILOPIN, ventiloState);
 } }
 </code> </code>
  
projets/2019/stagefevrierubo.txt · Dernière modification : 2024/04/16 22:26 de 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki