hello comunity
i have make one sketch for reading data to display inverter,
this is one logger uart,
comunicate with dspic33 and receive all value displayed.
i have add one code for displaying to one little oled direct.
and one other wemos mini d1 connected to wifi have one other lcd for reading in other side the value inverter.
//code Cent0 by Github whit help Cattledog
// Import required libraries
#define BLYNK_TEMPLATE_ID "TMPL4UgRsEkn1"
#define BLYNK_TEMPLATE_NAME "Quickstart Device"
#define BLYNK_AUTH_TOKEN "nhBpLQaJ"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <Arduino.h> //
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <Hash.h>
#include <ESPAsyncTCP.h>
//#include "ESPAsyncWebServer.h"
#include <ESPAsyncWebServer.h>
// #include <BetterOTA.h> // my code
#include <SoftwareSerial.h>
SoftwareSerial SSerial(4, 5); // 2 3
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <BlynkSimpleEsp8266.h>
// SCL GPIO5
// SDA GPIO4
#define OLED_RESET 0 // GPIO0
Adafruit_SSD1306 display(OLED_RESET);
float ProdI;
float VDc;
int VoltAc;
int Watt;
float VDC;
int Temp;
int WattHome;
int WattHome0;
BLYNK_WRITE(V2)
{
int pinValue = WattHome0; // assigning incoming value from pin V1 to a variable
Serial.println(pinValue);
//Blynk.virtualWrite(V2, WattHome0);
}
String readwatthome() {
return String(WattHome0);
}
String readHumi() {
return String(ProdI); // return String(PROD ISTANTANEA;
}
String readdc() {
return String(VDc); // aggiunto; String VDC ;
}
String readPres() {
return String(VoltAc);
}
const char* TEXT_INPUT1 = "HTML_STR_INPUT1"; // String type input
const char* TEXT_INPUT2 = "HTML_INT_INPUT2"; // Integer type input
const char* TEXT_INPUT3 = "HTML_FLOAT_INPUT3"; //Float type input
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v6.2.1/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<style>
html {
font-family: Arial;
display: inline-block;
margin: 0px auto;
text-align: center;
}
h2 { font-size: 2.0rem; }
p { font-size: 2.0rem; }
.units { font-size: 1.2rem; }
.dht-labels{
font-size: 1.5rem;
vertical-align:middle;
padding-bottom: 15px;
}
</style>
</head>
<body>
<h2>GTIL2 Energy Terrazza</h2>
<p>
<i class="fa-solid fa-bolt"></i>
<span class="dht-labels">Consumo</span>
<span id="watthome">%TEMPERATURE%</span>
<span class="units">W</span>
</p>
<p>
<i class="fa-solid fa-solar-panel"style="color:#059e8a;" ></i>
<span class="dht-labels">Volt Inverter DC</span>
<span id="voltdc">%VOLTDC%</span>
<span class="units">V</span>
</p>
<p>
<i class="fa-solid fa-plug"></i>
<span class="dht-labels">Volt Inverter AC</span>
<span id="voltac">%VOLTAC%</span>
<span class="units">V</span>
</p>
<p>
<i class="fa-light fa-solar-panel"></i>
<span class="dht-labels">Produzione</span>
<span id="prodi">%PRODI%</span>
<span class="units">W</span>
</p>
<form action="/get">
Enter string Input Here: <input type="text" name="HTML_STR_INPUT1">
<input type="submit" value="Submit">
</form><br>
<form action="/get">
Enter Integer Input Here: <input type="text" name="HTML_INT_INPUT2">
<input type="submit" value="Submit">
</form><br>
<form action="/get">
Enter Float Input Here: <input type="text" name="HTML_FLOAT_INPUT3">
<input type="submit" value="Submit">
</form>
</body>
<script>
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("watthome").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/watthome", true);
xhttp.send();
}, 10000 ) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("voltdc").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/voltdc", true);
xhttp.send();
}, 10000 ) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("voltac").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/voltac", true);
xhttp.send();
}, 10000 ) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("prodi").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/prodi", true);
xhttp.send();
}, 10000 ) ;
</script>
</html>)rawliteral";
// Replaces placeholder with DHT values
String processor(const String& var){
//Serial.println(var);
if(var == "TEMPERATURE"){
return String(WattHome0);
}
else if(var == "VOLTDC"){
return String(VDc);
}
else if(var == "VOLTAC"){
return String(VoltAc);
}
else if(var == "PRODI"){
return String(ProdI);
}
return String();
}
//int buffer[40];
const byte startMarker = 0x94;
const byte messageLength = 21; //18
byte receivedBytes[messageLength + 1]; //include start marker
boolean newData = false;
boolean newData2 = false;
boolean newData3 = false;
static boolean recvInProgress = false;
// Set your access point network credentials
//const char* ssid = "";//ESP8266-Access-Point
//const char* password = "";//123456789
char* ssid = "";//ESP8266-Access-Point
char* password = "";//123456789
char* pass = "";//12345678
// Set your Static IP address
IPAddress local_IP(192, 168, , );
// Set your Gateway IP address
IPAddress gateway(192, 168, , );
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8); //optional
//IPAddress secondaryDNS(8, 8, 4, 4); //optional
//String WattHome
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
void setup(){ // SETUP
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
// Serial port for debugging purposes
Serial.begin(500000); // 115200 500000
SSerial.begin(9600);
Serial.println();
// tolto messo nel loop
// 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
if (!WiFi.config(local_IP, gateway, subnet)) { // , primaryDNS, secondaryDNS
Serial.println("STA Failed to configure");
}
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(5000);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html, processor);
});
server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){ //______________________
request->send_P(200, "text/plain", readwatthome().c_str());
});
server.on("/watthome", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", String(WattHome0).c_str());
});
server.on("/voltdc", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", String(VDc).c_str());
});
server.on("/voltac", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", String(VoltAc).c_str());
});
server.begin();
server.on("/prodi", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", String(ProdI).c_str());
});
// Send a GET request to <ESP_IP>/get?input1=<inputMessage>
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage;
String inputParam;
// GET input1 value on <ESP_IP>/get?input1=<inputMessage>
if (request->hasParam(TEXT_INPUT1)) {
inputMessage = request->getParam(TEXT_INPUT1)->value();
inputParam = TEXT_INPUT1;
}
// GET input2 value on <ESP_IP>/get?input2=<inputMessage>
else if (request->hasParam(TEXT_INPUT2)) {
inputMessage = request->getParam(TEXT_INPUT2)->value();
inputParam = TEXT_INPUT2;
}
// GET input3 value on <ESP_IP>/get?input3=<inputMessage>
else if (request->hasParam(TEXT_INPUT3)) {
inputMessage = request->getParam(TEXT_INPUT3)->value();
inputParam = TEXT_INPUT3;
}
else {
inputMessage = "No Input Text sent on ESP32";
inputParam = "none";
}
Serial.println(inputMessage);
request->send(200, "text/html", "HTTP GET request sent to your ESP on input field ("
+ inputParam + ") with value: " + inputMessage +
"<br><a href=\"/\">Return to Home Page</a>");
});
// server.onNotFound(notFound);
server.begin();
// OTACodeUploader.begin(); // call this method if you want the code uploader to work
// OTATerminal.begin(); // call this method if you want the terminal to work
// WiFi.softAP(ssid, password);
// IPAddress IP = WiFi.softAPIP();
// Serial.print("AP IP address: ");
// Serial.println(IP);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 64x48)
// init done
display.display();
delay(50);
}
void recvBytesWithStartMarker()
{
newData2 = false;
static boolean recvInProgress = false;
static int ndx = 0;
byte rc;
while (SSerial.available() > 0 && newData == false)
{
rc = SSerial.read();
if (recvInProgress == true)
{
receivedBytes[ndx] = rc;
// if (ndx == 8 and rc != 0x05)
// if ((ndx == 12 and rc == 0x04) and (ndx == 21 and rc != 0x34))
if (ndx == 21 and rc != 0x34)
{
// if(ndx == 12 and rc != 0x00)
recvInProgress = false;
ndx = 0; //new data2 false da aggiungere
//__________________________________________________________________________________
// Serial.println("This just in false FALSE FALSE FALSE");
// for (byte i = 0; i <= messageLength; i++)
// {
// Serial.print(receivedBytes[i], HEX);
// Serial.print(" ");
// }
// Serial.println(" ");
// Serial.println("BYTE 21 ");
// Serial.println (receivedBytes[21], HEX);
// Serial.println("byte15 ");
// Serial.println (receivedBytes[15], HEX);
//____________
recvInProgress = false; /////////////////////////////////cambiato true//////////////////////////////////////
newData2 = false; /////////////////////////////////cambiato true//////////////////////////////////////
newData = false; // aggiunto _________________________
newData3 = false;
}
else
ndx++;
if (ndx == messageLength + 1)
// if (receivedBytes[ndx] = rc)
{
newData = true;
recvInProgress = true; // false
ndx = 0;
// receivedBytes[ndx] = rc; // non cambia nulla resta tutto uguale
Serial.println("data RC ok CHECK byte 15=0 & 21=34 ");
Serial.println (receivedBytes[15], HEX);
Serial.println (receivedBytes[21], HEX);
receivedBytes[ndx] = rc;
if ((receivedBytes[15] == 0x00) && (receivedBytes[21] == 0x34))
{
Serial.println("VERIFIED successfull");
recvInProgress = true;
// newData2 = true; /////////////////////////////////cambiato true//////////////////////////////////////
newData = true;
newData3 = true;
}
}
}
else if ( rc == 0x94 || rc == 0x95 || rc == 0x96 || rc == 0x93 )
{
recvInProgress = true;
ndx = 0; // save start marker
receivedBytes[ndx] = rc;
ndx++;
}
}
}
void showNewData()
{
// if ((newData == true) and (recvInProgress == true))
if (newData == true)
{
Serial.println("This just in ...OK void show new data ");
for (byte i = 0; i <= messageLength; i++)
{
Serial.print(receivedBytes[i], HEX);
Serial.print(" "); // IN LINEA
}
Serial.println(" ");
newData = false;
newData2 = true;
}
}
void collectData()
{
WattHome = (((int32_t)receivedBytes[15] << 24) |((int32_t)receivedBytes[16] <<16) |
((int32_t)receivedBytes[17] << 8 ) |(int32_t)receivedBytes[18])/10;
if( (WattHome < -40) || (WattHome > 3000)){
Serial.println(WattHome);
newData3 = false;
}
else
newData3 = true;
WattHome0 = WattHome ;
VoltAc = ((receivedBytes[1]*256+receivedBytes[2])/10);
ProdI = ((receivedBytes[3]*256+receivedBytes[4])*0.1);
VDc = ((receivedBytes[5]*256+receivedBytes[6])*0.125); // /8
Temp = ((receivedBytes[13]*256+receivedBytes[14])/24);
Serial.println(WattHome);
Serial.println(WattHome0);
}
void printNewData()
{
Serial.println("INVERTER LOG");
}
void printNewData2()
{
Serial.println(VoltAc);
Serial.println(ProdI);
Serial.println(VDc);
Serial.println(Temp);
Serial.println(WattHome0);
Serial.println("INVERTER LOG GTIL2 Cent0 Github");
}
void displayNewData()
{
}
void funzione()
{
server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){ //______________________
request->send_P(200, "text/plain", readwatthome().c_str());
});
server.on("/voltdc", HTTP_GET, [](AsyncWebServerRequest *request){ //___________________
request->send_P(200, "text/plain", readHumi().c_str());
});
server.on("/vdc", HTTP_GET, [](AsyncWebServerRequest *request){ //___________________
request->send_P(200, "text/plain", readdc().c_str());
});
server.on("/pressure", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", readPres().c_str()); //____________________
});
server.begin();
// newData2 = false;
// Change Web Link Button message to "Congratulations!"
Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
Blynk.setProperty(V3, "onImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}
void controllo8(void) { // OTA O T A ##################################################################### O T A O T A
// Port defaults to 8266
ArduinoOTA.setPort(8266);
// Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setHostname("GTIL2_DISPLAY");
// No authentication by default
// ArduinoOTA.setPassword("admin");
// Password can be set with it's md5 value as well
// MD5(admin) = 21
// ArduinoOTA.setPasswordHash("21");
// if (WiFi.waitForConnectResult() != WL_CONNECTED ){
(ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_FS
type = "filesystem";
}
Serial.println("\nStart");
// NOTE: if updating FS this would be the place to unmount FS using FS.end()
}));
//}
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address OTA: ");
Serial.println(WiFi.localIP());
newData3 = false; // AGGIUNTO PER CHIUDERE IL CICLO
}
void controllo9(void)
{
WiFiClient client;
HTTPClient http;
int TEXT ;
TEXT = 60;
TEXT= atoi((char *)TEXT_INPUT1); //_
if (WattHome0 > TEXT ){ // 650
http.begin(client, "http://192.168../api/relay/1?apikey=3Exxxxxxxxxxxxxxxxxx&value=0");
// start connection and send HTTP header
int httpCode = http.GET();
http.end();
}
Serial.println("SEND STOP espurna API");
Blynk.virtualWrite(V2, WattHome0);
newData3 = false;
}
void loop()
{
recvBytesWithStartMarker();
showNewData();
Blynk.run();
// if (newData2 == true)
// {
if (newData3 == true)
{
collectData();
// printNewData();
printNewData2();
// displayNewData();
funzione();
controllo8();
}
// controllo9();
// controllo8();
// ArduinoOTA.handle();
//BetterOTA.handle();
// if (millis() - timer1 > 200) {
// 1 second have passed
// do stuff
// timer1 = millis(); // reset timer, so it will run again after one more second.
// }
}
this is any function with my data.
how pass this data to virtual pin ?
i have test this code this code is added one line to any function but not work
Blynk.virtualWrite(V2, WattHome0);
but have problem with connection to cloud
void setup(){ // SETUP
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);
a this time connection to cloud is ok
how call the
BLYNK_WRITE(V2)
?
if have the code
void recvBytesWithStartMarker()
i use in the loop this code
void loop()
{
recvBytesWithStartMarker();
i need use this for call in the loop? BLYNK_WRITE(V2);
one little sketch for my code is this
i have start the code for send my data to blynk
```
BLYNK_WRITE(V2)
{
int pinValue = WattHome0; // assigning incoming value from pin V1 to a variable
Serial.println(pinValue);
//Blynk.virtualWrite(V2, WattHome0);
}
```
a this point the code is very long have the page for view data to intranet.