首页 > 解决方案 > 使用 GSM 模块 (Sim800L) 工作温度传感器 (LM35)

问题描述

我设置了一个温度传感器,它与 LCD 和一个调节亮度的棒一起工作。我现在希望温度传感器在达到特定温度时发送文本。有人可以帮忙吗?我拥有的 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
}

标签: arduinogsm

解决方案


您可以使用库Fona使用 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"); 
   }

推荐阅读