Server-Client communication NodeMCU
So I am trying to make a server with NodeMCU(LoLin NodeMCU V3) to receive data and activate a servo motor. This data is being sent by a NodeMCU client(Amica) which read a potentiometer value and sending it through an HTTP protocol. the access point is working and I can connect to it with my phone. the connection between client and server also is working. However, the value of the potentiometer is not updating. Furthermore, the server is not giving any IP in return to WiFi.localIP().
Do you have any suggestions or am I in the right direction?
Client Code
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
// AP Wi-Fi credentials
const char* ssid = "MyESP8266AP";
const char* password = "testpassword";
const char* host = "192.168.11.4";
// Local ESP web-server address
String data;
// DEEP_SLEEP Timeout interval
int sleepInterval = 5;
// DEEP_SLEEP Timeout interval when connecting to AP fails
int failConnectRetryInterval = 2;
int counter = 0;
float pt;
const int analogInPin = A0;
WiFiClient client;
void setup() {
pinMode(analogInPin, INPUT);
ESP.eraseConfig();
WiFi.persistent(false);
Serial.begin(115200);
Serial.println();
Serial.println("BEGIN");
delay(500);
WiFi.begin(ssid, password);
Serial.println("");
while (WiFi.status() != WL_CONNECTED) {
if(counter > 20){
Serial.println("- can't connect, going to sleep");
hibernate(failConnectRetryInterval);
}
delay(500);
Serial.print(".");
counter++;
}
Serial.println("- wifi connected");
Serial.println("IP Address: "); Serial.println(WiFi.localIP());
readSensor();
Serial.println("- send GET request");
sendHttp();
Serial.println();
Serial.println("- got back to sleep");
Serial.println("");
Serial.println("**************************");
hibernate(sleepInterval);
}
void sendHttp() {
if(client.connect(host,80)){
String url = "/update?pt=";
url+= String(pt);
client.print(String("GET")+url+"HTTP/1.1\r\n"+"Host: "+host+"\r\n"+"Connection: keep-alive\r\n\r\n");
delay(10);
Serial.println("Response:");
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
}
}
void readSensor() {
delay(200);
pt = analogRead(analogInPin);
if (isnan(pt)) {
pt = 0.00;
}
Serial.println("- potentiometer read : "+String(pt));
}
void hibernate(int pInterval) {
WiFi.disconnect();
ESP.deepSleep(10 * 600000 * pInterval, WAKE_RFCAL);
delay(100);
}
void loop() {}
Server Code
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <Servo.h>
const char *ssid = "MyESP8266AP";
const char *password = "testpassword";
IPAddress ip(192, 168, 11, 4);
IPAddress gateway(192, 168, 11, 1);
IPAddress subnet(255, 255, 255, 0);
ESP8266WebServer server(80);
Servo myservo; // create servo object to control a servo
//static const uint8_t D2 = 4;
float pt;
void setup(void) {
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(ip,gateway,subnet);
WiFi.softAP(ssid,password);
Serial.begin(115200);
Serial.println();
Serial.println("IP Address: "); Serial.println(WiFi.localIP());
delay(10);
server.on("/",handle_index);
server.on("/update",handle_update);
server.begin();
// attaches the servo on D2 or GPIO4 of the NodeMCU devkit v1.0
myservo.attach(D2);
}
void handle_index() {
server.send(200, "text/plain", String(pt));
}
void handle_update() {
pt = server.arg("pt").toFloat();
Serial.println(pt);
server.send(200, "text/plain", "updated");
servo(pt);
}
void loop(){
server.handleClient();
}
void servo(float pt){
if (pt>0 && pt<1024){
float pos = map(pt,0,1023,0,180);
myservo.write(pos);
}
}