I have a Nano, I2C LCD, SIM808 and a thermistor (47k). I am trying to make a box with a temperature probe that SMS a warning when the set limit is reached, and to reply with the current temp if asked with a codeword ("On").
It works, but chrashes. Either it just hangs, LCD freeze, LCD blacks out or any other weird thing.
The current setup blanks the LCD, bet continues to reply to SMSs.
Power source is a 9v 3A dc high quality converter. SIM808 powered directly, Nano and LCD with a ubec output 5v 3A. Thermistor by the 3.3 form nano. Using recommended resistors as needed.
Am I puzzled bu the blank LCD and the responding SIM808. The loop runs, and the lcd.clear() does not help? Nothing is displayed. It probably stops responding soon anyway. I just can't find any consistency nor predictability in its errors.
I have tried to minimise strings, and using a freemem routing did not show any decrease in memory.
#include <SoftwareSerial.h>
#include <RunningAverage.h>
#include <LiquidCrystal_I2C.h>
#define BCOEFFICIENT 3550 //3950 org, 3550 git korrekt temp ved 0 C.
LiquidCrystal_I2C lcd(0x27,16,2); //SDA - A4, SCL - A5, VCC - 5V
RunningAverage avgTemp(10); // define the moving average object
SoftwareSerial GPSmodule(2, 3); // RX, TX
const String varsel = "+XXXXXXXXXX"; //skriv inn telefonnummer for varsel med +XX
const int dogn = 3600000 ; //millisekunder i et dogn (86400000)
const int mintemp = 20; //setter varseltemp i C
String CellNum;
int avg;
float last2 = 0;
float lastTime = 0;
float last = 0;
void setup()
{
analogReference(EXTERNAL);
lcd.init();
lcd.backlight();
lcd.print(" Starter opp! ");
GPSmodule.begin(9600);
boot();
GPSmodule.println("AT");
delay(50);
GPSmodule.println("AT");
delay(50);
GPSmodule.println("AT+CMEE=1");
delay(50);
GPSmodule.println("AT+CMGF=1"); // AT command to set GPSmodule to SMS mode
delay(50);
GPSmodule.println("AT+CNMI=2,2,0,0,0"); // Set module to send SMS data to serial out upon receipt
delay(5000);
lcd.clear();
}
void loop()
{
float t1;
float Celcius;
String textMessage; // Variable to store text message
String CellNumtemp;
if (lastTime > millis()){
last = 0;
last2 = 0;
}
t1 = analogRead(A3);
if ( t1 < 1000 && (t1 > 0)) {
Celcius = totemp(t1);
}
if (millis() - 10000 > last){ //venter "pause" på å legge inn ny verdi i average
last = millis();
avgTemp.addValue(Celcius);
avg = avgTemp.getAverage();
lcd.clear();
if (avg > -20 && (avg < 40)){
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print(avg);
lcd.print(F(" \xDF""C"));
}
}
while (GPSmodule.available() > 0) {
char c = GPSmodule.read();
textMessage += c;
delay(10); //small delay to allow input buffer to fill
}
if (textMessage.indexOf("On") >= 0) { //"On" er kodeordet i SMSen, bytt til noe annet
CellNumtemp = textMessage.substring(textMessage.indexOf("+47"));
CellNum = CellNumtemp.substring(0,11);
String message = "Temperaturen er " + String(avg) + " C";
sendSMS(message);
}
if (avg <= mintemp){
if (millis() > dogn + last2){ //sender et varsel i dognet
CellNum = varsel;
String message2 = "Temperaturen er " + String(avg) + " C";
sendSMS(message2);
last2 = millis();
}
}
lastTime = millis();
}
void sendSMS(String message3) {
String sende;
sende = "AT+CMGS=\"" + CellNum + "\"";
GPSmodule.println(sende);
delay(100);
GPSmodule.print(message3);
delay(100);
GPSmodule.println((char)26);
delay(50);
GPSmodule.println();
CellNum = "";
delay(5000);
}
void boot(){
delay(500);
if (analogRead(A1) > 700){
}
else{
digitalWrite(5, HIGH); //til D9 på SIM808, skrur den på
delay(1200);
digitalWrite(5, LOW);
delay(500);
}
delay(5000);
}
float totemp(float i){
i = 1023 / i - 1;
i = 47000 / i; //RTS300R47K3.81A 47k thermoresistor
float steinhart;
steinhart = i / 47000; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (25 + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert absolute temp to C
return steinhart;
}