GPRS doesn't send informations when connected to anemometer - arduino

I've connected arduino to 4 different modules :
RTC
GPRS shield ( SARA G350 )
Wind vane
Anemometer
Everytime I launch my system the GPRS doesn't send anything unless I disconnect my anemometer.
I've tried the anemometer code on standalone and it works, I think the GPRS doesn't get enough power to send data when the anemometer is connected but how I'm not sure.
#include <SoftwareSerial.h>
#include <Wire.h>
#include <ds3231.h>
struct ts t;
String Angle;
int serial_in;
double x = 0;
double y = 0;
double a = 0;
double b = 0;
const int sensorPin = A5;
const int numReadings = 10;
int readings[numReadings];
int readIndex = 0;
int totalWind= 0;
int averageWind = 0;
int inputPin = A5;
int sensorValue = 0;
float sensorVoltage = 0;
float sensorVoltage2 = 0;
float windSpeed = 0;
float voltageConversionConstant = .004882814;
int sensorDelay = 2000;
float voltageMin = .4;
float windSpeedMin = 0;
float voltageMax = 2.0;
float windSpeedMax = 32;
SoftwareSerial mySerial(2, 3); //Tx & Rx sont connectés aux broches Arduino #7 et #8
void setup()
{
delay(15000);
Wire.begin();
DS3231_init(DS3231_INTCN);
//Commence la communication Serie
//Commence la communication Serie Arduino-Shield GPRS
mySerial.begin(9600);
delay(1000);
mySerial.println("AT"); //Handshaking
updateSerial();
mySerial.println("AT+UPSDA=2,0"); //Reset connexion
updateSerial();
delay(2000);
mySerial.println("AT+UPSD=2,1,\"sl2sfr\""); //Establissement de connexion avec l'APN
updateSerial();
delay(2000);
mySerial.println("AT+UPSDA=2,3");
updateSerial();
delay(2000);
mySerial.println("AT+UPSND=2,0");
updateSerial();
delay(2000);
}
void loop()
{
mySerial.println("AT+UPSDA=2,0"); //Reset connexion
updateSerial();
delay(2000);
mySerial.println("AT+UPSD=2,1,\"sl2sfr\""); //Establissement de connexion avec l'APN
updateSerial();
delay(2000);
mySerial.println("AT+UPSDA=2,3");
updateSerial();
delay(2000);
mySerial.println("AT+UPSND=2,0");
updateSerial();
delay(2000);
String Equipement = "STAINS";
mySerial.println("AT+UHTTP=0");
updateSerial();
delay(2000);
mySerial.println("AT+UHTTP=2,1,\"www.projetwmr.site\""); // Parametrage URL d'acces
updateSerial();
delay(2000);
String command = "AT+UHTTPC=2,5,\"/add.php\",\"post.ffs\",\"vite="; // Commande d'envoi des donnes via POST sur PHP
anemometre(); // Recuperation des données ANALOGIQUES
// convertion valeurs en String - Chaîne de caractères
command += String(windSpeed);
// or convertion précise
// command += String(Windspeed, 2);
command += "&equipement=";
command += String(Equipement);
DS3231_get(&t);
String heure = String(t.hour);
heure += ":";
heure += String(t.min);
heure += ":";
heure += String(t.sec);
String Date = String(t.year);
Date += "-";
Date += String(t.mon);
Date += "-";
Date += String(t.mday);
command += "&time=";
command += String(heure);
command += "&date=";
command += String(Date);
command += "&dire=";
girouette();
command += String(Angle);
command += "\",0"; //Fin de la commande PHP POST
mySerial.println(command);
updateSerial();
delay(45000);
}
void updateSerial()
{
delay(500);
while (Serial.available())
{
mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while(mySerial.available())
{
Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
}
}
void girouette()
{
int sensorValue = analogRead(A1);
if (sensorValue >= 89 && sensorValue<=95 )
{ Angle="0°"; }
if (sensorValue >=60 && sensorValue<=70 )
{ Angle="22.5°"; }
if (sensorValue >=180 && sensorValue<=190 )
{ Angle="45°"; }
if (sensorValue >= 124 && sensorValue<=130 )
{ Angle="67.5°"; }
if (sensorValue >= 285 && sensorValue<=292 )
{ Angle="90°"; }
if (sensorValue >=240 && sensorValue<=250 )
{ Angle="115.5°"; }
if (sensorValue >= 630 && sensorValue<=640 )
{ Angle="135°"; }
if (sensorValue >= 600 && sensorValue<=610 )
{ Angle="157.5°"; }
if (sensorValue >=940 && sensorValue<=952 )
{ Angle="180°"; }
if (sensorValue >=825 && sensorValue<= 840 )
{ Angle="202.5°"; }
if (sensorValue >= 880 && sensorValue<= 898 )
{ Angle="225°"; }
if (sensorValue >= 700 && sensorValue<= 712 )
{ Angle="247.5°"; }
if (sensorValue >= 785 && sensorValue<= 795 )
{ Angle="270°"; }
if (sensorValue >= 405 && sensorValue<= 415)
{ Angle="292.5°"; }
if (sensorValue >= 460 && sensorValue<= 470 )
{ Angle="315°"; }
if (sensorValue >= 78 && sensorValue<=87 )
{ Angle="337.5°"; }
if (sensorValue >= 1000 && sensorValue<=50 )
{ Angle="Error"; }
delay(100);
}
void anemometre()
{
sensorValue = analogRead(A5);
totalWind = totalWind - readings[readIndex];
readings[readIndex] = sensorValue;
totalWind = totalWind + readings[readIndex];
readIndex = readIndex + 1;
sensorVoltage2 = sensorValue * voltageConversionConstant;
if (readIndex >= numReadings) {
readIndex = 0;
averageWind = totalWind / numReadings;
sensorVoltage = averageWind * voltageConversionConstant;
if (sensorVoltage <= voltageMin) {
windSpeed = 0;
} else {
windSpeed = ((sensorVoltage - voltageMin) * windSpeedMax / (voltageMax - voltageMin))*1.55;
}
}
x = windSpeed;
if (x >= y) {
y = x;
} else {
y = y;
}
a = sensorVoltage;
if (a >= b) {
b = a;
} else {
b = b;
}
}

If you don't have a reason to use it, you should avoid SoftwareSerial, it is known to be quite inefficient and it's most likely leading to issues with the other libraries you're loading.
Always use hardware serial ports if you have them available. See here for alternatives.

Related

Rfid with Adafruit fingerprint together not working in Arduino Uno

I am trying to create an ATM module using Arduino. Im using a RFID reader to scan the card and Adafruit Fingerprint Module for fingerprint verification. The issue is both work but not together. Here is my code:
#include <Key.h>
#include <Keypad.h>
#include <Adafruit_Fingerprint.h>
#include <SD.h>
#include <SPI.h>
#include<SoftwareSerial.h>
SoftwareSerial mySerial(0, 1);
SoftwareSerial mySerials(2, 3);
int read_count = 0, tag_count = 0;
int j = 0, k = 0; // Variabvles to iterate in for loops
char data_temp, RFID_data[12], data_store[12];
boolean disp_control;
char filename[4], fileText[30], names[10];
int atm[5];
uint8_t n, num, p;
char key;
int p1, p2, p3, p4, z;
char fid;
File myFile;
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerials);
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypads = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
finger.begin(57600);
mySerial.begin(9600);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
Serial.println("Please Place Your Card");
}
void RecieveData()
{
if (mySerial.available() > 0)
{
data_temp = mySerial.read();
RFID_data[read_count] = data_temp;
read_count++;
}
}
void ReadData()
{
if (read_count == 12) {
disp_control = true;
for (j = 0; j < 12; j++) {
data_store[j] = RFID_data[j];
}
int x = 0;
for (int i = 9; i < 12; i++) {
filename[x] = data_store[i];
x++;
}
filename[3] = '.';
filename[4] = 't';
filename[5] = 'x';
filename[6] = 't';
filename[7] = '\0';
myFile = SD.open(filename, FILE_READ);
if (myFile) {
// read from the file until there's nothing else in it:
while (myFile.available()) {
for (int i = 0; i < 20; i++) {
fileText[i] = myFile.read();
}
int a, n = 0;
for (int i = 0; i < 4; i++) {
atm[a] = fileText[i];
a++;
}
for (int i = 5; fileText[i] != '\0'; i++) {
names[n] = fileText[i];
n++;
}
fid = fileText[4];
validate();
myFile.close();
}
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
read_count = 0;
tag_count++;
}
}
void validate() {
Serial.print("Hello ");
Serial.print(names);
Serial.println();
Serial.println();
Serial.println();
pin();
}
void pin() {
Serial.println("Please Enter Your ATM PIN");
p1 = keypads.getKey();
while (p1 == NO_KEY) {
p1 = keypads.getKey(); //UPDATE VALUE
}
Serial.print(p1 - 48);
p2 = keypads.getKey();
while (p2 == NO_KEY) {
p2 = keypads.getKey(); //UPDATE VALUE
}
Serial.print(p2 - 48);
p3 = keypads.getKey(); //UPDATE VALUE
while (p3 == NO_KEY) {
p3 = keypads.getKey(); //UPDATE VALUE
}
Serial.print(p3 - 48);
p4 = keypads.getKey(); //UPDATE VALUE
while (p4 == NO_KEY) {
p4 = keypads.getKey(); //UPDATE VALUE
}
Serial.print(p4 - 48);
Serial.println();
if ((p1 == fileText[0]) && (p2 == fileText[1]) && (p3 == fileText[2]) && (p4 == fileText[3])) {
Serial.println("Please verify Fingerprint");
}
else {
Serial.println("Invalid. Buzzer!!!!");
}
}
uint8_t getFingerprintID() {
uint8_t p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.println("No finger detected");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return p;
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
return p;
default:
Serial.println("Unknown error");
return p;
}
// OK success!
p = finger.image2Tz();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image converted");
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("Image too messy");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return p;
case FINGERPRINT_FEATUREFAIL:
Serial.println("Could not find fingerprint features");
return p;
case FINGERPRINT_INVALIDIMAGE:
Serial.println("Could not find fingerprint features");
return p;
default:
Serial.println("Unknown error");
return p;
}
// OK converted!
p = finger.fingerFastSearch();
if (p == FINGERPRINT_OK) {
Serial.println("Found a print match!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
return p;
} else if (p == FINGERPRINT_NOTFOUND) {
Serial.println("Did not find a match");
return p;
} else {
Serial.println("Unknown error");
return p;
}
// found a match!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
return finger.fingerID;
}
int getFingerprintIDez() {
uint8_t p = finger.getImage();
if (p != FINGERPRINT_OK) return -1;
p = finger.image2Tz();
if (p != FINGERPRINT_OK) return -1;
p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK) return -1;
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
return finger.fingerID;
// found a match!
/*z = finger.fingerID;
if ((fid - 48) == z) {
Serial.println("Success");
}
else {
Serial.println("Fail");
}*/
}
void loop() {
RecieveData();
ReadData();
getFingerprintIDez();
delay(50);
}
If I initialize in this format, then fingerprint works:
mySerial.begin(9600);
finger.begin(57600);
Serial.begin(9600);
And if i do it this way, then RFID works:
finger.begin(57600);
mySerial.begin(9600);
Serial.begin(9600);
I want both of them to work i.e; I first Scan the Card, then verify pin and then it checks the validity of the fingerprint. But the issue is only one of them works at a time. If RFID works, then fingerprint doesn't even blink and if Fingerprint works, RFID doesn't read.
I'm new to this and I don't know where I am going wrong.

Storing the value read previously until new pulse

I'm currently doing a project on an Arduino Uno. The project is based on receiving an IR Signal from an IR Remote and then based on the signal received, perform other operations.
The problem is that the signal gets reset every time. I want to store the value received from the IR Remote and then resets it if detects another pulse.
Here is my code :
int brojac = 0;
int pinData = 10;
unsigned long lengthHeader;
unsigned long bit;
int byteValue;
int vrime = 1000 ;
int storeValue = 0;
void setup()
{
Serial.begin(9600);
pinMode(pinData, INPUT);
}
void loop() {
lengthHeader = pulseIn(pinData, LOW);
if (lengthHeader > 1500)
{
for (int i = 1; i <= 32; i++) {
bit = pulseIn(pinData, HIGH);
if (i > 16 && i <= 24)
if (bit > 1000)
byteValue = byteValue + (1 << (i - 17));
}
}
Serial.print("byteValue = ");
Serial.println(byteValue);
if(byteValue == 66){
digitalWrite(11,HIGH);
}
else{
digitalWrite(11,LOW);
}
delay(vrime);
byteValue = 0;
delay(250);
}
I got the answer by storing the value in a variable until a new variable is detected.
int pinData = 10;
int led = 11;
unsigned long lengthHeader;
unsigned long bit;
int byteValue;
int storeValue = 0;
int previousValue = 0;
void setup()
{
Serial.begin(9600);
pinMode(pinData, INPUT);
pinMode(led, LOW);
}
void loop() {
lengthHeader = pulseIn(pinData, LOW);
if (lengthHeader > 1500)
{
for (int i = 1; i <= 32; i++) {
bit = pulseIn(pinData, HIGH);
if (i > 16 && i <= 24)
if (bit > 1000)
byteValue = byteValue + (1 << (i - 17));
}
}
Serial.print("byteValue = ");
Serial.println(byteValue);
**storeValue = byteValue;
if (storeValue != 0){
previousValue = storeValue;
}
Serial.print("Previous value = ");
Serial.println(previousValue);**
byteValue = 0;
delay(500);
}

Sending message to the extracted number

Please someone let me know why this code is not working. I want to extract message sender's number and then forward message to it using AT commands. It extracts the number of sender and stores it in a variable but why won't it send a message to that number?
#include <GSM.h>
GSM_SMS sms;
char RcvdMsg[200] = "";
int RcvdCheck = 0;
int RcvdConf = 0;
int index = 0;
int RcvdEnd = 0;
char MsgMob[15];
char MsgTxt[50];
int MsgLength = 0;
char number1[12] = "xxxxxxxxxx";
String number;
char inchar;
char outString[22];
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial1.print("ATE0\r");
Serial1.print("AT\r");
Serial1.print("AT+CMGF=1\r");
Serial1.print("AT+CNMI=1,2,0,0,0\r");
delay(1000);
}
void loop()
{
recSms();
}
void recSms()
{
if(Serial1.available())
{
char data = Serial1.read();
if(data == '+'){RcvdCheck = 1;}
if((data == 'C') && (RcvdCheck == 1)){RcvdCheck = 2;}
if((data == 'M') && (RcvdCheck == 2)){RcvdCheck = 3;}
if((data == 'T') && (RcvdCheck == 3)){RcvdCheck = 4;}
if(RcvdCheck == 4){RcvdConf = 1; RcvdCheck = 0;}
if(RcvdConf == 1)
{
if(data == '\n'){RcvdEnd++;}
if(RcvdEnd == 3){RcvdEnd = 0;}
RcvdMsg[index] = data;
index++;
if(RcvdEnd == 2){RcvdConf = 0;MsgLength = index-2;index = 0;}
if(RcvdConf == 0)
{
Serial.print("Mobile Number is: ");
for(int x = 4;x < 17;x++)
{
number+=RcvdMsg[x];
MsgMob[x-4] = RcvdMsg[x];
}
Serial.print(number);
Serial.println();
Serial.print("Message Text: ");
for(int x = 46; x < MsgLength; x++)
{
MsgTxt[x-46] = RcvdMsg[x];
inchar=MsgTxt[x-46];
}
Serial.print(inchar);
Serial.println();
RcvdCheck = 0;
RcvdConf = 0;
index = 0;
RcvdEnd = 0;
MsgMob[15];
MsgTxt[50];
MsgLength = 0;
Serial.flush();
Serial1.flush();
if(inchar == '#')
{
sendInfo();
}
}
}
}
}
void sendInfo()
{
Serial1.print("AA");
delay(1000); //delay of 1
Serial1.println("AT");
delay(1000);
Serial1.write("AT+CMGF=1\r\n"); //set GSM to text mode
delay(1000);
Serial1.write("AT+CPMS=\"SM\"\r\n"); //Preferred SMS Message Storage
delay(1000);
Serial1.print("AT+CMGS=\"");
Serial1.print(number1);
Serial1.print("\"");
delay(1000);
Serial1.print("HI");
delay(1000);
Serial1.write(0x1A); // sends ctrl+z end of message
delay(1000);
Serial.println("sms sent ");
} //end sendInfo()
Okay so the problem seems to be with these lines:
Serial1.print("AT+CMGS=\"");
Serial1.print(number1);
Serial1.print("\"");
But if we write the lines written below, program works just fine!
Serial1.write("AT+CMGS=\"");
Serial1.print(number);
Serial1.write("\"\r");

Rectangle pattern using arduino Mega

I am using an Arduino Mega to implement a Rectangle pattern movement of a device which mean the length and breadth should decrease each time it reaches the end edges. I have written the code but it is not working
//servo header
#include <Servo.h>
#include <Math.h>
Servo myservo;//servo Object
//motor pin config
int m1r = 9;
int m1f = 10;
int m2f = 12;
int m2r = 11;
//infrared counter
int ir1val=0;//pulse count from irval
int ir2val=0;//pulse count from irval
int len=1;
int bred=2;
//Different sensors used
int temp;
int buzz = 8;
const float radpulse = 0.05;//1 pulse = 0.05 meter
double pulsecount1;//pulse counter;
double pulsecount2;//pulse counter;
int pc1;//integer
int pc2;
void setup() {
// initialize the Motor pin as an output:
pinMode(m1f, OUTPUT);
pinMode(m1r, OUTPUT);
pinMode(m2f, OUTPUT);
pinMode(m2r, OUTPUT);
pinMode(buzz,OUTPUT);
myservo.attach(A0);//BLDC motor attachment
Serial.begin(9600);// standard serial bandwidth of all modules
}
void loop()
{
pulsecount1=len/radpulse;
pc1=(int)round(pulsecount1);
pulsecount2=bred/radpulse;
pc2=(int)round(pulsecount2);
pc1=3;
pc2=2;
myservo.write(30);
delay(5000);
labelL1:
if((pc2 == 0)&&(pc1 == 0))
{
goto Stop;
}
while(pc1 > ir1val)
{
temp=((5.0*analogRead(A3)*100)/1024);
if(temp >60)
{
// goto hibernate;
}
/* if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
digitalWrite(m2f,HIGH);
digitalWrite(m1f,HIGH);
//digitalWrite(m2f,HIGH);
if(digitalRead(A2))
{
ir1val++;
}
}
if(pc1 == ir1val)
{
/*if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
pc1--;
ir1val=0;
digitalWrite(m1f, HIGH);
digitalWrite(m2f,LOW);
delay(5000);
goto labelB1;
}
labelB1:
while(pc2 != ir1val)
{
temp=((5.0*analogRead(A3)*100)/1024);
if(temp >60)
{
// goto label2;
}
/* if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
digitalWrite(m1f,HIGH);
digitalWrite(m2f,HIGH);
if(digitalRead(A2))
{
ir2val++;
}
}
if(pc2 == ir2val)
{
/* if((analogread(A14) <= 300 || analogread(A14) >= 400) || (analogread(A15) <= 300) || analogread(A15) == 400))
{
goto Stop;
}*/
pc2=pc2--;
ir2val=0;
digitalWrite(m1f, HIGH);
digitalWrite(m2f,LOW);
delay(5000);
goto labelL1;
}
Stop:
myservo.write(0);// stop servo
digitalWrite(m1f,LOW);// stop motor
digitalWrite(m2f,LOW);//stop motor
digitalWrite(buzz,HIGH);//Buzzer
delay(5000);
goto Stop;
/*
hibernate:
temp=((5.0*analogRead(A2)*100)/1024);
if(temp >40)
{
delay(1000);
goto hibernate;
}
*/}

Arduino - cannot get buzzer to buzz

I've hooked up a buzzer to pin 13 & ground on my Arduino.
The "Blink" example works fine and the buzzer sounds every second off and on as expected.
However, when I try to do the same buzz with my code, I can't get it to buzz upon a specific event. This is a homegrown security system - when the door is opened, I want the Arduino to check a PHP page which returns "armed" if the system has been armed.
Everything else seems to work except the buzzer part.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xD3, 0x6C };
char serverName[] = "mysite.com";
String currentLine = "";
String armed = "No";
int nFrontWindow = 0;
int sFrontWindow = 1;
int kitchenWindow = 2;
int bedroomWindow = 3;
int frontDoor = 4;
int val0 = 0;
int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;
int threshold0 = 20;
int threshold1 = 20;
int threshold2 = 20;
int threshold3 = 20;
int threshold4 = 20;
int breach0 = 0;
int breach1 = 0;
int breach2 = 0;
int breach3 = 0;
int breach4 = 0;
int alarm0 = 0;
int alarm1 = 0;
int alarm2 = 0;
int alarm3 = 0;
int alarm4 = 0;
EthernetClient client;
void setup() {
pinMode(13, OUTPUT);
// initialize serial communications at 9600 bps:
Serial.begin(9600);
if(Ethernet.begin(mac) == 0) { // start ethernet using mac & IP address
Serial.println("Failed to configure Ethernet using DHCP");
while(true) // no point in carrying on, so stay in endless loop:
;
}
delay(1000); // give the Ethernet shield a second to initialize
}
void loop() {
// read the analog in values:
val0 = analogRead(nFrontWindow);
val1 = analogRead(sFrontWindow);
val2 = analogRead(kitchenWindow);
val3 = analogRead(bedroomWindow);
val4 = analogRead(frontDoor);
// print the analog in values:
if (val0 > threshold0)
{
Serial.print("nFrontWindow: ");
Serial.print(val0);
Serial.print("\n");
if (alarm0)
{
if (breach0 < 10)
breach0++;
}
else
{
if (breach0 > 9)
{
alarm0 = 1;
send_alert(0);
}
else
breach0++;
}
}
else
{
if (alarm0)
{
if (breach0 > 0)
breach0--;
else
{
alarm0 = 0;
send_alert(10);
}
}
else
{
if (breach0 > 0)
breach0--;
}
}
if (val1 > threshold1)
{
Serial.print("sFrontWindow: ");
Serial.print(val1);
Serial.print("\n");
if (alarm1)
{
if (breach1 < 10)
breach1++;
}
else
{
if (breach1 > 9)
{
alarm1 = 1;
send_alert(1);
}
else
breach1++;
}
}
else
{
if (alarm1)
{
if (breach1 > 0)
breach1--;
else
{
alarm1 = 0;
send_alert(11);
}
}
else
{
if (breach1 > 0)
breach1--;
}
}
if (val2 > threshold2)
{
Serial.print("kitchenWindow: ");
Serial.print(val2);
Serial.print("\n");
if (alarm2)
{
if (breach2 < 10)
breach2++;
}
else
{
if (breach2 > 9)
{
alarm2 = 1;
send_alert(2);
}
else
breach2++;
}
}
else
{
if (alarm2)
{
if (breach2 > 0)
breach2--;
else
{
alarm2 = 0;
send_alert(12);
}
}
else
{
if (breach2 > 0)
breach2--;
}
}
if (val3 > threshold3)
{
Serial.print("bedroomWindow: ");
Serial.print(val3);
Serial.print("\n");
if (alarm3)
{
if (breach3 < 10)
breach3++;
}
else
{
if (breach3 > 9)
{
alarm3 = 1;
send_alert(3);
}
else
breach3++;
}
}
else
{
if (alarm3)
{
if (breach3 > 0)
breach3--;
else
{
alarm3 = 0;
send_alert(13);
}
}
else
{
if (breach3 > 0)
breach3--;
}
}
if (val4 > threshold4)
{
Serial.print("frontDoor: ");
Serial.print(val4);
Serial.print("\n");
if (alarm4)
{
if (breach4 < 10)
breach4++;
}
else
{
if (breach4 > 9)
{
alarm4 = 1;
send_alert(4);
}
else
breach4++;
}
}
else
{
if (alarm4)
{
if (breach4 > 0)
breach4--;
else
{
alarm4 = 0;
send_alert(14);
}
}
else
{
if (breach4 > 0)
breach4--;
}
}
delay(100);
}
void send_alert(int pin)
{
if (client.connect(serverName, 80)>0) {
client.flush();
Serial.print("\nconnected... ");
String link = "GET [PHP FILE GOES HERE]";
link += pin;
link += " HTTP/1.1";
Serial.print("Sending alert code: ");
Serial.print(pin);
Serial.print("\n\n");
client.println(link);
client.println("Host: mysite.com");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
delay(3000);
} else {
Serial.println("connection failed");
//handle this
}
Serial.print("Server response:\n");
Serial.print("----------------\n");
while(client.available()){
char c = client.read();
Serial.print(c);
currentLine += c;
if (c == '\n') {
currentLine = "";
}
if (currentLine.endsWith("armed"))
{
if (pin == 4 || pin == 3 || pin == 2 || pin == 1 || pin == 0)
{
Serial.print("\nBZZZZZZZZZZ\n");
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
}
}
Serial.print("\n----------------\n");
//else
// Serial.println("result not found");
client.stop();
client.flush();
}
I think your problem is that pin 13 is used by the Ethernet Shield. So are pin 10, 11, and 12. So use a pin other then them.
I think you problem is that you aren't generating a square wave to make sound the buzzer.
Since you are using Arduino you can just use tone() and noTone() functions, well explained in Arduino site.

Resources