Работа датчика температуры (LM35) с модулем GSM (Sim800L) - PullRequest
0 голосов
/ 03 января 2019

У меня есть датчик температуры, работающий с ЖК-дисплеем и джойстиком для регулировки яркости. Теперь я хочу, чтобы датчик температуры отправлял текст всякий раз, когда он достигает определенной температуры. Может кто-нибудь, пожалуйста, помогите. У меня есть GSM-модуль SIM800L. ниже то, что я имею до сих пор:

#include<LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int sensor=A1; // Assigning Analog Pin A1 to variable 'sensor'
float tempc; //variable to store temperature in degree Celsius
float tempf; //variable to store temperature in Fahrenheit
float vout; //temporary variable to hold sensor reading

void setup()
{
    pinMode(sensor,INPUT); // Configuring pin A1 as INPUT Pin
    Serial.begin(9600);
    lcd.begin(16,2);
    delay(500);
}

void loop()
{
    vout=analogRead(sensor);
    vout=(vout*500)/1023;

    tempc=vout; // Storing value in degrees Celsius
    tempf=(vout*1.8)+32; // Converting Temperature value from degrees Celsius to Fahrenheit

    lcd.setCursor(0,0);
    lcd.print("DegreeC= ");
    lcd.print(tempc);
    lcd.setCursor(0,1);
    lcd.print("Fahrenheit=");
    lcd.print(tempf);

    delay(1000); //Delay of 1 second for ease of viewing in serial monitor
}

Ответы [ 2 ]

0 голосов
/ 10 января 2019
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"

//This part declares that the RX, TX and RST pins of the SIM800L must be connected
//to pin 2, 3 and 4 of the Arduino.
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4

SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

void setup() {
 // you initialisation code
}

void loop()
{
    vout=analogRead(sensor);
    vout=(vout*500)/1023;

    tempc=vout; // Storing value in degrees Celsius
    tempf=(vout*1.8)+32; // Converting Temperature value from degrees Celsius to Fahrenheit

    lcd.setCursor(0,0);
    lcd.print("DegreeC= ");
    lcd.print(tempc);
    lcd.setCursor(0,1);
    lcd.print("Fahrenheit=");
    lcd.print(tempf);

    delay(1000); //Delay of 1 second for ease of viewing in serial monitor
    if (tempc > 30.0) {
        SendSms();
    }
}

void SendSms() {  
        char sendto[] = "+19999999999"; //put the desired destination phone number for sms here
        char message[141];
        sprintf(message, "Alert TEMP is %.2f", tempc);// limit to 140
         //sends the message via SMS
        if (!fona.sendSMS(sendto, message)) {
      Serial.println(F("error"));
    } else {
      Serial.println(F("sent!"));
    }
} 
0 голосов
/ 03 января 2019

вы можете использовать библиотеку Fona для отправки SMS с Sim800L

, чтобы отправить сообщение, вы используете команду -> fona.sendSMS (sendto, message)

#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"

//This part declares that the RX, TX and RST pins of the SIM800L must be connected
//to pin 2, 3 and 4 of the Arduino.
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4

SoftwareSerial fonaSS = SoftwareSerial(FONA_RX, FONA_TX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

void setup() {
  while (!Serial);
  Serial.begin(115200);
  Serial.println(F("FONA basic test"));
  Serial.println(F("Initializing....(May take 3 seconds)"));
  fonaSS.begin(9600); 
  if (!fona.begin(fonaSS)) {            
    Serial.println(F("Couldn't find FONA"));
    while (1);
  }
  Serial.println(F("FONA is OK"));

   char sendto[21], message[141];
                :
                :
   //initialize sendto and message
                :
                :
   if (!fona.sendSMS(sendto, message)) {
      Serial.println(F("error"));
   } else {
      Serial.println(F("sent!"));
   }
}

чтобы адаптировать программу к вашему случаю: я поместил несколько строк кодов от настройки к циклу (от простого к постоянному sendto и определениям сообщений в цикле сейчас)

#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"

//This part declares that the RX, TX and RST pins of the SIM800L must be connected
//to pin 2, 3 and 4 of the Arduino.
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4

SoftwareSerial fonaSS = SoftwareSerial(FONA_RX, FONA_TX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

void setup() {
 fonaSS.begin(9600); 
 // you initialisation code
}

void loop()
{
    vout=analogRead(sensor);
    vout=(vout*500)/1023;

    tempc=vout; // Storing value in degrees Celsius
    tempf=(vout*1.8)+32; // Converting Temperature value from degrees Celsius to Fahrenheit

    lcd.setCursor(0,0);
    lcd.print("DegreeC= ");
    lcd.print(tempc);
    lcd.setCursor(0,1);
    lcd.print("Fahrenheit=");
    lcd.print(tempf);

    delay(1000); //Delay of 1 second for ease of viewing in serial monitor
    if (tempc > 30.0) {
        SendSms();
    }
}

void SendSms() {  
        char sendto[] = "+19999999999"; //put the desired destination phone number for sms here
        char message[141];
        sprintf(message, "Alert TEMP is %.2f", tempc);// limit to 140
         //sends the message via SMS
        if (!fona.sendSMS(sendto, message)) {
      Serial.println(F("error"));
    } else {
      Serial.println(F("sent!"));
    }
}

еще один способ отправить вам смсможет проверить команду hayes: например

void sendsms(){
    Serial.println("Sending text message...");
    fonaSS.print("AT+CMGF=1\r");  // SMS MODE
    delay(100);
    // phone number    
    fonaSS.print("AT+CMGS=\"+33676171212\"\r"); //indicate your phone number 
   delay(100);
   // message here    
   fonaSS.print("Message test \r");  
   // CTR+Z in mode ASCII, to indicate the end of message
   fonaSS.print(char(26));            
   delay(100);
   fonaSS.println();
   Serial.println("Text send"); 
   }
...