I am trying to use the examples for ITEAD display but they are all set to 4.3 is there any of them that are for the 2.4?
Here is the full list of code
#include "math.h"
#include "/Si7021/Particle_SI7021.h"
#include "/ITEADLIB_Nextion/ITEADLIB_Nextion.h"
SI7021 sensor;
//Display Text setup
NexText MainDate = NexText(0, 2, "t0");
NexText MainTmp = NexText(0, 3, "t2");
NexText MainHum = NexText(0, 4, "t3");
NexText ACDate = NexText(1, 5, "t1");
// Display Button setup
NexButton b0 = NexButton(1, 3, "btnup");
NexButton b1 = NexButton(1, 4, "btndwn");
/*******************************************************************************
IO mapping
*******************************************************************************/
// A0 : photoresistor
// A1 : relay: fan
// A2 : relay: heat
// A3 : relay: cool
// A5 : power
int photoresistor = A0; // This is where your photoresistor is plugged in. The other side goes to the "power" pin (below).
int fan = A1;
int heat = A2;
int cool = A3;
int power = A5;
int lightlevel;
// The following values are the read values of the photoresistor
int Closedvalue=5; // This is the average value that the photoresistor reads while in the fridge.
int Openvalue=260; // This is the average value that the photoresistor reads when exposed to average lighting.
int Cutoff=((Closedvalue+Openvalue)/2);
//here are the possible modes the thermostat can be in: off/heat/cool
#define MODE_OFF "Off"
#define MODE_HEAT "Heat"
#define MODE_COOL "Cool"
String externalMode = MODE_OFF;
String internalMode = MODE_OFF;
bool modeButtonClick = false;
//here are the possible states of the thermostat
#define STATE_INIT "Initializing"
#define STATE_IDLE "Idle"
#define STATE_HEATING "Heating"
#define STATE_COOLING "Cooling"
#define STATE_FAN_ON "Fan On"
#define STATE_OFF "Off"
#define STATE_PULSE_HEAT "Pulse Heat"
#define STATE_PULSE_COOL "Pulse Cool"
String state = STATE_INIT;
int rssi = 0;
void setup() {
Serial.begin(115200);
Serial1.begin(115200);
Serial.println("Si7021 test");
sensor.begin();
nexInit();
Time.setFormat("%I:%M%p %m/%d/%Y");
// photoresistor setup
pinMode(photoresistor,INPUT); // Our photoresistor pin is input (reading the photoresistor)
pinMode(power,OUTPUT); // The pin powering the photoresistor is output (sending out consistent power)
digitalWrite(power,HIGH);
Particle.variable("RSSI", &rssi, INT);
Particle.variable("Light Level", &lightlevel, INT);
//declare and init pins
pinMode(fan, OUTPUT);
pinMode(heat, OUTPUT);
pinMode(cool, OUTPUT);
//myDigitalWrite(fan, LOW);
//myDigitalWrite(heat, LOW);
//myDigitalWrite(cool, LOW);
}
void loop() {
// temperature is an integer in hundredths
float temperature = sensor.getFahrenheitHundredths();
temperature = temperature / 100;
// humidity is an integer representing percent
float humidity = sensor.getHumidityPercent();
// Wifi Signal
rssi = WiFi.RSSI();
//temperature to Display
MainTmp = temperature;
// Light level measurement
float lightlevel_measurement = analogRead(photoresistor);
lightlevel = (int)(lightlevel_measurement/4096*100);
// get humidity and temperature in one shot, saves power because sensor takes temperature when doing humidity anyway
// si7021_env data = sensor.getHumidityAndTemperature();
Serial.print("Humi: "); Serial.println(humidity);
Serial.print("Temp: "); Serial.println(temperature);
Serial1.print(MainTmp);
Particle.publish("All", Time.format());
Particle.publish("Wifi Signal", String(rssi), 60, PUBLIC);
Particle.publish("Light Level", String(lightlevel), 60, PUBLIC);
Particle.publish("Tempature", String(temperature), 60, PUBLIC);
delay(1000);
}
/*******************************************************************************
* Function Name : getTime
* Description : returns the time in the following format: 14:42:31
TIME_FORMAT_ISO8601_FULL example: 2016-03-23T14:42:31-04:00
* Return : the time
*******************************************************************************/
String getTime() {
String timeNow = Time.format(Time.now(), TIME_FORMAT_ISO8601_FULL);
timeNow = timeNow.substring(11, timeNow.length()-7);
return " " + timeNow;
}
You can use the same code, you just need to recreate a similar HMI that fits your display and has the same widgets with the same names.
Or you just upen the 4.3 HMI, move and resize the widgets so they fit the smaller screen and then change the device to 2.4"
BTW, with integer divisions it’s always best to rearrange the order this way
//lightlevel = (int)(lightlevel_measurement/4096*100); // you'll lose decimals and introduceextra truncation errors
lightlevel = (int)((lightlevel_measurement * 100)/4095); // 4095 is the max reading, so only 4095/4095 == 1.0
and since floating point calculations don’t mind it’s always better to have multiplications happen before divisions.
Your four publishes with only a delay(1000)
in between will violate the rate limit.
Rather incorporate all your data into one publish and either choose a unique event name or publish as PRIVATE
Particle.publish("All"
,String::format("%s: WiFi Signal: %d, Light Level: %.1f, Temperature: %.1f)"
,Time.format()
,rssi
,lightlevel
,temperature
)
,PRIVATE
);
This is depricated
Particle.variable("RSSI", &rssi, INT);
Particle.variable("Light Level", &lightlevel, INT);
it’s now
Particle.variable("RSSI", rssi, );
Particle.variable("Light Level", lightlevel);
And to be responsive to Nextion input, you need a nex_listen_list
and call nexLoop(nex_listen_list);
inside loop()
and for that you should replace delay(1000)
with a non-blocking delay.
What is a non-blocking delay?
use some kind of timing mechanism to trigger the event every time you want to do it...
For example, using millis() timer
void loop()
{
static unsigned long lastMillis = 0;
if(millis() - lastMillis > TIMER_INTERVAL) //one second interval defined as a global constant
{
//put your things to happen once a second here
lastMillis += TIMER_INTERVAL;
}
}
or the clock:
void loop()
{
static int lastSecond = 60;
if(Time.second() != lastSecond)
{
//put your things to happen once a second here
lastSecond = second;
}
}
in each of these examples loop() continues to run over and over again between action intervals and is not stopped using the (blocking) delay.
Besides @BulldogLowell create explanation, there is a nice article here:
and a good template is:
//============================
// non blocking delay
//============================
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
//-- PUT YOUR CODE HERE --
some code
//-- CODE END --
previousMillis = currentMillis; //checkpoint
}
//============================