gpt4 book ai didi

https - 如何使用 SIM900 (Arduino) 发出 HTTPS 请求

转载 作者:行者123 更新时间:2023-12-05 06:36:31 25 4
gpt4 key购买 nike

我尝试发出一个 HTTPS GET 请求,并使用收到的数据发出一个 HTTPS POST 请求...我正在使用带有 library GSM_GPRS_GPS_Shield_GSMSHIELD 的 arduino UNO和GSM GPRS屏蔽SIM900。下面是我当前的代码:

//include libs
#include "SIM900.h"
#include "inetGSM.h"
#include <SoftwareSerial.h>

InetGSM inet;

//data holder
int par1 = -1;
int par2 = -1;

void setup() {
Serial.begin(9600);
Serial.println("BEGIN");
boolean started = false;
SIM900power();

//initialize the SIM900
if (gsm.begin(2400)){
Serial.println("READY");
started=true;
}else Serial.println("IDLE");

//connect it to the network
if(started){
if (inet.attachGPRS("free", "", ""))
Serial.println("ATTACHED");
else Serial.println("ERROR");
delay(1000);

gsm.SimpleWriteln("AT+CIFSR");
delay(3000);
gsm.WhileSimpleRead();

//GET request
char * json = "";
while(strlen(json) < 4){
delay(2000);
char msg[200] = "";
Serial.println(inet.httpGET("my.site.com", 80, "/somethingToGet?param=1", msg, 200));

//interpret Json
char * msg_tmp = msg;
json = strstr (msg_tmp, "[{");
}
if(json != ""){
const byte posPar1 = (int)(strstr(json, "par1") - json) + 7;
const byte posPar2 = (int)(strstr(json, "par2") - json) + 7;
if(json[posPar1] != 'u')
par1 = extractNum(json, posPar1);
if(json[posPar2] != 'u')
par2 = extractNum(json, posPar2);
}

if(json == "" || par1 == -1 || par2 == -1){
SIM900power();
Serial.println("A JSON ERROR OCCURED");
while(1){}}
}
};

void loop() {
aPostRequest();
while(1){}
};

void SIM900power()
{
digitalWrite(9, HIGH);
delay(1000);
digitalWrite(9, LOW);
delay(8000);
}

//extract the data from the Json string
int extractPar(char * json, byte pos){
int num = 0;
while (json[pos] != '"'){
num = json[pos]-'0' + num * 10;
pos++;
}
return num;
}

//POST request
void aPostRequest(){
if( par1 != -1 && par2 != -1){
boolean dataFound = true;
while(dataFound){
delay(2000);

char params[100];
snprintf(params, 100, "par1=%d&par2=%d", par1,par2);

char msg[200] = "";

dataFound = (inet.httpPOST("my.site.com ", 80, "/something", params , msg, 200) == 0);
}
}
}

我有两个网站,一个用于测试的 HTTP 网站,另一个用于 HTTPS 的网站。您可以想象,它正在我的 HTTP 上运行。

我不知道如何解决这个问题,但我想我需要做 some tricky things with certificates在图书馆……有人可以帮忙吗?

PS:如果要测试代码,需要取消注释库文件inetGSM.h中的HTTPPOST()函数。您可以在文件 inetGSM.cpp 中编辑函数 httpGET()HTTPPOST()

更新

下面是 GET 函数的库代码(httpPOST() 作用相同):

int InetGSM::httpGET(const char* server, int port, const char* path, char* result, int resultlength)
{
boolean connected=false;
int n_of_at=0;
int length_write;
char end_c[2];
end_c[0]=0x1a;
end_c[1]='\0';

/*
Status = ATTACHED.
if(gsm.getStatus()!=GSM::ATTACHED)
return 0;
*/
while(n_of_at<3) {
if(!connectTCP(server, port)) {
#ifdef DEBUG_ON
Serial.println("DB:NOT CONN");
#endif
n_of_at++;
} else {
connected=true;
n_of_at=3;
}
}

if(!connected) return 0;

gsm.SimpleWrite("GET ");
gsm.SimpleWrite(path);
gsm.SimpleWrite(" HTTP/1.0\r\nHost: ");
gsm.SimpleWrite(server);
gsm.SimpleWrite("\r\n");
gsm.SimpleWrite("User-Agent: Arduino");
gsm.SimpleWrite("\r\n\r\n");
gsm.SimpleWrite(end_c);

switch(gsm.WaitResp(10000, 10, "SEND OK")) {
case RX_TMOUT_ERR:
return 0;
break;
case RX_FINISHED_STR_NOT_RECV:
return 0;
break;
}


delay(50);
#ifdef DEBUG_ON
Serial.println("DB:SENT");
#endif
int res = gsm.read(result, resultlength);

//gsm.disconnectTCP();

//int res=1;
return res;
}

我已经尝试将 HTTP/1.0 更改为 HTTPS/1.0,但没有任何附加内容。

更新 2

我通过我的 HTTP 服务器重定向了我的请求,因为我仍然没有找到答案,如果有人可以回答那些可能被阻止的人的话!

最佳答案

我试图向我在 AWS 中编码的 Lambda 函数发出 HTTPS 请求。该函数必须通过 POST 从 WEMOS D1 mini 向 AWS 发送一个 json 主体。 TBH,我不知道,这是否会解决您 Controller 上的问题,但可能值得尝试 :)

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>

#ifndef STASSID
#define STASSID "<yourWiFiSSID>"
#define STAPSK "<yourWifiPW>"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

const char* host = "<the host link here (has to look like **google.com** important, dont add the route here) >";
const int httpsPort = 443;
const String data = "<Json Object here e.g -> **{\"temperature\": 20.5, \"humidity\": 60}**>";

// Use web browser to view and copy
// SHA1 fingerprint of the certificate
const char fingerprint[] PROGMEM = "5F F1 60 31 09 04 3E F2 90 D2 B0 8A 50 38 04 E8 37 9F BC 76";

void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
Serial.print("connecting to ");
Serial.println(host);

Serial.printf("Using fingerprint '%s'\n", fingerprint);
client.setFingerprint(fingerprint);
client.setInsecure();

if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}

String url = "<your route here e.g **/photos/test**> ";
Serial.print("requesting URL: ");
Serial.println(url);

client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n"+
"Content-Length: " + data.length() + "\r\n" +
"Content-Type: application/json;charset=UTF-8\r\n\r\n"+
data +"\r\n");


Serial.println("request sent");

//READ INCOMING HTML
uint8_t * _buffer = new uint8_t[128];
String line = "";

if (client.connected()) {
int actualLength = client.read(_buffer, 128);
// If it fails for whatever reason
if(actualLength <= 0)
{
return;
}
// Concatenate the buffer content to the final response string
// I used an arduino String for convenience
// but you can use strcat or whatever you see fit
//TRANSFORM EVERY CHAR FROM INCOMING HTML
line += String((char*)_buffer).substring(0, actualLength);

if (line == "\r") {
Serial.println("headers received");

}
}

Serial.print("Line: ");
Serial.println(line);
if (line.startsWith("{\"state\":\"success\"")) {
Serial.println("esp8266/Arduino CI successfull!");
} else {
Serial.println("esp8266/Arduino CI has failed");
}
Serial.println("reply was:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("closing connection");
}

void loop() {
}

我真的希望这可能对某人有所帮助。

关于https - 如何使用 SIM900 (Arduino) 发出 HTTPS 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49048790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com