Connect Thingspeak on RT-Thread Project!

Created at 2023-02-02 15:26:10
ThingSpeak is an open-source software written in Ruby which allows users to communicate with internet enabled devices. This Paper takes you on a tour to connect Thingspeak to RT-Thread.
  1. Hardware Platform(The same method applies to different hardware platforms)

screenshot_image.png

Configure RW007 Software Package

screenshot_image.png

Connect D1s development board to the RW007 WIFI module via SPI0

screenshot_image.png

Enter pkgs —update to update the package

After updating the package, change ALIGN in the struct rw007_spi struct definition in the rw007 package to rt_align

Enter the WIFI command to connect to the WIFI

screenshot_image.png

Note: There are some issues may happen when simultaneously enabling the D1s board and the RW007 module, the issues may include:

  1. If D1s is power-on and then plug in the RW007 module, the serial terminal will be cut off, and the serial terminal software will be reopened
  2. With the RW007 module plugged in, if enable or reset D1s will enter the FEL mode and need to exit via the xfel.exe reset command

Configure Thingspeak

  1. Quick register to Thingspeak if you're a new user.
  2. Create the channel
    In this paper, we only created a data interface field1 to upload data.

screenshot_image.png

Check out the HTTP-related APIs in the channel.

screenshot_image.png

Look at the figure below, the last field1=0 in the [Write a Channel Feed API is the uploaded data (where 0 is not limited to numbers, the string is also feasible)

FB5E27C3-E2DC-4f86-A99E-918E84F05D75.png

Thingspeak uses the GET protocol, here we're bringing the webclient package to upload the data

Add the webclient package

screenshot_image.png

Add rt-thread/bsp/allwinner/d1s/applications/thingspeak.c file

Replace the value that defined by the 'GET_LOCAL_URI' to your own [Write a Channel Feed API]

#include <stdio.h>
#include <webclient.h>
#define GET_LOCAL_URI "http://api.thingspeak.com/update?api_key=XXXXXXXXXXXX&field1="
static int webclient_get_smpl(const char *uri)
{
    char *response = RT_NULL;
    size_t resp_len = 0;
    int index;
    if (webclient_request(uri, RT_NULL, RT_NULL, 0, (void **)&response, &resp_len) < 0)
    {
        rt_kprintf("webclient send get request failed.");
        return -RT_ERROR;
    }
    rt_kprintf("webclient send get request by simplify request interface.\n");
    rt_kprintf("webclient get response data: \n");
    for (index = 0; index < rt_strlen(response); index++)
    {
        rt_kprintf("%c", response[index]);
    }
    rt_kprintf("\n");
    if (response)
    {
        web_free(response);
    }
    return 0;
}
static int thingspeak(void)
{
    char uri[80];
    srand();
    int number;
    while(1)
    {
        number = rand() % 100;
        sprintf(uri, "%s%d", GET_LOCAL_URI, number);
        webclient_get_smpl(uri);
        rt_kprintf("random number is %d\r\n",number);
        rt_thread_mdelay(20000);
    }
}
MSH_CMD_EXPORT(thingspeak, thingspeak);

The program will upload the random 0~100 value of the development board to Thingspeak every 20 seconds.

After compiling, downloading, running, and connecting to WIFI, we can then use the thingspeak command to run the program.

screenshot_image.png

You can also query the data through other APIs in API Keys

screenshot_image.png

There are also provide the visual analysis data

screenshot_image.png

Are there any ideas you'd like to share? Comment to chat with us.

More

Follower
0
Views
637
0 Answer
There is no answer, come and add the answer

Write Your Answer

Log in to publish your answer.,Click here to log in.

Create
Post

Share