#include "xil_cache.h" #include "xparameters.h" #include "stdio.h" #include "xparameters.h" #include "xuartps.h" #include "xtime_l.h" #include "xgpiops.h" #include "sleep.h" #include "xil_cache.h" #include "xil_io.h" #include "xil_types.h" extern "C"{ #include "PmodALS.h" #include "PmodHYGRO.h" #include "PmodAQS.h" #include "PmodESP32.h" #include "PmodAD1.h" } /******************************************************************************/ /* */ /* Defines */ /* */ /******************************************************************************/ #define HOST_UART_DEVICE_ID XPAR_PS7_UART_1_DEVICE_ID #define HostUart XUartPs #define HostUart_Config XUartPs_Config #define HostUart_CfgInitialize XUartPs_CfgInitialize #define HostUart_LookupConfig XUartPs_LookupConfig #define HostUart_Recv XUartPs_Recv #define HostUartConfig_GetBaseAddr(CfgPtr) (CfgPtr->BaseAddress) #define PMODESP32_UART_BASEADDR XPAR_PMODESP32_0_AXI_LITE_UART_BASEADDR #define PMODESP32_GPIO_BASEADDR XPAR_PMODESP32_0_AXI_LITE_GPIO_BASEADDR #define COUNTS_PER_SECOND (XPAR_CPU_CORTEXA9_CORE_CLOCK_FREQ_HZ /2) #define TIMER_FREQ_HZ 100000000 #define MAX_WIDTH 320 #define MAX_HEIGHT 240 #define MAX_BUTTON 16 #define INTC_DEVICE_ID XPAR_PS7_SCUGIC_0_DEVICE_ID #define TMR_DEVICE_ID XPAR_TMRCTR_0_DEVICE_ID #define LEDS_DEVICE_ID XPAR_AXI_GPIO_0_DEVICE_ID #define INTC_TMR_INTERRUPT_ID XPAR_FABRIC_AXI_TIMER_0_INTERRUPT_INTR #define TMR_LOAD 0xF8000000 /******************************************************************************/ /* */ /* Function Declarations */ /* */ /******************************************************************************/ void EnableCaches(); void DisableCaches(); void startup(); void receiveData(XTime time); void setWifiMode(void); void connectWifi(void); void establishConnection(void); void cipsend(float temp, u16 co2, u8 light, float humidity, float ec,float ph); /******************************************************************************/ /* */ /* Variables */ /* */ /******************************************************************************/ PmodALS ALS; PmodHYGRO HYGRO; PmodAQS AQS; PmodESP32 ESP32; HostUart myHostUart; XGpioPs_Config *ConfigPtr; XGpioPs output; XGpioPs lighttest; XTime TimeStart; XTime TimeStart2; XTime TimeEnd; XTime TimeEndWP; XTime TimeEndWP2; PmodAD1 myDevice; const float ReferenceVoltage = 3.3; float ec,ph,ecV,phV; static int wptmrsw; u16 eCO2; u16 TVOC; u8 buf[5]; u8 light =0; float temp_degc =0; float hum_perrh=0; int countdown =60; char counter[20]; char lightval[20]; char tempval[100]; char humval[100]; char co2val[20]; char tvocval[20]; char phval[20]; char ecval[20]; int main() { EnableCaches(); startup(); setWifiMode(); xil_printf("Init Started\n\r"); connectWifi(); xil_printf("Wifi Done\n\r"); XTime_GetTime(&TimeStart); TimeEnd = TimeStart + ((XTime)COUNTS_PER_SECOND); TimeEndWP=TimeStart + 100*((XTime)COUNTS_PER_SECOND); //12 Hrs= 60*60*12=43200 //24 Hrs= 3600*24=86400 //TimeEndWP=TimeStart + 43200*((XTime)COUNTS_PER_SECOND); WP opens for 12Hrs while (true){ // Event 1: Operating the Water Pump for 100s(Testing)/12Hrs XTime_GetTime(&TimeStart); if(TimeStartTimeEndWP){ wptmrsw=1; wptmrsw=~wptmrsw; XGpioPs_WritePin(&output, 11 ,wptmrsw); xil_printf(" Status: wptmrsw%d"); //XGpio_DiscreteWrite(&lighttest,1,wptmrsw); XTime_GetTime(&TimeStart); TimeEndWP=TimeStart + 100*((XTime)COUNTS_PER_SECOND); }*/ /*Above Code is For Trying multitasking of event 2 and event 1 (water pump turn on for 12hrs and off for 12 hrs)*/ /* Event 2: Sensor Measuring Values, Upload To Thingspeak, , Light Control, Auto Dosing*/ if(TimeStart > TimeEnd){ if(countdown == 0){ countdown = 60; temp_degc = HYGRO_getTemperature(&HYGRO); hum_perrh = HYGRO_getHumidity(&HYGRO); light = ALS_read(&ALS); AQS_GetData(&AQS, buf); eCO2 = ((u16)buf[0] << 8) | ((u16)buf[1]); TVOC = ((u16)buf[2] << 8) | ((u16)buf[3]); // Light Control if(light>100){ //pin 13 is top right side XGpioPs_WritePin(&output, 13, 1); //Off when light too high } else if(light<10){ XGpioPs_WritePin(&output,13,1); // Shuts off when lower than 10 ie Night time } else{ XGpioPs_WritePin(&output, 13 ,0); // Turn on between 10 - 100 } AD1_RawData RawData; AD1_PhysicalData PhysicalData; AD1_GetSample(&myDevice, &RawData); // Capture raw samples // printf("Sample 1: %.02f; ", RawData[0]); // printf("Sample 2: %.02f\r\n ", RawData[1]); // Convert raw samples into floats scaled to 0 - VDD AD1_RawToPhysical(ReferenceVoltage, RawData, &PhysicalData); phV=PhysicalData[1]/0.61; ecV=PhysicalData[0]/0.61; // PhysicalData[1] ph Sensor voltage across 6.8M resistor // PhysicalData[0] ec Sensor voltage across 6.8M resistor // Change the Physical Data value to the multimeter voltage/induced voltage // the 0.61 may change according to the Voltage divider ratio. ph=(phV/3)*(-14)+14; ec=(ecV/3.4)*20; // EC auto dosing if(ec<1.2){ XGpioPs_WritePin(&output, 10, 0); //When EC is lower than 1.2, the valve gives water nutrients } else{ XGpioPs_WritePin(&output, 10 ,1); //sleep(30); // When EC reached 1.2, the valve stop suppling nutrients to water } xil_printf("Sending....\n\r"); establishConnection(); cipsend(temp_degc, eCO2, light, hum_perrh,ph,ec); } XTime_GetTime(&TimeStart); TimeEnd = TimeStart + ((XTime)COUNTS_PER_SECOND); countdown = countdown -1; sprintf(counter, "Counter: %d", countdown); sprintf(lightval,"Light Value: %d", light); sprintf(humval, "Humidity: %d.%02d RH", (int) hum_perrh, ((int)(hum_perrh*100)%100)); sprintf(tempval, "Temp: %d.%02d degC", (int) temp_degc, ((int)(temp_degc*100)%100)); sprintf(co2val, "CO2: %d ppm", eCO2); sprintf(tvocval, "TVOC: %d ppb",TVOC); sprintf(phval,"pH:%d.%02d ",(int) ph, ((int)(ph*100)%100)); sprintf(ecval,"ec:%d.%02d ms/cm",(int) ec, ((int)(ec*100)%100)); /*xil_printf(counter, "Counter: %d", countdown); xil_printf(lightval,"Light Value: %d", light); xil_printf(humval, "Humidity: %d.%02d RH", (int) hum_perrh, ((int)(hum_perrh*100)%100)); xil_printf(tempval, "Temp: %d.%02d degC", (int) temp_degc, ((int)(temp_degc*100)%100)); xil_printf(co2val, "CO2: %d ppm", eCO2); xil_printf(tvocval, "TVOC: %d ppb",TVOC); xil_printf(phval,"pH:%d.%02d ",(int) ph, ((int)(ph*100)%100)); xil_printf(ecval,"ec:%d.%02d ms/cm \n\r",(int) ec, ((int)(ec*100)%100));*/ } } DisableCaches(); return 0; } void startup(){ //Init ALS ALS_begin(&ALS, XPAR_PMODALS_0_AXI_LITE_SPI_BASEADDR); // DrawText(hdsDisp,(char*)"ALS initialized",100,120); //Init HYGRO HYGRO_begin( &HYGRO, XPAR_PMODHYGRO_0_AXI_LITE_IIC_BASEADDR, 0x40, // Chip address of PmodHYGRO IIC XPAR_PMODHYGRO_0_AXI_LITE_TMR_BASEADDR, XPAR_PMODHYGRO_0_DEVICE_ID, TIMER_FREQ_HZ // Clock frequency of AXI bus, used to convert timer data ); //Init AQS AQS_begin(&AQS, XPAR_PMODAQS_0_AXI_LITE_IIC_BASEADDR, 0x5B); // DrawText(hdsDisp,(char*)"AQS initialized",100,140); //Init ESP32 HostUart_Config *CfgPtr; ESP32_Initialize(&ESP32, PMODESP32_UART_BASEADDR, PMODESP32_GPIO_BASEADDR); CfgPtr = HostUart_LookupConfig(HOST_UART_DEVICE_ID); HostUart_CfgInitialize(&myHostUart, CfgPtr, HostUartConfig_GetBaseAddr(CfgPtr)); AD1_begin(&myDevice, XPAR_PMODAD1_0_AXI_LITE_SAMPLE_BASEADDR); // Wait for AD1 to finish powering on usleep(1); // 1 us (minimum) //Init GPIO ConfigPtr = XGpioPs_LookupConfig(XPAR_PS7_GPIO_0_DEVICE_ID); XGpioPs_CfgInitialize(&output, ConfigPtr, ConfigPtr->BaseAddr); XGpioPs_SetDirectionPin(&output, 13, 1); //Pin 1 output XGpioPs_SetOutputEnablePin(&output, 13,1); XGpioPs_SetDirectionPin(&output, 10, 1); //Pin 2 output XGpioPs_SetOutputEnablePin(&output, 10, 1); /* //Init LED int status; // INITIALISE THE PERIPHERALS & SET DIRECTIONS OF GPIO // Initialise LEDs status=XGpio_Initialize(&lighttest, LEDS_DEVICE_ID); ; if (status != XST_SUCCESS) return XST_FAILURE; // Set LEDs direction to outputs XGpio_SetDataDirection(&lighttest, 1,0x00);*/ } void receiveData(XTime time){ XTime tEnd, tCur; u8 recv_buffer=0; u32 num_received=0; XTime_GetTime(&tCur); tEnd = tCur + (time * COUNTS_PER_SECOND); do { num_received = ESP32_Recv(&ESP32, &recv_buffer,1); if(num_received >0){ num_received = ESP32_Recv(&ESP32, &recv_buffer,1); xil_printf("%c", recv_buffer); } if(tCur == tCur + COUNTS_PER_SECOND){ countdown = countdown -1; } else XTime_GetTime(&tCur); } while (tCur < tEnd); } void setWifiMode(void){ u8 tx[]="AT+CWMODE=3\r\n"; u32 num = strlen((char *) tx); xil_printf((char *) tx); ESP32_SendBuffer(&ESP32, tx, num); usleep(100); receiveData(3); } void connectWifi(void){ u8 tx[] = "AT+CWJAP=\"xxxxxxx\",\"xxxxxxx\"\r\n"; u32 num = strlen((char *) tx); xil_printf((char *) tx); ESP32_SendBuffer(&ESP32, tx, num); usleep(100); receiveData(30); } void establishConnection(void){ u8 tx[] = "AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",80\r\n"; u32 num = strlen((char *) tx); xil_printf((char *) tx); ESP32_SendBuffer(&ESP32, tx, num); receiveData(10); } void cipsend(float temp, u16 co2, u8 light, float humidity,float ec,float ph){ u8 command[150]; u8 finalcmd[50]; //field1 Ph field2 Temp field3 co2 field4 humidity field5 light //CE8VFL1E8ZJNGZM8 //DM4ANODDROIY3TCE (Andy Acc) sprintf((char*)command, "GET http://api.thingspeak.com/update?api_key=xxxxxxxxxx&field1=%d.%02d&field2=%d.%02d&field3=%d&field4=%d.%02d&field5=%d&field6=%d.%02d\r\n" ,(int) ph,((int) (ph * 100)) % 100,(int) temp_degc,((int) (temp_degc * 100)) % 100,co2,(int) hum_perrh,((int) (hum_perrh * 100)) % 100,light,(int) ec,((int) (ec * 100)) % 100); u32 length = strlen((char*)command); sprintf((char*)finalcmd, "AT+CIPSEND=%d\r\n", (int)length); u32 cmdlength =strlen((char*)finalcmd); xil_printf("Length %d\r\n", length); xil_printf((char *)finalcmd); ESP32_SendBuffer(&ESP32, finalcmd, cmdlength); sleep(1); xil_printf((char *)command); ESP32_SendBuffer(&ESP32, command, length); receiveData(4); } void EnableCaches() { #ifdef __MICROBLAZE__ #ifdef XPAR_MICROBLAZE_USE_ICACHE Xil_ICacheEnable(); #endif #ifdef XPAR_MICROBLAZE_USE_DCACHE Xil_DCacheEnable(); #endif #endif } void DisableCaches() { #ifdef __MICROBLAZE__ #ifdef XPAR_MICROBLAZE_USE_ICACHE Xil_ICacheDisable(); #endif #ifdef XPAR_MICROBLAZE_USE_DCACHE Xil_DCacheDisable(); #endif #endif }