如何使用ArduinoMega 2650将ESP8266-01中的数据发送到数据库托管

时间:2019-03-29 16:43:58

标签: arduino iot

我正在做一个IoT项目,我使用RFID卡将其发送到数据库,可以将其发送到本地主机,但是当我尝试将其在线或托管地发送到数据库时,我失败了,数据是没有发送到数据库。

尝试了String cmd2 =“ GET / index2.php?data7 =”函数;而且结果仍然失败,

对于我的代码:

#define nama_wifi "namewifissd"
#define pass_wifi "passwordwifissd"
#define ip_host "https://example.com"
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define RST_PIN 8
#define SS_PIN 9
LiquidCrystal_I2C lcd(0x27, 16, 2);
MFRC522 rfid(SS_PIN, RST_PIN);
//PIN INFRARED               
#define isObstaclePin1 2

//STATUS SENSOR IR TO DATABASE
int isObstacle1 = HIGH; //HIGH MEANS NO OBSTACLE (TIDAK ADA BARANG)
byte readCard[4];
String NEW_DATA_READ = "";
String strID;
char setID[50];
int successRead;
boolean connected = false;

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  Serial1.begin(115200);
  pinMode(isObstaclePin1, INPUT);
  SPI.begin();
  rfid.PCD_Init();

  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);
  Serial.println("Selamat Datang");
  delay(3000);
}

void loop(){
  isObstacle1 = digitalRead(isObstaclePin1);
  infra1();
}

//LOKER1
void infra1(){  
  if (isObstacle1 == HIGH){ 
    Serial.println("LOKER 1 KOSONG");
    tap();
    Serial.println("TIMER START");
  } 
  else if (isObstacle1 == LOW)
  {
    Serial.println("LOKER 1 BERISI");
  }
}

void senddataid(String x)
{
  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;
  }

  String cmd2 = "GET /index2.php?data7=";
  cmd2 +=x;
  cmd2 +="\r\n";
  cmd2 +="HTTP/1.1\r\n";
  Serial1.print("AT+CIPSEND=");
  Serial1.println(cmd2.length());

  if (Serial1.find(">")) {
    Serial.print(">");
  } else {
    Serial1.println("AT+CIPCLOSE");
    Serial.println("Koneksi Timeout");
    delay(1000);
    return;
  }

  Serial1.print(cmd2);
  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;
  }
}


int getID()
{
  if ( !rfid.PICC_IsNewCardPresent())
  {
    return 0;
  }

  if ( !rfid.PICC_ReadCardSerial())
  {
    return 0;
  }

  for (  int i = 0; i < 4 ; i++)
  {
    readCard[i] = rfid.uid.uidByte[i] ; 
    Serial.print(readCard[i], HEX);
    NEW_DATA_READ += String(readCard[i], HEX);
  }

  NEW_DATA_READ.trim();
  Serial.println(""); 
  rfid.PICC_HaltA();
  return 1;
 }

void tap()
{
  Serial.println("TAP RFID ANDA");

  do{ 
    delay(2000);
    successRead = getID();
  }while(!successRead);

  sprintf(setID, "%02X:%02X:%02X:%02X", readCard[0], readCard[1], readCard[2], readCard[3]);
  senddataid(setID);
}

我希望我可以通过esp8266-01将arduino mega 2650中的数据发送到在线/托管数据库中

0 个答案:

没有答案