###前提・実現したいこと
末尾に示す参考文献を参考に、以下に示すようにプログラムを作成し、Arduino M0 ProとESP-WROOM-02を用いてWifiに接続後、ダミーの文字列データをPHPに送信しました。
phpファイルはWindowsAzureのサーバー上に用意しており、
以下にシリアル上に表示される結果のように、サーバーへの接続と、データの送信も成功しています。
ところが、以下のソースコードに示すように、phpにて送信されたデータを表示するようにしているのですが、うまく表示してくれません。
1.データが送信できていない。(空データ)
2.表示の方法、コードに誤りがある。
上記の2点が考えられますが、よくわかりません。どなたかアドバイスやご意見等ございましたらお教えいただけますと幸甚に存じます。
###ArduinoIDE1.8.0 シリアルモニタ上の実行結果
...Wi-Fi connected IP address: +CIFSR:STAIP,"192.168.43.217" +CIFSR:STAMAC,"5c:cf:7f:29:fa:4e" temperature=2 TCP connection ready temperature=2 Sending.. Packet sent +IPD,453:HTTP/1.1 302 Redirect Content-Type: text/html; charset=UTF-8 Location: https://p******.azurewebsites.net/dev/wwwroot/SQL_SEND.php Server: Microsoft-IIS/8.0 Date: Tue, 04 Apr 2017 12:12:53 GMT Connection: keep-alive Content-Length: 195 <head><title>Document Moved</title></head> <body><h1>Object Moved</h1>This document may be found <a HREF="https://p******.scm.azurewebsites.net/dev/wwwroot/SQL_SEND.php">here</a></body>
###該当のソースコード
Arduinoに書き込んだプログラム
C
1/*****************************Wifi,Setting***************************/ 2// ライブラリの読み込み 3#include "ESP8266.h" 4#include "Milkcocoa.h" 5#include "Client_ESP8266.h" 6// 転送スピード 7#define SERIAL_SPEED 115200 8/************************* WiFi Access Point *********************************/ 9// Wi-Fi SSID 10#define WLAN_SSID "D********" 11// Wi-Fi PASSWORD 12#define WLAN_PASS "e********" 13ESP8266Client wifi; 14 15String data; 16String server = "p******.scm.azurewebsites.net"; 17 18String uri = "/dev/wwwroot/SQL_SEND.php"; 19 20byte dat [5]; 21String temp ,hum; 22 23/********************************** Setup ***************************************/ 24void setup() { 25// パソコンとのシリアル通信のポートを開ける 26 Serial.begin(SERIAL_SPEED); 27// Wi-Fiモジュールとのシリアル通信のポートを開ける 28 Serial1.begin(SERIAL_SPEED); / 29// Wi-Fi設定 30 setupWiFi(); 31} 32 33/* setupWiFi(); Wifi接続部 省略*/ 34 35 36void loop () { 37 hum= "1"; 38 temp="2"; 39 data = "temperature=" + temp;// + "&humidity=" + hum;// data sent must be under this form //name1=value1&name2=value2. 40 httppost(); 41 delay(1000); 42} 43 44void httppost () { 45 Serial1.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection. 46// Serial.println("TCP connection try"); 47 48 if( Serial1.find("OK")) { 49 Serial.println("TCP connection ready"); 50 } 51 delay(1000); 52 53 String postRequest = 54 "POST " + uri + " HTTP/1.0\r\n" + 55 "Host: " + server + "\r\n" + 56 "Accept: *" + "/" + "*\r\n" + 57 "Content-Length: " + data.length() + "\r\n" + 58 "Content-Type: application/x-www-form-urlencoded\r\n" + 59 "\r\n" + data; 60 Serial.println(data); 61// Serial.println(postRequest); 62 63 64 String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent. 65 66 Serial1.print(sendCmd); 67 Serial1.println(postRequest.length() ); 68 delay(500); 69 70 if(Serial1.find(">")) { 71 Serial.println("Sending.."); 72 Serial1.print(postRequest); 73 74 if( Serial1.find("SEND OK")) { 75 Serial.println("Packet sent"); 76 while (Serial1.available()) { 77 String tmpResp = Serial1.readString(); 78 Serial.println(tmpResp); 79 } 80 81 // close the connection 82 Serial1.println("AT+CIPCLOSE"); 83 } 84 } 85}
なんとか送ったデータを表示させたいと試みたphpのコード(Azureのサーバーにある)
php
1 2#$Temp $Humidityの書き出しテスト 3echo $_POST["temperature"]; 4$Temp=$_POST["temperature"]; 5echo $Temp; 6 7$Temp="TESTtest"; 8$Write="<p>Temperature : " . $Temp . " Celcius </p>"; 9echo $Write; 10 11echo "<br>Hello";
表示結果(http://p******.azurewebsites.net/SQL_send.php)
$Tempの情報が表示されない。
Connection established. resource(1) of type (SQL Server Connection) 57 Temperature : TESTtest Celcius Hello
###補足情報(言語/FW/ツール等のバージョンなど)
Arduino IDE 1.8.0
Arduino M0 Pro
ESP-WROOM-02
参考文献
https://www.instructables.com/id/Arduino-Esp8266-Post-Data-to-Website/step2/Circuit-Diagram-/
