Browse Source

erste codes für ultraschall/pir sensor, die direkt daten an nodered schicken

master
Johannes Krug 5 years ago
parent
commit
5f16f4c9cd

+ 35
- 0
arduino/HC_SR04/HC_SR04.ino View File

#include <NewPing.h>

#define TRIGGER_PIN 4

#define ECHO_PIN 3

#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {

Serial.begin(9600);

}

void loop() {

delay(50);

unsigned int uS = sonar.ping();

pinMode(ECHO_PIN,OUTPUT);

digitalWrite(ECHO_PIN,LOW);

pinMode(ECHO_PIN,INPUT);

Serial.print("Ping: ");

Serial.print(uS / US_ROUNDTRIP_CM);

Serial.println("cm");

}

+ 35
- 0
arduino/Ultrasonic_Sensor/Ultrasonic_Sensor.ino View File

// defines pins numbers
const int trigPin = 2; //D4
const int echoPin = 0; //D3

// defines variables
long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(2000);
}


+ 77
- 0
arduino/wlan_2bewegungssensoren/wlan_2bewegungssensoren.ino View File

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* SSID = "smartroom";
const char* PSK = "smarthome";
const char* MQTT_BROKER = "192.168.4.1";
WiFiClient espClient;
PubSubClient client(espClient);

// defines pins numbers
int ledPin = D1; // LED on Pin 1 of ESP
int pirPin1 = D0; // Input for HC-SR501 #1
int pirPin2 = D2; // Input for HC-SR501 #2
int pirValueNeu1; // Place to read PIR Value
int pirValueAlt1 = 0; //Place to store read PIR Value
int pirAn1;
int pirAus1;
int pirValueNeu2; // Place to read PIR Value
int pirValueAlt2 = 0; //Place to store read PIR Value
int pirAn2;
int pirAus2;

char msg[50];


void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(MQTT_BROKER, 1883);
pinMode(ledPin, OUTPUT);
pinMode(pirPin1, INPUT);
pinMode(pirPin2, INPUT);
digitalWrite(ledPin, LOW);
}

void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PSK);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void reconnect() {
while (!client.connected()) {
Serial.print("Reconnecting...");
if (!client.connect("ESP8266Client")) {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" retrying in 5 seconds");
delay(5000);
}
}
}


void loop() {

if((distance!=(distance_alt-1))&&(distance!=(distance_alt))&&(distance!=(distance_alt+1))){//+-1 um störungen herauszufiltern
snprintf (msg,50,"%d", distance);
Serial.print("Publish Motion: ");
Serial.println(msg);
client.publish("/home/data", msg);
delay(500);
}
}

+ 92
- 0
arduino/wlan_ultraschall/wlan_ultraschall.ino View File

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* SSID = "smartroom";
const char* PSK = "smarthome";
const char* MQTT_BROKER = "192.168.4.1";
WiFiClient espClient;
PubSubClient client(espClient);

// defines pins numbers
const int trigPin = 2; //D4
const int echoPin = 0; //D3

long duration;
int distance;
int distance_alt=0;
char msg[50];
int value = 0;
long lastMsg = 0;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input

Serial.begin(115200);
setup_wifi();
client.setServer(MQTT_BROKER, 1883);
}

void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PSK);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void reconnect() {
while (!client.connected()) {
Serial.print("Reconnecting...");
if (!client.connect("ESP8266Client")) {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" retrying in 5 seconds");
delay(5000);
}
}
}


void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
if((distance!=(distance_alt-1))&&(distance!=(distance_alt))&&(distance!=(distance_alt+1))){//+-1 um störungen herauszufiltern
snprintf (msg,50,"%d", distance);
Serial.print("Publish Motion: ");
Serial.println(msg);
client.publish("/home/data", msg);
delay(500);
}
distance_alt=distance;
}

Loading…
Cancel
Save