Working a temprature sensor (LM35) with a GSM module (Sim800L) - arduino

I have a temperature sensor set up working with an LCD and a stick to adjust the brightness. I now want the temperature sensor to send a text whenever it reaches a certain temperature. Can somebody please help.
The GSM unit i have is the SIM800L
below is what I have so far :
#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
}

you could use the library Fona to send SMS with a Sim800L
to send a message you use the command -> 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!"));
}
}
to adapt the program to your case: i have put some lines of codes from setup to loop (easy to understant sendto and message definitions in loop now)
#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!"));
}
}
another way to send sms
you could test the hayes command: for example
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");
}

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

Related

ASCII of integer to integer in Arduino

I am working on a project with LoRa and Arduino and I am facing a weird issue where when I transmit a integer the receiver receives at ASCII value which is not good at my case because I wanted to transmit sensor data(3 digits) which is not possible by ASCII. I will also attach my code(converted into a basic integer code for testing) I need a Solution to fix this BTW I am using Arduino UNO for transmitting and Arduino Mega for receiving and SX1278 LoRa Module for both transmitting and receiving.
Transmitter Code(Arduino UNO):
#include <SPI.h>
#include <LoRa.h>
int val = 5;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa Sender");
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
}
void loop() {
Serial.print("Sending packet: ");
// send packet
LoRa.beginPacket();
LoRa.print(val);
LoRa.endPacket();
delay(500);
}
Receiver Code(Arduino Mega):
#include <SPI.h>
#include <LoRa.h>
#define LORA_SS 53
#define LORA_RST 9
#define LORA_DIO0 8
int val;
void setup() {
pinMode(LORA_SS, OUTPUT);
digitalWrite(LORA_SS, HIGH);
LoRa.setPins(LORA_SS, LORA_RST, LORA_DIO0);
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa Receiver");
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
}
void loop() {
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print("Received packet '");
// read packet
while (LoRa.available()) {
//Serial.print((char)LoRa.read());
int val = LoRa.read();
}
Serial.print(val);
// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
}
}
Output of the receiver :
Received packet '53' with RSSI -5
To Fix this issue just send the value as an integer, since LoRa receives as char/string same it in a string variable in the receiver and convert it into integer with toInt(); and that will fix it!

Sending GPS values via XBee and Arduino (Float problem!!!)

I have XBEE S2C's. I have 2 Arduino MEGA. One part of them will be receiver and other is transmitter. I communicated and configured XBee's.I tested it That's okey. But the problem is I can't send float value like longtitude= 38,45682469 latitude= 85,24698534. When I try to do this I receive only latitude = 85
longtitude = 38. How can I change my code to receive healthy data.
TRANSMITTER
#include "SoftwareSerial.h"
#include <TinyGPS.h>
float lat,lon;
TinyGPS gps;
SoftwareSerial XBee(10, 11);
void setup(){
Serial.begin(9600);
XBee.begin(9600);
Serial.println("The GPS Received Signal:");
Serial3.begin(9600);
}
void loop(){
while(Serial3.available()){
if(gps.encode(Serial3.read()))
{
gps.f_get_position(&lat,&lon);
Serial.print("Position: ");
Serial.print("Latitude: ");
Serial.print(lat,8);
Serial.print(",");
Serial.print("Longitude: ");
Serial.println(lon,8);
}
}
XBee.write("l");
XBee.write(lat);
delay(100);
XBee.write("k");
XBee.write(lon);
delay(100);
}
Most robust is to convert float to string, send it over serial, and let the receiver convert back to float.
Sender:
XBee.write(String(lat,5)); // keep 5 decimals
Receiver:
String inString = "";
while (Serial.available()) {
char c = Serial.read();
inString += c;
}
float myFloat = inString.toFloat();
Further reading:
https://www.arduino.cc/en/Tutorial/StringToFloatExample
https://www.arduino.cc/en/Reference.StringConstructor

Error in sending data to cloud server and arduino lagging

The code im working on, is suppose to show temperature, humidity and able to take and show heart rate on the lcd. After data is shown, it will send data to "ThingSpeak". After sending, there will be a http code error -401 which is ok as it can only send data very 15 sec. But after awhile, it will change it error http code -301... and then it will hang. Another issue is when i try to use the temperature sensor with the heart rate sensor, the lcd will hang and it will not work till i reset.
#include "ThingSpeak.h"
#include "SPI.h"
#include "DHT.h"
#include <Ethernet.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(10, 9, 5, 4, 3, 2); //numbers of interface pins
#define redLED 8
int sensorPin = A8;
float tempC;
#define DHTPIN 6
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float h;
#define USE_ARDUINO_INTERRUPTS true // Set-up low-level interrupts for most acurate BPM math.
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library.
// Variables
const int PulseWire = A9; // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int blinkPin = 22; // The on-board Arduino LED, close to PIN 13.
int Threshold = 550; // Determine which Signal to "count as a beat" and which to ignore.
PulseSensorPlayground pulseSensor; // Creates an instance of the PulseSensorPlayground object called "pulseSensor"
byte mac[] = {0x90, 0xA2, 0xDA, 0x10, 0x40, 0x4F};
unsigned long myChannelNumber = ;
const char * myWriteAPIKey = "";
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(172, 17, 171, 199);
IPAddress myDns(172, 17, 171, 254);
float get_temperature(int pin)
{
float temperature = analogRead(pin); // Calculate the temperature based on the reading and send that value back
float voltage = temperature * 5.0;
voltage = voltage / 1024.0;
return ((voltage - 0.5) * 100);
}
EthernetClient client;
void setup()
{
lcd.begin(16, 2);
pinMode(redLED, OUTPUT);
pulseSensor.analogInput(PulseWire);
pulseSensor.blinkOnPulse(blinkPin); //auto-magically blink Arduino's LED with heartbeat.
pulseSensor.setThreshold(Threshold);
pulseSensor.begin();
dht.begin();
Ethernet.init(10); // Most Arduino Ethernet hardware
Serial.begin(9600); //Initialize serial
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware)
{
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true)
{
delay(10); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF)
{
Serial.println("Ethernet cable is not connected.");
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
}
else
{
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop()
{
h = dht.readHumidity();
{
tempC = get_temperature(sensorPin);
}
if (tempC < 31)
{
lcd.setCursor(0, 0);
lcd.print(tempC);
lcd.print(" "); //print the temp
lcd.print((char)223); // to get ° symbol
lcd.print("C");
lcd.print(" ");
lcd.print(h);
lcd.print("%");
delay(750);
}
else if (tempC > 31)
{
lcd.setCursor(0, 0);
lcd.print(tempC);
lcd.print(" "); //print the temp
lcd.print((char)223); // to get ° symbol
lcd.print("C");
lcd.print(" ");
lcd.print(h);
lcd.print("%");
delay(750);
}
int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int".
// "myBPM" hold this BPM value now.
if (pulseSensor.sawStartOfBeat())
{
lcd.setCursor(0,1);
lcd.print("BPM:"); // Print phrase "BPM: "
lcd.println(myBPM); // Print the value inside of myBPM.
lcd.print(" ");
delay(100);
}
// Write to ThingSpeak channel.
ThingSpeak.setField(1, tempC);
ThingSpeak.setField(2, h);
ThingSpeak.setField(3, myBPM);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if (x == 200)
{
Serial.println("Channel update successful.");
}
else
{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
}

Define Pin RX, TX Without SoftwareSerial Arduino

Hi I did 2 diferent codes, then after verifying that they work i try to combine them and I got a problem. I used GSM.h to controle my GSM module, and to controle the GPS I used the SoftwareSerial.h.
I tryed to combine these to codes and this 2 libraries conflit with each other.
Can some one help me?
This is my code
//GSM
#include <GSM.h>
#define PINNUMBER "3805"
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;
//N de telefone de envio
char remoteNumber[20]= "914181875";
//Conteudo do SMS
char txtMsg[200]="Tester";
int val = 0;
//GPS
//#include <SoftwareSerial.h>
#include <TinyGPS.h>
TinyGPS gps;
SoftwareSerial ss(4, 3);
static void print_float(float val, float invalid, int len, int prec);
int button = 7;
void setup()
{
// initialize serial communications
pinMode(button, INPUT);
Serial.begin(9600);
Serial.println("SMS Messages Sender");
// connection state
boolean notConnected = true;
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while(notConnected)
{
if(gsmAccess.begin(PINNUMBER)==GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
}
void loop()
{
val = digitalRead(button);
if (val == HIGH){
sendSMS();
}
}
void sendSMS(){
Serial.print("Message to mobile number: ");
Serial.println(remoteNumber);
// sms text
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the message
sms.beginSMS(remoteNumber);
sms.print(txtMsg);
sms.endSMS();
Serial.println("\nCOMPLETE!\n");
}
I would try using the AltSoftSerial library instead of SoftwareSerial:
https://github.com/PaulStoffregen/AltSoftSerial
The disadvantage of this library is it requires you to use specific pins for serial communication with your GPS hardware, which are different from the pins you're currently using:
// AltSoftSerial always uses these pins:
//
// Board Transmit Receive PWM Unusable
// ----- -------- ------- ------------
// Teensy 3.0 & 3.1 21 20 22
// Teensy 2.0 9 10 (none)
// Teensy++ 2.0 25 4 26, 27
// Arduino Uno 9 8 10
// Arduino Leonardo 5 13 (none)
// Arduino Mega 46 48 44, 45
// Wiring-S 5 6 4
// Sanguino 13 14 12
I don't have the hardware to test but the following code does compile:
//GSM
#include <GSM.h>
#define PINNUMBER "3805"
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;
//N de telefone de envio
char remoteNumber[20]= "914181875";
//Conteudo do SMS
char txtMsg[200]="Tester";
int val = 0;
//GPS
#include <AltSoftSerial.h>
AltSoftSerial ss;
#include <TinyGPS.h>
TinyGPS gps;
static void print_float(float val, float invalid, int len, int prec);
int button = 7;
void setup()
{
// initialize serial communications
pinMode(button, INPUT);
Serial.begin(9600);
Serial.println("SMS Messages Sender");
// connection state
boolean notConnected = true;
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while(notConnected)
{
if(gsmAccess.begin(PINNUMBER)==GSM_READY)
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
}
void loop()
{
val = digitalRead(button);
if (val == HIGH){
sendSMS();
}
}
void sendSMS(){
Serial.print("Message to mobile number: ");
Serial.println(remoteNumber);
// sms text
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(txtMsg);
// send the message
sms.beginSMS(remoteNumber);
sms.print(txtMsg);
sms.endSMS();
Serial.println("\nCOMPLETE!\n");
}

distribute data received from a serial communication with 2 arduino uno

I must send 3 values gave by 3 potentiometers ,connected by an Arduino Uno, and send them to another Arduino Uno whith a serial comunication. The received values must be distributed in 3 servo motors so that each knob able to control the servo motor movement.
the problem with this program is that the values received are not distributed correctly (for example the case that the value of the potentiometer 1 is to be read by the servo motor 3 or other cases). I ask if I could help to synchronize data received with the distribution of them to the servo motors. Thanks in advance.
sketch arduino with potentiometers:
#include <SoftwareSerial.h>
#define RX 2 //Pin tx
#define TX 3 //Pin rx
#define POTPIN A0
#define POTPIN2 A1
#define POTPIN3 A2
SoftwareSerial BTserial(RX, TX);
int lettura_pot;
int lettura_pot2;
int lettura_pot3;
byte val_servo;
byte val_servo2;
byte val_servo3;
void setup()
{
Serial.println("Inizializzazione seriale...");
Serial.begin(9600);
BTserial.begin(9600);
}
void loop()
{
BTserial.write(255); /* synch symbol */
lettura_pot = analogRead(POTPIN);
val_servo=map(lettura_pot,0,1023,0,180);
BTserial.write(val_servo);
Serial.println(val_servo);
lettura_pot2 = analogRead(POTPIN2);
val_servo2=map(lettura_pot2,0,1023,0,180);
BTserial.write(val_servo2);
Serial.println(val_servo2);
lettura_pot3 = analogRead(POTPIN3);
val_servo3=map(lettura_pot3,0,1023,0,180);
BTserial.write(val_servo3);
Serial.println(val_servo3);
}
sketch arduino with servo motors:
#include <SoftwareSerial.h>
#include<Servo.h>
SoftwareSerial BTserial(2, 3);
Servo myservo, myservo2, myservo3;
byte val_servo,val_servo2,val_servo3,a;
void setup() {
Serial.begin(9600);
BTserial.begin(9600);
myservo.attach(9);
myservo2.attach(10);
myservo3.attach(11);
}
void loop() {
if (BTserial.available() > 0) {
if (BTserial.available() == 255) { /* synch */
val_servo = BTserial.read();
val_servo2 = BTserial.read();
val_servo3 = BTserial.read();
}
Serial.print("SERVO1:");
Serial.println(val_servo);
Serial.print("SERVO2:");
Serial.println(val_servo2);
Serial.print("SERVO3:");
Serial.println(val_servo3);
myservo.write(val_servo);
myservo2.write(val_servo2);
myservo3.write(val_servo3);
BTserial.flush();
}
}
master code( no modificated):
#include <SoftwareSerial.h>
#define RX 2 //Pin tx
#define TX 3 //Pin rx
#define POTPIN A0
#define POTPIN2 A1
#define POTPIN3 A2
SoftwareSerial BTserial(RX, TX);
int lettura_pot;
int lettura_pot2;
int lettura_pot3;
byte val_servo;
byte val_servo2;
byte val_servo3;
void setup()
{
Serial.println("Inizializzazione seriale...");
Serial.begin(9600);
BTserial.begin(9600);
}
void loop()
{
BTserial.write(255);
lettura_pot = analogRead(POTPIN);
val_servo=map(lettura_pot,0,1023,0,180);
BTserial.write(val_servo);
Serial.println(val_servo);
lettura_pot2 = analogRead(POTPIN2);
val_servo2=map(lettura_pot2,0,1023,0,180);
BTserial.write(val_servo2);
Serial.println(val_servo2);
lettura_pot3 = analogRead(POTPIN3);
val_servo3=map(lettura_pot3,0,1023,0,180);
BTserial.write(val_servo3);
Serial.println(val_servo3);
}
slave code( modificated):
#include <SoftwareSerial.h>
#include<Servo.h>
SoftwareSerial BTserial(2, 3);
Servo myservo, myservo2, myservo3;
byte val_servo,val_servo2,val_servo3,a;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
BTserial.begin(9600);
myservo.attach(9);
myservo2.attach(10);
myservo3.attach(11);
}
void loop() {
// Read serial input:
if (BTserial.available() > 0) {
byte synch_symbol = BTserial.read();
if (synch_symbol == 255) {
while (BTserial.available() < 3) { }; /* wait for values */
val_servo = BTserial.read();
val_servo2 = BTserial.read();
val_servo3 = BTserial.read();
/* do something with values */
}
Serial.print("SERVO1:");
Serial.println(val_servo);
Serial.print("SERVO2:");
Serial.println(val_servo2);
Serial.print("SERVO3:");
Serial.println(val_servo3);
myservo.write(val_servo);
myservo2.write(val_servo2);
myservo3.write(val_servo3);
BTserial.flush();
}
}

Resources