#include "xil_cache.h" #include "xparameters.h" #include "stdio.h" #include "xparameters.h" #include "xuartps.h" #include "xtime_l.h" #include "xgpiops.h" extern "C"{ #include "PmodALS.h" #include "PmodHYGRO.h" #include "PmodAQS.h" #include "PmodESP32.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 /******************************************************************************/ /* */ /* 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); /******************************************************************************/ /* */ /* Variables */ /* */ /******************************************************************************/ PmodALS ALS; PmodHYGRO HYGRO; PmodAQS AQS; PmodESP32 ESP32; HostUart myHostUart; XGpioPs_Config *ConfigPtr; XGpioPs output; XTime TimeStart; XTime TimeEnd; 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]; int main() { EnableCaches(); startup(); setWifiMode(); xil_printf("Init Started\n\r"); connectWifi(); xil_printf("Wifi Done\n\r"); sleep(2); XTime_GetTime(&TimeStart); TimeEnd = TimeStart + ((XTime)COUNTS_PER_SECOND); while (true){ XTime_GetTime(&TimeStart); 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]); /*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 }*/ xil_printf("Sending....\n\r"); establishConnection(); cipsend(temp_degc, eCO2, light, hum_perrh); } 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); 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); } } 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)); //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); } 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=\"T12503_4\",\"00T12503400\"\r\n"; u8 tx[] = "AT+CWJAP=\"eee-iot\",\"hWV4IOcKp0JX\"\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){ u8 command[150]; u8 finalcmd[50]; //field1 Ph field2 Temp field3 co2 field4 humidity field5 light sprintf((char*)command, "GET http://api.thingspeak.com/update?api_key=CE8VFL1E8ZJNGZM8&field1=0&field2=%d.%02d&field3=%d&field4=%d.%02d&field5=%d\r\n" ,(int) temp_degc,((int) (temp_degc * 100)) % 100,co2,(int) hum_perrh,((int) (hum_perrh * 100)) % 100,light); 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 }