Arduino Mega2560 + Wifi Shield 2.0(seeedstudio)如何从我的电脑上的xampp服务器中的数据库接收数据

时间:2015-08-19 17:52:13

标签: php http arduino xampp wifi

我正在尝试通过Wi-Fi将我的Arduino Mega连接到我的xampp服务器。 我的主要目标是在我的数据库中获取一个值并将其存储在Arduino代码中的变量上。有了这个新变量,我将能够控制程序的其余部分......所以我需要你的帮助。有人可以给我建议吗?

我已经尝试过在互联网上找到的所有东西,但它只是不起作用。

我认为简单的方法是向服务器发送GET请求并阅读他的回复...但由于某种原因,我无法发送GET请求...

我直接在浏览器上测试它并显示值,所以我认为服务器端没问题。

这是我的Arduino代码:

$dira = "D:\User1\Desktop\AFPTest\"

$list = get-childitem $dira -filter *.afp -recurse | % { $_.FullName } | Sort-Object

$file3 = [System.IO.File]::ReadAllBytes("D:\User1\Desktop\AFPTest\000001.afp")

$file2 = [System.IO.File]::ReadAllBytes("D:\User1\Desktop\AFPTest\000002.afp")

$file1 = [System.IO.File]::ReadAllBytes("D:\User1\Desktop\AFPTest\000003.afp")

$data = $file1 + $file2


[io.file]::WriteAllBytes("D:\User1\Desktop\AFPTest\AFP.afp",$data)

这是我的PHP代码:

#include <Arduino.h>
#include <SoftwareSerial.h>
#include "WiFly.h"  
#include "HTTPClient.h"

#define SSID      "mySSID"
#define KEY       "myKEY"

#define AUTH      WIFLY_AUTH_WPA2_PSK

#define HTTP_GET_URL "http://192.168.1.80/xampp/training/send_data_db.php/?$temp=0"

// Pins' connection
// Arduino       WiFly
//  2    <---->    TX
//  3    <---->    RX
SoftwareSerial uart(10, 11);
WiFly wifly(uart);
HTTPClient http;
char get;

void setup() {
  Serial.begin(9600);    
  Serial.println("------- WIFLY HTTP --------");


  uart.begin(9600);         // WiFly UART Baud Rate: 9600
  // Wait WiFly to init
//  delay(3000);

  // check if WiFly is associated with AP(SSID)
  if (!wifly.isAssociated(SSID)) {
    while (!wifly.join(SSID, KEY, AUTH)) {
      Serial.println("Failed to join " SSID);
      Serial.println("Wait 0.1 second and try again...");
      delay(100);
    }

    wifly.save();    // save configuration, 
  }


Serial.println("\r\nTry to get url - " HTTP_GET_URL);
  Serial.println("------------------------------");
  while (http.get(HTTP_GET_URL, 10000) < 0) {
  }
  while (wifly.receive((uint8_t *)&get, 1, 1000) == 1) {
    Serial.print(get);
  }

  if (wifly.commandMode()) {
    Serial.println("\r\n\r\nEnter command mode. Send \"exit\"(with \\r) to exit command mode");
  }
}


void loop() {
  int c;
  while (wifly.available()) {
    c = wifly.read();
    if (c > 0) {
      Serial.write((char)c);
    }
  }

  while (Serial.available()) {
    c = Serial.read();
    if (c >= 0) {
      wifly.write((char)c);
    }
  }
}

0 个答案:

没有答案