Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include "TimerOne.h"
- char k;
- char lastk;
- char *readings;
- char command[20];
- int timeout = 0;
- int timeout2 = 0;
- int messageMarker = 3;
- // next set are for sending the sms
- int hall = 0;
- int lasthall = 0;
- int total = 0;
- int fiftyms = 0;
- int fiftymstosend = 600;
- int timesToSend = 1; // Numbers of SMS to send
- int count = 0;
- int valvestatus = 1;
- // number of combinations of carriage return and line feed
- void setup()
- {
- pinMode(13, OUTPUT);
- pinMode(12, OUTPUT);
- Serial.begin(9600);
- //Serial.println(".");
- // next are for sending data
- Timer1.initialize(50000); // 50 miliseconds
- Timer1.pwm(9, 512); // IMPORTANT
- Timer1.attachInterrupt(callback);
- Serial.println("AT+CMGR=1");
- delay(1000);
- Serial.println("AT+CMGD=1,4");
- delay(1000);
- for (int i=0;i<2;i++){
- delay(5000);
- }
- Serial.println("AT+CMGF=1");
- }
- void callback()
- {
- fiftyms++;
- hall = analogRead(A0);
- if(hall>512 && lasthall<512)
- {
- total++;
- }
- lasthall = hall;
- }
- void loop()
- {
- /* next are for sending data
- */
- startloop:
- count=0;
- if(fiftyms >= fiftymstosend)
- {
- while (count < timesToSend){
- delay(1500);
- Serial.print("AT+CMGS="); // send the SMS the number
- Serial.print(34,BYTE); // send the " char
- Serial.print("09109627081"); // send the number change ********* by the actual number
- Serial.println(34,BYTE); // send the " char
- delay(1500);
- Serial.print("r ");
- Serial.print(total); // the SMS body
- Serial.print(" ");
- Serial.print(valvestatus);
- delay(500);
- Serial.print(0x1A,BYTE); // end of message command 1A (hex)
- delay(5000);
- count++;
- }
- fiftyms = 0;
- total=0;
- }
- //Serial.println(".");
- int gotTheWord = 0;
- if(Serial.available())
- {
- //Serial.println("+");
- int comindex = 0;
- int numchar = 0;
- int enters=0;
- int del = 0;
- while(Serial.available()>0)
- {
- //Serial.println("x");
- k = Serial.read(), BYTE;
- if(k=='C')
- {
- k= Serial.read(), BYTE;
- if(k=='M')
- {
- k = Serial.read(), BYTE;
- if(k=='G')
- {
- k= Serial.read(), BYTE;
- if(k=='R')
- {
- //Serial.println("c.m.g.r");
- del=1;
- }
- }
- else if(k=='T')
- {
- k = Serial.read(), BYTE;
- if(k=='I')
- {
- //Serial.flush();
- Serial.println("---------------NEWMSG");
- Serial.end();
- delay(50);
- Serial.begin(9600);
- delay(50);
- Serial.println("AT+CMGR=1");
- messageMarker=4;
- del = 0;
- enters = 0;
- comindex = 0;
- timeout=0;
- timeout2=0;
- goto startloop;
- }
- }
- }
- }
- //Serial.print(k);
- if(lastk==13 && k==10)
- {
- enters++;
- }
- lastk = k;
- if(enters==messageMarker)//pangatlong combo o pangapat, mejo magulo eh
- {
- if (k == 10 || k == 13)
- {
- // bakit kaya hindi gumagana ung not equal? :\
- Serial.print("");
- }
- else{
- if(k=='N')
- {
- valvestatus = 1;
- digitalWrite(13,HIGH);
- digitalWrite(12,HIGH);
- }
- if(k=='F')
- {
- valvestatus = 0;
- digitalWrite(13,LOW);
- digitalWrite(12, LOW);
- }
- command[comindex] = k;
- comindex++;
- //command[comindex] = '\0';
- //Serial.print(k);
- Serial.print(k);
- }
- }
- if(enters > messageMarker && del == 1)
- {
- //Serial.flush();
- delay(100);
- Serial.print("<");
- Serial.print(enters);
- Serial.print(":::");
- Serial.print(messageMarker);
- Serial.print(">");
- Serial.println("AT+CMGD=1,4");
- Serial.println(command);
- String commstring = (String)command;
- if(commstring.equals("ON"))
- {
- valvestatus = 1;
- digitalWrite(12,HIGH);
- digitalWrite(13,HIGH);
- }
- if(commstring.equals("OFF"))
- {
- valvestatus = 1;
- digitalWrite(13,LOW);
- digitalWrite(12,LOW);
- }
- Serial.flush();
- gotTheWord = 1;
- }
- delay(10);
- //goto startloop;
- }
- }/* needed
- else
- {
- delay(50);
- timeout++;
- if(timeout==10)
- {
- timeout=0;
- Serial.println("AT+CMGR=1");
- }
- }*/
- //Serial.flush();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement