Jump to content
  • 0

Retrieve Data From Thingspeak through using Pmod ESP32


helloworld1029

Question

Previously I have asked this question in the forum, however, my explanation was not so clear and may result in confusion. Hence, I decided to rephrase my question in this new post.

Currently, I have been researching on how to allow my Zybo z720 board to receive data from Thingspeak channel through using Pmod ESP32 and control the status of the LED according to the data of Thingspeak. I have already known how to send the data (collected from ALS, AQS and HYGRO) from the zybo board to Thingspeak by using the AT command. I will show the code below. 

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=\"xxxxx\",\"xxxxxx"\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=xxxxxxxxxxxxx&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);
}

 

Beforehand, I have tried this project with the NodeMCU since I thought the code I used for NodeMCU should be similar to ESP32. It worked perfectly. As I changed the thingspeak data link and updated the data, I can see my LED light up or light off.  However, while I analysed the NodeMCU code, it does not use AT command and it also does not have any relationship with the code I used for sending data to Thingspeak.  

So, do I need to use the same AT command, or I need to use something like Json parsing to accomplish that? 

I will show all my work I have done below. 

Thanks

 

 

Screenshot (95).png

Cpp Code Update 1.txt Arduino IoT Control.txt

Edited by helloworld1029
Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Here is the code for Vitis. For Vivado  just add pmod esp32 into your block design.

#include "xparameters.h"
#include "xil_printf.h"
#include "sleep.h"
#include "stdio.h"
#include "xtime_l.h"
#include "stdlib.h"
#include "string.h"
#include "xgpiops.h"


extern "C"{


#include "PmodESP32.h"
}
/******************************************************************************/
/*                                                                            */
/* Defines  																  */
/*                                                                            */
/******************************************************************************/


#ifdef __MICROBLAZE__
#define HOST_UART_DEVICE_ID XPAR_AXI_UARTLITE_0_BASEADDR
#define HostUart XUartLite
#define HostUart_Config XUartLite_Config
#define HostUart_CfgInitialize XUartLite_CfgInitialize
#define HostUart_LookupConfig XUartLite_LookupConfig
#define HostUart_Recv XUartLite_Recv
#define HostUartConfig_GetBaseAddr(CfgPtr) (CfgPtr->RegBaseAddr)
#include "xuartlite.h"
#include "xil_cache.h"
#else
#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)
#include "xuartps.h"
#endif

#define PMODESP32_UART_BASEADDR XPAR_PMODESP32_0_AXI_LITE_UART_BASEADDR
#define PMODESP32_GPIO_BASEADDR XPAR_PMODESP32_0_AXI_LITE_GPIO_BASEADDR
#define BLOCK_SIZE 40
/******************************************************************************/
/*																			  */
/* Function Declarations         											  */
/*                                                                            */
/******************************************************************************/
void EnableCaches();
void DisableCaches();
void startup();
void receiveData(XTime time);
void receiveData2(XTime time);
void setWifiMode(void);
void connectWifi(void);
void establishConnection(void);
void cipsend(void);
/******************************************************************************/
/*                                                                            */
/* Variables 																  */
/*                                                                            */
/******************************************************************************/

HostUart myHostUart;

XTime TimeStart;
XTime TimeEnd;
PmodESP32 ESP32;
XGpioPs_Config *ConfigPtr;
XGpioPs output;




int countdown =60;



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));
  
  	   // 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);

/* 
Write your code here to initialise GPIO
*/ 

}

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 receiveData2(XTime time){
    XTime tEnd, tCur;
    u8 recv_buffer=0;
    u32 num_received=0;

    int i=0;
    int max_index = BLOCK_SIZE-1;


    int *ptr= (int *) malloc(sizeof(int)*BLOCK_SIZE);
  

    if(ptr==NULL)
    {
        perror("some error");
        //return 1;
    }

    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);

                        if(i > max_index)
                        {
                            ptr=(int *)realloc(ptr, (max_index+1 + BLOCK_SIZE)*sizeof(int));
                            
                            if(ptr == NULL)
                            {
                                perror("insufficient memory!");
                                break;
                            }
                            printf("\nReallocated!");
                            max_index += BLOCK_SIZE;
                        }
                        ptr[i]=recv_buffer;
                        i++;

                    

                }
        if(tCur == tCur + COUNTS_PER_SECOND){
           countdown = countdown -1;
        }
        else
            XTime_GetTime(&tCur);
    } while (tCur < tEnd);


    xil_printf("Data %d: %c",i ,ptr[45]);


    if (ptr[45]=='1'){
    	xil_printf("LED On");
    	XGpioPs_WritePin(&output, 13, 0);
    }else if (ptr[45]=='0')
    {
    	xil_printf("LED Off");
    	XGpioPs_WritePin(&output, 13, 1);
    } else {
    	xil_printf("LED On");
    	XGpioPs_WritePin(&output, 13, 0);
    }


    free(ptr); // Clear the memory to prevent "ptr==null"

}

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=\"<Wifi SSID>\",\"<Password>\"\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




	sprintf((char*)command, "GET https://api.thingspeak.com/channels/<channels id> /fields/1/last?key=<channel's key>\r\n"); // convert the GET Function to char form
       // From here, you can acquire the latest Thingspeak's entry data in the +IPD format. 


	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


	receiveData2(10); // Receive the Json parsed data and control the relay...


}

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
}

 

Edited by VerticalFarmingStudent
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...