I have two Adafruit Feather HUZZAH modules which I am programming with the Arduino IDE.
The goal is to send sensor data from clients to a display on a server.
I have achieved a WiFi connection between the two, but have been unable to receive a response from a request. The image below shows a blank after the "Response: " following a successful connection and request.
I am expecting to receive the text "response" from my server, as it should hear the request from the client and reply. It does not seem to be doing this, but I am not sure whether this is because I am mis-using the WiFiClient functions or not - TCP/IP and HTML is new to me. I have been using this code here.
Here is the code for my server:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#ifndef APSSID
#define APSSID "SensorNet"
#define APPSK "ATEC123456"
#endif
const char *ssid = APSSID;
const char *password = APPSK;
ESP8266WebServer server(80);
void handleRoot() {
server.send(200, "text/html", "<h1>You are connected</h1>");
}
void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRoot);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
WiFiClient client;
if (client && client.connected()) {
String request = client.readStringUntil('\n');
Serial.println(request);
client.print("response\n");
client.stop();
}
}
Here is the code from my client:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#ifndef STASSID
#define STASSID "your-ssid"
#define STAPSK "your-password"
#endif
const char* ssid = "SensorNet";
const char* password = "ATEC123456";
const char* host = "192.168.4.1";
const uint16_t port = 80;
const int led = 0;
const int led2 = 2;
void setup() {
Serial.begin(115200);
// We start by connecting to a WiFi network
digitalWrite(led, 1);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
/* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
would try to act as both a client and an access-point and could cause
network-issues with your other WiFi-devices on your WiFi-network. */
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
digitalWrite(led2, 0);
Serial.println(WiFi.localIP());
}
void loop() {
Serial.print("connecting to ");
Serial.print(host);
Serial.print(':');
Serial.println(port);
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("connection failed");
delay(5000);
return;
}
if (client.connected()) {
client.print("request\n");
delay(100);
String response = client.readStringUntil('\n');
Serial.println("Response: ");
Serial.print(response);
client.stop();
}
delay(10000); // execute once every 5 minutes, don't flood
}