I am building a simple website which should update a text file when the host receives data from another device connected to the same network. The device in question is a small weather station which measures temperature. The temperature is sent by the station in a PHP GET command through Wifi from an ESP8266 chip. The signal is received by the home router and redirected to a laptop on the network which is hosting the website.
I am having issues because the ESP8266 chip seems to send the data/PHP GET command correctly. Yet the website never updates itself.
In order to simplify the system for troubleshooting, the weather station is replaced by the laptop. The laptop is connected directly to the ESP8266 chip through a USB-to-Serial(UART) cable. All the AT commands to communicate with the ESP8266 chip are typed in the Arduino IDE Serial Monitor.
The website is very simple. Whenever it receives data from the ESP8266 chip, it writes it in a text file.
The website is stored on a laptop whose IP is 192.168.1.2.
wlp2s0 Link encap:Ethernet HWaddr 18:cf:5e:ee:3f:10
inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
phodor@UBUNTU:~$ ls -la /var/www/html
-rwxrwxrwx 1 root root 0 Jul 31 22:41 data.txt
-rwxr-xr-x 1 root root 127 Jul 31 21:40 index.php
phodor@UBUNTU:~$ cat /var/www/html/index.php
$a = $_GET['temp'];
$dataSaved = file_put_contents("data.txt",$a . "\n",FILE_APPEND);
echo "Temperature is " . $a . " oC.";
Temperature is 28 oC.
WIFI GOT IP
> GET /index.php?temp=10 HTTP/1.1\r\nHost: 192.168.1.2\r\n\r\n
Recv 56 bytes
Ok I've figure out the problem.
It turns out that the Arduino IDE Serial Monitor does not recognize "\r" and "\n" as special characters and treats them as 4 characters. However, when I type "Enter" (or click on the "Send button") in the Arduino IDE Serial Monitor, it automatically sends a carriage return & a line feed character (two characters more than the string I am trying to send). Therefore, whenever I want to send "\r\n" to Serial via the IDE, i need to do it by pressing Enter. If there is such a "\r\n" in the middle of the string I want to send (as is the case in my example), the string must be cut in two (or more).
I therefore modified this part of my code:
AT+CIPSEND=56 OK > GET /index.php?temp=10 HTTP/1.1\r\nHost: 192.168.1.2\r\n\r\n
AT+CIPSEND=33 OK > GET /index.php?temp=10 HTTP/1.1 (here I press "Enter") Recv 33 bytes SEND OK AT+CIPSEND=21 OK > Host: 192.168.1.2 (here I press "Enter") Recv 21 bytes SEND OK
Note that "GET /index.php?temp=10 HTTP/1.1" is 31 characters long. Two characters will be added by the Serial Monitor when I click "Enter". This explains
AT+CIPSEND=33 for sending this line, and not
Similarly, "Host: 192.168.1.2" is 19 characters long. When adding 2 characters for carriage-return-and-line-feed, we get 21 characters, so
The server than answers with the correct response.
+IPD,215:HTTP/1.1 200 OK Date: Sat, 06 Aug 2016 13:39:59 GMT Server: Apache/2.4.18 (Ubuntu) Content-Length: 67 Content-Type: text/html; charset=UTF-8 <!DOCTYPE html> <html> <body> Temperature is 10 oC.</body> </html> CLOSED