0% found this document useful (0 votes)
15 views

Code 2

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Code 2

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

#include <WiFi.

h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
//----------------------------------------------------------------------
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);

#define NTP_SERVER "pool.ntp.org"


#define UTC_OFFSET 0
#define UTC_OFFSET_DST 0
//---------------DHT22--------------------
#define DHTPIN 14
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
float Humidity;
float Temperature;
//--------- Flag structure --------------------------------------
typedef struct _vFlag
{
uint8_t BTFlag = 0;
uint8_t DC_Flag = 0;
uint8_t CANFlag = 0;
uint8_t I2C_Flag = 0;
uint8_t RFIDWrite = 0;
uint8_t RFIDRead = 0;
uint8_t dht22 = 0;
uint8_t sensor1_Flag = 0;
uint8_t initial_Flag = 0;
uint8_t FunctionFlag = 0;
} vFlag;
vFlag *flag_Ptr;
vFlag flag;
//--------- uart structure --------------------------------------
//----------uart--------------
#define LINE_BUFFER_LENGTH 64
typedef struct _vUart
{
char c;
int lineIndex = 0;
int line1Index = 0;
int BTlineIndex = 0;
bool lineIsComment;
bool lineSemiColon;
char line[128];
char BTline[20];
String inputString;
String BTinputString;
String S1inputString;
int V[16];
char ctemp[30];
char I2C_Data[80];
int DC_Spped = 50;
float Voltage[16];
int Buffer[128];
int StartCnt = 0;
int ReadCnt = 0;
int sensorValue = 0;
} vUart;
vUart *Uart_Ptr;
vUart Uart;

//---------------------------------------------------------------------------------
#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
//----------------------------------------------------------------
TaskHandle_t hled;
TaskHandle_t huart;
//------------------------------------------------------------------------------
void initial()
{
Serial.println(F("Create Task"));
//----------------------------------------------------------------------
xTaskCreatePinnedToCore(
vUARTTask, "UARTTask" // A name just for humans
,
1024 // This stack size can be checked & adjusted by reading the Stack
Highwater
,
NULL, 3 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0
being the lowest.
,
&huart //handle
,
0);

//--------------- create task----------------------------------


xTaskCreatePinnedToCore(
vLEDTask, "LEDTask" // A name just for humans
,
1024 // This stack size can be checked & adjusted by reading the Stack
Highwater
,
NULL, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0
being the lowest.
,
&hled //handle
,
0);
//----------------------------------------------------------------------
}

void setup()
{
Serial.begin(9600);
Serial.println(F("init"));
initial();
pinMode(LED_BUILTIN, OUTPUT);
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");

WiFi.begin("Wokwi-GUEST", "", 6);


while (WiFi.status() != WL_CONNECTED) {
delay(250);
spinner();
}

Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Online");
LCD.setCursor(0, 1);
LCD.println("Updating time...");
configTime(8*3600, 0, "pool.ntp.org","time.nist.gov"); // enable NTP for Taipei
time
//configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);

dht.begin();
}

void loop()
{
//

Serial.print(F("Main at core:"));
Serial.println(xPortGetCoreID());
while(1)
{
if(flag.dht22==0)
{
for(int i=0;i<40;i++)
{
printLocalTime();
delay(200);
}
LCD.clear();
flag.dht22=1;
}

if(flag.dht22==1)
{
float h = dht.readHumidity();

float t = dht.readTemperature();

float f = dht.readTemperature(true);

if (isnan(h) || isnan(t) || isnan(f))


{
Serial.println("Failed to read from DHT sensor!");
return;
}

float hif = dht.computeHeatIndex(f, h);

float hic = dht.computeHeatIndex(t, h, false);

Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" % ");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F ");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");

String Line="Temp: "+String(t);


String Line1="Humi: "+String(h);
for(int i=16;i>=0;i--)
{
LCD.setCursor(i,0);
LCD.print(Line);
LCD.print(" ");
LCD.setCursor(i,1);
LCD.print(Line1);
LCD.print(" ");
delay(200);
}
delay(200);
LCD.clear();
flag.dht22=0;
}
vTaskDelay(5);
}
}
//-------------------------------------------
void vUARTTask(void *pvParameters)
{
(void)pvParameters;

Serial.print(F("UARTTask at core:"));
Serial.println(xPortGetCoreID());
vTaskDelay(100);
for (;;)
{
while (Serial.available() > 0)
{
Uart.c = Serial.read();

if ((Uart.c == '\n') || (Uart.c == '\r'))


{ // End of line reached
if (Uart.lineIndex > 0)
{ // Line is complete. Then execute!
Uart.line[Uart.lineIndex] = '\0'; // Terminate string
//Serial.println( F("Debug") );
//Serial.println( Uart.inputString );
processCommand(Uart.line); // do something with the command
Uart.lineIndex = 0;
Uart.inputString = "";
}
else
{
// Empty or comment line. Skip block.
}
Uart.lineIsComment = false;
Uart.lineSemiColon = false;
Serial.println(F("ok>"));
}
else
{
//Serial.println( c );
if ((Uart.lineIsComment) || (Uart.lineSemiColon))
{
if (Uart.c == ')')
Uart.lineIsComment = false; // End of comment. Resume line.
}
else
{
if (Uart.c == '/')
{ // Block delete not supported. Ignore character.
}
else if (Uart.c == '~')
{ // Enable comments flag and ignore all characters until ')' or EOL.
Uart.lineIsComment = true;
}
else if (Uart.c == ';')
{
Uart.lineSemiColon = true;
}
else if (Uart.lineIndex >= LINE_BUFFER_LENGTH - 1)
{
Serial.println("ERROR - lineBuffer overflow");
Uart.lineIsComment = false;
Uart.lineSemiColon = false;
}
else if (Uart.c >= 'a' && Uart.c <= 'z')
{ // Upcase lowercase
Uart.line[Uart.lineIndex] = Uart.c - 'a' + 'A';
Uart.lineIndex = Uart.lineIndex + 1;
Uart.inputString += (char)(Uart.c - 'a' + 'A');
}
else
{
Uart.line[Uart.lineIndex] = Uart.c;
Uart.lineIndex = Uart.lineIndex + 1;
Uart.inputString += Uart.c;
}
}
}
} //while (Serial.available() > 0)
vTaskDelay(5);
}
}
//-------------------------------------------------------------------------
static void vLEDTask(void *pvParameters)
{
(void)pvParameters;

Serial.println(F("LEDTask at core:"));
Serial.println(xPortGetCoreID());
pinMode(LED_BUILTIN, OUTPUT);
for (;;) // A Task shall never return or exit.
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
vTaskDelay(200);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
vTaskDelay(200);
}
}
//----------------------------------------
void processCommand(char *data)
{
int len, xlen, ylen, zlen, alen;
int tempDIO;
String stemp;

len = Uart.inputString.length();
//---------------------------------------
if (strstr(data, "VER") != NULL)
{
Serial.println(F("ESP32_20230811"));
}
//-------------- RFID --------------------
if (strstr(data, "DHT22_ON") != NULL)
{
flag.dht22 = 1;
Serial.println(F("DHT22_ON"));
}
if (strstr(data, "DHT22_OFF") != NULL)
{
flag.dht22 = 0;
Serial.println(F("DHT22_OFF"));
}

}
//-----------------------------------------
void printLocalTime()
{
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
LCD.setCursor(0, 1);
LCD.println("Connection Err");
return;
}
//LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Online");

LCD.setCursor(8, 0);
LCD.println(&timeinfo, "%H:%M:%S");

LCD.setCursor(0, 1);
LCD.println(&timeinfo, "%d/%m/%Y %Z");
}

//----------------------------------------------
void spinner()
{
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
LCD.setCursor(15, 1);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
//-----------------------------------------

You might also like