本站改版新增arduino频道
arduino wifi联网 httppost 上报数据
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <WiFi.h>
#include <HTTPClient.h>
Adafruit_BMP280 bmp;
const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";
const char* serverName = "http://yourserver.com/api";
void setup() {
Serial.begin(9600);
if (!bmp.begin(0x76)) {
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
}
void loop() {
float temperature = bmp.readTemperature();
float pressure = bmp.readPressure() / 100.0;
String url = serverName;
url += "?temperature=" + String(temperature);
url += "&pressure=" + String(pressure);
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
delay(5000);
}
Copyright © 2014 ESP56.com All Rights Reserved
晋ICP备14006235号-22 晋公网安备14108102001165号
执行时间: 0.0095388889312744 seconds