#include <LiquidCrystal_I2C_Hangul.h>
#include <WiFi.h>
#include "ThingsBoard.h"
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
#define lenght 16.0
double percent = 100.0;
unsigned char b;
unsigned int peace;
// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C_Hangul lcd(0x27, lcdColumns, lcdRows);
// custom charaters
byte p1[8] = {
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10,
0x10};
byte p2[8] = {
0x18,
0x18,
0x18,
0x18,
0x18,
0x18,
0x18,
0x18};
byte p3[8] = {
0x1C,
0x1C,
0x1C,
0x1C,
0x1C,
0x1C,
0x1C,
0x1C};
byte p4[8] = {
0x1E,
0x1E,
0x1E,
0x1E,
0x1E,
0x1E,
0x1E,
0x1E};
byte p5[8] = {
0x1F,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F};
// WiFi network name and password:
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* token ="Hs5In450J8WbpyB72KJF";
const char* cloud = "thingsboard.cloud";
WiFiClient espClient;
ThingsBoard tb(espClient);
void setup() {
Serial.begin(115200);
delay(100);
// connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
}
lcd.init();
lcd.backlight();
lcd.createChar(0, p1);
lcd.createChar(1, p2);
lcd.createChar(2, p3);
lcd.createChar(3, p4);
lcd.createChar(4, p5);
// set up the LCD's number of columns and rows:
lcd.begin(lcdColumns, lcdRows);
pinMode(32, INPUT);
}
void loop() {
if (!tb.connected()) {
// Connect to the ThingsBoard
Serial.print("Connecting to: ");
Serial.print(cloud);
Serial.print(" with token ");
Serial.println(token);
if (!tb.connect(cloud, token)) {
Serial.println("Failed to connect");
return;
}
}
int val = analogRead(32);
float volt = val * (5.0 / 4096);
percent = val/4096.0*100.0;
Serial.println(val);
// voltage display
lcd.setCursor(0, 0);
Serial.print("Voltage:");
lcd.print("Voltage:");
lcd.setCursor(8, 0);
Serial.print(volt);
lcd.print(volt);
lcd.setCursor(12, 0);
Serial.println("V");
lcd.print("V");
// bargraph display
lcd.setCursor(0,1);
double a=lenght/100*percent;
// drawing black rectangles on LCD
if (a>=1) {
for (int i=1;i<a;i++) {
lcd.write(4);
b=i;
}
a=a-b;
}
peace=a*5;
// drawing charater's colums
switch (peace) {
case 0:
break;
case 1:
break;
case 2:
lcd.write(1);
break;
case 3:
lcd.write(2);
break;
case 4:
lcd.write(3);
break;
}
// clearing line
for (int i =0;i<(lenght-b);i++) {
lcd.print(" ");
}
tb.sendTelemetryFloat("Voltage:", volt);
tb.loop();
delay(100);
}