Arduino编写ESP32C3通过HTTP连接校园网

 

​
#include <WiFi.h>
#include <HTTPClient.h>

const char *ssid = "wifi名字";    //你的网络名称
//const char *password = "wifi密码"; //你的网络密码

const char *headerKeys[] = {"网站账号", "网站密码"};// 校园网的账号和密码

const char *http_get_com = "http://172.16.0.151:8888/webauth.do?wlanacip=172.16.0.21&wlanacname=sxzros&wlanuserip=10.10.99.112&mac=c8:58:c0:e3:e3:92&vlan=1007&url=http://www.msftconnecttest.com";// 对应的校园网网址

 HTTPClient http; // 声明HTTPClient对象

void setup()
{
  Serial.begin(115200);
  Serial.println();

  WiFi.begin(ssid);

  http.begin(http_get_com); // 准备启用连接

  http.collectHeaders(headerKeys, 2); // 准备需要接收的响应头内容

  int httpCode = http.GET(); // 发起GET请求

  if (httpCode > 0) // 如果状态码大于0说明请求过程无异常
  {
    if (httpCode == HTTP_CODE_OK) // 请求被服务器正常响应,等同于httpCode == 200
    {
      Serial.print("Content-Type = ");
      Serial.println(http.header("Content-Type"));

      Serial.print("Content-Length = ");
      Serial.println(http.header("Content-Length"));
    }
  }
  else
  {
    Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  }

  http.end(); // 结束当前连接
/*
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected!");

  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  */
}



void loop()
{
  
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }

    while (WiFi.status() == WL_CONNECTED)
  {
    Serial.println("WiFi connected!");

    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
     delay(1000);
  }

}

​

通过获取了wifi的名字(一般校园网应该开始不用密码的吧,作者本人学校是不用密码,需要密码自己加)

发送http请求需要登入的网站一般是GET状态。

直接自己修改就行了,作者本人是移植代码再修改的

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值