STONE HMI ESP32 Line Count and Alarm

Hayden
Posts: 16
Joined: Thu Jul 01, 2021 8:10 am

STONE HMI ESP32 Line Count and Alarm

Postby Hayden » Tue Oct 12, 2021 12:52 am

Project Overview

Here we do the production line counting application, after boot, stone TFT The LCD serial port screen will have a start interface. After a short stay, it will jump to the specified page 1 and display the time setting. At this time, it is necessary to set the current time. Another option is to have a record of the number of fires, which will be added one each time, which can be used as the basis for fire control on the production line. After the time setting is completed, it will be displayed on the next interface, and click OK Enter the set production target as the assessment basis. After setting, click OK to enter the counting interface. Here, the data uploaded to esp32 through the sensor, and then transmitted to the stone TFT LCD serial port screen through esp32. One will be added each time. The current output rate will be calculated according to the target quantity, and the data will be updated every time an object is detected.

The communication functions are as follows:

① The serial port screen of stone TFT LCD realizes the function of button switching interface;
② The serial port screen of stone TFT LCD realizes the function of automatic jump when starting up;
③ The serial port screen of stone TFT LCD realizes time setting;
④ The serial port screen of stone TFT LCD realizes data variable distribution;
⑤ Stone TFT LCD serial port screen realizes serial command communication.

Modules required for the project:

STONE TFT LCD
Arduino ESP32
③ Infrared detection module;
④ Smoke detection module.
图片1.png
图片1.png (340.54 KiB) Viewed 14527 times
Connection
图片2.png
图片2.png (1.52 MiB) Viewed 14527 times
图片3.png
图片3.png (1.58 MiB) Viewed 14527 times

Hayden
Posts: 16
Joined: Thu Jul 01, 2021 8:10 am

Re: STONE HMI ESP32 Line Count and Alarm

Postby Hayden » Tue Oct 12, 2021 12:53 am

Code

Code: Select all

/*
  Button

  Turns on and off a light emitting diode(LED) connected to digital pin 13,
  when pressing a pushbutton attached to pin 2.

  The circuit:
  - LED attached from pin 13 to ground
  - pushbutton attached to pin 2 from +5V
  - 10K resistor attached to pin 2 from ground

  - Note: on most Arduinos there is already an LED on the board
    attached to pin 13.

  created 2005
  by DojoDave <http://www.0j0.org>
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Button
*/
//#include <HardwareSerial.h>
#include "stdlib.h"
#include <math.h>

uint8_t i = 0;
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 200;           // interval at which to blink (milliseconds)
// constants won't change. They're used here to set pin numbers:
const int IRPin = 13;     // the number of the pushbutton pin
int IRNewState = 0;         // variable for reading the pushbutton status
int IROldState = 0;         // variable for reading the pushbutton status
unsigned short IRCount = 0;
unsigned short Targetvalue = 100;//Important
unsigned short CompleteRate = 0;

uint8_t RecievedTemp[9] = {0};//A5 5A 06 83 00 28 01 00 0C //A5 5A 06 83 00 28 01 FF FA 
uint8_t CurrentOutput[8] = {0xA5, 0x5A, 0x05, 0x82, 0x00, 0x34, 0x00, 0x00};
uint8_t CurrentRate[8] = {0xA5, 0x5A, 0x05, 0x82, 0x00, 0x38, 0x00, 0x00};

const int MQ2Pin = 4;     // the number of the pushbutton pin
int MQ2NewState = 0;         // variable for reading the pushbutton status
int MQ2OldState = 0;         // variable for reading the pushbutton status
unsigned short MQ2Count = 0;

uint8_t FireTimes[8] = {0xA5, 0x5A, 0x05, 0x82, 0x00, 0x3D, 0x00, 0x00};

void setup() 
{
  Serial.begin(115200);
//  Serial.print("Project start\r\n");
        
//  Serial2.begin(115200);
//  Serial2.print("Serial2.begin");
  
  // initialize the pushbutton pin as an input:
  pinMode(IRPin, INPUT);//IR
  pinMode(MQ2Pin, INPUT_PULLUP);//MQ2
  
  //digitalWrite(IRPin,LOW);
  //srand(0);
}

void loop() 
{
  // read the state of the pushbutton value:
  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) 
  {
    previousMillis = currentMillis;
    
    IRNewState = digitalRead(IRPin);
    if(IRNewState != IROldState)
    {
      //delay(50);
      IROldState = IRNewState;
      // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
      if (IRNewState == HIGH) 
      {
//        Serial.print("Quantity+1\r\n");
        IRCount ++;
        CurrentOutput[6] = IRCount >> 8;
        CurrentOutput[7] = IRCount & 0xFF;
        Serial.write(CurrentOutput, 8);

        delay(2);
        CompleteRate = (IRCount *100) / Targetvalue;
        CurrentRate[6] = CompleteRate >> 8;
        CurrentRate[7] = CompleteRate & 0xFF;
        Serial.write(CurrentRate, 8);   
        
//        Serial.print("\r\n");   
      } 
      else 
      {
//        Serial.print("Not increase\r\n");
      }
    }

    // read the state of the pushbutton value:
    MQ2NewState = digitalRead(MQ2Pin);
    if(MQ2NewState != MQ2OldState)
    {
      //delay(50);
      MQ2OldState = MQ2NewState;
      // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
      if (MQ2NewState == LOW) 
      {
//        Serial.print("Smoke Alarm+1\r\n");
        MQ2Count ++;
        FireTimes[6] = MQ2Count >> 8;
        FireTimes[7] = MQ2Count & 0xFF;
        Serial.write(FireTimes, 8);

//        Serial.print("\r\n"); 
      } 
      else 
      {
//        Serial.print("Normal\r\n");
      }
    }
  }
  
  //Serial2
  if(Serial.available() != 0)
  {
    for(i = 0; i < 9; i ++)
    {
        RecievedTemp[i] = Serial.read();
    }

    //merge
    Targetvalue = 0;
    Targetvalue = Targetvalue | RecievedTemp[7];
    Targetvalue = Targetvalue << 8;
    Targetvalue = Targetvalue | RecievedTemp[8];

    if(Targetvalue == 0)
    {
      Targetvalue = 100;
    }
//    Serial.write(&Targetvalue, 1);
//    Serial.print(Targetvalue);
  }
}
Resulting effect
图片4.png
图片4.png (1.59 MiB) Viewed 14526 times

Who is online

Users browsing this forum: No registered users and 8 guests