Hi Guys
I am trying to send data from a HTU21 sensor connected to an ESP8266 'server' over wifi and have it display the temperature and humidity on another ESP8266 (setup as a client) and OLED display.
I've come up with the following which works great at displaying the temperature but for the life of me, I can't get it to show the humidity information on the OLED.
I am guessing it's the way I am reading out the string or something and have googled the hell out of it trying to resolve myself or find other examples to guide me - all to no avail, so any help and guidance would be greatly appreciated.
Moving forward from this, I want to add other 'servers' to display temperatures, pH, ORP etc from my pools pump room and display that on the client also but just need to move in baby steps ![]()
Cheers
Hoops
This is the server code:
#include <SPI.h> // SD
#include <Wire.h> // I2C->OLED
#include <ESP8266WiFi.h> //
#include "Adafruit_HTU21DF.h"
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
WiFiServer server(80); // launches the server
IPAddress ip(192, 168, 1, 80); // fix IP of the server
IPAddress gateway(192, 168, 0, 1); // WiFi router's IP
IPAddress subnet(255, 255, 255, 0);
char ssid[] = "*****";
char pass[] = "*****";
unsigned long HTUtimer = 0;
unsigned long clientTimer = 0;
float h, t;
void setup() {
Serial.begin(115200); // only for debug
Wire.begin(); // default SDA and SCL
// server_start(0); // starts the WiFi server
delay(1000);
}
void loop() {
t = htu.readTemperature(); // reads the SHT for temperature
h = htu.readHumidity(); // reads the SHT for Humidity
// Listenning for new clients
WiFiClient client = server.available();
if (client) {
if (client.connected()) {
//}
client.println (t, 1);
client.println (h, 1);
Serial.print(t, 1); // Serial prints the temp
Serial.print(" ");
Serial.println(h, 1); // Serial prints the humidity
client.flush();
client.stop(); // disconnects the client
clientTimer = millis();
}
}
if (millis() - clientTimer > 30000) { // stops and restarts the WiFi server after 30 sec
WiFi.disconnect(); // idle time
delay(500);
server_start(1);
}
}
void server_start(byte restart) {
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
server.begin();
delay(500);
clientTimer = millis();
}
and here is the client code:
#include <Wire.h> // I2C->OLED
#include <ESP8266WiFi.h>
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
#define OLED_RESET LED_BUILTIN
SSD1306 display(0x3c, D3, D5);
IPAddress server(192, 168, 1, 80); // fix IP of the server
WiFiClient client;
char ssid[] = "******";
char pass[] = "*****";
unsigned long askTimer = 0;
String answer;
void setup() {
// Initialising the UI will init the display too.
Serial.begin(115200); // only for debug
display.init();
display.flipScreenVertically(); // allows screen to be flipped vertically.
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
}
void loop () {
if (millis() - askTimer > 2340) { // time between two connection to the server
client.connect(server, 80); // connects to the server
client.println("Haliho szerver!\r"); // trigger message to the server, its value is scrapped
answer = client.readStringUntil('\r'); // received the server's answer
// client.flush();
Serial.println(answer); // Serial prints the temp
client.flush();
display.clear();
display.setFont(ArialMT_Plain_24);
display.drawString(20, 10, answer + " C");
display.display();
askTimer = millis();
}
}