NAOS (Arduino OS)

Build This proudly introducing, NAOS. Easy Arduino OS with plentiful of features including SD control, RTC, 7 program space, the WHOLE set of 9 + gas sensors pre programmed in for environment monitoring and maybe more!

This project isn't possible if it weren't for Nextion displays and their GUI. It's half of the workload tied into the ability to choose one thing from another. All programs right now are programmed to run all at the same time, however I would advise against so since this uses quite a bit of memory. You need 31144 bytes of program storage, and 6267 bytes of dynamic memory. Meaning Arduino Mega 2650's or higher will be needed to run everything.

If there are better ways to share this type of creation, Im open to suggestions. For now, it can hang out here.

Heres a quick demo of initial GUI for the OS.

What is your video about? No sound, no text, nothing.

Hello
what are the key figures of this OS?
Did you made any bench mark tests?

The video is just a dry run of the UI for the OS. Ill be doing a better video sometime later after finishing the code. Mind you I usually dont talk in my videos because I dont have a radio voice in the slightest sense. Kind of old where I just dont care about learning video editing to create nice presentations. Actually, Ill start doing powerpoint presentation. I know how to work that.

As far as key figures and benchmark, I don't know or haven't done that yet. If you have recommendations on benchmarking it, let me know. Im basically still average in programming so my philosophy is if it runs well, its good enough. I will need about up to a month since Im in the transition of a move. Ill upload anything as they are ready for release.

Here is another demo with actual testing of the system. Again, voice is not all that great. You have been warned.

NAOS © 2021 by Sergio Williams is licensed under CC BY-NC 4.0 [image][image][image]

OTA_NAOS.ino (1.9 KB)
NAOS_Wifi.ino (32.8 KB)
NAOS Nextion.zip (278.5 KB)

Before posting code, please use Tools / Auto Format at the top of the IDE. It makes the code much easier to read and you will probably find it very helpful yourself. Once you've done that, use Edit / Copy for Forum.

#include <doxygen.h>
#include <NexButton.h>
#include <NexCheckbox.h>
#include <NexConfig.h>
#include <NexCrop.h>
#include <NexDualStateButton.h>
#include <NexGauge.h>
#include <NexGpio.h>
#include <NexHardware.h>
#include <NexHotspot.h>
#include <NexNumber.h>
#include <NexObject.h>
#include <NexPage.h>
#include <NexPicture.h>
#include <NexProgressBar.h>
#include <NexRadio.h>
#include <NexRtc.h>
#include <NexScrolltext.h>
#include <NexSlider.h>
#include <NexText.h>
#include <NexTimer.h>
#include <Nextion.h>
#include <NexTouch.h>
#include <NexUpload.h>
#include <NexVariable.h>
#include <NexWaveform.h>
#include "RTClib.h"
#include <DHT.h>
#include <DHT_U.h>
#include <SPI.h>
#include <avr/wdt.h>
#include <SD.h>
#include <MQUnifiedsensor.h>
#include <Adafruit_Sensor.h>
#include <EEPROM.h>
#include <Adafruit_ADS1X15.h>
#include "Nextion.h"
#include <Wire.h>
#define flashp A15
#define mq2pin A0
#define mq3pin A1
#define mq4pin A2
#define mq5pin A3
#define mq6pin A6
#define mq7pin A7
#define mq8pin A8
#define mq9pin A9
#define mq135pin A10
#define         Board                   ("Arduino MEGA")
#define         Type2                    ("MQ-2") //MQ2
#define         Type3                    ("MQ-3") //MQ3
#define         Type4                    ("MQ-4") //MQ4
#define         Type5                    ("MQ-5") //MQ5
#define         Type6                    ("MQ-6") //MQ6
#define         Type7                    ("MQ-7") //MQ7
#define         Type8                    ("MQ-8") //MQ8
#define         Type9                    ("MQ-9") //MQ9
#define         Type135                    ("MQ-135") //MQ135
#define         Voltage_Resolution5      (5)
#define         ADC_Bit_Resolution      (10) // For arduino UNO/MEGA/NANO
#define         RatioMQ2CleanAir        (9.83) //RS / R0 = 9.83 ppm 
#define         RatioMQ3CleanAir        (60) //RS / R0 = 9.83 ppm 
#define         RatioMQ4CleanAir        (4.4) //RS / R0 = 9.83 ppm 
#define         RatioMQ5CleanAir        (6.5) //RS / R0 = 9.83 ppm 
#define         RatioMQ6CleanAir        (10) //RS / R0 = 9.83 ppm 
#define         RatioMQ7CleanAir        (27.5) //RS / R0 = 9.83 ppm 
#define         RatioMQ8CleanAir        (70) //RS / R0 = 9.83 ppm 
#define         RatioMQ9CleanAir        (9.6) //RS / R0 = 9.83 ppm 
#define         RatioMQ135CleanAir        (3.6) //RS / R0 = 9.83 ppm 
MQUnifiedsensor MQ2(Board, Voltage_Resolution5, ADC_Bit_Resolution, A0, Type2);
MQUnifiedsensor MQ3(Board, Voltage_Resolution5, ADC_Bit_Resolution, A1, Type3);
MQUnifiedsensor MQ4(Board, Voltage_Resolution5, ADC_Bit_Resolution, A2, Type4);
MQUnifiedsensor MQ5(Board, Voltage_Resolution5, ADC_Bit_Resolution, A3, Type5);
MQUnifiedsensor MQ6(Board, Voltage_Resolution5, ADC_Bit_Resolution, A6, Type6);
MQUnifiedsensor MQ7(Board, Voltage_Resolution5, ADC_Bit_Resolution, A7, Type7);
MQUnifiedsensor MQ8(Board, Voltage_Resolution5, ADC_Bit_Resolution, A8, Type8);
MQUnifiedsensor MQ9(Board, Voltage_Resolution5, ADC_Bit_Resolution, A9, Type9);
MQUnifiedsensor MQ135(Board, Voltage_Resolution5, ADC_Bit_Resolution, A10, Type135);
unsigned long timenow = millis();
int dread = 500;
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
const int chipSelect = 10;
DHT dht(A11,DHT11);
RTC_DS1307 rtc;
int16_t adc0, adc1, adc2, adc3; //ADC Stuff
uint32_t number1 = 0;
uint32_t number2 = 0;
uint32_t number3 = 0;
uint32_t number4 = 0;
uint32_t number5 = 0;
uint32_t number6 = 0;
uint32_t number7 = 0;
uint32_t number8 = 0;
uint32_t number9 = 0;
uint32_t number10 = 0;
uint32_t CurrentPage = 0;
const int numReadings = 10;   //How many samples to take before averaging
const int numReadings2= 10;   //How many samples to take before averaging
const int numReadings3 = 10;   //How many samples to take before averaging
const int numReadings4 = 10;   //How many samples to take before averaging
int readings[numReadings];      // the readings from the Temp Sensor
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average
int readings2[numReadings2];      // the readings from the Temp Sensor
int readIndex2 = 0;              // the index of the current reading
int total2 = 0;                  // the running total
int average2 = 0;                // the average
int readings3[numReadings3];      // the readings from the Temp Sensor
int readIndex3 = 0;              // the index of the current reading
int total3 = 0;                  // the running total
int average3 = 0;                // the average
int readings4[numReadings4];      // the readings from the Temp Sensor
int readIndex4 = 0;              // the index of the current reading
int total4 = 0;                  // the running total
int average4 = 0;                // the average
int flash = 0;
//If the RL value is different from 10K please assign your RL value with the following method:
//    MQ2.setRL(10);
    /*
 * Nextion Screen Setup and Startup
 */
 NexDSButton sdr = NexDSButton(3, 5, "sdr");  
NexButton sde = NexButton(3, 2, "sde");  
NexButton set = NexButton(1, 8, "set");  
NexButton sm = NexButton(1, 9, "sm"); 
NexButton bt = NexButton(3, 6, "bt"); 
NexButton clr = NexButton(4, 2, "clr"); 
NexButton cal = NexButton(1, 17, "cal"); 
NexDSButton p1 = NexDSButton(1, 11, "p1");  
NexDSButton p2 = NexDSButton(1, 12, "p2");  
NexDSButton p3 = NexDSButton(1, 13, "p3");  
NexDSButton p4 = NexDSButton(1, 14, "p4");  
NexDSButton p5 = NexDSButton(1, 15, "p5");  
NexDSButton p6 = NexDSButton(1, 16, "p6");  
NexCheckbox c2 = NexCheckbox(5, 6, "c2");  // Checkbox added
NexCheckbox c3 = NexCheckbox(5, 7, "c3");  // Checkbox added
NexCheckbox c4 = NexCheckbox(5, 8, "c4");  // Checkbox added
NexCheckbox c5 = NexCheckbox(5, 9, "c5");  // Checkbox added
NexCheckbox c6 = NexCheckbox(5, 10, "c6");  // Checkbox added
NexCheckbox c7 = NexCheckbox(5, 11, "c7");  // Checkbox added
NexCheckbox c8 = NexCheckbox(5, 3, "c8");  // Checkbox added
NexCheckbox c9 = NexCheckbox(5, 5, "c9");  // Checkbox added
NexCheckbox c13 = NexCheckbox(2, 4, "c13");  // Checkbox added
NexPage page0 = NexPage(0, 0, "page0");  // Page added as a touch event
NexPage page1 = NexPage(1, 0, "page1");  // Page1 added as a touch event
NexPage page2 = NexPage(2, 0, "page2");  // Page2 added as a touch event
NexPage page3 = NexPage(3, 0, "page3");  // Page3 added as a touch event
NexPage page4 = NexPage(4, 0, "page4");  // Page4 added as a touch event
NexPage page5 = NexPage(5, 0, "page5");  // Page5 added as a touch event
NexTouch *nex_listen_list[] = 
{
&sde,
&sm,
&sdr,
&clr,
&cal,
&bt,
&p1,
&p2,
&p3,
&p4,
&p5,
&p6,
&c2,
&c3,
&c4,
&c5,
&c6,
&c7,
&c8,
&c9,
&c13,
&set,
  &page0,
  &page1,
  &page2,
  &page3,
  &page4,
  &page5,
  NULL
};


//void setGain(adsGain_t gain)
//{ads.setGain(GAIN_SIXTEEN);} // 2/3x gain +/- 6.144V 1 bit = 3mV (default)
void calPushCallback(void *ptr)  // Press event for button st
{
  /*
   * Calibrate Sensors
   */
   calibratesensors();
}
void clrPushCallback(void *ptr)  // Press event for button st
{
 //clear the nextion screen monitor here
}
void btPushCallback(void *ptr)  // Press event for button st
{
}
void sdPushCallback(void *ptr)  // Press event for button st
{
  Serial.print("Initializing SD card...");
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    Serial2.print("sds.val=");
    Serial2.print(0);
    Serial2.write(0xff);
        Serial2.write(0xff);
            Serial2.write(0xff);
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");
      Serial2.print("sds.val=");
    Serial2.print(1);
    Serial2.write(0xff);
        Serial2.write(0xff);
            Serial2.write(0xff);
}
void sdePushCallback(void *ptr)  // Press event for button st
{
  String dataString = "";
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (dataFile) 
    {dataFile.println(dataString);
    Serial.println("Datalog.txt erased!");}
  else
    {Serial.println("error opening datalog.txt");}
}
void sdrPushCallback(void *ptr)  // Press event for button st
{ //record values onto SD card
  sdr.getValue(&number6);
  if (number6=1)
  {String dataString2 = "";
   int sensor2 = analogRead(0);
    dataString2 += String(sensor2);
    dataString2 += ",";
    String dataString3 = "";
   int sensor3 = analogRead(1);
    dataString3 += String(sensor3);
    dataString3 += ",";
    String dataString4 = "";
   int sensor4 = analogRead(2);
    dataString4 += String(sensor4);
    dataString4 += ",";
    String dataString5 = "";
   int sensor5 = analogRead(3);
    dataString5 += String(sensor5);
    dataString5 += ",";
    String dataString6 = "";
   int sensor6 = analogRead(6);
    dataString6 += String(sensor6);
    dataString6 += ",";
    String dataString7 = "";
   int sensor7 = analogRead(7);
    dataString7 += String(sensor7);
    dataString7+= ",";
    String dataString8 = "";
   int sensor8 = analogRead(8);
    dataString8 += String(sensor8);
    dataString8 += ",";
    String dataString9 = "";
   int sensor9 = analogRead(9);
    dataString9 += String(sensor9);
    dataString9 += ",";
    String dataString135 = "";
   int sensor135 = analogRead(10);
    dataString135 += String(sensor135);
    dataString135 += ",";
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
  if (dataFile) 
    {dataFile.println(dataString2);
    Serial.println(dataString2);
    dataFile.println(dataString3);
    Serial.println(dataString3);
    dataFile.println(dataString4);
    Serial.println(dataString4);
    dataFile.println(dataString5);
    Serial.println(dataString5);
    dataFile.println(dataString6);
    Serial.println(dataString6);
    dataFile.println(dataString7);
    Serial.println(dataString7);
    dataFile.println(dataString8);
    Serial.println(dataString8);
    dataFile.println(dataString9);
    Serial.println(dataString9);
    dataFile.println(dataString135);
    Serial.println(dataString135);}}
  else
    {Serial.println("error opening datalog.txt");}
  if (number6=0)
  { File dataFile = SD.open("datalog.txt", FILE_WRITE);   
    dataFile.close();}
}
void smPushCallback(void *ptr)  // Press event for button st
{
serialprint();
}
void p1PushCallback(void *ptr)  //Program One goes here.
{
  p1.getValue(&number1);
  if (number1=1)
  {
  program1();
  p1.getValue(&number1);
  nexLoop(nex_listen_list);}
  if (number1=0)
  {//what to do or clean up when program done
  }
}
void p2PushCallback(void *ptr)  //Program Two goes here.
{
  p2.getValue(&number1);
  if (number2=1)
  {
    program2();
    p2.getValue(&number1);
    nexLoop(nex_listen_list);}
  if (number2=0)
  {//what to do or clean up when program done
  }
}
void p3PushCallback(void *ptr)  //Program Three goes here.
{
  p3.getValue(&number3);
  if (number3=1)
  {
    program3();
    p3.getValue(&number3);
    nexLoop(nex_listen_list);}
  if (number3=0)
  {//what to do or clean up when program done
  }
}
void p4PushCallback(void *ptr)  //Program Four goes here.
{
  p4.getValue(&number4);
  if (number4=1)
  {
    program4();
    p4.getValue(&number4);
    nexLoop(nex_listen_list);}
  if (number4=0)
  {//what to do or clean up when program done
  }
}
void p5PushCallback(void *ptr)  //Program Five goes here.
{
  p5.getValue(&number5);
  if (number5=1)
  {
    program5();
    p5.getValue(&number5);
    nexLoop(nex_listen_list);}
  if (number5=0)
  {//what to do or clean up when program done
  }
}
void p6PushCallback(void *ptr)  //Program Six goes here.
{
  p6.getValue(&number6);
  if (number6=1)
  {
    program6();
    nexLoop(nex_listen_list);}
  if (number6=0)
  {//what to do or clean up when program done
  }
}
void page0PushCallback(void *ptr)  // If page 0 is loaded on the display, the following is going to execute:
{
  CurrentPage = 0;  // Set variable as 0 so from now on arduino knows page 0 is loaded on the display
}  // End of press event
void page1PushCallback(void *ptr)  // If page 1 is loaded on the display, the following is going to execute:
{
  CurrentPage = 1;  // Set variable as 1 so from now on arduino knows page 0 is loaded on the display
}  // End of press event
void page2PushCallback(void *ptr)  // If page 2 is loaded on the display, the following is going to execute:
{
  CurrentPage = 2;  // Set variable as 0 so from now on arduino knows page 0 is loaded on the display
}  // End of press event
void page3PushCallback(void *ptr)  // If page 3 is loaded on the display, the following is going to execute:
{
  CurrentPage = 3;  // Set variable as 0 so from now on arduino knows page 0 is loaded on the display
}  // End of press event
void page4PushCallback(void *ptr)  // If page 4 is loaded on the display, the following is going to execute:
{
  CurrentPage = 4;  // Set variable as 0 so from now on arduino knows page 0 is loaded on the display
}  // End of press event
void page5PushCallback(void *ptr)  // If page 5 is loaded on the display, the following is going to execute:
{
  CurrentPage = 5;  // Set variable as 0 so from now on arduino knows page 0 is loaded on the display
}  // End of press event


void setup() {
  Serial.begin(115200);
  Serial2.begin(115200);
  Serial3.begin(115200);
  Wire.begin();
    if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running, let's set the time!");}
   rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    {readings[thisReading] = 0;}
    for (int thisReading2 = 0; thisReading2 < numReadings2; thisReading2++)
    {readings2[thisReading2] = 0;}
    for (int thisReading3 = 0; thisReading3 < numReadings3; thisReading3++)
    {readings3[thisReading3] = 0;}
    for (int thisReading4 = 0; thisReading4 < numReadings4; thisReading4++)
    {readings4[thisReading4] = 0;}
    cal.attachPush(calPushCallback, &cal);
    clr.attachPush(clrPushCallback, &clr);
    sde.attachPush(smPushCallback, &sm);
    sdr.attachPush(smPushCallback, &sm);
    sm.attachPush(smPushCallback, &sm);
    set.attachPush(sdPushCallback, &set);
    p1.attachPush(p1PushCallback, &p1);
    p2.attachPush(p2PushCallback, &p2);
    p3.attachPush(p3PushCallback, &p3);
    p4.attachPush(p4PushCallback, &p4);
    p5.attachPush(p5PushCallback, &p5);
    p6.attachPush(p6PushCallback, &p6);
    //int16_t adc0, adc1, adc2, adc3;
  //adc0 = ads.readADC_SingleEnded(0);
  //adc1 = ads.readADC_SingleEnded(1);
  //adc2 = ads.readADC_SingleEnded(2);
  //adc3 = ads.readADC_SingleEnded(3);
  pinMode(flashp, INPUT_PULLUP);
  pinMode(53, OUTPUT);
  pinMode(16, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  if (analogRead(flashp) == HIGH)
  {flash = 1;}
  else
  {flash = 0;}
      Serial2.print("sds.val=");
    Serial2.print(0);
    Serial2.write(0xff);
        Serial2.write(0xff);
            Serial2.write(0xff);
  dht.begin(); 
}

void loop() 
{ nexLoop(nex_listen_list);
  delay(50); 
  if (Serial3.available()) 
      {Serial.write(Serial3.read());}   // read it and send it out Serial1 (pins 0 & 1)
  float temp = dht.readTemperature(true);
    float humid = dht.readHumidity();
      /*ADC Smoothing*/
  total = total - readings[readIndex];
  readings[readIndex] = temp;
  total = total + readings[readIndex];
  readIndex = readIndex + 1;
  if (readIndex >= numReadings) {
    readIndex = 0;}
  average = total / numReadings; 
    total2 = total2 - readings2[readIndex2];
  readings2[readIndex2] = humid;
  total2 = total2 + readings2[readIndex2];
  readIndex2 = readIndex2 + 1;
  if (readIndex2 >= numReadings2) {
    readIndex2 = 0;}
  average2 = total2 / numReadings2; 
    total3 = total3 - readings3[readIndex3];
  readings3[readIndex3] = adc2;
  total3 = total3 + readings3[readIndex3];
  readIndex3 = readIndex3 + 1;
  if (readIndex3 >= numReadings3) {
    readIndex3 = 0;}
  average3 = total3 / numReadings3; 
    total4 = total4 - readings4[readIndex4];
  readings4[readIndex4] = adc3;
  total4 = total4 + readings4[readIndex4];
  readIndex4 = readIndex4 + 1;
  if (readIndex4 >= numReadings4) {
    readIndex4 = 0;}
  average4 = total4 / numReadings4;
    MQ2.update(); // Update data, the arduino will be read the voltage on the analog pin
  MQ2.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup}
  MQ3.update(); // Update data, the arduino will be read the voltage on the analog pin
  MQ3.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
   MQ4.update(); // Update data, the arduino will be read the voltage on the analog pin
  MQ4.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
    MQ5.update(); // Update data, the arduino will be read the voltage on the analog pin
  MQ5.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
   MQ6.update(); // Update data, the arduino will be read the voltage on the analog pin
  MQ6.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
    MQ7.update(); // Update data, the arduino will be read the voltage on the analog pin
  MQ7.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
    MQ8.update(); // Update data, the arduino will be read the voltage on the analog pin
  MQ8.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
    MQ9.update(); // Update data, the arduino will be read the voltage on the analog pin
  MQ9.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
    MQ135.update(); // Update data, the arduino will be read the voltage on the analog pin
  MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
    MQ135.setA(605.18); MQ135.setB(-3.937); // Configurate the ecuation values to get CO concentration
  float CO = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
  MQ135.setA(77.255); MQ135.setB(-3.18); // Configurate the ecuation values to get Alcohol concentration
  float Alcohol = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
  MQ135.setA(110.47); MQ135.setB(-2.862); // Configurate the ecuation values to get CO2 concentration
  float CO2 = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
  MQ135.setA(44.947); MQ135.setB(-3.445); // Configurate the ecuation values to get Tolueno concentration
  float Tolueno = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
  MQ135.setA(102.2 ); MQ135.setB(-2.473); // Configurate the ecuation values to get NH4 concentration
  float NH4 = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
  MQ135.setA(34.668); MQ135.setB(-3.369); // Configurate the ecuation values to get Acetona concentration
  float Acetona = MQ135.readSensor(); // Sensor will read PPM concentration using the model and a and b values setted before or in the setup
  //nexLoop(nex_listen_list);
  if(millis()>=timenow+dread)
  {timenow+=dread;
    Serial.print("Temperature: ");
  Serial.println(average);
      Serial.print("Humidity: ");
  Serial.println(average2);
  Serial2.print("tmp.val=");
  Serial2.print(average);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);
  DateTime now = rtc.now();
  Serial2.print("yr.val=");
  Serial2.print(now.year(), DEC);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.print("mnt.val=");
  Serial2.print(now.month(), DEC);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.print("day.val=");
  Serial2.print(now.day(), DEC);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.print("hor.val=");
  Serial2.print(now.hour(), DEC);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.print("min.val=");
  Serial2.print(now.minute(), DEC);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.print("sec.val=");
  Serial2.print(now.second(), DEC);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);}
  if (digitalRead(13) == HIGH)  //If wifi has turned on built in LED to signal connection
  {Serial2.print("wif.val=");
  Serial2.print(1);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);}
  else
  {Serial2.print("wif.val=");
  Serial2.print(0);
  Serial2.write(0xff);
  Serial2.write(0xff);
  Serial2.write(0xff);}
}
void program1()
{  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
}
void program2()
{
  digitalWrite(LED_BUILTIN, brightness);
  brightness = brightness + fadeAmount;
  if (brightness <= 0 || brightness >= 255) 
  {fadeAmount = -fadeAmount;}
  delay(30);
}
void program3()
{ digitalWrite(LED_BUILTIN, LOW); }  // turn the LED on (HIGH is the voltage level)
void program4()
{}
void program5()
{
}
void program6()
{
  MQ2.serialDebug(); // Will print the table on the serial port
  Serial.println("MQ2 Table:");
  MQ3.serialDebug(); // Will print the table on the serial port
  Serial.println("MQ3 Table:");
  MQ4.serialDebug(); // Will print the table on the serial port
  Serial.println("MQ4 Table:");
  MQ5.serialDebug(); // Will print the table on the serial port
  Serial.println("MQ5 Table:");
  MQ6.serialDebug(); // Will print the table on the serial port
  Serial.println("MQ6 Table:");
  MQ7.serialDebug(); // Will print the table on the serial port
  Serial.println("MQ7 Table:");
  MQ8.serialDebug(); // Will print the table on the serial port
  Serial.println("MQ8 Table:");
  MQ9.serialDebug(); // Will print the table on the serial port
  Serial.println("MQ9 Table:");
  MQ135.serialDebug(); // Will print the table on the serial port
  Serial.println("MQ135 Table:");
  delay(500); //Sampling frequency
  }
void calibratesensors()
{
  /*Calibrate MQ2 Sensor*/
MQ2.setRegressionMethod(1); //_PPM =  a*ratio^b
  MQ2.setA(574.25); MQ2.setB(-2.222); // Configurate the ecuation values to get LPG concentration
  MQ2.init(); 
Serial.print("Calibrating MQ2 please wait.");
Serial2.print("c2.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
  float calcR2 = 0;
  for(int i = 1; i<=10; i ++)
  {
    MQ2.update(); // Update data, the arduino will be read the voltage on the analog pin
    calcR2 += MQ2.calibrate(RatioMQ2CleanAir);
    Serial.print(".");
  }
  MQ2.setR0(calcR2/10);
  Serial.println("  done!.");
Serial2.print("c2.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
  
  if(isinf(calcR2)) {Serial.println("Warning: Conection issue founded, R2 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
  if(calcR2 == 0){Serial.println("Warning: Conection issue founded, R2 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
MQ2.serialDebug(true);

/*Calibrate MQ3 Sensor*/
MQ3.setRegressionMethod(1); //_PPM =  a*ratio^b
  MQ3.setA(4.8387); MQ3.setB(-2.68); // Configurate the ecuation values to get LPG concentration
  MQ3.init(); 
Serial.print("Calibrating MQ3 please wait.");
Serial2.print("c3.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
  float calcR3 = 0;
  for(int i = 1; i<=10; i ++)
  {
    MQ3.update(); // Update data, the arduino will be read the voltage on the analog pin
    calcR3 += MQ3.calibrate(RatioMQ3CleanAir);
    Serial.print(".");
  }
  MQ3.setR0(calcR3/10);
  Serial.println("  done!.");
Serial2.print("c3.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
  
  if(isinf(calcR3)) {Serial.println("Warning: Conection issue founded, R3 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
  if(calcR3 == 0){Serial.println("Warning: Conection issue founded, R3 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
MQ3.serialDebug(true);

/*Calibrate MQ4 Sensor*/
MQ4.setRegressionMethod(1); //_PPM =  a*ratio^b
  MQ4.setA(1012.7); MQ4.setB(-2.786); // Configurate the ecuation values to get LPG concentration
  MQ4.init(); 
Serial.print("Calibrating MQ4 please wait.");
Serial2.print("c4.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
  float calcR4 = 0;
  for(int i = 1; i<=10; i ++)
  {
    MQ4.update(); // Update data, the arduino will be read the voltage on the analog pin
    calcR4 += MQ4.calibrate(RatioMQ4CleanAir);
    Serial.print(".");
  }
  MQ4.setR0(calcR4/10);
  Serial.println("  done!.");
Serial2.print("c4.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
  
  if(isinf(calcR4)) {Serial.println("Warning: Conection issue founded, R4 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
  if(calcR4 == 0){Serial.println("Warning: Conection issue founded, R4 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
MQ4.serialDebug(true);


/*Calibrate MQ5 Sensor*/
MQ5.setRegressionMethod(1); //_PPM =  a*ratio^b
  MQ5.setA(1163.8); MQ5.setB(-3.874); // Configurate the ecuation values to get LPG concentration
  MQ5.init(); 
Serial.print("Calibrating MQ5 please wait.");
Serial2.print("c5.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
  float calcR5 = 0;
  for(int i = 1; i<=10; i ++)
  {
    MQ5.update(); // Update data, the arduino will be read the voltage on the analog pin
    calcR5 += MQ5.calibrate(RatioMQ5CleanAir);
    Serial.print(".");
  }
  MQ5.setR0(calcR5/10);
  Serial.println("  done!.");
Serial2.print("c5.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
  
  if(isinf(calcR5)) {Serial.println("Warning: Conection issue founded, R5 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
  if(calcR5 == 0){Serial.println("Warning: Conection issue founded, R5 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
MQ5.serialDebug(true);


/*Calibrate MQ6 Sensor*/
MQ6.setRegressionMethod(1); //_PPM =  a*ratio^b
  MQ6.setA(2127.2); MQ6.setB(-2.526); // Configurate the ecuation values to get LPG concentration
  MQ6.init(); 
Serial.print("Calibrating MQ6 please wait.");
Serial2.print("c6.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
  float calcR6 = 0;
  for(int i = 1; i<=10; i ++)
  {
    MQ6.update(); // Update data, the arduino will be read the voltage on the analog pin
    calcR6 += MQ6.calibrate(RatioMQ6CleanAir);
    Serial.print(".");
  }
  MQ6.setR0(calcR6/10);
  Serial.println("  done!.");
Serial2.print("c6.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
  
  if(isinf(calcR6)) {Serial.println("Warning: Conection issue founded, R6 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
  if(calcR6 == 0){Serial.println("Warning: Conection issue founded, R6 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
MQ6.serialDebug(true);



/*Calibrate MQ7 Sensor*/
MQ7.setRegressionMethod(1); //_PPM =  a*ratio^b
  MQ7.setA(99.04); MQ7.setB(-1.518); // Configurate the ecuation values to get LPG concentration
  MQ7.init(); 
Serial.print("Calibrating MQ7 please wait.");
Serial2.print("c7.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
  float calcR7 = 0;
  for(int i = 1; i<=10; i ++)
  {
    MQ7.update(); // Update data, the arduino will be read the voltage on the analog pin
    calcR7 += MQ7.calibrate(RatioMQ7CleanAir);
    Serial.print(".");
  }
  MQ7.setR0(calcR7/10);
  Serial.println("  done!.");
Serial2.print("c7.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
  
  if(isinf(calcR7)) {Serial.println("Warning: Conection issue founded, R7 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
  if(calcR7 == 0){Serial.println("Warning: Conection issue founded, R7 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
MQ7.serialDebug(true);



/*Calibrate MQ8 Sensor*/
MQ8.setRegressionMethod(1); //_PPM =  a*ratio^b
  MQ8.setA(976.97); MQ8.setB(-0.688); // Configurate the ecuation values to get LPG concentration
  MQ8.init(); 
Serial.print("Calibrating MQ8 please wait.");
Serial2.print("c8.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
  float calcR8 = 0;
  for(int i = 1; i<=10; i ++)
  {
    MQ8.update(); // Update data, the arduino will be read the voltage on the analog pin
    calcR8 += MQ8.calibrate(RatioMQ8CleanAir);
    Serial.print(".");
  }
  MQ8.setR0(calcR8/10);
  Serial.println("  done!.");
Serial2.print("c8.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
  
  if(isinf(calcR8)) {Serial.println("Warning: Conection issue founded, R8 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
  if(calcR8 == 0){Serial.println("Warning: Conection issue founded, R8 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
MQ8.serialDebug(true);



/*Calibrate MQ9 Sensor*/
MQ9.setRegressionMethod(1); //_PPM =  a*ratio^b
  MQ9.setA(1000.5); MQ9.setB(-2.186); // Configurate the ecuation values to get LPG concentration
  MQ9.init(); 
Serial.print("Calibrating MQ8 please wait.");
Serial2.print("c9.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
  float calcR9 = 0;
  for(int i = 1; i<=10; i ++)
  {
    MQ9.update(); // Update data, the arduino will be read the voltage on the analog pin
    calcR9 += MQ9.calibrate(RatioMQ9CleanAir);
    Serial.print(".");
  }
  MQ9.setR0(calcR9/10);
  Serial.println("  done!.");
Serial2.print("c9.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
  
  if(isinf(calcR9)) {Serial.println("Warning: Conection issue founded, R9 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
  if(calcR9 == 0){Serial.println("Warning: Conection issue founded, R9 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
MQ9.serialDebug(true);



/*Calibrate MQ135 Sensor*/
MQ135.setRegressionMethod(1); //_PPM =  a*ratio^b
  MQ135.init(); 
Serial.print("Calibrating MQ135 please wait.");
Serial2.print("c13.val=");
Serial2.print(0);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
  float calcR135 = 0;
  for(int i = 1; i<=10; i ++)
  {
    MQ135.update(); // Update data, the arduino will be read the voltage on the analog pin
    calcR135 += MQ135.calibrate(RatioMQ135CleanAir);
    Serial.print(".");
  }
  MQ135.setR0(calcR135/10);
  Serial.println("  done!.");
Serial2.print("c13.val=");
Serial2.print(1);
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);
  
  if(isinf(calcR135)) {Serial.println("Warning: Conection issue founded, R135 is infite (Open circuit detected) please check your wiring and supply"); while(1);}
  if(calcR135 == 0){Serial.println("Warning: Conection issue founded, R135 is zero (Analog pin with short circuit to ground) please check your wiring and supply"); while(1);}
MQ135.serialDebug(true);
}
void serialprint()
{      Serial2.print("sp1.txt=");
  Serial2.print("Temperature:");
  Serial2.write(0xff);
    Serial2.write(0xff);
     Serial2.write(0xff);
     delay(500);
       Serial2.print("sp1.txt+=");
       Serial2.print(average);
  Serial2.write(0xff);
    Serial2.write(0xff);
     Serial2.write(0xff);
     delay(500);
      Serial2.print("sp1.txt+=");
        Serial2.print("Humidity:");
  Serial2.write(0xff);
    Serial2.write(0xff);
     Serial2.write(0xff);
     delay(500);
       Serial2.print("sp1.txt+=");
       Serial2.print(average2);
  Serial2.write(0xff);
    Serial2.write(0xff);
     Serial2.write(0xff);
     delay(500);
}
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

#ifndef STASSID
#define STASSID "your wifi ssid"
#define STAPSK  "your wifi password"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;
const char* host = "OTANAOS";

void setup() {
  Serial.begin(115200);
  pinMode(13, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
  /* switch on led */
  digitalWrite(13, LOW);

  Serial.println("Booting");
  WiFi.mode(WIFI_STA);

  WiFi.begin(ssid, password);

  while (WiFi.waitForConnectResult() != WL_CONNECTED) 
  {WiFi.begin(ssid, password);
    Serial.println("Retrying connection...");}

  ArduinoOTA.setHostname(host);
  ArduinoOTA.onStart([]() 
  {digitalWrite(13, HIGH);
  delay(100);
  digitalWrite(13, LOW);
  delay(100);
  digitalWrite(13, HIGH);
  delay(100);
  digitalWrite(13, LOW);
  delay(100);
  digitalWrite(13, HIGH);
  delay(100);
  digitalWrite(13, LOW);   // Turn the LED on (Note that LOW is the voltage level
  // but actually the LED is on; this is because
  // it is active low on the ESP-01)
  delay(500);                      // Wait for a second
  digitalWrite(16, HIGH);  // Turn the LED off by making the voltage HIGH
  delay(500); 
  });

  ArduinoOTA.onEnd([]() 
  {  digitalWrite(13, LOW);   // Turn the LED on (Note that LOW is the voltage level
  // but actually the LED is on; this is because
  // it is active low on the ESP-01)
  delay(500);                      // Wait for a second
  digitalWrite(13, HIGH);  // Turn the LED off by making the voltage HIGH
  delay(500); });

  ArduinoOTA.onError([](ota_error_t error) 
  {(void)error;
    ESP.restart();});

  /* setup the OTA server */
  ArduinoOTA.begin();
  Serial.println("Ready");
  digitalWrite(13,LOW);
  }

void loop() {
  ArduinoOTA.handle();
  digitalWrite(13, LOW);
}

@Boffin Appreciate the advice.

Here is better OTA 8266 with NTP clock pull and write to default Serial on 8266. Meant to be read by ArduinoMega2650 on a open Serial line, and passed through to the Nextion screen.

#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <time.h>
unsigned long timenow = millis();
int dread = 1000;
#ifdef CREDENTIALS
const char* ssid = mySSID;
const char* password = myPASSWORD;
#else
const char* ssid = "Your SSID";
const char* password = "Your SSID Password";
const char* host = "OTANAOS"; //change this to whatever you want to see when flashing 8266 from arduino ide
#endif
const char* NTP_SERVER = "ch.pool.ntp.org";
const char* TZ_INFO    = "MST7MDT,M3.2.0,M11.1.0";  // enter your time zone (https://remotemonitoringsystems.ca/time-zone-abbreviations.php)
tm timeinfo;
time_t now;
long unsigned lastNTPtime;
unsigned long lastEntryTime;

bool getNTPtime(int sec) 
{{uint32_t start = millis();
    do
      {time(&now);
      localtime_r(&now, &timeinfo);
      Serial.print(".");
      delay(10);}
     while (((millis() - start) <= (1000 * sec)) && (timeinfo.tm_year < (2016 - 1900)));
    if (timeinfo.tm_year <= (2016 - 1900)) return false;  // the NTP call was not successful
    Serial.print("now ");  Serial.println(now);
    char time_output[30];
    strftime(time_output, 30, "%a  %d-%m-%y %T", localtime(&now));
    Serial.println(time_output);
    Serial.println();}
  return true;
}

void showTime(tm localTime) 
{
  Serial.print("day.val=");
  Serial.print(localTime.tm_mday);
  Serial.write(0xff);
    Serial.write(0xff);
      Serial.write(0xff);
      Serial.print("mn.val=");
  Serial.print(localTime.tm_mon + 1);
    Serial.write(0xff);
      Serial.write(0xff);
        Serial.write(0xff);
        Serial.print("yr.val=");
  Serial.print(localTime.tm_year - 100);
    Serial.write(0xff);
      Serial.write(0xff);
        Serial.write(0xff);
        Serial.print("hor.val=");
  Serial.print(localTime.tm_hour);
    Serial.write(0xff);
      Serial.write(0xff);
        Serial.write(0xff);
        Serial.print("mnt.val=");
  Serial.print(localTime.tm_min);
    Serial.write(0xff);
      Serial.write(0xff);
        Serial.write(0xff);
        //Serial.print("sec.val=");
  //Serial.print(localTime.tm_sec);
}

void setup() {
  Serial.begin(115200);
  while(!Serial){}
  Serial.print("wif.val=");
  Serial.print(0);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  WiFi.mode(WIFI_STA);
  Serial.println("\n\nNTP Time Test\n");
  WiFi.begin(ssid, password);
  int counter = 0;
  while (WiFi.status() != WL_CONNECTED) 
    {delay(200);
    if (++counter > 100) ESP.restart();
    Serial.print ( "." );}
  Serial.println("\n\nWiFi connected\n\n");
  Serial.print("wif.val=");
  Serial.print(1);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
    ArduinoOTA.setHostname(host);
  ArduinoOTA.onStart([]() 
    {digitalWrite(13, LOW); 
  delay(500); 
  digitalWrite(13, HIGH);
  delay(500); });
  ArduinoOTA.onEnd([]() 
  {digitalWrite(13, LOW);
  delay(500);
  digitalWrite(13, HIGH);
  delay(500); });
  ArduinoOTA.onError([](ota_error_t error) 
  {(void)error;
  ESP.restart();});
  ArduinoOTA.begin();
  Serial.println("Ready");
  digitalWrite(13,LOW);
  configTime(0, 0, NTP_SERVER);
  // See https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv for Timezone codes for your region
  setenv("TZ", TZ_INFO, 1);
  if (getNTPtime(10))  // wait up to 10sec to sync
  {} 
  else 
  {Serial.println("Time not set");
    ESP.restart();}
  showTime(timeinfo);
  lastNTPtime = time(&now);
  lastEntryTime = millis();
}

void loop() 
{
  ArduinoOTA.handle();
  if(millis()>=timenow+dread)
  {timenow+=dread;
  getNTPtime(10);
  showTime(timeinfo);}
}

Getting ready for Blynk integration. At this stage Ill be pushing any current updates for ESP8266/ESP32 through that application wirelessly and on a schedule. Any who want in please send me a message. Ill add you to the update list. I cannot guarantee full maintain and constant updates at the moment, but they will be available sooner than later.

This build may take it's leave from using the Arduino Mega 2560 because I am trying to keep everything simple, cheap, and easily updatable. THe 8266 has enough going on for it to use as a replacement Mega. In the case of not having enough pins for my particular use, I will be using a multiplexer for incoming sensors for a few I2C devices. You will start to see code including such in later builds.

Anyone interested in using the same setup as myself and powering 9 MQ gas sensors and more, you will need some power to back it up. USB will not cut it nor will the standard 9v power supply sold most everywhere. Therefore I suggest powering the sensors through an external power source, possibly all turned on and off through a relay.

Code explanation, Blynk integration will come when they are ready to present.

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