基于 esp8266 的磁场测量
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ArduinoHttpClient.h>
#include <Wire.h>
#include <HMC5883L.h>
// 创建一个HMC5883L对象
HMC5883L compass;
// WiFi的SSID和密码
const char *ssid = "xyygkj";
const char *password = "xyygkj8888";
// const char *ssid = "Xiaomi 12";
// const char *password = "12345678";
// 服务器地址和端口
char *serverAddres = "chart.iot.imlogic.cn";
int port = 80;
WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddres, port);
long lastMsgTime = 0; // 上次消息发送的时间
void wifi_init(); // WiFi初始化函数
void checkSettings(); // 检查HMC5883L设置的函数
void setup()
{
Serial.begin(9600); // 串口初始化
Wire.begin(D3, D4); // 初始化I2C
wifi_init(); // 初始化WiFi
Serial.println("Initialize HMC5883L");
while (!compass.begin()) // 初始化HMC5883L
{
Serial.println("Could not find a valid HMC5883L sensor, check wiring!");
delay(500);
}
// 设置HMC5883L的范围、测量模式、数据速率和采样数
compass.setRange(HMC5883L_RANGE_1_3GA);
compass.setMeasurementMode(HMC5883L_CONTINOUS);
compass.setDataRate(HMC5883L_DATARATE_15HZ);
compass.setSamples(HMC5883L_SAMPLES_1);
checkSettings(); // 打印设置信息
}
void loop()
{
long now = millis();
if (now - lastMsgTime > 2000) // 每隔2秒发送一次数据
{
lastMsgTime = now;
Vector raw = compass.readRaw(); //
Vector norm = compass.readNormalize(); //
// 打印原始数据
Serial.print(" Xraw = ");
Serial.print(raw.XAxis);
Serial.print(" Yraw = ");
Serial.print(raw.YAxis);
Serial.print(" Zraw = ");
Serial.print(raw.ZAxis);
Serial.print(" Xnorm = ");
Serial.print(norm.XAxis);
Serial.print(" Ynorm = ");
Serial.print(norm.YAxis);
Serial.print(" ZNorm = ");
Serial.print(norm.ZAxis);
Serial.println();
String contentType = "application/json";
// 计算raw
float RawResult = sqrt(raw.XAxis * raw.XAxis + raw.YAxis * raw.YAxis + raw.ZAxis * raw.ZAxis);
// 计算磁场强度
float NormResult = sqrt(norm.XAxis * norm.XAxis + norm.YAxis * norm.YAxis + norm.ZAxis * norm.ZAxis);
// 创建JSON数据
String data = "{\"Xraw\": " + String(raw.XAxis) + ",\"Yraw\": " + String(raw.YAxis) + ",\"Zraw\": " + String(raw.ZAxis) + ",\"Xnorm\": " + String(norm.XAxis) + ",\"Ynorm\": " + String(norm.YAxis) + ",\"Znorm\": " + String(norm.ZAxis) + ",\"RawResult\": " + String(RawResult) + ",\"NormResult\": " + String(NormResult) + "}";
client.post("/items/hmc5883l", contentType, data); // 发送POST请求
int statusCode = client.responseStatusCode(); // 获取响应状态码
String response = client.responseBody(); // 获取响应内容
}
}
void wifi_init()
{
WiFi.begin(ssid, password); // 连接WiFi
while (WiFi.status() != WL_CONNECTED) // 等待连接成功
{
delay(500);
Serial.println("Connecting to WiFi..\n");
}
Serial.println("Connected to the WiFi network\n");
}
void checkSettings()
{
Serial.print("Selected range: ");
switch (compass.getRange()) // 打印当前选择的范围
{
case HMC5883L_RANGE_0_88GA:
Serial.println("0.88 Ga");
break;
case HMC5883L_RANGE_1_3GA:
Serial.println("1.3 Ga");
break;
case HMC5883L_RANGE_1_9GA:
Serial.println("1.9 Ga");
break;
case HMC5883L_RANGE_2_5GA:
Serial.println("2.5 Ga");
break;
case HMC5883L_RANGE_4GA:
Serial.println("4 Ga");
break;
case HMC5883L_RANGE_4_7GA:
Serial.println("4.7 Ga");
break;
case HMC5883L_RANGE_5_6GA:
Serial.println("5.6 Ga");
break;
case HMC5883L_RANGE_8_1GA:
Serial.println("8.1 Ga");
break;
default:
Serial.println("Bad range!");
}
Serial.print("Selected Measurement Mode: ");
switch (compass.getMeasurementMode()) // 打印当前选择的测量模式
{
case HMC5883L_IDLE:
Serial.println("Idle mode");
break;
case HMC5883L_SINGLE:
Serial.println("Single-Measurement");
break;
case HMC5883L_CONTINOUS:
Serial.println("Continuous-Measurement");
break;
default:
Serial.println("Bad mode!");
}
Serial.print("Selected Data Rate: ");
switch (compass.getDataRate()) // 打印当前选择的数据速率
{
case HMC5883L_DATARATE_0_75_HZ:
Serial.println("0.75 Hz");
break;
case HMC5883L_DATARATE_1_5HZ:
Serial.println("1.5 Hz");
break;
case HMC5883L_DATARATE_3HZ:
Serial.println("3 Hz");
break;
case HMC5883L_DATARATE_7_5HZ:
Serial.println("7.5 Hz");
break;
case HMC5883L_DATARATE_15HZ:
Serial.println("15 Hz");
break;
case HMC5883L_DATARATE_30HZ:
Serial.println("30 Hz");
break;
case HMC5883L_DATARATE_75HZ:
Serial.println("75 Hz");
break;
default:
Serial.println("Bad data rate!");
}
Serial.print("Selected number of samples: ");
switch (compass.getSamples()) // 打印当前选择的采样数
{
case HMC5883L_SAMPLES_1:
Serial.println("1");
break;
case HMC5883L_SAMPLES_2:
Serial.println("2");
break;
case HMC5883L_SAMPLES_4:
Serial.println("4");
break;
case HMC5883L_SAMPLES_8:
Serial.println("8");
break;
default:
Serial.println("Bad number of samples!");
}
}
评论