Hardware components | ||||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 |
1.Arduino detect sparrow which annoying fammers, and make sound to kick out them.
2.Arduino send these emergence data to MQTTbroker(RaspherryPi) using wiz2750SR.
3.MQTTborker send data to app(openhab,processing)
4.openhab show the graph of sparrow emergences, frequences
5.processing show graphics about sparrow(this function could be removed)
#include "mbed.h"
#include "SPI.h"
#include "MQTTEthernet.h"
#include "MQTTClient.h"
#define ECHO_SERVER_PORT 7
Serial pc(USBTX, USBRX); //Enabling the Serial transmission between WIZ750SR and PC.
Serial serial(D1,D0); // Enabling Serial transmission betwenn Wiz750SR and W7500.
char c[100]="";
char *temp="hi";
int main(void) {
printf("Wait a second...\r\n");
// char* topic = "Vending machine";
MQTTEthernet ipstack = MQTTEthernet();
MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);
char* hostname = "172.16.73.4"; //Give the IP Address of the MQTT Broker.
int port = 1883; // Port number of the MQTT broker.
int rc = ipstack.connect(hostname, port);
if (rc != 0)
printf("rc from TCP connect is %d\n", rc);
// printf("Topic: %s\r\n",topic);
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.MQTTVersion = 3;
data.clientID.cstring = "parents";
if ((rc = client.connect(data)) == 0)
printf("rc from MQTT connect is %d\n", rc);
while (true) {
if (serial.readable()) // if anything available/ readable in Serial port
{
int i;
c[i]=0;
for(i=0;i<=1;i++){
char c1 =serial.getc();
c[i] = c1;
i++;
}
pc.printf("The value returned is %s ",c);
MQTT::Message message;
char buf[100];
//strcpy(c,);
sprintf(buf, "%s", c);
message.qos = MQTT::QOS0;
message.retained = false;
message.dup = false;
message.payload = (void*)c;
message.payloadlen = strlen(c);
rc = client.publish("/cdilab/sensors/temperature", message);
pc.printf("Rc result: %c \n ",rc);
client.yield(60);
}
}
}
code for arduino
Arduinothis detect birds using ultrasonic radar. And if detect, it send signal to wiznet
#include <SoftwareSerial.h>
#include <Servo.h>
#define TRIG 2
#define ECHO 3
#define SERV 9
Servo servo;
int k=0;
SoftwareSerial mySerial(10, 11); // RX, TX
int oldD[180];
int newD[180];
int count,detect=0;
int servoDirection = 1, rad = 0;
int speakerpin = 12; //digital pin for buzzer
int birds=0;
void setup() {
Serial.begin(9600);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
servo.attach(SERV); //pin for servo
mySerial.begin(9600);
pinMode(8,OUTPUT);
}
int compare(int oldD[],int newD[]){
count=0;
for(int i=0;i<180;i++){
if(abs(oldD[i]-newD[i])>10)
count++;
}
if(count>10)
return 1;
else
return 0;
}
void alert(){
digitalWrite(8,HIGH);
delay(1000);
digitalWrite(8,LOW);
}
void loop() {
// Serial.print(size
digitalWrite(TRIG, LOW);
delayMicroseconds(2); //stop for 2uS
digitalWrite(TRIG, HIGH);
delayMicroseconds(10); //operate for 10uS
digitalWrite(TRIG, LOW);
long distance = pulseIn(ECHO, HIGH, 5800) / 58;
Serial.print("r");
Serial.print(rad);
Serial.print("d");
Serial.println(distance);
newD[rad]=distance;
rad += servoDirection;
if (rad > 180) {
rad = 179;
servoDirection = -1;
}
else if (rad < 0) {
rad = 1;
servoDirection = 1;
}
if((rad==1)&&(servoDirection==-1)){//every one cycle
detect=compare(oldD,newD);
if(detect==1){
if(birds!=0){
Serial.println("bird");
mySerial.println(birds);
Serial.println(birds);
}
}
for(int i=0;i<180;i++)
Serial.print(oldD[i]);
Serial.println(" ");
for(int i=0;i<180;i++)
Serial.print(newD[i]);
memcpy(oldD,newD,sizeof(oldD));
}
servo.write(rad);
delay(20); //delay for survo
if(birds==0)
birds++;
if(detect==1){
alert();
}
detect=0;
}
Thanks to namgoocha.
Comments