fis
fis

Reputation: 35

Go to link using if condition using Arduino IDE

I'm just trying to get myself here "http://192.168.1.103:30000/?k=23&v=capture" when an if condition meet its requirement.

#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);
  }

Let's say:

  1. I already run Chrome.
  2. How can that link above be called without even typing it on Chrome? I want to connect to it automatically.

Any method you could teach? I got the feeling this code itself is wrong. Thanks.

-- EDIT --

NEW CODE FOR 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);
  }
}

NEW CODE FOR 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));
}

Upvotes: 1

Views: 162

Answers (1)

Mathews Sunny
Mathews Sunny

Reputation: 1642

First understand a fact that you doesn't need google chrome for requesting a website.

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

What you do in the above line is that you request for /?k=23&v=capture addressed content in the ip address 192.168.1.103, Actully this is what you do when you use a google chrome. On PC you require a google chrome (or any other browser) for web site request because its difficult to request using commands each time (Think of requesting for a single page using a hell of http commands instead of using chrome, Ohh that's mess). So understand chrome isn't needed to access a site.

Upvotes: 1

Related Questions