c - 使用arduino wifishield从网络读取数据

标签 c arduino wifi

我有arduino wifishield。我想从网页上读取包含一些命令的文本文件。我将解析html文件中的命令并从网页上控制arduino。 问题是我不明白我应该如何读取数据,所以arduino将远程打开led。如果我每次需要打开串行监视器来打开led时都使用下面的代码。但我希望这个过程在arduino内部完成,我不想依赖串行监视器。正如我所说,它应该在未插入计算机时工作。我该怎么做,我想知道我应该首先了解什么才能做到这一点。

#include <SPI.h>
#include <WiFi.h>
char lf=10;
int x=0;
char ssid[] = "AIRTIES_RT-205"; //  your network SSID (name)
char pass[] = "";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)
String readString, readString1;
int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "taylankaan-001-site1.myasp.net";    // name address for Google (using DNS)

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if ( fv != "1.1.0" )
    Serial.println("Please upgrade the firmware");

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
  //  Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid);

    // wait 10 seconds for connection:
    delay(10000);
  }

  //printWifiStatus();

  //Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected to server");
    // Make a HTTP request:
    client.println("GET /asp.txt HTTP/1.1");
    client.println("Host: taylankaan-001-site1.myasp.net");
    client.println("Connection: close");
    client.println();
  }
}
String command;

void loop() {

while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read();

    if (c == '\n') {
      parseCommand(command);
      command = "";
    }
    else {
      command += c;
    }

  }

   if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting from server.");
    client.stop();

    while (true);
  }
  }





void printWifiStatus() {
  // print the SSID of the network you're attached to:
    Serial.println("Connected to network");

    Serial.print(WiFi.SSID());


 if(WiFi.RSSI()>=-35){
     Serial.println("Very good connection");
   }
   if(WiFi.RSSI()>=-65 && WiFi.RSSI()<-35){
     Serial.println("%50 good connected");
   }
   if(WiFi.RSSI()<-65){
     Serial.println("Worst connection");
   }


  }




void parseCommand(String com) {
  String part1;
  String part2;

  //PINON 13

  part1 = com.substring(0, com.indexOf(" "));

  part2 = com.substring(com.indexOf(" ") + 1);

  if (part1.equalsIgnoreCase("openled")) {

    digitalWrite(9, HIGH);
  } 
}

最佳答案

嗯,这很有趣,但是在意识到它与串行端口有关之后,我尝试了一些事情,而我等待串行端口的原因是: while (!Serial) { ;//等待串口连接。仅莱昂纳多需要 } 我不关心它的用途,现在发现除非串行端口打开,否则它会阻止一切。

关于c - 使用arduino wifishield从网络读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28794047/

相关文章:

c - socket() 中的类型和协议(protocol)有什么区别?

c - 使用 __VA_ARGS__ 定义字符串化宏时出错

c# -++后缀和前缀的由来

android - 为什么我的 Android 手机通过未知服务器发送数据?

android - 在没有互联网的情况下从 WLAN 请求下载时,下载不会在 Android 9 上启动

C - 缩写为只执行一次的循环

c++ - Arduino C++ 计算触发点

android - DTR/RTS 发送到基于 FTDI 的 Arduino 板后会发生什么?

c++ - 在 Arduino ide 中读取多个文本文件

android - Wifi 刚刚进入 IDLE