/**
pinMode(ledPin2, OUTPUT);
const int ledPin2 = 2;
char big_data[60000] = "val="; //Объявляет большой буфер и задаёт начальное значение
sprintf(...) //Добавляет в конец форматированную строку
memset(big_data, 0, sizeof(big_data)); //Обнуляет (очищает) строку
strcat(big_data, "val="); //Снова начинает строку с "val=" после очистки
char letter = 'X';
int number = 42;
float temp = 23.4567;
char name[] = "ESP32";
char buffer[100];
sprintf(buffer, "Char: %c, Int: %d, Float: %.2f, String: %s", letter, number, temp, name);
float some_data = %.2f
millis() = %d
String name = "ESP32";
sprintf(buffer, "Name: %s", name.c_str());
String name = "ESP32";
name.reserve(5000); // резервируем память для предотвращения фрагментации!
*/
#include <HTTPClient.h>
#include <WiFi.h>
#include <HardwareSerial.h>
HardwareSerial BMS(1);
String response_str_7025; // ответ от бмс
int loop_counter = 0;
byte response_array_7025[512]; // массив байтов ответа
int responseLength = 0; // для подсчета длины ответа
String big_data = ""; // данные для отправки
String vout;
String iout;
String power_out;
String vin;
String ah_total;
String wh_total;
String outer_temp;
String inner_temp;
void wifi_connection(){
//WiFi.begin("netis_kitchen", "password");
WiFi.begin("netis_7AB60F", "password");
//WiFi.begin("netis_2.4G_007C9D", "d11v09n03");
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
WiFi.setHostname("solar_esp");
Serial.print("OK! IP=");
Serial.println(WiFi.localIP());
}
void send_http(){
Serial.println("send_http_func");
// Указываем URL и данные для POST запроса
HTTPClient http;
http.begin("https://fuguja.com/esp/solar.php");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Отправляем POST запрос
float httpResponseCode = http.POST(big_data);
String payload;
if (httpResponseCode == 200) {
payload = http.getString();
}
http.end();
}
void setup() {
response_str_7025.reserve(3000);
response_str_7025 = "";
big_data.reserve(80000); // резервируем 5000 бит
vout.reserve(10);
iout.reserve(10);
power_out.reserve(10);
vin.reserve(10);
ah_total.reserve(10);
wh_total.reserve(10);
outer_temp.reserve(10);
inner_temp.reserve(10);
// для бмс біло 16 17 для 7025 -
BMS.begin(115200, SERIAL_8N1, 16, 17);
big_data = "val=";
delay(1000);
Serial.begin(115200);
wifi_connection();
}
void loop() {
Serial.println(loop_counter);
loop_counter++;
if(loop_counter == 10){
send_http(); // отправка
Serial.println(big_data);
loop_counter = 0;
big_data = "val=";
}
byte ask[] = {
0x01, 0x03, 0x00, 0x02,
0x00, 0x0D, 0x25, 0xCF
};
BMS.write(ask, sizeof(ask));
delay(200); // даём BMS время ответить
responseLength = 0;
response_str_7025 = "";
while (BMS.available()) {
byte b = BMS.read();
if (responseLength < sizeof(response_array_7025)){
response_str_7025 += "0";
response_str_7025 += String(b, HEX);
response_str_7025 += " ";
response_array_7025[responseLength++] = b; // запись битов в массив
}
}
/*
01 03 1A
01 0D 00 B3 00 30 05 C1 00 6C 00 00 01 22 00 00 00 00 00 03 00 27 00 EA 22 B8 B8 A9
*/
vout = String((response_array_7025[3] << 8) | response_array_7025[4]); // дели на 100
iout = String((response_array_7025[5] << 8) | response_array_7025[6]); // дели на 100
power_out = String((response_array_7025[7] << 8) | response_array_7025[8]); // трехзнач дели на 100
vin = String((response_array_7025[9] << 8) | response_array_7025[10]);
// 17 18 15 16
ah_total = String((response_array_7025[13] << 24) | (response_array_7025[14] << 16) | (response_array_7025[11] << 8) | response_array_7025[12]);
wh_total = String((response_array_7025[17] << 24) | (response_array_7025[18] << 16) | (response_array_7025[15] << 8) | response_array_7025[16]);
inner_temp = String((response_array_7025[25] << 8) | response_array_7025[26]);
big_data += "{v:"+vout+"i:"+iout+"p:"+power_out+"vin:"+vin+"ah:"+ah_total+"wh:"+wh_total+"ot:"+outer_temp+"it:"+inner_temp+"wf:"+String(WiFi.RSSI())+"ms:"+String(millis())+"}"+response_str_7025;
delay(5000); // 10 секунд пауза перед следующим запросом
}