本文介绍了用于从Thingspeak(云平台)获取或读取数据的可视化C ++程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道如何/尝试编写可视化C ++程序来获取或读取来自Thingspeak(云平台)的数据吗?

Hi do anyone know how to/have tried writing a visual C++ program to get or read data from Thingspeak (a cloud platform)?

推荐答案

感谢此处发布。

Thanks for post here.

> >您知道如何/尝试编写可视化C ++程序来获取或读取来自Thingspeak(云平台)的数据吗?

>>Hi do anyone know how to/have tried writing a visual C++ program to get or read data from Thingspeak (a cloud platform)?

请参阅ThingSpeak支持>文档>读取数据,您可以使用HTTP GET从通道中的所有字段读取数据。所以我认为你可以参考


Refer to ThingSpeak Support>Documentation>Read Data, you could read data from all fields in a channel with HTTP GET. So I think you could refer toAbout WinINet and Using WinINet.

这是一个简单的演示。

#include "pch.h"
#include <iostream>
#include <windows.h>
#include <wininet.h>
#include <initializer_list>

#pragma comment(lib, "wininet.lib")

using namespace std;

#define READ_BUFFER_SIZE 4096

LPCWSTR stringToLPCWSTR(std::string orig);
void ReleaseHandle(std::initializer_list<HINTERNET> hInt);

int main()
{   
	const string strUrl = "https://thingspeak.com/channels/597820/feed.json";

	TCHAR szHostName[128];
	TCHAR szUrlPath[256];
	URL_COMPONENTS relURL = { 0 };
	relURL.dwStructSize = sizeof(URL_COMPONENTS);
	//relURL.nPort = 80;
	relURL.lpszHostName = szHostName;
	relURL.dwHostNameLength = ARRAYSIZE(szHostName);
	relURL.lpszUrlPath = szUrlPath;
	relURL.dwUrlPathLength = ARRAYSIZE(szUrlPath);
	InternetCrackUrl(stringToLPCWSTR(strUrl), (DWORD)wcslen(stringToLPCWSTR(strUrl)), 0, &relURL);


	HINTERNET hInternet = InternetOpen(L"Microsoft Internet Explorer", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
	if (!hInternet) {
		std::cout << "InternetOpen failed!!!" << std::endl;
		return -1;
	}

	HINTERNET hHttpSession = InternetConnect(hInternet, relURL.lpszHostName, 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
	if (!hHttpSession) {
		std::cout << "InternetConnect failed!!!" << std::endl;
		ReleaseHandle({hInternet});
		return -1;
	}

	HINTERNET hHttpRequest = HttpOpenRequest(hHttpSession, L"GET", relURL.lpszUrlPath, NULL, L"", NULL, 0, 0);
	if (!hHttpRequest) {
		std::cout << "HttpOpenRequest failed!!!" << std::endl;
		ReleaseHandle({ hInternet, hHttpSession});
		return -1;
	}


	BOOL bRet = FALSE;
	bRet = HttpSendRequest(hHttpRequest, NULL, 0, NULL, 0);
	if (!bRet) {
		std::cout << "HttpSendRequest Error!!!" << std::endl;
		ReleaseHandle({ hInternet, hHttpSession,hHttpRequest });
		return -1;
	}

	string strData;		// data from thingspeak.
	DWORD dwRetCode = 0;
	DWORD dwSizeOfRead = sizeof(DWORD);
	char szBuffer[READ_BUFFER_SIZE + 1] = {0};
	DWORD dwReadSize = READ_BUFFER_SIZE;
	
	if (!HttpQueryInfo(hHttpRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, szBuffer, &dwSizeOfRead, NULL))
	{
		std::cout << "Query Error!!!";
		ReleaseHandle({ hInternet, hHttpSession,hHttpRequest });
		return -1;
	}
	if (strstr(szBuffer, "404"))
	{
		std::cout << "404 Error!!!";
		ReleaseHandle({ hInternet, hHttpSession,hHttpRequest });
		return -1;
	}

	while (true)
	{
		bRet = InternetReadFile(hHttpRequest, szBuffer, READ_BUFFER_SIZE, &dwReadSize);
		if (!bRet || (0 == dwReadSize))
			break;
		szBuffer[dwReadSize] = '\0';
		strData.append(szBuffer);
	}
	strData = std::move(strData);

	std::cout << strData.c_str() << std::endl;		// Here you will get the data.

	ReleaseHandle({ hInternet, hHttpSession,hHttpRequest });

	return 0;
}

LPCWSTR stringToLPCWSTR(std::string orig)
{
	size_t origsize = orig.length() + 1;
	const size_t newsize = 100;
	size_t convertedChars = 0;
	wchar_t *wcstring = (wchar_t *)malloc(sizeof(wchar_t) *(orig.length() - 1));
	mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE);
	return wcstring;
}

void ReleaseHandle(std::initializer_list<HINTERNET> hInt)
{
	for (auto h : hInt)
		InternetCloseHandle(h);
}

希望这些能帮到你。

祝福,

杰克


这篇关于用于从Thingspeak(云平台)获取或读取数据的可视化C ++程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 12:47