Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Stampede
#37432 This is exactly the help I was looking for in my project. I just can't seem to get the Sparkfun Thing to connect to the webserver. I added the LED light functionality in order to debug, but it never comes back on to say we're signing off. Which is why I'm thinking we're never connecting.

Here is my code.
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>

// RaspPi Access Point w/ DHCP Login Cred
const char WiFiSSID[] = "TheAlamo";
const char WiFiPSK[] = "12345678";

const int LED_PIN = 5;
const int ANALOG_PIN = A0;

const char host[] = "192.168.4.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);
  }
  digitalWrite(LED_PIN, HIGH);
}

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

  digitalWrite(LED_PIN, LOW);
     
// Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 8000;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
//temp variable is set with a function to check the temp of an DS18B20 sensor. Let me know if you need it
  String data = "temp1=" + (String)temp;
// "temp1=" is what is going to be sent using GET to the apache server, see code in add.php

  // We now create a URI for the request
  String url = "GET /add.php?" + data + " HTTP/1.1";
client.println(url);
  client.println("Host: 192.168.4.1");
  client.println("Connection: close");
  client.println();
  delay(500);
     
  //End of Send. 
  digitalWrite(LED_PIN, HIGH);

  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  client.stop(); //Stopping client

  ESP.deepSleep(60U*60*1000000); //U for unsigned
  delay(1000); //for above sleep
}
}


I'm not throwing any errors, which is good but makes it more difficult to debug.
User avatar
By Stampede
#37470 Thanks Gio, that's where I think the problem is also. When I try to run the PHP code as recommended on a Apache server on my laptop I did get a few errors, specifically undefined variable.
I've tweaked the code a little, but I will run Wireshark and Network tools (CTRL + Shift + I) in order to see how our connectivity is doing and how far our variable data is traveling over the network.

The date code in the provided PHP didn't work for me, and I changed the If ! statement to isset. Also I removed the save to local file portion because I was getting permission errors. Anyone have any ideas what I could be missing?

Code: Select all<?php

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

if(isset($_GET['temp1'])){
$tempesp = "Temperature=";
$tempesp .= ($_GET["temp1"]);   
echo $tempesp;
echo '<br />';
}

?>