#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" extern "C"{ #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(void); /******************************************************************************/ /* */ /* Variables */ /* */ /******************************************************************************/ HostUart myHostUart; XGpioPs_Config *ConfigPtr; XGpioPs output; XTime TimeStart; XTime TimeEnd; PmodESP32 ESP32; int countdown =60; char counter[20]; int main() { EnableCaches(); startup(); setWifiMode(); // Set Mode 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){ establishConnection(); cipsend(); } DisableCaches(); return 0; } void startup(){ //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)); } 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 DemoRun() { u8 recv_buffer; u32 num_received; xil_printf("Entering Pmod ESP32 Command Line Interface!\r\n"); xil_printf("Enter AT commands to interact with the ESP32\r\n"); while (1) { // TODO: add exit functionality (ctrl-Z?) num_received = ESP32_Recv(&myESP32, &recv_buffer, 1); if (num_received > 0) { xil_printf("%c", recv_buffer); } num_received = HostUart_Recv(&myHostUart, &recv_buffer, 1); if (num_received > 0) { xil_printf("%c", recv_buffer); while (0 == ESP32_Send(&myESP32, &recv_buffer, 1)); } } } * * * */ void setWifiMode(void){ u8 tx[]="AT+CWMODE=3\r\n";// buffer u32 num = strlen((char *) tx);// convert buffer to length xil_printf((char *) tx); // Display AT+CWMODE Function ESP32_SendBuffer(&ESP32, tx, num);// Send AT+CWMODE Function usleep(100); receiveData(3); // Response from server } void connectWifi(void){ u8 tx[] = "AT+CWJAP=\"ssid\",\"password\"\r\n"; // buffer with ssid and password //AT+CWJAP="SINGTEL-9CCC","krymmf4943" //u8 tx[] = "AT+CWJAP=\"eee-iot\",\"hWV4IOcKp0JX\"\r\n"; //u8 tx[] = "AT+CWJAP=\"T12503_4\",\"00T12503400\"\r\n"; u32 num = strlen((char *) tx); // convert buffer to length xil_printf((char *) tx); // Display AT+CWJAP Function ESP32_SendBuffer(&ESP32, tx, num);// Send AT+CWJAP Function usleep(100); receiveData(30); // Response from server } void establishConnection(void){ u8 tx[] = "AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",80\r\n"; //buffer u32 num = strlen((char *) tx); // convert buffer to length xil_printf((char *) tx); // Display AT+CIPSTART Function ESP32_SendBuffer(&ESP32, tx, num); // Send AT+CIPSTART Function receiveData(10); // Receive information from server } void cipsend(void){ u8 command[150]; //GET Function u8 finalcmd[50]; //AT+CIPSEND //field1 Ph field2 Temp field3 co2 field4 humidity field5 light sprintf((char*)command, "GET https://api.thingspeak.com/channels/xxxxxxx/fields/1/last?key=xxxxxxxxxxxxxxxx\r\n"); // convert the GET Function to char form u32 length = strlen((char*)command); // count how many bits for GET function sprintf((char*)finalcmd, "AT+CIPSEND=%d\r\n", (int)length); // convert the AT+CIPSEND Command to char form u32 cmdlength =strlen((char*)finalcmd); // count how many bits for AT+CIPSEND function xil_printf("Length %d\r\n", length); // Display how many bits does the length have xil_printf((char *)finalcmd); // Display AT+CIPSEND= "finalcmd" ESP32_SendBuffer(&ESP32, finalcmd, cmdlength); // Send AT+CIPSEND command to server sleep(1); xil_printf((char *)command); // Display GET+"update link" ESP32_SendBuffer(&ESP32, command, length); // Send GET+"update link" to server //sleep(1); //ESP32_SendBuffer(&ESP32, command, length); //xil_printf("testing"); receiveData(4); //receiveData(100); // Response } 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 }