Skip to content

Instantly share code, notes, and snippets.

@Blank1611

Blank1611/central_unit.ino

Last active May 23, 2020
Embed
What would you like to do?
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 2);
//f for full(green) led, e for empty(red) led
const int tank_1f = 3; // tank_1f is for tank 1 full led
const int tank_1e = 2; // tank_1e is for tank 1 empty led
const int tank_2f = 5;
const int tank_2e = 4;
//uncomment below lines to add more leds and EDIT respective pin numbers
//const int tank_3f = ;
//const int tank_3e = ;
//const int tank_4f = ;
//const int tank_4e = ;
//const int tank_5f = ;
//const int tank_5e = ;
//const int tank_6f = ;
//const int tank_6e = ;
//const int tank_7f = ;
//const int tank_7e = ;
const int buzzer = 6; //buzzer to arduino pin
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
String incomingString ="";
int startIndex = 0;
int endIndex = 0;
void setup() {
// put your setup code here, to run once:
// initialize the digital pin as an output.
pinMode(tank_1f, OUTPUT);
pinMode(tank_1e, OUTPUT);
pinMode(tank_2f, OUTPUT);
pinMode(tank_2e, OUTPUT);
//uncomment below lines for more leds
// pinMode(tank_3f, OUTPUT);
// pinMode(tank_3e, OUTPUT);
// pinMode(tank_4f, OUTPUT);
// pinMode(tank_4e, OUTPUT);
// pinMode(tank_5f, OUTPUT);
// pinMode(tank_5e, OUTPUT);
// pinMode(tank_6f, OUTPUT);
// pinMode(tank_6e, OUTPUT);
// pinMode(tank_7f, OUTPUT);
// pinMode(tank_7e, OUTPUT);
//pinMode(buzzer, OUTPUT); // Set buzzer - pin as an output
inputString.reserve(200);
mySerial.begin(9600); // Set the baudrate of GSM module
Serial.begin(9600); //Set the baudrate of Serial monitor
Serial.println("Initializing...");
delay(1000);
mySerial.println("AT"); //Once the handshake test is successful, it will back to OK
updateSerial();
mySerial.println("AT+CSQ"); //Signal quality test, value range is 0-31 , 31 is the best
updateSerial();
mySerial.println("AT+CCID"); //Read SIM information to confirm whether the SIM is plugged
updateSerial();
mySerial.println("AT+CREG?"); //Check whether it has registered in the network
updateSerial();
mySerial.println("AT+CMGF=1");
updateSerial();
mySerial.println("AT+CNMI=1,2,0,0,0");
updateSerial();
delay(1000);
}
void loop() {
message();
if (inputString.length()>0){
tank_status(inputString);
inputString="";
delay(1000);
}
}
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 buzzer_sound(){
Serial.println("buzzer sound start");
tone(buzzer, 1000); // Send 1KHz sound signal...
delay(30000); // ...for 1 sec 1000, 30000 for 30 sec
noTone(buzzer); // Stop sound...
Serial.println("buzzer sound stop");
delay(1000); // ...for 1sec
}
void message()
{
while(mySerial.available()>0)
{
char c = mySerial.read();
inputString+=c;
}
}
void tank_status(String message){
//for tank 1 empty state
if(strncmp(message, "@t1e#", 5) == 0){
//Serial.println("Tank 1 Empty");
digitalWrite(tank_1e, HIGH);
digitalWrite(tank_1f, LOW);
//uncomment below line to enable buzzer
//buzzer_sound();
}
else if(strncmp(message, "@t1f#", 5) == 0){
//Serial.println("Tank 1 Full");
digitalWrite(tank_1e, LOW);
digitalWrite(tank_1f, HIGH);
//uncomment below line to enable buzzer
//buzzer_sound();
}
else if(strncmp(message, "@t2e#", 5) == 0){
//Serial.println("Tank 2 Empty");
digitalWrite(tank_2e, HIGH);
digitalWrite(tank_2f, LOW);
//uncomment below line to enable buzzer
//buzzer_sound();
}
else if(message, "@t2f#", 5) == 0){
Serial.println("Tank 2 FULL");
digitalWrite(tank_2e, LOW);
digitalWrite(tank_2f, HIGH);
//uncomment below line to enable buzzer
//buzzer_sound();
}
// uncomment below for all 7 tanks
// else if(message, "@t3e#", 5) == 0){
// Serial.println("Tank 3 Empty");
// digitalWrite(tank_3e, HIGH);
// digitalWrite(tank_3f, LOW);
//
// //uncomment below line to enable buzzer
// //buzzer_sound();
// }
// else if(message, "@t3f#", 5) == 0){
// Serial.println("Tank 3 Full");
// digitalWrite(tank_3e, LOW);
// digitalWrite(tank_3f, HIGH);
//
// //uncomment below line to enable buzzer
// //buzzer_sound();
// }
// else if(message, "@t4e#", 5) == 0){
// Serial.println("Tank 4 Empty");
// digitalWrite(tank_4e, HIGH);
// digitalWrite(tank_4f, LOW);
//
// //uncomment below line to enable buzzer
// //buzzer_sound();
// }
// else if(message, "@t4f#", 5) == 0){
// Serial.println("Tank 4 Full");
// digitalWrite(tank_4e, LOW);
// digitalWrite(tank_4f, HIGH);
//
// //uncomment below line to enable buzzer
// //buzzer_sound();
// }
// else if(message, "@t5e#", 5) == 0){
// Serial.println("Tank 5 Empty");
// digitalWrite(tank_5e, HIGH);
// digitalWrite(tank_5f, LOW);
//
// //uncomment below line to enable buzzer
// //buzzer_sound();
// }
// else if(message, "@t5f#", 5) == 0){
// Serial.println("Tank 5 Full");
// digitalWrite(tank_5e, LOW);
// digitalWrite(tank_5f, HIGH);
//
// //uncomment below line to enable buzzer
// //buzzer_sound();
// }
// else if(message, "@t6e#", 5) == 0){
// Serial.println("Tank 6 Empty");
// digitalWrite(tank_6e, HIGH);
// digitalWrite(tank_6f, LOW);
//
// //uncomment below line to enable buzzer
// //buzzer_sound();
// }
// else if(message, "@t6f#", 5) == 0){
// Serial.println("Tank 6 Full");
// digitalWrite(tank_6e, LOW);
// digitalWrite(tank_6f, HIGH);
//
// //uncomment below line to enable buzzer
// //buzzer_sound();
// }
// else if(message, "@t7e#", 5) == 0){
// Serial.println("Tank 7 Empty");
// digitalWrite(tank_7e, HIGH);
// digitalWrite(tank_7f, LOW);
//
// //uncomment below line to enable buzzer
// //buzzer_sound();
// }
// else if(message, "@t7f#", 5) == 0){
// Serial.println("Tank 7 Full");
// digitalWrite(tank_7e, LOW);
// digitalWrite(tank_7f, HIGH);
//
// //uncomment below line to enable buzzer
// //buzzer_sound();
// }
//mySerial.write("AT+CMGD=1[4]") //delete message
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.