2017-02-01 5 views
2

私は最近Arduinoからセンサーデータを既存のMySQLデータベースに書き込むためにArduino Uno Wifiを買った。私は、イーサネットや無線LANのシールドの助けを借りてまったく同じことをする既存のライブラリ(thisのようなもの)があるので、それは大きな問題ではないはずだと思った。私がUno Wifiを購入した直後に発見した問題は、それらのすべてが(Ethernet.hやWifi.hのような)ライブラリのライブラリを必要とすることです。私はすでに無線LAN対応のArduinoを購入しているので、私の意見ではあまり意味がないものを使うためには、余分なシールドが必要です。私はThingSpeakのようなものがサポートされていることを知っていますが、自分のデータベースの柔軟性を失いたくないし、ThingSpeakが私に提供する追加の分析サービスもすべて必要としません。Arduno Uno Wifiを使用してSQLデータベースに書き込むことはできますか?

私のちょっとした疑問は、私の(新しい)Arduino Uno WIFIを使って、既存のデータベースに簡単なINSERT INTO文でデータを書き込む方法ですか?

EDIT:

私はそれは私がこれまでに思い付いたものだ、今日はいくつかのことを試してみました:

#include <MySQL_Connection.h> 
#include <MySQL_Cursor.h> 
#include <UnoWiFiDevEd.h> 

IPAddress server_addr(88, 198, 61, 231); // IP of the MySQL *server* here 
char user[] = "USERxxxxxx";    // MySQL user login username 
char password[] = "xxxxxxxxx";  // MySQL user login password 

// Sample query 
char INSERT_SQL[] = "INSERT INTO `arduino_test`(`Humidity`, `Temperature`, `DateTime`) VALUES (60, 23, '2017-02-02 20:34:20')"; 

WiFiClient client; 
MySQL_Connection conn((Client *)&client); 

void setup() { 
    Serial.begin(9600); 
    while (!Serial); // wait for serial port to connect 

    Wifi.begin(); 

    Serial.println("Connecting"); 

    if (conn.connect(server_addr, 3306, user, password)) { 
    delay(5000); 
    Serial.println("Connection successful"); 
    } else { 
    Serial.println("Connection failed!"); 
    } 
} 


void loop() { 
    delay(2000); 

    Serial.println("Recording data."); 

    // Initiate the query class instance 
    MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn); 
    // Execute the query 
    cur_mem->execute(INSERT_SQL); 
    // Note: since there are no results, we do not need to read any data 
    // Deleting the cursor also frees up memory used 
    delete cur_mem; 
} 

プログラムでこの結果は、サーバーへの接続を確立することができることだけではありませんシリアルモニタに「接続中」と表示されます。

EDIT 2:

私は@cagdasアイデアのおかげで最初の接続を確認するために、何かを考え出しました。そして「レスポンス:10709」:接続は、これは「200国家」をプリントアウトしなければならない動作する場合

#include <MySQL_Connection.h> 
#include <MySQL_Cursor.h> 
#include <Wire.h> 
#include <UnoWiFiDevEd.h> 


IPAddress server_addr(88, 198, 61, 231); // IP of the MySQL *server* here 
char user[] = "USERxxxxxx";    // MySQL user login username 
char password[] = "xxxxxxx";  // MySQL user login password 

// Sample query 
char INSERT_SQL[] = "INSERT INTO `arduino_test`(`Humidity`, `Temperature`, `DateTime`) VALUES (60, 23, '2017-02-02 20:34:20')"; 

WifiData client; 
MySQL_Connection conn((Client *)&client); 

void setup() { 

    char* connector = "rest"; 
    char* server = "download.arduino.org"; 
    char* method = "GET"; 
    String resource = "/latest.txt"; 

    Serial.begin(9600); 
    Ciao.begin(); 

    pinMode(2, INPUT); 

    delay(5000); 
    doRequest(connector, server, resource, method); 

    Wifi.begin(); 

    Serial.println("Connecting"); 

    if (conn.connect(server_addr, 3306, user, password)) { 
    Serial.println("Connection successful"); 
    } else { 
    Serial.println("Connection failed!"); 
    } 
} 

void loop() { 
    delay(2000); 

    Serial.println("Recording data."); 

    // Initiate the query class instance 
    MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn); 
    // Execute the query 
    cur_mem->execute(INSERT_SQL); 
    // Note: since there are no results, we do not need to read any data 
    // Deleting the cursor also frees up memory used 
    delete cur_mem; 
} 

void doRequest(char* conn, char* server, String command, char* method) { 
    CiaoData data = Ciao.write(conn, server, command, method); 
    if (!data.isEmpty()) { 
    Ciao.println("State: " + String (data.get(1))); 
    Ciao.println("Response: " + String (data.get(2))); 
    Serial.println("State: " + String (data.get(1))); 
    Serial.println("Response: " + String (data.get(2))); 
    } 
    else { 
    Ciao.println ("Write Error"); 
    Serial.println ("Write Error"); 
    } 
} 

: これが今の私のスケッチです。しかし、実際にはわずか0しか表示されませんでした。ループを削除して書き込みを行うと、

void loop() {} 

が正しく動作しますが、データベースには何も書き込まれません。 "if(conn.connect(server_addr、3306、user、password))"ステートメントにループのコードを入れても、正しい結果が返されません。 この時点で私はまだこのコードがうまくいかない理由はまだ分かりませんが、考えられる解決策についてより深い洞察を与えるかもしれないと思います。 また、ArduinoのIDEは私にあなたがそれを含めることによって、ESP8266のオーバーロードさWiFiClientを使用する資格があり、この

The sketch is using 50% of the memory

Global variables use 88% of the memory

There is only little RAM left -> stability issues possible (sorry, it's in german)

+0

? – cagdas

+0

@cagdasこの奇妙な振る舞いがどこから来ているか分かりますか?あるいは、何を次にするべきか? –

+0

あなたのWiFiクレデンシャルはどこですか? – cagdas

答えて

1

のようなものを伝えます。

#include <ESP8266WiFi.h> 
#include <MySQL_Connection.h> 
#include <MySQL_Cursor.h> 

WiFiClient client; 
MySQL_Connection conn((Client *)&client); 

ネットワーククライアントはそうWiFiClientはあなたが必要なものを実装する必要があり、Arduinoの側に夕食の継承を持っています。

あなたはとあなたのコードに続けます:答えは理にかなっているん

WiFi.begin(SSID,PASS); 
+0

ちょっとカグダ、お返事ありがとう、遅い返事をおかけして申し訳ありません。私はそれを試して、残念ながらそれは本当に私のために動作しません。 ESP8266WiFi.hは "/Arduino/libraries/ESP8266WiFi/src/ESP8266WiFiType.h:26:19:致命的なエラー:queue.h:そのようなファイルまたはディレクトリはありません"という例外をスローする #include " –

関連する問題