I am trying to connect my nodemcu client with nodemcu server, while my client is connected with two sensors (DHT11 and Soil Moisture) and it is workign perfectly, but on the server side I am unable to get all values, it is only giving me value of one parameter. Server Side Code
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
String apiWritekey = "PBD5LAJC4BSV3OKC"; // replace with your THINGSPEAK WRITEAPI key here
const char* ssid = "Dhamaal";// your wifi SSID name
const char* password = "03334204224" ;// wifi pasword
///~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~///
///~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~///
const char* httpserver = "api.thingspeak.com";
ESP8266WebServer server;
WiFiClient client;
// Configuration parameters for Access Point
char * ssid_ap = "ESP_AP";
char * password_ap = "123456789";
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
// Keep track of the sensor data that's going to be sent by the client
float t1=0.0;
float h1=0.0;
float s1=0.0;
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(ip,gateway,subnet);
WiFi.softAP(ssid_ap,password_ap);
// 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 handleIndex() {
server.send(200,"text/plain",String(t1));
server.send(200,"text/plain",String(h1));
server.send(200,"text/plain",String(s1));// we'll need to refresh the page for getting the latest value
}
void handleUpdate() {
// The value will be passed as a URL argument
t1 = server.arg("value").toFloat();
h1 = server.arg("value").toFloat();
s1 = server.arg("value").toFloat();
delay(100);
WiFi.begin(ssid, password);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("");
Serial.print("NodeMcu connected to wifi...");
Serial.println(ssid);
Serial.println();
Serial.println(WiFi.localIP() );
delay(1000);
///~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~///
float t2 = t1;
float h2 = h1;
float s2 = s1;
if (client.connect(httpserver,80))
{
String tsData = apiWritekey;
tsData+="&field1=";
tsData+=String(t2);
tsData+="&field2=";
tsData+=String(h2);
tsData +="&field3=";
tsData += String(s2);
tsData += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");//command to update
client.print("Host: api.thingspeak.com\n");//host name here thingspeak.com
client.print("Connection: close\n");//closing connection
client.print("X-THINGSPEAKAPIKEY: "+apiWritekey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n"); // the 2 carriage returns indicate closing of Header fields & starting of data
client.print(tsData);
Serial.print("Temp: ");
Serial.print(t2);//printing on serial monitor
Serial.print("Humi: ");
Serial.print(h2);
Serial.print("Soil: ");
Serial.print(s2);
Serial.println("uploaded to Thingspeak server....");//dialogue box
//}
digitalWrite(D4, LOW);
client.stop();//stopping client
Serial.println("Waiting to upload next reading...");
Serial.println();
delay(5000);
} }
void loop() {
server.handleClient();
}
while the code of my client is
#include <DHT.h>
#include <ESP8266WiFi.h>
// Initialize sensor parameters
// Initialize network parameters
const char* ssid = "ESP_AP";
const char* password = "123456789";
const char* host = "192.168.11.4"; // as specified in server.ino
#define DHTPIN 0 //pin where the dht11 is connected
DHT dht(DHTPIN, DHT11);
// Set up the client objet
WiFiClient client;
// Configure deep sleep in between measurements
const int sleepTimeSeconds = 2;
void setup() {
// Connect to the server
WiFi.begin(ssid,password);
Serial.begin(115200);
dht.begin();
while(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("IP Address (AP): "); Serial.println(WiFi.localIP());
void loop() {
// put your main code here, to run repeatedly:
while (WiFi.status() == WL_CONNECTED) {
delay(500);
int s = analogRead(A0);
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print("Temperature: "); Serial.println(t);
Serial.print("Humidity: "); Serial.println(h);
Serial.print("Soil Moisture: "); Serial.println(s);
// Connect to the server and send the data as a URL parameter
if(client.connect(host,80)) {
String url = "/update?value=";
url += String(t);
String url1 = "/update?value=";
url1 += String(h);
String url2 = "/update?value=";
url2 += String(s);
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
client.print(String("GET ") + url1 + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" +
"Connection: keep-alive\r\n\r\n"); // minimum set of required URL headers
client.print(String("GET ") + url2 + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" +
"Connection: keep-alive\r\n\r\n"); // minimum set of required URL headers
delay(5000);
}
// 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);
}
}
}
Kindly help me to get all values at server side, Thanks