I'm new here, and I'm new to programming and Arduino in general.
I am building something to basically act as a remote weather station, and I want it to text(sms) me the sensor data.
I've posted my code below, and what I'm trying to figure out, is how come I can't seem to get the correct formatting for this project.
I can get it to message me all the data over multiple messages, but I can't understand why I can't format it into one message, because this would
certainly be under 140 characters.
I'm looking for something like the following example:
4012mV Battery pack
65.10% Relative humidity
14.62 'C Temperature
1023.44mb Barometric pressure
As I said, I have managed to get it to work over 2 or three separate messages, but when I try to string it all together, I either get a "Failed" message,
or just a blank message delivered to my phone.
I'm sure it's the way I'm formatting the text/strings and converting to charArray... which to be brutally honest, was really confusing and took me a
couple of days to figure out on its own :S
I hope someone might be able to help, and put me out of my misery :D
Wes
Code: Select all
#include "Adafruit_FONA.h"
#include <SFE_BMP180.h>
#include <Wire.h>
#include "DHT.h"
#define FONA_RX 5
#define FONA_TX 3
#define FONA_RST 4 //sms
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
SFE_BMP180 pressure;
double pressNEW;
#include <SoftwareSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;
Adafruit_FONA_3G fona = Adafruit_FONA_3G(FONA_RST);
#define phoneNumber "phone number"
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(115200);
dht.begin();
if (pressure.begin())
Serial.println("BMP180 init success");
else
{
Serial.println("BMP180 init fail (disconnected?)\n\n");
while(1); // Pause forever.
}
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("SMS Data Sender");
fonaSerial->begin(4800);
if (! fona.begin(*fonaSerial)) {
Serial.println(F("Couldn't find FONA"));
while (1);
}
Serial.println(F("FONA is OK"));
Serial.println(F("Found "));
Serial.println("Allowing 25s for boot time and signal acquisition.");
delay(25000);
}
void loop() {
/////////////////////////////////////////// read the battery voltage
uint16_t vbat;
if (! fona.getBattVoltage(&vbat)) {
Serial.println(F("Failed to read Batt"));
} else {
Serial.print(F("VBat = ")); Serial.print(vbat); Serial.println(F(" mV"));
}
String str1;
String str2="mV";
String str3;
str1=String(vbat);
//str3=str1 + str2;
/////////////////////////////////////////////////CHECK DHT
float h = dht.readHumidity();
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
String str1dht;
String str2dht="% Humidity\n";
String str3dht;
String str4dht="'C Temperature";
String str5dht;
str1dht=String(h);
str3dht=String(t);
//str5dht=str1dht + str2dht + str3dht + str4dht;
////////////////////////////////////////////////CHECK BMP180
double pressNEW;
pressNEW=getPressure();
String baroSTR1;
String baroSTR2="mb barometric pressure";
baroSTR1=String(pressNEW);
//String baroSTR3=baroSTR1+baroSTR2;
///////////////////////////////////////////////////////////SEND ALL IN ONE
char completeMSG[141];
String space=" ";
String str1COMP;
str1COMP=str1; //batt mV
String battMV="mV battery, ";
String str2COMP;
str2COMP=str1dht; //humidity %
String hum="%RH, ";
String str3COMP;
str3COMP=str3dht; //temp C
String tem="'C, ";
String str4COMP;
str4COMP=baroSTR1; //baro pressure mb
String str5COMP;
str5COMP=str1COMP + battMV + str2COMP + space + str3COMP + tem + str4COMP;
str5COMP.toCharArray(completeMSG,140);
flushSerial();
if (!fona.sendSMS(phoneNumber, completeMSG)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("Sent!"));
}
Serial.println("\nCOMPLETE!\n");
delay(20000);
}
//////////////////////////////////////////////////////////////////////////
void flushSerial() {
while (Serial.available())
Serial.read();
}
double getPressure()
{
char status;
double T,P,p0,a;
// You must first get a temperature measurement to perform a pressure reading.
// Start a temperature measurement:
// If request is successful, the number of ms to wait is returned.
// If request is unsuccessful, 0 is returned.
status = pressure.startTemperature();
if (status != 0)
{
// Wait for the measurement to complete:
delay(status);
// Retrieve the completed temperature measurement:
// Note that the measurement is stored in the variable T.
// Use '&T' to provide the address of T to the function.
// Function returns 1 if successful, 0 if failure.
status = pressure.getTemperature(T);
if (status != 0)
{
// Start a pressure measurement:
// The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
// If request is successful, the number of ms to wait is returned.
// If request is unsuccessful, 0 is returned.
status = pressure.startPressure(3);
if (status != 0)
{
// Wait for the measurement to complete:
delay(status);
// Retrieve the completed pressure measurement:
// Note that the measurement is stored in the variable P.
// Use '&P' to provide the address of P.
// Note also that the function requires the previous temperature measurement (T).
// (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
// Function returns 1 if successful, 0 if failure.
status = pressure.getPressure(P,T);
if (status != 0)
{
return(P);
}
else Serial.println("error retrieving pressure measurement\n");
}
else Serial.println("error starting pressure measurement\n");
}
else Serial.println("error retrieving temperature measurement\n");
}
else Serial.println("error starting temperature measurement\n");
}