Go Down

Topic: ESP8266 Client Server Questions (Read 241 times) previous topic - next topic

KevinRoach

Hello!

I am putting together a Client, Wemos D1 Mini to monitor water temp and water weather. It will send that info the the server on the bank. The server will monitor weather conditions and ground temp.

The clint is setup to go into sleep mode for whatever time I set and reset when it wakes up. At that point send the info to the server.

My question is: Is there a way or is it possible to have the server go into sleep mode and wake up when contacted by the client? Maybe even have the server wake the client?

Thanks,
Kevin

Client Code:
Code: [Select]
/*------------------------------------------------------------------------------
  03/25/2019
  Author: Makerbro
  Platforms: ESP8266
  Language: C++/Arduino
  File: simple_server_client/client/client.ino
  ------------------------------------------------------------------------------
  Description:
  Client-side code for YouTube video demonstrating how to communicate over WiFi
  between two ESP8266:
  https://youtu.be/gZhUi24_qms
  Do you like my videos? You can support the channel:
  https://patreon.com/acrobotic
  https://paypal.me/acrobotic
  ------------------------------------------------------------------------------
  Please consider buying products from ACROBOTIC to help fund future
  Open-Source projects like this! We'll always put our best effort in every
  project, and release all our design files and code for you to use.
  https://acrobotic.com/
  https://amazon.com/shops/acrobotic
  ------------------------------------------------------------------------------
  License:
  Please see attached LICENSE.txt file for details.
------------------------------------------------------------------------------*/
// Setup the client to send sensor data to the server
#include <ESP8266WiFi.h>

// Initialize sensor parameters
int sensorPin = 0;
float volts = 0.0, temperatureC = 0.0;

// Initialize network parameters
const char* ssid = "ANY_SSID";
const char* password = "ANY_PASSWORD";
const char* host = "192.168.11.4"; // as specified in server.ino

// Set up the client objet
WiFiClient client;

// Configure deep sleep in between measurements
const int sleepTimeSeconds = 2;

void setup() {
  // Configure the sensor pin (optional)
  pinMode(A0, INPUT);
  // Connect to the server
  WiFi.begin(ssid,password);
  Serial.begin(115200);
  while(WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.print("IP Address (AP): "); Serial.println(WiFi.localIP());
  // Get a measurement from the sensor
  volts = analogRead(A0)*3.3/1024; // 10-Bit ADC on a 3.3VDC board
  temperatureC = (volts - 0.5) * 100; // Conversion from voltage to temperature follows the TMP36 datasheet
  Serial.print("Temperature C: "); Serial.println(temperatureC);
  // Connect to the server and send the data as a URL parameter
  if(client.connect(host,80)) {
    String url = "/update?value=";
    url += String(temperatureC);
    client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host +  "\r\n" +
                 "Connection: keep-alive\r\n\r\n"); // minimum set of required URL headers
    delay(10);
    // Read all the lines of the response and print them to Serial
    Serial.println("Response: ");
    while(client.available()){
      String line = client.readStringUntil('\r');
      Serial.print(line);
    }
  }
  Serial.println("ESP8266 in sleep mode");
  ESP.deepSleep(sleepTimeSeconds * 1e6);
}

void loop() {
  // put your main code here, to run repeatedly:

  int reading = analogRead(sensorPin);
 // measure the 3.3v with a meter for an accurate value
 //In particular if your Arduino is USB powered
 float voltage = reading * 3.3;
 voltage /= 1024.0;
 
 // now print out the temperature
 float temperatureC = (voltage - 0.5) * 100;
 Serial.print(temperatureC);
 Serial.println(" degrees C");
 
 delay(1000);

}


Server Code:
Code: [Select]
/*------------------------------------------------------------------------------
  03/25/2019
  Author: Makerbro
  Platforms: ESP8266
  Language: C++/Arduino
  File: simple_server_client/server/server.ino
  ------------------------------------------------------------------------------
  Description:
  Server-side code for YouTube video demonstrating how to communicate over WiFi
  between two ESP8266:
  https://youtu.be/gZhUi24_qms
  Do you like my videos? You can support the channel:
  https://patreon.com/acrobotic
  https://paypal.me/acrobotic
  ------------------------------------------------------------------------------
  Please consider buying products from ACROBOTIC to help fund future
  Open-Source projects like this! We'll always put our best effort in every
  project, and release all our design files and code for you to use.
  https://acrobotic.com/
  https://amazon.com/shops/acrobotic
  ------------------------------------------------------------------------------
  License:
  Please see attached LICENSE.txt file for details.
------------------------------------------------------------------------------*/
// Setup the server to receive data over WiFi
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

// Configuration parameters for Access Point
char * ssid_ap = "ANY_SSID";
char * password_ap = "ANY_PASSWORD";
IPAddress ip(192,168,11,4); // arbitrary IP address (doesn't conflict w/ local network)
IPAddress gateway(192,168,11,1);
IPAddress subnet(255,255,255,0);

// Set up the server object
ESP8266WebServer server;

// Keep track of the sensor data that's going to be sent by the client
float sensor_value = 0.0;

void setup() {
  WiFi.mode(WIFI_AP);
  WiFi.softAPConfig(ip,gateway,subnet);
  WiFi.softAP(ssid_ap,password_ap);
  // Print IP Address as a sanity check
  Serial.begin(115200);
  Serial.println();
  Serial.print("IP Address: "); Serial.println(WiFi.localIP());
  // Configure the server's routes
  server.on("/",handleIndex); // use the top root path to report the last sensor value
  server.on("/update",handleUpdate); // use this route to update the sensor value
  server.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  server.handleClient();
}

void handleIndex() {
  server.send(200,"text/plain",String(sensor_value)); // we'll need to refresh the page for getting the latest value
}

void handleUpdate() {
  // The value will be passed as a URL argument
  sensor_value = server.arg("value").toFloat();
  Serial.println(sensor_value);
  server.send(200,"text/plain","Updated");
 
}

tasmod

#1
Apr 13, 2019, 11:50 am Last Edit: Apr 13, 2019, 11:53 am by tasmod
Server/Client aside. 

If you have put either to sleep then the wifi is asleep (off).  One wakes when the rtc triggers it locally, the other is still asleep so cannot respond. 

I can't see how you can then wake the other unless it is in sync to wake up for a period around the same time?

Maybe use ntp time poll when awake on each, to sync the rtc for next wake up time ?

KevinRoach

Ok! Great idea! I could on both setup a rel time clock and set times for the readings. Server side start a little sooner and could stay on a little longer to catch the transfer. The server on the bank is not as big as an issue as the client in the water for battery life. Maybe even thinking solar panel on both to charge the batteies or just change it out every so often.

Thanks,
Kevin

tasmod

Yep, rtc cheap and easy to use.  Get the server to sync the client say twice a day and they should stay within a couple of seconds of each other. Probably much better time than that anyway.

Go Up