sever-client

孟跃
2023-12-01

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = “Maker.pro”;
const char* password = “87654321”;

//Your IP address or domain name with URL path
const char* serverNameTemp = “http://192.168.4.1/temperature”;
const char* serverNameAlti = “http://192.168.4.1/altitude”;
const char* serverNamePres = “http://192.168.4.1/pressure”;

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

String temperature;
String altitude;
String pressure;

unsigned long previousMillis = 0;
const long interval = 5000;

void setup() {
Serial.begin(115200);

// Address 0x3C for 128x64, you might need to change this value (use an I2C scanner)
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F(“SSD1306 allocation failed”));
for(;; // Don’t proceed, loop forever
}
display.clearDisplay();
display.setTextColor(WHITE);

WiFi.begin(ssid, password);
Serial.println(“Connecting”);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}

void loop() {
unsigned long currentMillis = millis();

if(currentMillis - previousMillis >= interval) {
// Check WiFi connection status
if(WiFi.status()== WL_CONNECTED ){
temperature = httpGETRequest(serverNameTemp);
altitude = httpGETRequest(serverNameAlti);
pressure = httpGETRequest(serverNamePres);
Serial.println(“Temperature: " + temperature + " *C - Altitude: " + altitude + " m - Pressure: " + pressure + " hPa”);

  display.clearDisplay();
  
  // display temperature
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("T: ");
  display.print(temperature);
  display.print(" ");
  display.setTextSize(1);
  display.cp437(true);
  display.write(248);
  display.setTextSize(2);
  display.print("C");
  
  // display altitude
  display.setTextSize(2);
  display.setCursor(0, 25);
  display.print("A: ");
  display.print(altitude);
  display.setTextSize(1);
  display.setCursor(110, 30);
  display.print(" m"); 
  
  // display pressure
  display.setTextSize(2);
  display.setCursor(0, 50);
  display.print("P:");
  display.print(pressure);
  display.setTextSize(1);
  display.setCursor(110, 56);
  display.print("hPa");
       
  display.display();
  
  // save the last HTTP GET Request
  previousMillis = currentMillis;
}
else {
  Serial.println("WiFi Disconnected");
}

}
}

String httpGETRequest(const char* serverName) {
HTTPClient http;

// Your IP address with path or Domain name with URL path
http.begin(serverName);

// Send HTTP POST request
int httpResponseCode = http.GET();

String payload = “–”;

if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();

return payload;
}

//
// Import required libraries
#include “WiFi.h”
#include “ESPAsyncWebServer.h”

#include <Wire.h>
#include <Adafruit_Sensor.h>
//#include <Adafruit_BME280.h>
#include <Adafruit_BMP085.h>
// Set your access point network credentials
const char* ssid = “Maker.pro”;
const char* password = “87654321”;

Adafruit_BMP085 bmp;

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

String readTemp() {
return String(bmp.readTemperature());
//return String(1.8 * bme.readTemperature() + 32);
}

String readAlti() {
return String(bmp.readAltitude());
}

String readPres() {
return String(bmp.readPressure() / 100.0F);
}

void setup(){
// Serial port for debugging purposes
Serial.begin(115200);
Serial.println();

// Setting the ESP as an access point
Serial.print(“Setting AP (Access Point)…”);
// Remove the password parameter, if you want the AP (Access Point) to be open
WiFi.softAP(ssid, password);

IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);

server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, “text/plain”, readTemp().c_str());
});
server.on("/altitude", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, “text/plain”, readAlti().c_str());
});
server.on("/pressure", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, “text/plain”, readPres().c_str());
});

bool status;

status = bmp.begin();
if (!status) {
Serial.println(“Could not find a valid BMp180 sensor, check wiring!”);
while (1);
}

// Start server
server.begin();
}

void loop(){

}

 类似资料: