Read the data from Thingspeak and Achieve IoT Function.

lou yu on 4 May 2021
Latest activity Edit by lou yu on 14 Jun 2021

Hi, I am currently trying to use my FPGA to retrieve data from my Thingspeak LED status graph. Previously, I tried this with my Mode NCU. As I gave "1" to one of the fields in my LED status channel, my LED will light up. It was successfully done. Now, I want to try with FPGA (To be specific, it is Digilent's Zybo Z720 board).

I have learnt that I need to use Pmod ESP32 to achieve this function. Previously, I used the AT commands to send my data from sensor to Thingspeak channel.

Here is the code: 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=\"________\",\"_____________\"\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=____________&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); }

Now, I am a bit confusing with the reverse part (receiving data from Thingspeak). Should I use the same AT command like AT+CIPSEND, or change the AT command. Or should I need to do something like Json Parsing.

I appreciate your favourable response. Thanks Luke

Christopher Stapels
Christopher Stapels on 5 May 2021

I'm not a big fan of the AT command set. Its really versatile, but fairly difficult to read. You can definitely use AT commands to read data from ThingSpeak, though you will have to modify the format a bit (see the Read Data API) but I think it would be much easier to speak over a serial connection to the ESP32, then have the ESP use the ThingSpeak library to process incoming commands from the FPGA.

lou yu
lou yu on 10 Jun 2021

Sorry for late reply. I used the AT+CIPSEND and GET https://api.thingspeak.com/channels/xxxxxxx/fields/1.json?api_key=xxxxxxx&results=2 to get the IPD response as followed:

if true
  % code
AT+CWJAP="ssid","password"
AT+CWJAP="ssid","password"
WIFI DISCONNECT
WIFI CONNECTED
WIFI GOT IP

OK Wifi Done

AT+CIPSTART="TCP","api.thingspeak.com",80 AT+CIPSTART="TCP","api.thingspeak.com",80 CONNECT

OK Length 99 AT+CIPSEND=99 GET https://api.thingspeak.com/channels/xxxxxxx/fields/1.json?api_keyxxxxxxxxxxx&results=2 TCPED9

Recv 99 bytes

SEND OK

+IPD,265:{"channel": {"id":xxxxxx, "name":"xxxxx", "latitude":"0.0", "longitude":"0.0", "field1":"Field Label 1", "created_at":"2021-06-09T02:13:29Z", "updated_at":"2021-06-09T02:13:29Z", "last_entry_id":1}, "feeds": [{"created_at":"2021-06-10T01:54:48Z", "entry_id":1,"field1":"1"}]} CLOSED end

Now, I want to Json Parse the IPD response in C, and I don't know how to achieve it. btw, i know how to parse the normal java script

Thanks

Christopher Stapels
Christopher Stapels on 14 Jun 2021

There are a whole bunch of JSON libraries for C at json.org at the bottom. On my devices, I generally use arduinoJson since I write in the Arduino IDE a lot.

lou yu
lou yu on 14 Jun 2021 (Edited on 14 Jun 2021)

Hi, Mr Christopher. Now, I change my GET<link> function so that I can receive the data in the +IPD format (after setting AT+CIPSEND) and do not need to worry about Json Parsing. You can see my serial terminal below. I wonder how to extract the data from the +IPD response so that I can use the latest entry data to control the relay with Xilinx Vitis. I don't know if you have touched on Xilinx Vitis or FPGA things cuz i am using Xilinx Vitis software to do this task. I also found some tutorial about achieving this task with Arduino but doesn't help for my project. If you know how to do IoT control in xilinx system, that will be great! Thanks in advance.