I have a Nextion Display, Photon and a 4 relay board. I want to be able to push a button on the Display and turn on one of the relays and then push again to turn off the relay.I have Particle.Fuction working and I can control the relay from console.particle.io but i have 8 separate functions.
here is the code
#include <Particle_SI7021.h>
#include <ITEADLIB_Nextion.h>
#include <MQTT.h>
#include <math.h>
SI7021 sensor;
//MainPage Page Setup
NexProgressBar j100 = NexProgressBar(0, 8, "j100");
NexText t0 = NexText(0, 1, "t0");
NexText t2 = NexText(0, 9, "t2");
NexNumber n0 = NexNumber(0, 6, "n0");
NexNumber n1 = NexNumber(0, 7, "n1");
NexButton b0 = NexButton(0, 4, "b1");
NexButton b1 = NexButton(0, 5, "b0");
//AC Page Setup
//NexProgressBar j100 = NexProgressBar(0, 3, "j100");
//NexText t0 = NexText(1, 3, "t0");
NexNumber n100 = NexNumber(1, 7, "n100");
NexNumber n101 = NexNumber(1, 8, "n101");
NexButton b100 = NexButton(1, 6, "b100");
NexButton b101 = NexButton(1, 1, "b101");
NexButton b102 = NexButton(1, 2, "b102");
//Setting Page Setup
//NexProgressBar j0 = NexProgressBar(0, 3, "j0");
//NexText t0 = NexText(2, 4, "t0");
NexButton b200 = NexButton(2, 3, "b200");
NexButton b201 = NexButton(2, 1, "b201");
NexButton b202 = NexButton(2, 2, "b202");
NexButton b203 = NexButton(2, 5, "b203");
//Display Page Setup
//NexProgressBar j0 = NexProgressBar(0, 3, "j0");
//NexText t0 = NexText(3, 7, "t0");
NexNumber n300 = NexNumber(3, 3, "n300");
NexText t300 = NexText(3, 4, "t300");
NexButton b300 = NexButton(3, 2, "b300");
NexButton b301 = NexButton(3, 5, "b301");
NexButton b302 = NexButton(3, 2, "b302");
//DateTime Page Setup
//NexProgressBar j0 = NexProgressBar(0, 3, "j0");
//NexText t0 = NexText(4, 3, "t0");
NexNumber n400 = NexNumber(4, 2, "n400");
NexButton b400 = NexButton(4, 1, "b400");
//Relay Page Setup
NexButton b1100 = NexButton(11, 1, "b1100");
NexButton b1101 = NexButton(11, 2, "b1101");
NexButton b1102 = NexButton(11, 3, "b1102");
NexButton b1103 = NexButton(11, 4, "b1103");
NexButton b1104 = NexButton(11, 5, "b1104");
NexButton b1105 = NexButton(11, 6, "b1105");
NexButton b1106 = NexButton(11, 7, "b1106");
NexButton b1107 = NexButton(11, 8, "b1107");
USARTSerial & nexSerial = Serial1;
/*******************************************************************************
IO mapping
*******************************************************************************/
// A0 : photoresistor
// A5 : power
int photoresistor = A0; // This is where your photoresistor is plugged in. The other side goes to the "power" pin (below).
int power = A5;
int lightlevel;
int acrelay = D3;
int heatrelay = D4;
int fanrelay = D5;
int auxrelay = D6;
// 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);
String dspHostname = "hostname";
String dspSSID = "None";
String dspIP = "None";
int temperature = 0;
int humidity = 0;
int ACRelayOn (String command);
int ACRelayOff (String command);
int HeatRelayOn (String command);
int HeatRelayOff (String command);
int FanRelayOn (String command);
int FanRelayOff (String command);
int AuxRelayOn (String command);
int AuxRelayOff (String command);
void setup() {
Serial.begin(115200);
Serial1.print("baud=115200");
Serial1.write(0xff);
Serial1.write(0xff);
Serial1.write(0xff);
Serial1.begin(115200);
Serial.println("Si7021 test");
Time.zone(-5);
nexInit();
sensor.begin();
NexTouch *nex_listen_list[] = {
&b203,
&b1100,
&b1101,
&b1102,
&b1103,
&b1104,
&b1105,
&b1106,
&b1107,
NULL
};
//Initiize the relay control pings as output
pinMode(acrelay, OUTPUT);
pinMode(heatrelay, OUTPUT);
pinMode(fanrelay, OUTPUT);
pinMode(auxrelay, OUTPUT);
// Initialize all relays to an OFF state
digitalWrite(acrelay, LOW);
digitalWrite(heatrelay, LOW);
digitalWrite(fanrelay, LOW);
digitalWrite(auxrelay, LOW);
// 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);
WiFi.setHostname(dspHostname);
dspSSID = WiFi.SSID();
dspIP = WiFi.localIP();
Particle.variable("Office Temperature", &temperature, INT);
Particle.variable("Office Humidity", &humidity, INT);
Particle.variable("WiFi SSID", &dspSSID, STRING);
Particle.function("TurnOnAC", TurnOnAC);
Particle.function("TurnOffAC", TurnOffAC);
Particle.function("TurnOnHeat", TurnOnHeat);
Particle.function("TurnOffHeat", TurnOffHeat);
Particle.function("TurnOnFan", TurnOnFan);
Particle.function("TurnOffFan", TurnOffFan);
Particle.function("TurnOnAux", TurnOnAux);
Particle.function("TurnOffAux", TurnOffAux);
WiFiAccessPoint aps[20];
int found = WiFi.scan(aps, 20);
for (int i=0; i<found; i++) {
WiFiAccessPoint& ap = aps[i];
Serial.print("SSID: ");
Serial.println(ap.ssid);
Serial.print("Security: ");
Serial.println(ap.security);
Serial.print("Channel: ");
Serial.println(ap.channel);
Serial.print("RSSI: ");
Serial.println(ap.rssi);
}
}
void loop() {
//Relay Control
// temperature is an integer in hundredths
float temperature = sensor.getFahrenheitHundredths();
temperature = temperature / 100;
// humidity is an integer representing percent
float humidity = sensor.getHumidityPercent();
WiFiSignal sig = WiFi.RSSI();
float strength = sig.getStrength();
//Particle.publish("WiFi SSID", String(dspSSID), 1800, PRIVATE);
Particle.publish(dspHostname, String(temperature), 120000, PRIVATE);
Particle.publish(dspHostname, String(humidity), 120000, PRIVATE);
//Particle.publish("WiFi Strength", String(strength), 1800, PRIVATE);
Serial.print("Humi: "); Serial.println(humidity);
Serial.print("Temp: "); Serial.println(temperature);
//Displaying on Nextion
n0.setValue(temperature);
n1.setValue(humidity);
t0.setText(Time.format(Time.now(), TIME_FORMAT_DEFAULT));
j100.setValue(strength);
t2.setText(dspHostname + " : " +dspSSID + " : " + dspIP);
//nexLoop(nex_listen_list);
}
/*******************************************************************************
* 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_DEFAULT);
timeNow = timeNow.substring(11, timeNow.length()-6);
return " " + timeNow;
}
//Turn on the AC Relay
int TurnOnAC(String command){
if(command == "TurnOnAC"){
digitalWrite(acrelay, HIGH);
return 1;
}
else
{
digitalWrite(acrelay, LOW);
return 0;
}
return -1;
}
//Turn off the AC Relay
int TurnOffAC(String command){
if(command == "TurnOffAC"){
digitalWrite(acrelay, LOW);
return 1;
}
else
{
digitalWrite(acrelay, HIGH);
return 0;
}
return -1;
}
//Turn on the Heat Relay
int TurnOnHeat(String command){
if(command == "TurnOnHeat"){
digitalWrite(heatrelay, HIGH);
return 1;
}
else
{
digitalWrite(heatrelay, LOW);
return 0;
}
return -1;
}
//Turn off the Heat Relay
int TurnOffHeat(String command){
if(command == "TurnOffHeat"){
digitalWrite(heatrelay, LOW);
return 1;
}
else
{
digitalWrite(heatrelay, HIGH);
return 0;
}
return -1;
}
//Turn on the Fan Realy
int TurnOnFan(String command){
if(command == "TurnOnFan"){
digitalWrite(fanrelay, HIGH);
return 1;
}
else
{
digitalWrite(fanrelay, LOW);
return 0;
}
return -1;
}
//Turn off the Fan Realy
int TurnOffFan(String command){
if(command == "TurnOffFan"){
digitalWrite(fanrelay, LOW);
return 1;
}
else
{
digitalWrite(fanrelay, HIGH);
return 0;
}
return -1;
}
//Turn on the Aux Relay
int TurnOnAux(String command){
if(command == "TurnOnAux"){
digitalWrite(auxrelay, HIGH);
return 1;
}
else
{
digitalWrite(auxrelay, LOW);
return 0;
}
return -1;
}
//Turn off the Aux Relay
int TurnOffAux(String command){
if(command == "TurnOffAux"){
digitalWrite(auxrelay, LOW);
return 1;
}
else
{
digitalWrite(auxrelay, HIGH);
return 0;
}
return -1;
}