c - "if (strcmp(URL, "/") == 0)"在Arduino网络草图中的使用

标签 c arduino client strcmp

我试图理解下面 Arduino 草图中的“if (strcmp(URL, "/") == 0)”行(请参阅大约一半的 sendMyPage 函数):

#include <WiServer.h>

#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
#define AREF_VOLTAGE 5
const int tmpPin = A0;
int tmpReading = 0;
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,31,199}; // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"MERCURY_7F3F70"}; // max 32 bytes

unsigned char security_type = 3; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"11163127"}; // max 64 characters

// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};

// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------
float testTmp (void){
  tmpReading = analogRead (tmpPin);
  float voltage = tmpReading * AREF_VOLTAGE;
  voltage /= 1023;
  float tmpC = (voltage - 0.5) * 100;
  return tmpC;
}

// This is our page serving function that generates web pages
**boolean sendMyPage(char* URL)** {

    // Check if the requested URL matches "/"
    **if (strcmp(URL, "/") == 0) {**
        // Use WiServer's print and println functions to write out the page content
        float tmpC = testTmp ();
        WiServer.print("<html>");
//        WiServer.print("Hello World!");
        WiServer.print(tmpC);
        WiServer.print("</html>");

        // URL was recognized
        return true;
    }
    // URL not found
    return false;
}


void setup() {
  // Initialize WiServer and have it use the sendMyPage function to serve pages
  WiServer.init(sendMyPage);

  // Enable Serial output and ask WiServer to generate log messages (optional)
  Serial.begin(57600);
  WiServer.enableVerboseMode(true);
}

void loop(){

  // Run WiServer
  WiServer.server_task();

  delay(10);
}

该函数似乎接受 URL 作为参数,如果 URL 有效,则使用 WiSever.print 将数据发送到页面。但是 'if (strcmp(URL, "/") == 0)' 如何判断 URL 是否正确呢?

我以前见过这种检查,但不明白它是如何工作的。

谢谢!

最佳答案

行:

if (strcmp(URL, "/") == 0) {  

正在测试字符串 URL 是否与字符串文字“/”完全匹配。如果是,则返回0

注意,如果您想测试字符串 URL 是否包含“/”,则使用:

 if (strstr(URL, "/") != NULL) {   

关于但是'if (strcmp(URL, "/") == 0)'如何判断URL是否正确呢?
它不是。需要更多的字符来确定 URL 是否正确,所写的注释似乎并没有真正表达该代码块的实际用途。

关于c - "if (strcmp(URL, "/") == 0)"在Arduino网络草图中的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23200430/

相关文章:

c - 使 "warning: array ' foo' 假设有一个元素“出错

c - SPOJ : PALIN - The Next Palindrome: wrong output

javascript - 寻找将 WEB 应用程序与 Silverlight 和 VB6 集成的客户端集成解决方案

c - 就安全性而言,fputs 与 fwrite

c - 遍历 C 中微 Controller 上的引脚,其中占空比每小时变化一次

Arduino Loop 和闹钟

c++ - 为什么我的 Arduino 类构造函数需要参数?

php - 需要PHP票务系统 源码

javascript - 需要帮助 : Firebase. ServerValue.TIMESTAMP #firebase

c - 如何从十六进制值中提取低位字节?