ESP8266 reads JSON, but doesn't read the PHP file - php

There is a code for ESP8266, which parses the data on my site and performs the switching on / off of the led. When it was a static JSON file, it all worked without problems. But when I transferred a file to PHP that dynamically updates the data and displays it in JSON format, the script doesn't get it to read. What could be the problem?
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#define pin 5
const char* ssid = "ssid";
const char* password = "password";
const char* host = "www.site.ru"; // domain
String path = "/lightAPI.php";
void setup() {
pinMode(pin, OUTPUT);
pinMode(pin, HIGH);
digitalWrite(5, HIGH);
Serial.begin(9600);
delay(10);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
int wifi_ctr = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: " + WiFi.localIP());
}
void loop() {
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
client.print(String("GET ") + path + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: keep-alive\r\n\r\n");
delay(2000); // wait for server to respond
// read response
String section="header";
while(client.available()){
String line = client.readStringUntil('\r');
// Serial.print(line);
// we’ll parse the HTML body here
if (section=="header") { // headers..
Serial.print("");
if (line=="\n") { // skips the empty space at the beginning
section="json";
}
}
else if (section=="json") { // print the good stuff
section="ignore";
String result = line.substring(1);
// Parse JSON
int size = result.length() + 1;
char json[size];
result.toCharArray(json, size);
StaticJsonBuffer<200> jsonBuffer;
JsonObject& json_parsed = jsonBuffer.parseObject(json);
if (!json_parsed.success())
{
Serial.println("parseObject() failed");
return;
}
// Make the decision to turn off or on the LED
if (strcmp(json_parsed["light"], "OFF") == 0) {
digitalWrite(5, HIGH);
Serial.println("LED OFF");
}
else {
digitalWrite(5, LOW);
Serial.println("LED ON");
}
}
}
}
PHP file
<?php
header('Content-Type: application/json');
$status = file_get_contents('txt/lightStatus.txt');
$json = array('light' => $status, 'time' => date("G"));
echo json_encode($json);
?>

There's something wrong with handling the response. It works when connecting to my server but it doesn't work when connecting to yours.
This is what ESP8266 gets when connecting to my server:
HTTP/1.1 200 OK
Date: Sat, 17 Jun 2017 18:21:37 GMT
Server: Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.6.19
X-Powered-By: PHP/5.6.19
Content-Length: 31
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/json
{"light":"OFF","time":"20"}
And this is what it gets when connecting to yours:
HTTP/1.1 200 OK
Server: nginx admin
Date: Sat, 17 Jun 2017 18:25:53 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
28
{"light":"OFF","online":"0","time":"21"}
0
Unfortunately, I don't have time now to investigate the problem with your code but meanwhile here is a working one which uses HTTPClient to handle request and response (I would recommend using this anyway):
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#define pin 5
const char* ssid = "ssid";
const char* password = "password";
void setup() {
pinMode(pin, OUTPUT);
pinMode(pin, HIGH);
digitalWrite(5, HIGH);
Serial.begin(9600);
delay(10);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
int wifi_ctr = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: " + WiFi.localIP());
}
void loop() {
HTTPClient http;
http.begin("http://bot.erm.today/lightAPI.php");
int statusCode = http.GET();
StaticJsonBuffer<200> jsonBuffer;
JsonObject& json_parsed = jsonBuffer.parseObject(http.getString());
http.end();
if (!json_parsed.success())
{
Serial.println("parseObject() failed");
return;
}
// Make the decision to turn off or on the LED
if (strcmp(json_parsed["light"], "OFF") == 0) {
digitalWrite(5, HIGH);
Serial.println("LED OFF");
}
else {
digitalWrite(5, LOW);
Serial.println("LED ON");
}
}

Json is simply a format, a structure, it needs no processing. PHP on the other hand is a server side language that means that is is interpreted by another application and it is actually that application that is interpreting the code that does the work. The esp8266 lacks that application. And will not be able to run your PHP file. I would recommend looking into making an API in php that is stored on a server somewhere and have your esp call out to that. Or see if you can implement your code right on the esp, though you may be limited by CPU, memory, and processing power. Good luck!

Related

file_get_contents does not send data content in a POST request

I am attempting to send a POST request to an arduino from a PHP script that is called by a web page. I am using file_get_contents() to accomplish this. I have read the examples from the PHP manual here: Here. It appears that the Arduino is receiving the POST request but there is no content included in the data stream, only headers and content parameters. Here is my relevant php
// send database updates to the arduino
$url = 'http://'.$ip;
$options = array(
'http' => array(
'header' => "Content-type: application/x-www/form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($responseText)
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
currently on the Arduino I am just dumping the entire POST request to the terminal so that I can verify that it is being received. Here is the output of the Arduino terminal.
Ethernet WebServer Example
server is at 192.168.3.3
new client
POST / HTTP/1.0
Host: 192.168.3.3
Connection: close
Content-Length: 80
Content-type: application/x-www/form-urlencoded
client disconnected
As you can see the entire POST request is there accept the content. It shows the Content-Length but no actual content.
Here is the Arduino code that produced the terminal output. It is nothing more than the Web Server example that comes with the Arduino IDE.
/*
Web Server
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
modified 02 Sept 2015
by Arturo Guadalupi
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 3, 3);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
String postData;
void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Ethernet WebServer Example");
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// start the server
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
//postData = postData + c;
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
//client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
/*client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("<br />");
}
client.println("</html>");*/
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
Serial.println("client disconnected");
}
}
What is causing the content of the POST request to not be sent from the PHP?
After I let my brain rest for about half a day I determined the cause of my problem. It was not in the PHP at all. It was being caused by this IF block in the Arduino code
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
//client.println("Refresh: 5");
client.println();
/*client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("<br />");
}
client.println("</html>");*/
break;
}
it was causing while loop that it is nested in to terminate at the blank line between the header and the content. My new working code for the Arduino is
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 3, 3);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
String postData;
void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
Ethernet.init(10); // Most Arduino shields
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Ethernet WebServer Example");
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// start the server
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
while (client.connected()) {
while (client.available()) {
char c = client.read();
postData = postData + c;
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
}
client.stop();
}
// give the web browser time to receive the data
delay(1);
Serial.println("\r\nclient disconnected");
Serial.println(postData);
Serial.println(postData.substring(postData.indexOf("status=")+7, postData.indexOf("status=")+8 ));
}
}

I am reading a stream of bytes from server using esp8266 by running a .php script on the server but i do not get the correct data

This is the GET request & response which is sent/received to/from the server
Connecting to MD1
.......
WiFi connected
IP address:
192.168.43.135
connecting to smbbitumen.com
Requesting URL: http://www.smbbitumen.com/smbbitumen.com/myftp/API/Users/read.php
HTTP/1.1 200 OK
Date: Sat, 06 Jun 2020 03:00:17 GMT
Server: nginx/1.17.9
Content-Type: text/html
X-Powered-By: PHP/5.4.45
Vary: Accept-Encoding
Accept-Ranges: none
X-Server-Cache: true
X-Proxy-Cache: MISS
Transfer-Encoding: chunked
48
1111000019:30:0020:30:0019:40:0020:40:0019:45:0020:45:0019:50:0020:50:00
0
closing connection
1111000019:30:0020:30:0019:40:0020:40:0019:45:0020:45:0019:50:0020:50:00 -> This is the stream of data which i am reading into esp but it does not get updated in realtime, it remains same. Any ideas, ehy this is happening.....
Also one more thing, when i run the above php link in browser,everytime it returns correct data.
php script which i am using to read data
<?php
clearstatcache();
$myfile = fopen("TEST.txt", "r");
if($myFile === FALSE)
echo "ERROR";
else
echo fread($myfile,filesize("TEST.txt"));
fclose($myfile);
?>
ESP8266 code for connecting to server and running the above script using GET request
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <WiFiClientSecure.h>
#include <Ticker.h>
#include <EEPROM.h>
#include <ESP8266HTTPClient.h>
#include<ESP8266WiFiMulti.h>
const char *ssid = "***";
const char *password = "******";
HTTPClient myhttp;
void setup()
{
Serial.begin(115200);
WiFi.begin(ssid,password);
while(WiFi.status()!= WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting...");
}
}
void loop()
{
if(WiFi.status() == WL_CONNECTED)
{
myhttp.begin("http://www.smbbitumen.com/smbbitumen.com/myftp/API/Users/read.php");
int httpCode = myhttp.GET();
if(httpCode > 0)
{
String payload = myhttp.getString();
Serial.println(myhttp.getString());
}
httpCode = 0;
myhttp.end();
}
delay(2000);
}
The issue is related to cache. Change the code in read.php as below
<?php
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
clearstatcache();
$myfile = fopen("TEST.txt", "r");
if($myFile === FALSE)
echo "ERROR";
else
echo fread($myfile,filesize("TEST.txt"));
fclose($myfile);
clearstatcache();
?>
The similar thing can be done by changing the settings for cache on the server.

Debugging POST request ESP8266 PHP

I used Postman to debug my PHP POST API, and it works (ignoring the json versos non-json input).
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
//
// include database and object files
require_once '../config/database.php';
require_once '../objects/resdatalog.php';
// instantiate database and resdatalog object
$database = new Database();
$db = $database->getConnection();
// initialize object
$rdl = new Resdatalog($db);
$rdl->Location = $_GET['Location'];
$rdl->Temperature = $_GET['Temperature'];
$rdl->Humidity = $_GET['Humidity'];
$rdl->Pressure = $_GET['Pressure'];
$rdl->RecDate = $_GET['RecDate'];
$rdl->AmbientTemp = $_GET['AmbientTemp'];
$rdl->AmbientHum = $_GET['AmbientHum'];
$rdl->AmbientPressure = $_GET['AmbientPressure'];
$ret=$rdl->insert();
?>
But, the client needs to be an ESP8266 module monitoring the environmentals in various locations. I have been trying to make that work, but debugging has just baffled me. No matter what I display on the client side, it looks OK. But it seems never to actually get to the web page. I put code in the web page to "echo" confirmations, but nothing shows up in the client. What is the best way to debug this setup:
/*
BME280 I2C Test.ino
This code shows how to record data from the BME280 environmental sensor
using I2C interface. This file is an example file, part of the Arduino
BME280 library.
GNU General Public License
Written: Dec 30 2015.
Last Updated: Oct 07 2017.
Connecting the BME280 Sensor:
Sensor -> Board
-----------------------------
Vin (Voltage In) -> 3.3V
Gnd (Ground) -> Gnd
SDA (Serial Data) -> A4 on Uno/Pro-Mini, 20 on Mega2560/Due, 2 Leonardo/Pro-Micro
SCK (Serial Clock) -> A5 on Uno/Pro-Mini, 21 on Mega2560/Due, 3 Leonardo/Pro-Micro
*/
#include <BME280I2C.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <Arduino.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
#include <ezTime.h>
// ESP8266WiFiMulti WiFiMulti;
#define SERIAL_BAUD 115200
BME280I2C bme; // Default : forced mode, standby time = 1000 ms
// Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off,
Timezone Charlotte;
String Location;
String Temperature;
String Humidity;
String Pressure;
String RecDate;
String AmbientTemp="0";
String AmbientHum="0";
String AmbientPressure="0";
const char* ssid = "mySSID"; // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "mySSIDPW"; // The password of the Wi-Fi network
//////////////////////////////////////////////////////////////////
void setup()
{
//set up the serial output
Serial.begin(SERIAL_BAUD);
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.begin(ssid, password); // Connect to the network
Serial.print("Connecting to ");
Serial.print(ssid); Serial.println(" ...");
int i = 0;
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000);
Serial.print(++i); Serial.print(' ');
}
Serial.println('\n');
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
waitForSync();
// Begin the BME setup:
Wire.begin(4,5);
while(!bme.begin())
{
Serial.println("Could not find BME280 sensor!");
delay(1000);
}
// bme.chipID(); // Deprecated. See chipModel().
switch(bme.chipModel())
{
case BME280::ChipModel_BME280:
Serial.println("Found BME280 sensor! Success.");
break;
case BME280::ChipModel_BMP280:
Serial.println("Found BMP280 sensor! No Humidity available.");
break;
default:
Serial.println("Found UNKNOWN sensor! Error!");
}
}
//////////////////////////////////////////////////////////////////
void loop()
{
printBME280Data(&Serial);
delay(500);
}
//////////////////////////////////////////////////////////////////
void printBME280Data(Stream* serialclient)
{
float temp(NAN), hum(NAN), pres(NAN);
BME280::TempUnit tempUnit(BME280::TempUnit_Fahrenheit);
BME280::PresUnit presUnit(BME280::PresUnit_inHg);
bme.read(pres, temp, hum, tempUnit, presUnit);
Charlotte.setLocation("America/New_York");
String localdatetime = Charlotte.dateTime("Y-m-d H:i:s");
serialclient->print("\tTemp: ");
serialclient->print(temp);
serialclient->print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F'));
serialclient->print("\t\tHumidity: ");
serialclient->print(hum);
serialclient->print("% RH");
serialclient->print("\t\tPressure: ");
serialclient->print(pres);
serialclient->print(" Inches Hg");
serialclient->println(" Date/Time: "+ localdatetime);
// Now, I'll try to build and post an HTTP command to my cloud API
//
Location="TiVo Cabinet";
Temperature=String(temp);
Humidity=String(hum);
Pressure=String(pres);
RecDate=localdatetime;
AmbientTemp=String(AmbientTemp);
AmbientHum=String(AmbientHum);
AmbientPressure=String(AmbientPressure);
String content =
"{\"Location\": \"" + String(Location) + "\"" +
" , \"Temperature\" : \"" + String(Temperature) + "\"" +
" , \"Humidity\" : \"" + String(Humidity) + "\"" +
" , \"Pressure\" : \"" + String(Pressure) + "\"" +
" , \"RecDate\" : \"" + String(RecDate) + "\"" +
" , \"AmbientTemp\" : \"" + String(AmbientTemp) + "\"" +
" , \"AmbientHum\" : \"" + String(AmbientHum) + "\"" +
" , \"AmbientPressure\" : \"" + String(AmbientPressure) + "\"" +
"}";
serialclient->println("Content: " + content);
// Now, we're gonna try to send this line to the server....
WiFiClient client;
if (!client.connect("telemetry.shafferassoc.com", 80)) {
Serial.println("WiFiClient connection failed");
delay(5000);
} else {Serial.println("WiFiClient connection OK");
}
client.println("Host: telemetry.shafferassoc.com:80\r\n");
client.println("POST TelemetryWebSite/API/Resdatalog/Insert.php HTTP/1.1");
client.println("Accept: */*");
client.println("Content-Length: " + String(content.length()));
client.println("Content-Type: application/json");
// client.println();
client.println(content);
Serial.println("Length: " +String(content.length()));
Serial.println(content);
Serial.println("receiving from remote server");
// not testing 'client.connected()' since we do not need to send data here
while (client.available()) {
char ch = static_cast<char>(client.read());
Serial.print(ch);
}
delay(5000);
}
Here is a snippet of the Serial output:
11:06:49.892 -> WiFiClient connection OK
11:06:49.892 -> Length: 204
11:06:49.892 -> {"Location": "TiVo Cabinet" , "Temperature" : "73.27" , "Humidity" : "0.00" , "Pressure" : "29.57" , "RecDate" : "2020-04-28 11:06:50" , "AmbientTemp" : "0" , "AmbientHum" : "0" , "AmbientPressure" : "0"}
11:06:49.933 -> receiving from remote server`

ESP8266 send GET request to remote server

http://www.universalcard.byethost7.com is my server. Where I kept index.php file. Code is as given below
<?php
if(isset($_GET['username']) && isset($_GET['pin']) && isset($_GET['cost'])) {
$username = $_GET['username'];
$pin = $_GET['pin'];
$cost = $_GET['cost'];
$filecontent = "Username is: ".$username." and PIN is: ".$pin." and cost is: ".$cost."\n";
$filestatus = file_put_contents('uc.txt',$filecontent,FILE_APPEND);
if($filestatus != false )
{
echo "Data written to file..";
}else{
echo "Ohh sorry..";
}
} else {
echo "Something went wrong..";
}
?>
And I want to send a GET request from ESP8266 with Arduino IDE.
In this GET request, I am sending 3 variables 'username' , 'pin' and 'cost' with some values (data type is String). And these values are appending to a file "uc.txt". So when I send a request using a browser, values will append to the text file.
But when I tried to send using ESP8266 it is not appending
Arduino Code is below
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
const char* ssid = "rainbow";
const char* password = "12345678";
const char* host = "universalcard.byethost7.com";
const int httpsPort = 443;
// Use web browser to view and copy
// SHA1 fingerprint of the certificate
//const char* fingerprint = "CF 05 98 89 CA FF 8E D8 5E 5C E0 C2 E4 F7 E6 C3 C7 50 DD 5C";
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Use WiFiClientSecure class to create TLS connection
WiFiClientSecure client;
Serial.print("connecting to ");
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}
String url = "/index.php?username=2bv14is114&pin=5555&cost=1111";
Serial.print("requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println("request sent");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
String line = client.readStringUntil('\n');
if (line.startsWith("{\"state\":\"success\"")) {
Serial.println("esp8266/Arduino CI successfull!");
} else {
Serial.println("esp8266/Arduino CI has failed");
}
Serial.println("reply was:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("closing connection");
}
void loop() {
}
And the output in Serial monitor is below
Your host has some sort of protection (maybe against bots), that expects a _test cookie set by JavaScript, if not present.
You could acquire the cookie, by first visiting the site with the browser and copy paste the cookie into your code.
You would need to do it from the same IP, that your ESP8266 will be introduced to the server, since the cookie is IP bound.
In this case you would have a problem, if you have a dynamic IP and also the longevity of the cookie is unknown.
You could also acquire the cookie by parsing the response, but the cookie is AES encrypted and that would be somewhat complicated.
The most sensible solution would be to switch to a host without such protection.
This is a solution to basically the same problem in this and this question.

PHP POST Arduino

Could someone help me figure out this.
here is my arduino sketch
#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
// Default Pin GizDuino CC3000
#define ADAFRUIT_CC3000_IRQ 3
#define ADAFRUIT_CC3000_VBAT 5
#define ADAFRUIT_CC3000_CS 10
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
SPI_CLOCK_DIV2);
//Router Specifics
#define WLAN_SSID "Cisco"
#define WLAN_PASS "nothingts"
//Router Security
#define WLAN_SECURITY WLAN_SEC_WPA2
//Web transactions on port 80
char server[] = "tmm.site50.net";
Adafruit_CC3000_Client client;
float tempPin = A1;
uint32_t ip;
void setup()
{
Serial.begin(115200);
Serial.println(F("Hello, Patient 1!\n"));
displayDriverMode();
Serial.print("Free RAM: "); Serial.println(getFreeRam(), DEC);
/* Initialise CC3000 */
Serial.println(F("\nInitialising the CC3000 ..."));
if (!cc3000.begin())
{
Serial.println(F("Unable to initialise the CC3000! Check your wiring?"));
while(1);
}
/* Setting MAC Address Spoof */
uint8_t macAddress[6] = { 0x08, 0x00, 0x28, 0x01, 0x79, 0xB7 };
if (!cc3000.setMacAddress(macAddress))
{
Serial.println(F("Failed trying to update the MAC address"));
while(1);
}
/* End MAC Spoof */
uint16_t firmware = checkFirmwareVersion();
if ((firmware != 0x113) && (firmware != 0x118)) {
Serial.println(F("Wrong firmware version!"));
for(;;);
}
displayMACAddress();
/* Cleanup ng Past Connection */
Serial.println(F("\nDeleting old connection profiles"));
if (!cc3000.deleteProfiles()) {
Serial.println(F("Failed!"));
while(1);
}
/* Connecting to Wi-Fi Router */
char *ssid = WLAN_SSID;
Serial.print(F("\nAttempting to connect to ")); Serial.println(ssid);
/* NOTE: Secure connections are not available in 'Tiny' mode! */
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("Connected!"));
/* Wait for DHCP to complete */
Serial.println(F("Request DHCP"));
while (!cc3000.checkDHCP())
{
delay(100);
}
/* Display the IP address DNS, Gateway, etc. */
while (! displayConnectionDetails()) {
delay(1000);
}
}
void loop()
{
uint32_t ip = 0;
Serial.print(F("tmm.site50.net -> "));
while (ip == 0)
{
if (! cc3000.getHostByName("tmm.site50.net", &ip))
{
Serial.println(F("Couldn't resolve!"));
while(1){}
}
}
cc3000.printIPdotsRev(ip);
Serial.println(F(""));
Serial.println(F("Posting!"));
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
if (client.connected())
{
{
client.println("POST /sample.php HTTP/1.1");
Serial.println("POST /sample.php HTTP/1.1");
client.println("Host: tmm.site50.net");
Serial.println("Host: tmm.site50.net");
client.println("From: your#email.tld");
Serial.println("From: your#email.tld");
client.println("User-Agent: gizDuinodev1/1.0");
Serial.println("User-Agent: gizDuinodev1/1.0");
client.println("Content-Type: Application/x-www-form-urlencoded");
Serial.println("Content-Type: Application/x-www-form-urlencoded");
client.println("Content-Length: 7"); // notice not println to not get a linebreak
Serial.println("Content-Length: 7");
client.println(""); // to get the empty line
Serial.println("");
client.println("temp=50");
Serial.println("temp=50");
Serial.println("POSTED!");
delay (1000);
// While we're connected, print out anything the server sends:
while (client.connected())
{
if (client.available())
{
char c = client.read();
Serial.print(c);
}
}
Serial.println();
}
}
else // If the connection failed, print a message:
{
Serial.println(F("Connection failed"));
}
delay(5000);
}
/**************************************************************************/
/*!
#brief Displays the driver mode (tiny of normal), and the buffer
size if tiny mode is not being used
#note The buffer size and driver mode are defined in cc3000_common.h
*/
/**************************************************************************/
void displayDriverMode(void)
{
#ifdef CC3000_TINY_DRIVER
Serial.println(F("CC3000 is configure in 'Tiny' mode"));
#else
Serial.print(F("RX Buffer : "));
Serial.print(CC3000_RX_BUFFER_SIZE);
Serial.println(F(" bytes"));
Serial.print(F("TX Buffer : "));
Serial.print(CC3000_TX_BUFFER_SIZE);
Serial.println(F(" bytes"));
#endif
}
/**************************************************************************/
/*!
#brief Tries to read the CC3000's internal firmware patch ID
*/
/**************************************************************************/
uint16_t checkFirmwareVersion(void)
{
uint8_t major, minor;
uint16_t version;
#ifndef CC3000_TINY_DRIVER
if(!cc3000.getFirmwareVersion(&major, &minor))
{
Serial.println(F("Unable to retrieve the firmware version!\r\n"));
version = 0;
}
else
{
Serial.print(F("Firmware V. : "));
Serial.print(major); Serial.print(F(".")); Serial.println(minor);
version = major; version <<= 8; version |= minor;
}
#endif
return version;
}
/**************************************************************************/
/*!
#brief Tries to read the 6-byte MAC address of the CC3000 module
*/
/**************************************************************************/
void displayMACAddress(void)
{
uint8_t macAddress[6];
if(!cc3000.getMacAddress(macAddress))
{
Serial.println(F("Unable to retrieve MAC Address!\r\n"));
}
else
{
Serial.print(F("MAC Address : "));
cc3000.printHex((byte*)&macAddress, 6);
}
}
/**************************************************************************/
/*!
#brief Tries to read the IP address and other connection details
*/
/**************************************************************************/
bool displayConnectionDetails(void)
{
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
{
Serial.println(F("Unable to retrieve the IP Address!\r\n"));
return false;
}
else
{
Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
Serial.println();
return true;
}
}
and here is the php
http://tmm.site50.net/sample.php
<html>
<body>
<label>Temperature:</label>
<input type="text" id="temp" name="temp" value="" />
</br>
<label>Device:</label>
<input type="text" id="module" name="module" value="" />
</body>
</html>
the thing is, when the device sent.. it seems that there is a bad header..
in arduino serial monitor it gives me this reply.
tmm.site50.net -> 31.170.162.243
Posting!
POST /sample.php HTTP/1.1
Host: 31.170.162.243
From: your#email.tld
User-Agent: gizDuinodev1/1.0
Content-Type: Application/x-www-form-urlencoded
Content-Length: 7
temp=50
POSTED!
HTTP/1.1 200 OK
Date: Fri, 30 Jan 2015 00:51:38 GMT
Server: Apache
X-Powered-By: PHP/5.2.17
Content-Length: 351
Connection: close
Content-Type: text/html
<html>
<body>
<label>Temperature:</label>
<input type="text" id="temp" name="temp" value="" />
</br>
<label>Device:</label>
<input type="text" id="module" name="module" value="" />
</body>
</html>
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->
this is what the server replies to me
The HTTP request
Correct format
A proper HTTP POST request looks something like this:
POST /url HTTP/1.1\r\n
Host example.com\r\n
Content-Type: application/x-www-form-urlencoded\r\n
Content-Length: 7\r\n
\r\n
foo=bar
This will send bar as the value of foo. In a GET request you could add this to the URL (/url/?foo=bar), but POST requests don't work like this: you have to add a message body.
Content-Length should be the amount of characters in the body (here: foo=bar, so 7 characters). The Content-Type and Content-Length fields are required to do what you want. The Host field is required in any HTTP/1.1 request.
Note that linebreaks are \r\n. Fortunately, Arduino's println() adds both the carriage return \r and the newline feed \n. However, you should make sure that you don't use print() unless you want to continue on the same line. So here's a working example:
Working Arduino example
client.println("POST /sample.php HTTP/1.1");
client.println("Host: tmm.site50.net");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Content-Length: 7");
client.println("");
client.println("temp=50");
This will send 50 as the temperature.
PHP back-end
Storing the data in a file
Then you probably want to save the sent data on the server. To do this, you fetch the information from the $_POST superglobal, and save it in a file:
<?php
$handle = fopen('temp.log', 'w');
fwrite($handle, date('d/m/Y H:i:s') . " - " . $_POST['temp']) . "\n";
fclose($handle);
?>
This opens temp.log in write-mode (see fopen()), then writes a timestamp and the current value to that file (see fwrite()), and closes the handle (see fclose()). The file then looks something like this:
30/01/2015 09:12:40 - 50
You can simply go to http://tmm.site50.net/temp.log to view the file.
Logging
Every time the Arduino sends a request, the old result will be overwritten. Alternatively, you may want to log the data, and add new lines instead of overwriting old data. Then, use the a (append) mode in fopen() instead of w (write):
$handle = fopen('temp.log', 'a');
This will automatically append new lines to the end of the file.
Permissions
Depending on the permissions on your server, you may need to create temp.log before PHP can write to it.

Categories