Bonjour,
J'ai un Arduino qui récupère des infos de capteurs.
Dans un premier sketch, je récupère les infos et génère un JSON qui est sorti vers le port série. Tous les capteurs fonctionnent, PIR y compris.
Dans un second, je récupère les mêmes infos, je génère aussi un JSON qui est publié sur un broker MQTT. Et la, tous les capteurs fonctionnent SAUF le PIR qui me sort toujours 0, que je gesticule devant ou pas.
Le code qui gère le PIR est identique en tous points...
Voici le code qui fonctionne :
#include <EmonLib.h>
EnergyMonitor emon1;
int tension = 230.0;
int pin_sct = 2;
//YF-S201_1
volatile int flow_frequency_1; // Measures flow meter pulses
unsigned int l_min_1; // Calculated litres/hour
unsigned char flowmeter_1 = 2; // digital PIN 2
unsigned long currentTime;
unsigned long cloopTime;
//LCD
//#include <Wire.h>
//#include <LiquidCrystal_I2C.h>
//LiquidCrystal_I2C lcd(0x27,15, 2);
//DHT22
#include "dht.h"
#define dht_apin_1 A0 // Analog Pin sensor is connected to
dht DHT_1;
//PIR
int pirPin_1 = 52; //the digital pin connected to the PIR sensor's output
int pir_1; //valeur (0/1) renvoyée à Node Red
//fonction YF-S201_1
void flow_1 () // Interrupt function
{
flow_frequency_1++;
}
void setup()
{
//Demarrage du port série
Serial.begin(115200);
//LCD
//lcd.init();
//lcd.backlight();
//PIR
pinMode(pirPin_1, INPUT);
digitalWrite(pirPin_1, LOW);
//YF-S201 (flow sensor)
pinMode(flowmeter_1, INPUT);
attachInterrupt(0, flow_1, RISING);
sei();
currentTime = millis();
cloopTime = currentTime;
//AS-103 capteur de courant
emon1.current(pin_sct,29);
}
void loop ()
{
currentTime = millis();
// Every second, calculate and print litres/hour + récupère la valeur des sondes
if(currentTime >= (cloopTime + 1000))
{
cloopTime = currentTime; // Updates cloopTime
//YF-S201 : Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min. (Results in +/- 3% range)
l_min_1 = (flow_frequency_1 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flow rate in L/hour
flow_frequency_1 = 0; // Reset Counter
//lecture DHT22
int readData = DHT_1.read22(dht_apin_1);
//variables DHT22
float t_1 = DHT_1.temperature;
float h_1 = DHT_1.humidity;
//photoresistance
int phR_1 = analogRead(A1);
//PIR
if(digitalRead(pirPin_1) == HIGH)
{
pir_1 = 1;
}
if(digitalRead(pirPin_1) == LOW)
{
pir_1 = 0;
}
double Irms = emon1.calcIrms(1480); //calcul du courant ?
//sortie JSON série
//ouverture JSON
Serial.print("{");
//DHT22_1
Serial.print("\"DHT_T_1\":");
Serial.print(t_1);
Serial.print(",\"DHT_H_1\":");
Serial.print(h_1);
//PIR_1
Serial.print(",\"PIR_1\":");
Serial.print(pir_1);
//photo resistance
Serial.print(",\"phR_1\":");
Serial.print(phR_1);
Serial.print(",\"YF_S201_1\":");
Serial.print(l_min_1, DEC); //litres par minute
Serial.print(",\"AS_103_1\":");
Serial.print(Irms);
//fermeture JSON
Serial.println("}");
//affichage LCD
//lcd.setCursor(0, 0);
//lcd.print(t);
//lcd.setCursor(0, 1);
//lcd.print(h);
}
}
Et le code que je voudrais voir fonctionner (le PIR me sort '0' ...)
#include <SPI.h>
#include <PubSubClient.h>
#include <Ethernet.h>
//DHT-22
#include <DHT.h>
#define DHT_1PIN A0 //Data Pin
#define DHT_1TYPE DHT22
#define MQTT_TOPIC "Arduino1" //TOPIC to Publish
DHT dht_1(DHT_1PIN, DHT_1TYPE);
//mq-2 capteur de fumées
int mq2_1 = A4;
int mq2Thres = 400;
#include <EmonLib.h>
EnergyMonitor emon1;
int tension = 230.0;
int pin_sct = 2;
//YF-S201_1
volatile int flow_frequency_1; // Measures flow meter pulses
unsigned int l_min_1; // Calculated litres/hour
unsigned char flowmeter_1 = 2; // digital PIN 2
unsigned long currentTime;
unsigned long cloopTime;
//PIR
int pirPin_1 = 52; //the digital pin connected to the PIR sensor's output
int pir_1; //valeur (0/1) renvoyée à Node Red
//fonction YF-S201_1
void flow_1 () // Interrupt function
{
flow_frequency_1++;
}
//Ethernet Shield Setup - Start.
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 50, 211); // Should be changed according to the subnet
IPAddress server (192,168,50,210);
//Setting up mqtt client
EthernetClient ethClient;
PubSubClient client(ethClient);
void setup() {
//PIR
pinMode(pirPin_1, INPUT);
digitalWrite(pirPin_1, LOW);
//MQ-2
pinMode(mq2_1, INPUT);
//YF-S201 (flow sensor)
pinMode(flowmeter_1, INPUT);
attachInterrupt(0, flow_1, RISING);
sei();
//AS-103 capteur de courant
emon1.current(pin_sct,29);
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial.println("Setting up server");
Ethernet.begin(mac, ip);
delay(2000);
dht_1.begin();
Serial.println("Setting up server");
client.setServer(server,1883);
MQTT_connect();
}
void loop() {
float h_1 = dht_1.readHumidity();
float t_1 = dht_1.readTemperature();
if (isnan(h_1) ||isnan(t_1)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
//YF-S201 : Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min. (Results in +/- 3% range)
l_min_1 = (flow_frequency_1 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flow rate in L/hour PAS CERTAIN QUE CE SOIT CORRECT MAIS BON ... :)
flow_frequency_1 = 0; // Reset Counter
//photoresistance
int phR_1 = analogRead(A1);
//PIR
if(digitalRead(pirPin_1) == HIGH)
{
pir_1 = 1;
}
if(digitalRead(pirPin_1) == LOW)
{
pir_1 = 0;
}
//AS-103
double Irms = emon1.calcIrms(1480); //calcul du courant ?
//MQ-2
int mq2_1Value = analogRead(mq2_1);
//Build json object
String jsonPayload = "{\"DHT_1_t\":\"";
jsonPayload += t_1;
jsonPayload +="\",\"DHT_1_h\":\"";
jsonPayload += h_1;
jsonPayload +="\",\"YF-S201_1\":\"";
jsonPayload += l_min_1;
jsonPayload +="\",\"phR_1\":\"";
jsonPayload += phR_1;
jsonPayload +="\",\"PIR_1\":\"";
jsonPayload += pir_1;
jsonPayload +="\",\"AS-103_1\":\"";
jsonPayload += Irms;
jsonPayload +="\",\"MQ-2_1\":\"";
jsonPayload += mq2_1Value;
jsonPayload +="\"}";
const char *payload = jsonPayload.c_str(); // convert string to const char*
Serial.println(jsonPayload);
//airquality.publish(jsonPayload);
if(!client.connected()){
Serial.println("Not Connected");
}
boolean result = client.publish(MQTT_TOPIC, payload);
Serial.println(result);
delay(1000);
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
if(!client.connected()){
Serial.println("Not connected");
boolean connectionResult = client.connect("pjd-mqtt",MQTT_TOPIC,1,0,"Test Connection");
Serial.println(connectionResult);
Serial.println("MQTT Connected! ");
}
}
void callback(char* topic, byte* payload, unsigned int length) {
}
Si vous avez une idée ... Ça m'arrangeait
Merci !!