Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用SIM900(Arduino)发出HTTPS请求_Https_Arduino_Sim900 - Fatal编程技术网

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

如何使用SIM900(Arduino)发出HTTPS请求,https,arduino,sim900,Https,Arduino,Sim900,我尝试发出HTTPS GET请求,并使用接收到的数据发出HTTPS POST请求。。。我正在使用arduino UNO和GSM GPRS屏蔽SIM900。以下是我当前的代码: //include libs #include "SIM900.h" #include "inetGSM.h" #include <SoftwareSerial.h> InetGSM inet; //data holder int par1 = -1; int par

我尝试发出HTTPS GET请求,并使用接收到的数据发出HTTPS POST请求。。。我正在使用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/1.0
更改为
HTTPS/1.0
,但没有附加任何内容

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

我试图对我在AWS中编码的Lambda函数发出HTTPS请求。该函数必须通过POST将一个json正文从WEMOS D1 mini发送到AWS。TBH,我不知道,这是否能解决控制器上的问题,但可能值得一试:)

#包括
#包括
#ifndef STASSID
#定义STASSID“”
#定义STAPSK“”
#恩迪夫
常量字符*ssid=STASSID;
const char*password=STAPSK;
const char*host=“”;
常数int httpsPort=443;
常量字符串数据=“**{“温度\”:20.5,“湿度\”:60}**>”;
//使用web浏览器查看和复制
//SHA1证书的指纹
常量字符指纹[]PROGMEM=“5F F1 60 31 09 04 3E F2 90 D2 B0 8A 50 38 04 E8 37 9F BC 76”;
无效设置(){
序列号开始(115200);
Serial.println();
串行打印(“连接到”);
序列号println(ssid);
WiFi.模式(WiFi_STA);
WiFi.begin(ssid,密码);
while(WiFi.status()!=WL_已连接){
延迟(500);
连续打印(“.”);
}
Serial.println(“”);
Serial.println(“WiFi连接”);
Serial.println(“IP地址:”);
Serial.println(WiFi.localIP());
//使用WiFiClientSecure类创建TLS连接
WiFiClientSecure客户端;
串行打印(“连接到”);
Serial.println(主机);
Serial.printf(“使用指纹“%s”\n”,指纹);
客户端。设置指纹(指纹);
client.setunsecure();
如果(!client.connect(主机,httpsPort)){
Serial.println(“连接失败”);
返回;
}
字符串url=“”;
Serial.print(“请求URL:”);
Serial.println(url);
client.print(字符串(“POST”)+url+“HTTP/1.1\r\n”+
主机:“+Host+”\r\n+
“连接:关闭\r\n”+
内容长度:“+data.Length()+”\r\n+
“内容类型:application/json;charset=UTF-8\r\n\r\n”+
数据+“\r\n”);
Serial.println(“已发送请求”);
//读取输入HTML
uint8_t*_buffer=新的uint8_t[128];
字符串行=”;
if(client.connected()){
int actualLength=client.read(_buffer,128);
//如果它因为任何原因失败了

如果(事实上)我也希望如此。这是一个实习项目,所以当你发布答案时我无法测试。我不知道这是指纹问题还是端口设置问题。有同样需要的人必须进行调查!
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;
}
#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 INCOMMING 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 INCOMMING 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() {
}