Nextion code

Hi,

I'm new to the Nextion and I'm looking for some help to fix a minor issue with my code.

I'm trying to take a contactless temperature reading of a user using the mlx90614 and if the temperature is above 38'C it will send an SMS. This works fine but I was trying to use a button on the screen that will allow the temperature check to happen.

At the minute, the button is ineffective and the readings are constant. I've used a "touch release event" for the button also. Any guidance would be appreciated.

Thanks,

Joe.

#include <Nextion.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 2);

Adafruit_MLX90614 mlx = Adafruit_MLX90614();

NexText tTempC = NexText(1, 5, "tTempC");
NexButton bScan = NexButton(1, 4, "bScan");

NexTouch *nex_listen_list[] =
{
&bScan,
NULL
};

float t = 0;

void setup()
{
Serial.begin(9600);
mySerial.begin(9600);

mlx.begin();

nexInit();

bScan.attachPop(bScanPopCallback, &bScan);
}

void loop(void)
{
nexLoop(nex_listen_list);

if(&bScan)
{
bScanPopCallback();
}

if (t > 38)
{
SMS();
Serial.print("tTemp.txt=");
Serial.print(""");
Serial.print("High Temperature");
//Serial.print(user_code_1);
Serial.print(""");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
else
{
Serial.print("tTemp.txt=");
Serial.print(""");
Serial.print("Temperature is ok");
Serial.print(""");
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
delay(3000);
}
}

void bScanPopCallback()
{
t = mlx.readObjectTempC();
static char temperatureCTemp[6];
dtostrf(t, 6, 2, temperatureCTemp);
tTempC.setText(temperatureCTemp);
}

void SMS()
{
mySerial.println("AT"); //Once the handshake test is successful, it will back to OK
delay(1000);
mySerial.println("ATCOPS?"); //Once the handshake test is successful
delay(1000);
mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
delay(1000);
mySerial.println("AT+CMGS="+############"");//change ZZ with country code and xxxxxxxxxxx with phone number to sms
delay(1000);
mySerial.print("Warning! High Temperature Recorded"); //text content
delay(1000);
mySerial.write(26);
}

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
https://forum.arduino.cc/index.php?topic=712198.0
Then look down to "code problems" about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

Hi,

Thanks for your reply.

I've uploaded a fritzing file of what the circuit looks like. I should have mentioned I was using an Arduino Uno and a SIM900 GSM in the previous post, apologies.

Also, I will post my code that way in the future, I never knew. Cheers Tom

Circuit design.zip (4.62 KB)

Hi,
zip is not good to post, most members will not open them.
If your circuit is a fritzy image and not using proper circuit symbols, then it may not convey all the circuit info.

In fact your file is in fritzy format.
Attach a jpg of your circuit and post your code inside code tags as described in the forum "how to"..
Fritzy should allow you to export your circuit as a jpg.

Tom... :slight_smile:

[code]
#include <Nextion.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3, 2);

Adafruit_MLX90614 mlx = Adafruit_MLX90614();

NexText tTempC = NexText(1, 5, "tTempC");
NexButton bScan = NexButton(1, 4, "bScan");

NexTouch *nex_listen_list[] =
{
  &bScan,
  NULL
};

float t = 0;

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);

  mlx.begin();

  nexInit();

  bScan.attachPop(bScanPopCallback, &bScan);
}


void loop(void)
{
  nexLoop(nex_listen_list);

  if (&bScan)
  {
    bScanPopCallback();
  }

  if (t > 38)
  {
    SMS();
    Serial.print("tTemp.txt=");
    Serial.print("\"");
    Serial.print("High Temperature");
    //Serial.print(user_code_1);
    Serial.print("\"");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
  }
  else
  {
    Serial.print("tTemp.txt=");
    Serial.print("\"");
    Serial.print("Temperature is ok");
    Serial.print("\"");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    delay(3000);
  }
}

void bScanPopCallback()
{
  t = mlx.readObjectTempC();
  static char temperatureCTemp[6];
  dtostrf(t, 6, 2, temperatureCTemp);
  tTempC.setText(temperatureCTemp);
}

void SMS()
{
  mySerial.println("AT"); //Once the handshake test is successful, it will back to OK
  delay(1000);
  mySerial.println("ATCOPS?"); //Once the handshake test is successful
  delay(1000);
  mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
  delay(1000);
  mySerial.println("AT+CMGS=\"+353857123356\"");//change ZZ with country code and xxxxxxxxxxx with phone number to sms
  delay(1000);
  mySerial.print("Warning! High Temperature Recorded"); //text content
  delay(1000);
  mySerial.write(26);
}

[/code]

Hi,

Ok done. Hopefully, the third time is the charm.

Not going to work without power and ground to the Nextion, or power to the thing with SIM on it.

You are using for the Nextion the same serial port that is used for the serial monitor, that is going to cause problems. I have not studied your code in detail because you are using the Nextion library, about which I know very little, but I'd address the obvious first. I suggest a Mega, which has 3 spare serial ports.

Thanks for your reply,

the GSM and Nextion require separate power supplies due to their current requirements.

I have a temperature displayed on the screen so I think the serial port does work. I just want the button I created "bScan" to display the temperature only when it's pressed. Right now a temperature reading happens without the button and continues to read the temperature from the mlx90614 displaying different readings every few seconds.

JoeBuckley:
The GSM and Nextion require separate power supplies due to their current requirements.

We only know what you tell us, and the schematic suggests otherwise. Even if they have got separate power supplies there is nothing to suggest the grounds are connected.

I can't help any more as I don't know the Nextion libraries. When it comes to Nextion questions I answer more of them here than anyone else. I could help you more if you use the methods I set out in my tutorial 'using Nextion displays with Arduino' at the top of the display section of the forum.

Good luck with your project.

That's no problem at all. Hopefully I can get it sorted, thanks for your time.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.