#include <SoftwareSerial.h>
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
#include <SoftwareSerial.h>
SoftwareSerial GPRS(7, 12); // RX, TX
int period = 10000;
unsigned long time_now = 0;
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int buttonPin2 = 3; // the number of the pushbutton pin
const int buttonPin3 = 5; // the number of the pushbutton pin
const int buttonPin4 = 6; // the number of the pushbutton pin
// Variables will change:
int ledState = LOW; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
int ledState2 = LOW; // the current state of the output pin
int buttonState2; // the current reading from the input pin
int lastButtonState2 = LOW; // the previous reading from the input pin
int ledState3 = LOW; // the current state of the output pin
int buttonState3; // the current reading from the input pin
int lastButtonState3 = LOW; // the previous reading from the input pin
int ledState4 = LOW; // the current state of the output pin
int buttonState4; // the current reading from the input pin
int lastButtonState4 = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
long lastDebounceTime2 = 0; // the last time the output pin was toggled
long debounceDelay2 = 50; // the debounce time; increase if the output flickers
long lastDebounceTime3 = 0; // the last time the output pin was toggled
long debounceDelay3 = 50; // the debounce time; increase if the output flickers
long lastDebounceTime4 = 0; // the last time the output pin was toggled
long debounceDelay4 = 50; // the debounce time; increase if the output flickers
#define cs 10
#define dc 9
#define rst 8
int counter = 20;
TFT TFTscreen = TFT(cs, dc, rst);
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
bool fan = false;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
GPRS.begin(9600);
Serial.begin(9600);
TFTscreen.begin();
TFTscreen.setRotation(2);
TFTscreen.background(0, 0, 0);
TFTscreen.setCursor(0, 0);
GPRS.println("AT");
updateSerial();
GPRS.println("AT+CMGF=1");
delay(100);
updateSerial();
GPRS.println("AT+CNMI=1,2,0,0,0");
delay(200);
dht.begin();
}
void loop() {
while(GPRS.available()) {
Serial.write(GPRS.read());
}
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
if(t > 30){
fan = true;
}
else{
fan = false;
}
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
int reading2 = digitalRead(buttonPin2);
int reading3 = digitalRead(buttonPin3);
int reading4 = digitalRead(buttonPin4);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if (reading2 != lastButtonState2) {
// reset the debouncing timer
lastDebounceTime2 = millis();
}
if (reading3 != lastButtonState3) {
// reset the debouncing timer
lastDebounceTime3 = millis();
}
if (reading4 != lastButtonState4) {
// reset the debouncing timer
lastDebounceTime4 = millis();
}
//================================================================================================
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
CheckToScroll();
ledState ? TFTscreen.println("Door 1 Open") : TFTscreen.println("Door 1 Close");
ChangeReport(1);
counter++;
}
}
}
//================================================================================================
if ((millis() - lastDebounceTime2) > debounceDelay2) {
if (reading2 != buttonState2) {
buttonState2 = reading2;
// only toggle the LED if the new button state is HIGH
if (buttonState2 == HIGH) {
ledState2 = !ledState2;
CheckToScroll();
ledState2 ? TFTscreen.println("Door 2 Open") : TFTscreen.println("Door 2 Close");
ChangeReport(2);
counter++;
}
}
}
//================================================================================================
if ((millis() - lastDebounceTime3) > debounceDelay3) {
if (reading3 != buttonState3) {
buttonState3 = reading3;
// only toggle the LED if the new button state is HIGH
if (buttonState3 == HIGH) {
ledState3 = !ledState3;
CheckToScroll();
ledState3 ? TFTscreen.println("Door 3 Open") : TFTscreen.println("Door 3 Close");
ChangeReport(3);
counter++;
}
}
}
//================================================================================================
if ((millis() - lastDebounceTime4) > debounceDelay4) {
if (reading4 != buttonState4) {
buttonState4 = reading4;
// only toggle the LED if the new button state is HIGH
if (buttonState4 == HIGH) {
ledState4 = !ledState4;
CheckToScroll();
ledState4 ? TFTscreen.println("Door 4 Open") : TFTscreen.println("Door 4 Close");
ChangeReport(4);
counter++;
}
}
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
lastButtonState2 = reading2;
lastButtonState3 = reading3;
lastButtonState4 = reading4;
if(millis() >= time_now + period){
time_now += period;
statusReport(t);
}
}
void CheckToScroll(){
if(counter >= 20){
TFTscreen.background(0, 0, 0);
TFTscreen.setCursor(0, 0);
counter = 0;
}
}
void statusReport(float t){
Serial.println("Sending Status Report...");
GPRS.println("AT+CMGS=\"+xxxxxxxxxxx\""); //Phone Number
delay(500);
GPRS.print("Temperature: ");
GPRS.println(t);
GPRS.print("Fan: ");
GPRS.println(fan ? "ON" : "OFF");
GPRS.println(ledState ? "Door 1 Open" : "Door 1 Close");
GPRS.println(ledState2 ? "Door 2 Open" : "Door 2 Close");
GPRS.println(ledState3 ? "Door 3 Open" : "Door 3 Close");
GPRS.println(ledState4 ? "Door 4 Open" : "Door 4 Close");
GPRS.write( 0x1a ); // ctrl+Z character
delay(500);
}
void ChangeReport(int door){
Serial.println("Sending Change Report...");
GPRS.println("AT+CMGS=\"+xxxxxxxxxxx\""); //Phone Number
delay(500);
GPRS.print("Door ");
GPRS.print(door);
GPRS.println(" has been opened!");
GPRS.write( 0x1a ); // ctrl+Z character
delay(500);
}
void updateSerial()
{
delay(500);
while (Serial.available())
{
GPRS.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while(GPRS.available())
{
Serial.write(GPRS.read());//Forward what Software Serial received to Serial Port
}
}
The Arduino automates part of a system that sends an SMS report depending on any change and a fixed status report every 10secs(10 secs just for testing). There is also a DHT11 sensor for temperature measurement. I debounced 4 push buttons, that's why the code is so long.
Hardwares:
Arduino Uno
GSM SIM900
TFT screen 1.8"
DHT11
4 push buttons
The AT commands work well. Other codes with the same circuitry work well..No issues...
But with this code, I'm not receiving any message but it enters the function and prints into the serial monitor...
Please any suggestions?