Hello all my friends I want to ask how to send sensor data if the sensor sent is more than one, I use an IR sensor that is sent 1 or 0 to the database using ESP8266 01, I can send one sensor but when two sensors fail . I use the code below
Arduino Code
#include <SPI.h>
#include <MFRC522.h>
#define nama_wifi "Wifi"
#define pass_wifi "1gakada123"
#define ip_host "192.168.137.1"
boolean connected = false;
#define SS_PIN 9
#define RST_PIN 8
MFRC522 rfid(SS_PIN, RST_PIN);
MFRC522::MIFARE_Key key;
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
String request_string;
String strID,val;
#define isObstaclePin1 2 // This is our input pin
#define isObstaclePin2 3
int isObstacle1 = HIGH; // HIGH MEANS NO OBSTACLE
int isObstacle2 = HIGH;
int value1;
int value2;
void setup() {
// put your setup code here, to run once:
pinMode(isObstaclePin1, INPUT);
pinMode(isObstaclePin2, INPUT);
Serial1.begin(115200);
Serial.begin(9600);
Serial1.setTimeout(5000);
Serial.println("CHECK ESP8266");
delay (1000);
Serial1.println("AT+RST");
delay(1000);
if(Serial1.find("WIFI GOT IP"))
{
Serial.println("ESP8266 SIAP");
}
else {
Serial.println(" Tidak Ada Response dari ESP8266 ");
while(1);
}
delay(1000);
for (int i=0; i<5; i++){
connect_to_wifi();
if (connected){
break;
}
}
if (!connected){
while(1);
}
delay(5000);
Serial1.println("AT+CIPMUX=0");
delay(1000);
}
void loop() {
// put your main code here, to run repeatedly:
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd+= ip_host;
cmd+="\",80";
Serial1.println(cmd);
Serial.println(cmd);
if (Serial1.find("Error")){
Serial.println("Koneksi eror");
return;
}
isObstacle1 = digitalRead(isObstaclePin1);
value1=isObstacle1;
isObstacle2 = digitalRead(isObstaclePin2);
value2=isObstacle2;
infra1();
infra2();
cmd = "GET /arduinovb/index.php?data1&data2=";
cmd+=value1;
cmd+="\r\n";
cmd+="HTTP/1.0/1/\r\n";
cmd+=value2;
cmd+="\r\n";
cmd+="HTTP/1.0/1/\r\n";
Serial1.print("AT+CIPSEND=");
Serial1.println( cmd.length());
if (Serial1.find(">")){
Serial.print(">");
} else {
Serial1.println("AT+CIPCLOSE");
Serial.println("Koneksi Timeout");
delay(1000);
return;
}
Serial1.print(cmd);
delay(2000);
while(Serial1.available())
{
char c =Serial1.read();
Serial.write(c);
if (c=='\r') Serial.print('\n');
}
Serial.println("-----end");
delay(10000);
}
void connect_to_wifi()
{
Serial1.println("AT+CWMODE=1");
String cmd = "AT+CWJAP=\"";
cmd+=nama_wifi;
cmd+="\",\"";
cmd+=pass_wifi;
cmd+="\"";
Serial1.println(cmd);
Serial.println(cmd);
if (Serial1.find("OK")){
Serial.println("Berhasil Terkoneksi ke internet");
connected=true;
} else {
Serial.println("Gagal Terkoneksi");
connected=false;
}
}
void infra1(){
if (isObstacle1 == LOW)
{
Serial.println(isObstacle1);
}
else
{
Serial.println(isObstacle1);
}
delay(5000);
}
void infra2(){
if (isObstacle2 == LOW)
{
Serial.println(isObstacle2);
}
else
{
Serial.println(isObstacle2);
}
delay(5000);
}
index.php
<?php
include ("koneksi.php");
$var1 = $_GET['data1'];
$var2 = $_GET['data2'];
mysqli_query($konek, "INSERT INTO tbl_blogs(title,description) VALUES('$var1','$var2')");
?>
koneksi.php
<?php
$servername = "localhost";
$database = "blog";
$username = "root";
$password = "";
$konek = mysqli_connect ($servername, $username, $password, $database);
if ($konek!=false){
echo "berhasil";
} else {
echo "gagal";}
?>