『WEMos D1 R1 WiFi UNO』和『DOIT ESP32 DEVKIT V1 BOARD』(Arduino) 與 簡單 PHP Http Get通訊

『WEMos D1 R1 WiFi UNO』和『DOIT ESP32 DEVKIT V1 BOARD』(Arduino) 與 簡單 PHP Http Get通訊

『WEMos D1 R1 WiFi UNO』和『DOIT ESP32 DEVKIT V1 BOARD』(Arduino) 與 簡單 PHP Http Get通訊


資料來源: https://randomnerdtutorials.com/esp32-http-get-post-arduino/


https://github.com/jash-git/IOT-Smart-Application-Design-Practice-Class-Project-2022


https://github.com/jash-git/Jash-good-idea-20220101-001/tree/main/Arduino_Base64_C_lib_Project/Arduino%E5%92%8CPHP%E9%80%9A%E8%A8%8A%E6%9C%83%E5%8B%95%E7%A8%8B%E5%BC%8F%E5%82%99%E4%BB%BD



『PHP』

<?php
	$val = $_GET['string'];
	$bf = fopen("test.txt","a+");
	fwrite ($bf,$val."\r\n");
	fclose($bf);
	echo "get data=";
	if(($val%2)==0)
	{
		echo 0;
	}
	else
	{
		echo 1;
	}	
?>

『WEMos D1 R1 WiFi UNO』

/*
 *  This sketch sends data via HTTP GET requests to data.sparkfun.com service.
 *
 *  You need to get streamId and privateKey at data.sparkfun.com and paste them
 *  below. Or just customize this script to talk to other HTTP servers.
 *
 */

#include <ESP8266WiFi.h>

const char* ssid     = "jash_TStar";
const char* password = "asd700502";

const char* host = "192.168.0.191";
const char* streamId   = "....................";
const char* privateKey = "....................";

int Blue=D4;//D4 有連接WIFI指示燈
int Green=D5;//D5 命令對應燈
int Red=D6;//D6 命令對應燈


void setup() {
  //---
  //設定燈號腳位工作模式
  pinMode(Blue, OUTPUT); 
  pinMode(Green, OUTPUT); 
  pinMode(Red, OUTPUT); 
  //---設定燈號腳位工作模式
  
  //---
  //等待取得網路IP時,設定三燈全亮
  digitalWrite(Blue, HIGH);
  digitalWrite(Green, HIGH);
  digitalWrite(Red, HIGH);
  //---等待取得網路IP時,設定三燈全亮
  Serial.begin(9600);
  delay(50);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(50);
    Serial.print(".");
  }
  
  //---
  //取得網路IP時,所有燈熄滅
  digitalWrite(Blue, LOW);//digitalWrite(Blue, HIGH);
  digitalWrite(Green, LOW);
  digitalWrite(Red, LOW);
  //---取得網路IP時,所有燈熄滅
  
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  delay(1000);
}

int value = 0;

void loop() {
  value=random(10,50);

  Serial.print("connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 8080;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    client.stop();
    digitalWrite(Blue, HIGH);
    return;
  }
  else
  {
    digitalWrite(Blue, LOW);
  }
  
  // We now create a URI for the request
  //String url = "/php_code/val.php";
  String url = "/jash_web/val2DB.php";
  url += "?string=";
  url += value;
  
  Serial.print("Requesting URL: ");
  Serial.println(url);
  
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
               
  unsigned long timeout = millis();
  while (client.available() == 0) 
  {
    if (millis() - timeout > 5000)
    {
      
      Serial.println(">>> Client Timeout(5sec)!");
      Serial.println("");
      client.stop();
      digitalWrite(Blue, HIGH);
      return;
    }
    else
    {
      digitalWrite(Blue, LOW);
    }
  }
  
  // Read all the lines of the reply from server and print them to Serial
  while(client.available())
  {
    String line = client.readStringUntil('\r');
    int index=line.indexOf("get data=");//搜尋資料列
    if(index!=-1)
    {
      digitalWrite(Blue, LOW);
      //----
      //過濾不需要資料
      int value=line.length();
      char Buf[100];
      line.toCharArray(Buf, 100);
      line="";      
      //Serial.println(value);
      for(int i=0;i<value;i++)
      {
        if((Buf[i]>=48)&&(Buf[i]<=57))//只留數字範圍
        {
          line+=Buf[i];
        }
      }
      //----過濾不需要資料
      switch(line.toInt())
      {
        case 0:
          Serial.println("Red on & Green off");
          digitalWrite(Red, HIGH);
          digitalWrite(Green,LOW);
          break;
        case 1:
          Serial.println("Red off & Green on");
          digitalWrite(Red, LOW);
          digitalWrite(Green,HIGH);          
          break;  
      }
      break;
    }   
  }
  
  client.stop();
  Serial.println("closing connection");
  Serial.println("");
  delay(1000);  
}


『DOIT ESP32 DEVKIT V1 BOARD』

/*
  Rui Santos
  Complete project details at Complete project details at https://RandomNerdTutorials.com/esp32-http-get-post-arduino/

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/

#include <WiFi.h>
#include <HTTPClient.h>

int value = 0;
const char* ssid = "jash_TStar";
const char* password = "asd700502";

//Your Domain name with URL path or IP address with path
String serverName = "http://192.168.0.191:8080/jash_web/val2DB.php";
//String serverName = "http://192.168.0.191:8080/php_code/val.php";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;

void setup() {
  Serial.begin(115200); 

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {
  //Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED){
      HTTPClient http;

	  value=random(10,50);
	  String serverPath = serverName;
	  serverPath += "?string=";
	  serverPath += value;
      
      // Your Domain name with URL path or IP address with path
      http.begin(serverPath.c_str());
      
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      
      if (httpResponseCode>0) {
        //Serial.print("HTTP Response code: ");
        //Serial.println(httpResponseCode);
        String line = http.getString();
        Serial.println(line);
        int index=line.indexOf("get data=");//搜尋資料列
        if(index!=-1)
        {
          //----
          //過濾不需要資料
          int value=line.length();
          char Buf[100];
          line.toCharArray(Buf, 100);
          line="";      
          //Serial.println(value);
          for(int i=0;i<value;i++)
          {
            if((Buf[i]>=48)&&(Buf[i]<=57))//只留數字範圍
            {
              line+=Buf[i];
            }
          }
          //----過濾不需要資料
          switch(line.toInt())
          {
            case 0:
              Serial.println("Red on & Green off");
              //digitalWrite(Red, HIGH);
              //digitalWrite(Green,LOW);
              break;
            case 1:
              Serial.println("Red off & Green on");
              //digitalWrite(Red, LOW);
              //digitalWrite(Green,HIGH);          
            break;  
          }//switch
        }//if(index!=-1)      
      }//if (httpResponseCode>0)
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *