Can you help me in this project

Can you help me in this project? The problem that I have is that I want to fulfill the condition that the phone number only accepts the one from which the message was sent in the setup() .

#include <GPRS_Shield_Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DFRobot_DHT11.h>

boolean started = false;
#define PIN_RX 11
#define PIN_TX 10
GPRS gprs(PIN_TX, PIN_RX, 9600);
int pos = 0;
#define MESSAGE_LENGTH 190
char smsbuffer[MESSAGE_LENGTH];
char n[16];
char p[16];
char phone[16];
char datetime[24];
char mytemp[40];
char myhum[40];

#define restgprs 38
DFRobot_DHT11 DHT;
#define DHT11_PIN 30
#define SIGNAL_PIN A0

#define LED1 34
#define LED2 35
#define LED3 36
#define LED4 37

#define FAN1 42
#define FAN2 43
#define FAN3 44
#define FAN4 45

LiquidCrystal_I2C lcd(0x27, 20, 4);
float v = 0.0;
float in_v = 0.0;
float R1 = 30000.0;
float R2 = 7500.0;
float v_value = 0;
float r_voltage = 5.0;
float AcsValue = 0.0;

void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(FAN1, OUTPUT);
  pinMode(FAN2, OUTPUT);
  pinMode(FAN3, OUTPUT);
  pinMode(FAN4, OUTPUT);
  pinMode(restgprs, OUTPUT);
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, HIGH);
  digitalWrite(LED4, HIGH);
  digitalWrite(FAN1, HIGH);
  digitalWrite(FAN2, HIGH);
  digitalWrite(FAN3, HIGH);
  digitalWrite(FAN4, HIGH);
  digitalWrite(restgprs, HIGH);
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  delay(1000);
  if (gprs.init()) {
    deletsms();
    lcd.setCursor(1, 0);
    lcd.print("I'm waiting");
    lcd.setCursor(1, 1);
    lcd.print("Send: my phone");
    Serial.println("I'm waiting");
    pos = gprs.isSMSunread();
    gprs.readSMS(pos, smsbuffer, MESSAGE_LENGTH, p, datetime);
    if (!strcmp(smsbuffer, "My phone")) {
      Serial.println(p);
      Serial.println(smsbuffer);
      sprintf(phone, p);
      Serial.println(phone);
      gprs.sendSMS(p, "I saved your number");
      gprs.deleteSMS(pos);
      lcd.clear();
      lcd.setCursor(1, 0);
      lcd.print("saved");
      delay(1000);
      Serial.println("\nstatus = READY");
      started = true;
    } else {
      delay(1000);
      return (setup());
    }
  } else {
    digitalWrite(restgprs, LOW);
    Serial.println("\nstatus = NOT READY");
    delay(3000);
    return (setup());
  }
}

void loop() {
  if (started) {

    v_value = analogRead(SIGNAL_PIN);
    v = (v_value * r_voltage) / 1024.0;
    in_v = v / (R2 / (R1 + R2));
    AcsValue = in_v * (r_voltage / 1024) * 2.25;
    readsms();
    lcdsh();
    if (v_value < 100) {
      gprs.sendSMS(phone, "There is a fault with the volt");
      delay(1000);
    }
    if (DHT.temperature > 35) {
      /* digitalWrite(LED1, HIGH);
          digitalWrite(LED2, HIGH);
          digitalWrite(LED3, HIGH);*/
      digitalWrite(LED4, HIGH);
      gprs.sendSMS(phone, "The temperature level is high\r lights turned off");
    }
    if (DHT.temperature < 18) {
      //digitalWrite(LED1, LOW);
      digitalWrite(LED2, LOW);
      //digitalWrite(LED3, LOW);
      //digitalWrite(LED4, LOW);
      gprs.sendSMS(phone, "The heat level is low\r lights turned on");
    }
    if (DHT.humidity > 80) {
      //digitalWrite(FAN1, LOW);
      //digitalWrite(FAN2, LOW);
      //digitalWrite(FAN3, LOW);
      digitalWrite(FAN4, LOW);
      gprs.sendSMS(phone, "The humidity level is high\r fans turned on");
    }
    if (DHT.humidity < 45) {
      /*digitalWrite(FAN1, HIGH);
            digitalWrite(FAN2, HIGH);
            digitalWrite(FAN3, HIGH);*/
      digitalWrite(FAN4, HIGH);
      gprs.sendSMS(phone, "The humidity level is low\r fans turned off");
    }
  }
}

void lcdsh() {
  DHT.read(DHT11_PIN);
  lcd.begin(16, 2);
  lcd.print("temp:");
  lcd.print(DHT.temperature);
  lcd.print(" humi:");
  lcd.print(DHT.humidity);
  if (v_value < 100) {
    lcd.setCursor(0, 1);
    lcd.print("V:");
    lcd.print(0.00);
    lcd.print("  A:");
    lcd.print(0.00);
  } else {
    lcd.setCursor(0, 1);
    lcd.print("V:");
    lcd.print(in_v);
    lcd.print("  A:");
    lcd.print(AcsValue);
  }
}

void readsms() {
  sprintf(mytemp, "temp: %d", DHT.temperature);
  sprintf(myhum, "humidity: %d", DHT.humidity);
  pos = gprs.isSMSunread();
  if (pos > 0) {
    gprs.readSMS(pos, smsbuffer, MESSAGE_LENGTH, n, datetime);
    /* if (n != phone) {
      //Serial.println(n);
     
      //return (setup());
    } 
     else {*/
    if (n == phone) {
      if (!strcmp(smsbuffer, "Read sensor")) {
        Serial.println(smsbuffer);
        gprs.sendSMS(phone, mytemp);
        gprs.sendSMS(phone, myhum);
        //gprs.deleteSMS(pos);
      }
      if (!strcmp(smsbuffer, "Open all led")) {
        Serial.println(smsbuffer);
        digitalWrite(LED1, LOW);
        digitalWrite(LED2, LOW);
        /*digitalWrite(LED3, LOW);
      digitalWrite(LED4, LOW);*/
        gprs.sendSMS(phone, "Light turned on successfully");
        //gprs.deleteSMS(pos);
      }
      if (!strcmp(smsbuffer, "Close all led")) {
        Serial.println(smsbuffer);
        digitalWrite(LED1, HIGH);
        digitalWrite(LED2, HIGH);
        digitalWrite(LED3, HIGH);
        digitalWrite(LED4, HIGH);
        gprs.sendSMS(phone, "Light turned off successfully");
        // gprs.deleteSMS(pos);
      }
      if (!strcmp(smsbuffer, "Open all fan")) {
        Serial.println(smsbuffer);
        /*digitalWrite(FAN1, LOW);
      digitalWrite(FAN2, LOW);*/
        digitalWrite(FAN3, LOW);
        digitalWrite(FAN4, LOW);
        gprs.sendSMS(phone, "Fans turned on successfully");
        // gprs.deleteSMS(pos);
      }
      if (!strcmp(smsbuffer, "Close all fan")) {
        Serial.println(smsbuffer);
        digitalWrite(FAN1, HIGH);
        digitalWrite(FAN2, HIGH);
        digitalWrite(FAN3, HIGH);
        digitalWrite(FAN4, HIGH);
        gprs.sendSMS(phone, "Fans turned off successfully");
        //gprs.deleteSMS(pos);
      }
    } else {
      gprs.sendSMS(n, "Sorry, the phone number is unknown");
      gprs.deleteSMS(pos);
    }
  }
}

void deletsms() {
  for (int i = 0; i < 20; i++) {
    //pos = gprs.isSMSunread();
    if (pos != 0) {
      Serial.println("delsms");
      Serial.print("\nFind SMS at the pos ");
      Serial.println(pos);
      if (gprs.deleteSMS(pos) == 1) {
        Serial.print("\nDeleted SMS at the pos ");
        Serial.println(pos);
      } else {
        Serial.print("\nCant del SMS at the pos ");
        Serial.println(pos);
        return (setup());
      }
    }
  }
}

Welcome to the forum

            return (setup());

What is the purpose of these lines of code ?

Instead of the above, which will probably crash an Arduino, use a while loop.

The while condition for exit should be success in obtaining the desired data.

You can't directly compare two character arrays. If the arrays contain strings then you would use the 'strcmp()' function:

    if (strcmp(n, phone) != 0) // n does not match phone
    {
      Serial.println(n);
      return;
    } 
    else // n is equal to phone
    {

I tested it, but it tells me that the phone number is wrong even though it is the same number sent from it.

Show us your serial output.

Better still, show how your code looks now.

Unfortunately, no solution worked. I will remove this option from the project. Thank you all.

#include <GPRS_Shield_Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DFRobot_DHT11.h>

boolean started = false;
#define restgprs 38
#define PIN_RX 11
#define PIN_TX 10
GPRS gprs(PIN_TX, PIN_RX, 9600);
int pos = 0;
#define MESSAGE_LENGTH 190
char smsbuffer[MESSAGE_LENGTH];
char n[16];
char p[16];
char datetime[24];
char mytemp[40];
char myhum[40];

DFRobot_DHT11 DHT;
#define DHT11_PIN 30
#define SIGNAL_PIN A0
#define SIGNAL_AMP A8

#define LED1 34
#define LED2 35
#define LED3 36
#define LED4 37

#define FAN1 42
#define FAN2 43
#define FAN3 44
#define FAN4 45

LiquidCrystal_I2C lcd(0x27, 20, 4);
float v = 0.0;
float in_v = 0.0;
float R1 = 30000.0;
float R2 = 7500.0;
float v_value = 0;
float r_voltage = 5.0;
double  AcsValue = 0.0;

void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(FAN1, OUTPUT);
  pinMode(FAN2, OUTPUT);
  pinMode(FAN3, OUTPUT);
  pinMode(FAN4, OUTPUT);
  pinMode(restgprs, OUTPUT);
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, HIGH);
  digitalWrite(LED4, HIGH);
  digitalWrite(FAN1, HIGH);
  digitalWrite(FAN2, HIGH);
  digitalWrite(FAN3, HIGH);
  digitalWrite(FAN4, HIGH);
  digitalWrite(restgprs, HIGH);
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  if (gprs.init()) {
    //deletsms();
    lcd.setCursor(1, 0);
    lcd.print("I'm waiting");
    lcd.setCursor(1, 1);
    lcd.print("Send: my phone");
    Serial.println("I'm waiting");
    pos = gprs.isSMSunread();
    gprs.readSMS(pos, smsbuffer, MESSAGE_LENGTH, p, datetime);
    if (!strcmp(smsbuffer, "My phone")) {
      Serial.println(smsbuffer);
      Serial.println(p);
      gprs.sendSMS(p, "I saved your number");
      gprs.deleteSMS(pos);
      lcd.clear();
      lcd.setCursor(1, 0);
      lcd.print("It's saved");
      Serial.println("\nstatus = READY");
      started = true;
    } else {
      gprs.deleteSMS(pos);
      return (setup());
    }
  } else if (!gprs.init()) {
    digitalWrite(restgprs, LOW);
    Serial.println("\nstatus = NOT READY");
    delay(3000);
    return (setup());
  }
}

void loop() {
  if (started) {
    v_value = analogRead(SIGNAL_PIN);
    v = (v_value * r_voltage) / 1024.0;
    in_v = v / (R2 / (R1 + R2));
    AcsValue = in_v * (R2 / (R1 + R2))/2.5;
    readsms();
    lcdsh();
    if (in_v < 5.5) {
      gprs.sendSMS(p, "There is a fault with the volt");
      delay(1000);
    }
    if (DHT.temperature > 35) {
      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);
      digitalWrite(LED4, HIGH);
      gprs.sendSMS(p, "temperature is high\r lights turned off");
    }
    if (DHT.temperature < 15) {
      digitalWrite(LED1, LOW);
      digitalWrite(LED2, LOW);
      digitalWrite(LED3, LOW);
      digitalWrite(LED4, LOW);
      gprs.sendSMS(p, "temperature is low\r lights turned on");
    }
    if (DHT.humidity > 70) {
      digitalWrite(FAN1, LOW);
      digitalWrite(FAN2, LOW);
      digitalWrite(FAN3, LOW);
      digitalWrite(FAN4, LOW);
      gprs.sendSMS(p, "humidity is high\r fans turned on");
    }
    if (DHT.humidity < 35) {
      digitalWrite(FAN1, HIGH);
      digitalWrite(FAN2, HIGH);
      digitalWrite(FAN3, HIGH);
      digitalWrite(FAN4, HIGH);
      gprs.sendSMS(p, "humidity is low\r fans turned off");
    }
  }
}

void lcdsh() {
  DHT.read(DHT11_PIN);
  lcd.begin(16, 2);
  lcd.print("temp:");
  lcd.print(DHT.temperature);
  lcd.print(" humi:");
  lcd.print(DHT.humidity);
  if (v_value < 100) {
    lcd.setCursor(0, 1);
    lcd.print("V:");
    lcd.print(0.00);
  } else {
    lcd.setCursor(0, 1);
    lcd.print("V:");
    lcd.print(in_v);
  }
  if (v_value < 100) {
    lcd.print("  A:");
    lcd.print(0.00);
  } else {
    lcd.print("  A:");
    lcd.print(AcsValue);
  }
}

void readsms() {
  sprintf(mytemp, "temp: %d", DHT.temperature);
  sprintf(myhum, "humidity: %d", DHT.humidity);
  pos = gprs.isSMSunread();
   gprs.readSMS(pos, smsbuffer, MESSAGE_LENGTH, n, datetime);
  if (pos > 0) {
   
    if (!strcmp(smsbuffer, "Read sensor")) {
      Serial.println(smsbuffer);
      gprs.sendSMS(p, mytemp);
      gprs.sendSMS(p, myhum);
      gprs.deleteSMS(pos);
    }
    if (!strcmp(smsbuffer, "Open all led")) {
      Serial.println(smsbuffer);
      digitalWrite(LED1, LOW);
      digitalWrite(LED2, LOW);
      digitalWrite(LED3, LOW);
      digitalWrite(LED4, LOW);
      gprs.sendSMS(p, "Light turned on successfully");
      gprs.deleteSMS(pos);
    }
    if (!strcmp(smsbuffer, "Close all led")) {
      Serial.println(smsbuffer);
      digitalWrite(LED1, HIGH);
      digitalWrite(LED2, HIGH);
      digitalWrite(LED3, HIGH);
      digitalWrite(LED4, HIGH);
      gprs.sendSMS(p, "Light turned off successfully");
      gprs.deleteSMS(pos);
    }
    if (!strcmp(smsbuffer, "Open all fan")) {
      Serial.println(smsbuffer);
      digitalWrite(FAN1, LOW);
      digitalWrite(FAN2, LOW);
      digitalWrite(FAN3, LOW);
      digitalWrite(FAN4, LOW);
      gprs.sendSMS(p, "Fans turned on successfully");
      gprs.deleteSMS(pos);
    }
    if (!strcmp(smsbuffer, "Close all fan")) {
      Serial.println(smsbuffer);
      digitalWrite(FAN1, HIGH);
      digitalWrite(FAN2, HIGH);
      digitalWrite(FAN3, HIGH);
      digitalWrite(FAN4, HIGH);
      gprs.sendSMS(p, "Fans turned off successfully");
      gprs.deleteSMS(pos);
    }
    deletsms();
  }
}
void deletsms() {
  for (int i = 0; i <= 5; i++) {
    if (pos != 0) {
      gprs.readSMS(pos, smsbuffer, MESSAGE_LENGTH, p, datetime);
      Serial.println(smsbuffer);
      Serial.println(pos);
      gprs.deleteSMS(pos);
      Serial.println("delete SMS");
      pos--;
      return (loop());
    }
  }
}

You've got that, in a (nominally) six iteration for loop.
What do expect to happen?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.