This question already has answers here:
Receive JSON POST with PHP
(12 answers)
Closed 2 years ago.
I'm trying to send a temperature reading to a php file in my website. However, the temperature variable doesn't seem to be passed. Below I use the following Arduino code:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define SERVER_IP "XXX.XX.XXX.XX"
#ifndef STASSID
#define STASSID "XXX"
#define STAPSK "XXX"
#endif
void setup() {
Serial.begin(115200);
Serial.println(); Serial.println(); Serial.println();
WiFi.begin(STASSID, STAPSK);
while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
Serial.println(""); Serial.print("Connected! IP address: "); Serial.println(WiFi.localIP());
}
void loop() {
if ((WiFi.status() == WL_CONNECTED)) {
WiFiClient client;
HTTPClient http;
Serial.print("[HTTP] begin...\n");
http.begin(client, "http://" SERVER_IP "/device.php"); //HTTP
http.addHeader("Content-Type", "application/json");
Serial.print("[HTTP] POST...\n");
int httpCode = http.POST("{\"temp\":\"15\"}");
if (httpCode > 0) {
Serial.printf("[HTTP] POST... code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK) {
const String& payload = http.getString();
Serial.println("received payload:\n<<");
Serial.println(payload);
Serial.println(">>");
}
} else {
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
delay(100000);
}
with the following code in my device.php:
<?php header('Content-type: application/json'); header('Content-Type: text/html; charset=utf-8'); require 'config.php';
header("Access-Control-Allow-Origin: *");
$temp = $_POST["temp"];
$ins = mysqli_query($link,"INSERT INTO `test`(`id`,`test`)VALUES(NULL,'$temp')");
$responseArray = array('status' => $_POST);
$encoded = json_encode($responseArray); header('Content-Type: application/json'); echo $encoded;
?>
However, I end up with php error saying:
PHP Notice: Undefined index: temp in /var/www/html/device.php on line 6
I get empty entry in mysql and serial monitor shows empty payload:
[HTTP] begin...
[HTTP] POST...
[HTTP] POST... code: 200
received payload:
<<
{"status":[]}
>>
the solution as per Tangentially Perpendicular comments was to change content type header to
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
and Posted data to
int httpCode = http.POST("temp=15");
Related
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 trying to send the sensor data from Arduino Nano connected with ENC28J60 module for Ethernet connection, the Nano get the IP addresses from my router without any issue, but when I want to send the data to my php page it didn't work!
I tried the link in Postman and work but in Arduino Nano not work:
my code :
#include <UIPEthernet.h>
#include <ArduinoHttpClient.h>
#include "utility/logging.h"
EthernetClient client;
unsigned long next;
char serverAddress[] = "test.tech"; // server address
int port = 8080;
void setup() {
Serial.begin(115200);
uint8_t mac[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};
// Ethernet.init(3); Serial.println("cs pin");
Ethernet.begin(mac);
Serial.println("Initiliazed");
Serial.print(("localIP: "));
Serial.println(Ethernet.localIP());
Serial.print(("subnetMask: "));
Serial.println(Ethernet.subnetMask());
Serial.print(("gatewayIP: "));
Serial.println(Ethernet.gatewayIP());
Serial.print(("dnsServerIP: "));
Serial.println(Ethernet.dnsServerIP());
next = 0;
}
HttpClient client1 = HttpClient(client, serverAddress, port);
void loop()
{
Serial.println("making GET request");
client1.get("/Sensor/insert.php?temp=20&time=2020-08-10 19:58:46&Date=2020-08-10&Clock=19:58:46");
int statusCode = client1.responseStatusCode();
String response = client1.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
Serial.println("Wait five seconds");
delay(5000);
}
Thanks for all:
this code it works with me using ENC28J60 and aI can send the data to PHP server, and I hope some one will find it usefull:
#include <Time.h> //http://www.pjrc.com/teensy/td_libs_Time.html
#include <TimeLib.h> //TimeLib library is needed https://github.com/PaulStoffregen/Time
#include <UIPEthernet.h> //UIPE ETHERNT LIBRARY FOR THE ETHERNET MODULE ENC28J60
#define DHTPIN 2
#define DHTTYPE DHT11 // DHT 22 (AM2302), AM2321
#define DEBUG // TURN DEBUGGING ON OR OFF, TO TURN IT OFF JUST COMMENT THIS LINE
DHT dht(DHTPIN, DHTTYPE);
/* ETHERNET SETTINGS
ARDUINO UNO PINS USED FOR THE ENC28J60 MODULE: 10=CS, 11=MOSI, 12=MISO, 13=SCK
THE ETHERNET MAC ADDRESS MUST BE UNIQUE IN THE PRIVATE LOCAL NETWORK
REMEMBER THAT THE MAC ADDRESS IS IN HEXADECIMAL FORMAT
IN THIS EXAMPLE WE USE THE FOLLOWING MAC ADDRESS : OE:96:03:38:94:92 */
byte mac[] = { 0x0E, 0x96, 0x03, 0x38, 0x94, 0x92 };
EthernetClient client; // CREATES A CLIENT WHICH CAN CONNECT TO A SPECIFIED INTERNET IP ADDRESS AND PORT
char server[] = "www.mydomain.tech"; /// IP ADDRESS OF THE LOCAL SERVER WE CONNECT AND SEND DATA TO
int interval = 18000; // DELAY INTERVAL TO SAVE THE DATA IN THE DATABSE
long myinterval = 18000; //3 Minutes-> 180000ms INTERVAL BETWEEN DATA READINGS, I SET IT IN 6 SECONDS FOR TESTING PURPOSES
long previousMillis = 0;
int status = -2; // INITIALIZE THE CONNECTION STATUS VARIABLE
void setup() {
Serial.begin(115200); // OPEN SERIAL COMMUNICATIONS AND WAIT FOR A PORT TO OPEN
Ethernet.begin(mac); // INITIALIZES THE ETHERNET LIBRARY AND NETWORK SETTINGS
/* ETHERNET SERIAL PRINTS
INFORMATION ABOUT CLIENTS IP ADDRESS, THE SUBNET MASK OF THE NETWORK, THE GATEWAYS
IP ADDRESS AND FINALLY THE DNS SERVER IP ADDRESS WHICH IS THE SAME AS THE GATEWAYS IP
THESE INFORMATION WILL ONLY BE VISIBLE WHEN DEBUGGING IS ON */
dht.begin();
#ifdef DEBUG
Serial.println("Temperature Logger");
Serial.println("*********************");
Serial.print("IP Address : ");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask : ");
Serial.println(Ethernet.subnetMask());
Serial.print("Default Gateway IP: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS Server IP : ");
Serial.println(Ethernet.dnsServerIP());
#endif
}
void loop() {
unsigned long currentMillis = millis();
// RESET ENC28J60 IN CASE OF FAILURE TO CONNECT WITH THE LOCAL SERVER
if (!client.connect(server, 80)) {
#ifdef DEBUG
Serial.println("Connection Failure: Resetting ENC28j60!");
#endif
Enc28J60.init(mac); //RESET AND INITIALIZE THE ENC28J60 AND STARTS PACKET TRANSMISSION-RECEPTION
} else {
client.stop();
}
/* CONNECT TO THE SPECIFIED IP ADDRESS AND PORT
IF A CONNECTION IS ESTABLISHED START SENDING DATA EVERY 3 MINUTES
DATA WILL BE SENT AFTER THE FIRST 3 MINUTES
ALSO WE CHECK THE STATUS OF THE SERVER
THE FUNCTION CONNECT() RETURNS AN INT INDICATING CONNECTION STATUTS:
SUCCESS (1)
TIMED_OUT (-1)
INVALID SERVER (-2)
TRUNCATED (-3)
INVALID RESPONSE (-4)*/
status = client.connect(server, 80);
if (status == 1) {
#ifdef DEBUG
Serial.println("\nConnection Success");
#endif
if (currentMillis - previousMillis > myinterval) {
previousMillis = currentMillis;
//READ DATA FROM THE DHT11 SENSOR
/* THE READ11() FUNCTION RETURNS:
DHTLIB_OK (0): THE SENSOR SAMPLE AND ITS CHECKSUM ARE OK
DHTLIB_ERROR_CHECKSUM (-1): THE CHECKSUM TEST FAILED. THIS MEANS THE DATA WAS RECEIVED BUT MAY NOT BE CORRECT
DHTLIB_ERROR_TIMEOUT (-2): A TIME OUT OCCURRED AND COMMUNICATION HAS FAILED
BELOW WE CHECK THE STATUS OF THE SENSOR */
#ifdef DEBUG
#endif
// PRINT THE HUMIDITY AND TEMPERATURE VALUES IN THE SERIAL MONITOR
#ifdef DEBUG
Serial.print("\nValues: ");
Serial.print(dht.readHumidity(), 1);
Serial.print(",\t");
Serial.println(dht.readTemperature(), 1);
Serial.println("Connected...sendingData");
#endif
// MAKE A HTTP REQUEST
// SPECIFY THE PHP FILE LOCATION IN THE SERVER
//PRINT DATA TO THE SERVER THE CLIENT IS CONNECTED TO
//STRUCTURE: "GET /filepath/yourfile.php?"
client.print( "GET /temptest/write_data.php?");
client.print("temp=");
client.print(dht.readTemperature(), 1); //THE VALUE TO WRITE IN THE DATABSE FOR TEMPERATURE
client.print("&&");
client.print("hum="); //DATABASE VARIABLE IS USED
client.print(dht.readHumidity(), 1); //THE VALUE TO WRITE IN THE DATABSE FOR HUMIDITY
client.println( " HTTP/1.1");
client.print( "Host: " );
client.println(server);
client.println( "Connection: close" );
client.println();
client.println();
client.stop(); // DISCONNECT FROM THE SERVER
delay(interval); //SOME INTERVAL FOR THE DATA TO BE WRITTEN IN THE DATABASE
}
}
// IN CASE OF A TIME_OUT
else if (status == -1) {
#ifdef DEBUG
Serial.println("Connection Timed Out !");
#endif
}
// IN CASE OF AN INVALID SERVER
else if (status == -2) {
#ifdef DEBUG
Serial.println("Invalid Server !");
#endif
}
// IN CASE OF TRUNCATION
else if (status == -3) {
#ifdef DEBUG
Serial.println("Truncated !");
#endif
}
// IN CASE OF AN INVALID RESPONSE
else if (status == -4) {
#ifdef DEBUG
Serial.println("Invalid Response !");
#endif
}
// IN CASE OF AN UNKNOWN ERROR
else {
#ifdef DEBUG
Serial.println("Unknown Error !");
#endif
}
}
I am running a PHP file From Arduino, I want to update a table in the database but not able to run a PHP file from Arduino.
I tried to write an Arduino code that shown below
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDA, 0x02 };
char serverName[] = "192.168.0.162";
int serverPort = 80;
EthernetClient client;
void setup() {
Serial.begin(9600);
if (Ethernet.begin(mac) == 0) {
while(true);
} else {
Serial.println(Ethernet.localIP());
}
delay(1000);
}
void loop() {
if (Serial.available() > 0) {
String incoming = Serial.readString();
Serial.println(incoming);
insertToDb(incoming);
}
}
void insertToDb(String value) {
String postData = "value=" + value;
Serial.println(postData);
Serial.println("connecting...");
if (client.connect(serverName, serverPort)) {
Serial.println("connected");
client.print("GET /localhost/par/api/update.php");
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(serverName);
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded;");
client.print("Content-Length: ");
client.println(postData.length());
client.println();
client.println(postData);
Serial.println("insert conplete");
} else {
Serial.println("connection failed");
}
client.stop();
}
Here I am running localhost(XAMPP) want to run a PHP file. Running a PHP file from Arduino but the problem is PHP file is not executing from Arduin. The Problem arrives itself the file not running from Arduino. Anyone Expert in Arduino please suggest me to debug code. Thanks in Advance.
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!
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.