-->
Page 3 of 5

Re: Posting and getting data from ESP on Apache WebServer

PostPosted: Sat Jan 02, 2016 12:01 pm
by Stampede
Moving along!
So I've tested the server side and it is solid. Bolted on a little bit more to the previously posted PHP code. Now if you add this after the PHP tags.
Code: Select all<HTML>
<HEAD>

<meta http-equiv="refresh" content="5">

</HEAD>
<BODY>
</BODY>
</HTML>


The page will refresh itself every 5 seconds. Makes it easier to monitor if data is coming through. I have a 2nd computer connected to the server constantly updating to see how data transmission is going.

Now I've tested the server using some Java test code available here: https://www3.ntu.edu.sg/home/ehchua/programming/webprogramming/HTTP_Basics.html

I also pushed data from my 2nd computer to the server using "192.168.4.1/data.php?temp1=33" something to that effect and it does receive it and display it. What I can't seem to get is the microcontroller to send data. It connects and gets an IP address on the network but data does not transmit ! :o

Code: Select all#include <ESP8266WiFi.h>

//Raspberry Pi Login Credentials.
const char WiFiSSID[] = "Alamo";
const char WiFiPSK[] = "12345678";

//Pin Definitions
const int LED_PIN = 5;
const int ANALOG_PIN = A0;

//Variables
const char host[] = "192.168.44.1";


void setup()
{
  pinMode(LED_PIN, OUTPUT);
  pinMode(ANALOG_PIN, INPUT);

  byte ledStatus = LOW;

  WiFi.mode(WIFI_STA);
  WiFi.begin(WiFiSSID, WiFiPSK);

  while (WiFi.status() != WL_CONNECTED)
  {
    // Blink the LED
    digitalWrite(LED_PIN, ledStatus); // Write LED high/low
    ledStatus = (ledStatus == HIGH) ? LOW : HIGH;

    delay(100);
  }
}

void loop()
{
//for testing purposes set temp to a default value.
//int temp = analogRead(ANALOG_PIN);
int temp = 43;

  WiFiClient client;
  const int httpPort = 80;

  if (!client.connect(host, httpPort)) {
    digitalWrite(LED_PIN, HIGH);
    Serial.println("connection failed");
    return;
  }

String data = "temp1=" + (String)temp;
String url = "GET /data.php?" + data + " HTTP/1.1";

client.println(url);
  client.println("Host: 192.168.44.1");
  client.println("Connection: close");
  client.println();
  client.flush();
  delay(500);

  //End of Send. 
  digitalWrite(LED_PIN, HIGH);
  delay(100);

    while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  client.stop(); //Stopping client
  //Deep sleep not necessary for testing
  //ESP.deepSleep(60U*60*1000000); //U for unsigned
  delay(100); //for above sleep
}


What do you guys think? Where is my code faulty?

Re: Posting and getting data from ESP on Apache WebServer

PostPosted: Sun Jan 03, 2016 2:54 pm
by 8n1
Hi Stampede,

Your code should work fine. I've just tested a slightly modified version of it. I think you just missunderstood how all this works.
You can verify that it works by either checking the apache logfiles(access.log) or by using a simple Serial.print statement which prints the response you get from the apache webserver.

Your code already has the right Serial.print statements in it but you forgot(or somehow removed) to initialise the serial port so you don't get any serial output at all. Just add "Serial.begin(115200);" as first statement in the setup() and you can see whats happening.

Someone mentioned it before but again go sure that the ip addresss is right. You say it works using:"http://192.168.4.1/data.php?temp1=33" but in your code your calling "http://192.168.44.1..."

I suggest you do both: Check the logfiles and do some debuging using Serial.print.

Re: Posting and getting data from ESP on Apache WebServer

PostPosted: Sun Jan 03, 2016 4:28 pm
by Stampede
Hi 8n1, thanks so much for trying out my code.
Yes I see the typo now, it is ...44.1 as written in the code. I can verify the 8266 has connected to the network and been assigned an IP address.

I maybe misunderstanding how it should work. What I'm trying to do with the PHP code that I had written is to receive the temp1 variable over the network and have the php page act as a live viewer of the microcontroller's temperature data. I will trying viewing the Apache logs to see if any data has come in that I didn't realize.

Here is my data.php code.
Code: Select all<?php

echo date('h:i:s M-d-Y ');
echo '<br />';

if(isset($_GET["temp1"])){
  $val = $_GET["temp1"];
  $tempesp = "Temperature=";
  $tempesp .= ($val);   
  echo $tempesp;
} else {
  echo "No data received";
}

?>
<HTML>
<HEAD>

<meta http-equiv="refresh" content="5">

</HEAD>
<BODY>
</BODY>
</HTML>


I will add the serialize code, unfortunately I'm using the SparkFun Thing rather than a true blue 8266 so my serial monitor output is cut. I have to modify my board in order to read the output, which at this point I may just do.

Thank you for the help.

Re: Posting and getting data from ESP on Apache WebServer

PostPosted: Sun Jan 03, 2016 11:46 pm
by Stampede
Wow 8n1, I'm so grateful for your post. I never would have thought to look at the Apache logs. I am indeed successfully sending data from the microcontroller to the server. Here is a snippet.

192.168.44.101 - - [02/Jan/2016:13:33:39 +0000] "GET /data.php?temp1=34 HTTP/1.1" 200 468 "-" "-"
192.168.44.101 - - [02/Jan/2016:13:33:39 +0000] "GET /data.php?temp1=34 HTTP/1.1" 200 468 "-" "-"
192.168.44.101 - - [02/Jan/2016:13:33:39 +0000] "GET /data.php?temp1=34 HTTP/1.1" 200 468 "-" "-"


So the Apache log shows data is coming in. Now this maybe an Apache quirk but it appears that only the connected client who sends the data is able to view the data from my data.php page. So if I connect via IP .103 and I send data.php?temp1=55 then I will be able to see the displayed data. Now If I look a the data.php page from another client (different IP) I will see the message "No data received".

Has anyone run into anything like this? I am going to scour the web now for ideas, I'm hoping it is just an apache config setting.