将温度传感器(LM35)与GSM模块(Sim800L)配合使用

时间:2019-01-03 17:35:55

标签: arduino gsm

我有一个温度传感器,它可以与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
}

2 个答案:

答案 0 :(得分:1)

您可以使用库Fona通过Sim800L发送短信

要发送消息,请使用命令-> fona.sendSMS(发送消息)

#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和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() {
 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"); 
   }

答案 1 :(得分:-1)

#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!"));
    }
}