使用Arduino IDE转到使用if条件的链接

时间:2017-08-24 17:12:35

标签: arduino esp8266

当if条件符合要求时,我只是想让自己“http://192.168.1.103:30000/?k=23&v=capture”。

#include <ESP8266WiFi.h>
// I purposely don't include the ssid and ssid1 here
WiFiServer server(80);

void setup() {
  pinMode(1, INPUT);
  Serial.begin(115200);
  delay(10);
  Serial.println();
  WiFi.softAP(ssid1, password1);
  Serial.println(WiFi.localIP());
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_AP_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());
  // Start the server
  server.begin();
  Serial.println("Server started");
}

void loop() {
  String link = "http://192.168.1.103:30000/?k=23&v=capture";
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  int var = digitalRead(1);
  if (var == HIGH) {
    client.print(link);
  }

让我们说:

  1. 我已经投放了Chrome。
  2. 如果不在Chrome上输入上述链接,如何调用上述链接?我想自动连接它。
  3. 你能教的任何方法?我觉得这段代码本身就错了。 感谢。

    - 编辑 -

    UNO的新代码

    //language c++
    
    #include <Servo.h>
    #include <Wire.h>
    #include <LiquidCrystal_I2C.h>
    
    #define I2C_ADDR 0x3F // Scanning address
    LiquidCrystal_I2C lcd(I2C_ADDR, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
    Servo Servo1;
    int servopin = 9;
    
    void setup() {
      Serial.begin(9600);
      lcd.begin(16, 2);
      // initialize the lcd for 16 chars 2 lines, turn on backlight
      lcd.backlight(); // finish with backlight on
      lcd.setCursor(3, 0); //Start at character 4 on line 0
      lcd.print("WAITING...");
      pinMode(12, OUTPUT); // pin LaserLight
      pinMode(11, INPUT); // pin LaserDetector
      pinMode(10, INPUT); // pin PIR
      pinMode(9, OUTPUT); // pin Servo
      pinMode(8, OUTPUT); // MCU PIN GPIO2
      Servo1.attach(servopin);
    }
    
    void loop() {
      digitalWrite(12, HIGH);
      boolean inputlaser = digitalRead(11);
      boolean inputpir = digitalRead(10);
      Serial.println(inputlaser);
      Serial.println(inputpir);
      if (inputlaser < 1) {
        digitalWrite(8, HIGH);
        lcd.setCursor(0, 0);
        lcd.print("camera on");
        lcd.setCursor(0, 1);
        lcd.print("robber!");
        delay(5000);
        Servo1.write(180);
      } else if (inputpir > 0) {
        Servo1.write(180);
        lcd.setCursor(0, 0);
        lcd.print("robber inside!");
        lcd.setCursor(0, 1);
        lcd.print("HELP ROBBER!");
        delay(500);
      } else {
        lcd.clear();
        lcd.setCursor(3, 0);
        lcd.print("standby...");
        delay(500);
      }
    }
    

    MCU的新代码

    #include <ESP8266WiFi.h>
    
    char server[] = "192.168.1.103";
    WiFiClient client;
    
    void setup() {
      pinMode(4, INPUT);
      digitalWrite(4, LOW);
      Serial.begin(115200);
      delay(10);
      Serial.println();
      WiFi.softAP(ssid1, password1);
      Serial.println(WiFi.localIP());
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
      WiFi.mode(WIFI_AP_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());
    }
    
    void loop() {
      boolean var = digitalRead(4);
      if (var == HIGH) {
        client.connect(server, 30000);
        Serial.println("connected");
        // Make your API request:
        client.println("GET /?k=23&v=capture");
        client.println("Host: 192.168.1.103");
        client.println("Connection: close");
        client.println();
      } else {
        // if you didn't get a connection to the server:
        Serial.println("connection failed");
      }
      Serial.println(digitalRead(4));
    }
    

1 个答案:

答案 0 :(得分:1)

首先要了解一个事实,即您不需要Google Chrome来请求网站。

 client.println("GET /?k=23&v=capture");
 client.println("Host: 192.168.1.103");

您在上述行中执行的操作是您在IP地址/?k=23&v=capture中请求192.168.1.103已解决的内容,这实际上就是您使用Google Chrome时所执行的操作。在PC上,你需要谷歌浏览器(或任何其他浏览器)来获取网站请求,因为每次都难以使用命令请求(想想使用一些http命令而不是使用chrome来请求单个页面,哦,那就是&#39;混乱)。因此,了解访问网站并不需要Chrome。