LinkIt One学习笔记--Lwifi模块

LWIFI学习

https://docs.labs.mediatek.com/resource/linkit-one/en/tutorials/using-http-with-the-linkit-one-development-board 官网链接

这个里面可以 Post a JSON object to a server and receive a response 本人是小白,以下程序又不专业的地方尽情谅解,同时欢迎大神来一起讨论~

#include "LTask.h" 
#include "LWiFi.h" 
#include "LWiFiClient.h"

#define SITE_URL "httpbin.org"//配置主机名SITE_URL “httpbin.org” //配置主机名  
#define WIFI_AP "your_ap_ssid"
#define WIFI_PASSWORD "your_password" 
#define WIFI_AUTH LWIFI_WPA  // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.  
#define SITE_URL "labs.mediatek.com" 

int tempF = 50; 
int tempC = 15;
int pressure = 15;
int humidity = 15;  


LWiFiClient c;

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

  // keep retrying until connected to AP
  Serial.println("Connecting to AP");
  while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
  {
    delay(1000);
  }

  // keep retrying until connected to website
  Serial.println("Connecting to WebSite");
  while (0 == c.connect(SITE_URL, 80))
  {
    Serial.println("Re-Connecting to WebSite");
    delay(1000);
  }

  // send HTTP request, ends with 2 CR/LF
  Serial.println("send HTTP GET request");
  c.println("GET / HTTP/1.1");
  c.println("Host: " SITE_URL);
  c.println("Connection: close");
  c.println();
  //发送HTTP请求,以2 CR / LF结束//发送HTTP请求,以2 CR / LF结束
  String PostData = buildJson();
  Serial.println("Connected. Sending HTTP GET Request ...."); 
  c.println("POST /post HTTP/1.1");  //modify the POST path.println("POST /post HTTP/1.1");  //modify the POST path
  c.println("Host: " SITE_URL);
  c.println("Connection: close");
  c.print("Content-Length: ");
  c.println(PostData.length());
  c.println();
  c.println(PostData);  //send the HTTP POST body.println(PostData);  //send the HTTP POST body
   
  // waiting for server response
  Serial.println("waiting HTTP response:");
  while (!c.available())
  {
    delay(1000);
  }
}

  //构建JSON方法
  String buildJson()
  {
    String data = "{";
    data+="\n";
    data+= "\"d\": {";
    data+="\n";
    data+="\"temperature (F)\": ";
    data+=tempF;
    data+= ",";
    data+="\n";
    data+="\"temperature (C)\": ";
    data+=tempC;
    data+= ",";
    data+="\n";
    data+="\"pressure\": ";
    data+=pressure;
    data+= ",";
    data+="\n";
    data+="\"humidity\": ";
    data+=humidity;
    data+="\n";
    data+="}";
    data+="\n";
    data+="}";
    return data;
  }

boolean disconnectedMsg = false;

void loop()
{
  // Make sure we are connected, and dump the response content to Serial
  while (c)
  {
    int v = c.read();
    if (v != -1)
    {
      Serial.print((char)v);
    }
    else
    {
      Serial.println("no more content, disconnect");
      c.stop();
      while (1)
      {
        delay(10);
      }
    }
  }

  if (!disconnectedMsg)
  {
    Serial.println("disconnected by server");
    disconnectedMsg = true;
  }
  delay(1000);
}

于是现在应该呈现的是如官网图所示,然而我这个编完串口监视器只能显示Connecting to AP,具体原因不清楚,请各位大神指正!

效果图

PS:这块板子刷程序的时候总会出现“打开串行端口COM10出错”,我之前一直是多刷几次,有一次就行,一般程序刚编出来第一次都是好使的,错误的具体原因也不太清楚。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值