Components and supplies
NodeMCU ESP8266 Breakout Board
INA219 DC current sensor
Arduino Pro Mini 328 - 5V/16MHz
0.96" i2c OLED display
Tools and machines
Soldering iron (generic)
Apps and platforms
Arduino IDE
Project description
Code
Code for Arduino
arduino
1#include <SoftwareSerial.h> 2#include <Wire.h> 3SoftwareSerial nodemcu(2,3); 4#include <Adafruit_INA219.h> 5#include <Adafruit_SSD1306.h> 6#include <SPI.h> 7long int data; 8 9#define OLED_RESET 4 10Adafruit_SSD1306 display(OLED_RESET); 11Adafruit_INA219 ina219; 12 13unsigned long previousMillis = 0; 14unsigned long interval = 100; 15float shuntvoltage = 0; 16float busvoltage = 0; 17float current_mA = 0; 18float loadvoltage = 0; 19float energy = 0; 20 21float sdata1 = 0; 22float sdata2 = 0; 23float sdata3 = 0; 24float sdata4 = 0; 25 26String cdata; 27 28void setup() 29{ 30Serial.begin(9600); 31nodemcu.begin(9600); 32 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 33 ina219.begin(); 34} 35 36 37void loop() 38{ 39 unsigned long currentMillis = millis(); 40 if (currentMillis - previousMillis >= interval) 41 { 42 previousMillis = currentMillis; 43 ina219values(); 44 displaydata(); 45 } 46if(nodemcu.available() == 0 ) 47{ 48 sdata1 = loadvoltage; 49 sdata2 = current_mA; 50 sdata3 = loadvoltage * current_mA; 51 sdata4 = energy; 52 53 54 cdata = cdata + sdata1+","+sdata2+","+sdata3+","+sdata4; 55 Serial.println(cdata); 56 nodemcu.println(cdata); 57 delay(1000); 58 cdata = ""; 59} 60} 61 62 63void displaydata() { 64 display.clearDisplay(); 65 display.setTextColor(WHITE); 66 display.setTextSize(1); 67 display.setCursor(0, 0); 68 display.println(loadvoltage); 69 display.setCursor(31, 0); 70 display.println("V"); 71 display.setCursor(62, 0); 72 display.setCursor(75, 0); 73 display.println(current_mA); 74 display.setCursor(110, 0); 75 display.println("mA"); 76 display.setCursor(0, 6); 77 display.println("--------------------"); 78 79 80 display.setCursor(101, 14); 81 display.println("A"); 82 display.setCursor(108, 17); 83 display.println("b"); 84 display.setCursor(114, 20); 85 display.println("i"); 86 display.setCursor(120, 23); 87 display.println("d"); 88 89 90 91 display.setCursor(0, 13); 92 display.println(loadvoltage * current_mA); 93 display.setCursor(57, 13); 94 display.println("mW"); 95 display.setCursor(0, 23); 96 display.println(energy); 97 display.setCursor(57, 23); 98 display.println("mWh"); 99 display.display(); 100} 101 102void ina219values() { 103 shuntvoltage = ina219.getShuntVoltage_mV(); 104 busvoltage = ina219.getBusVoltage_V(); 105 current_mA = ina219.getCurrent_mA(); 106 loadvoltage = busvoltage + (shuntvoltage / 1000); 107 energy = energy + loadvoltage * current_mA / 3600; 108} 109
Code for Nodemcu
arduino
1#define BLYNK_PRINT Serial 2 3 4#include <ESP8266WiFi.h> 5#include <BlynkSimpleEsp8266.h> 6#include <SoftwareSerial.h> 7#include <SimpleTimer.h> 8 9char auth[] = "nSC5994bDxGyYpYM4ds3sFeKoDSHBdKz"; 10 11// Your WiFi credentials. 12// Set password to "" for open networks. 13char ssid[] = "ABID"; 14char pass[] = "8901234567"; 15 16SimpleTimer timer; 17 18String myString; // complete message from arduino, which consistors of snesors data 19char rdata; // received charactors 20 21float firstVal, secondVal,thirdVal,forthVal; // 22// This function sends Arduino's up time every second to Virtual Pin (1). 23// In the app, Widget's reading frequency should be set to PUSH. This means 24// that you define how often to send data to Blynk App. 25void myTimerEvent() 26{ 27 // You can send any value at any time. 28 // Please don't send more that 10 values per second. 29 Blynk.virtualWrite(V1, millis() / 1000); 30 31} 32 33 34 35void setup() 36{ 37 // Debug console 38 Serial.begin(9600); 39 40 Blynk.begin(auth, ssid, pass); 41 42 timer.setInterval(1000L,sensorvalue1); 43 timer.setInterval(1000L,sensorvalue2); 44 timer.setInterval(1000L,sensorvalue3); 45 timer.setInterval(1000L,sensorvalue4); 46} 47 48void loop() 49{ 50 if (Serial.available() == 0 ) 51 { 52 Blynk.run(); 53 timer.run(); // Initiates BlynkTimer 54 } 55 56 if (Serial.available() > 0 ) 57 { 58 rdata = Serial.read(); 59 myString = myString+ rdata; 60 // Serial.print(rdata); 61 if( rdata == '\n') 62 { 63String l = getValue(myString, ',', 0); 64String m = getValue(myString, ',', 1); 65String n = getValue(myString, ',', 2); 66String o = getValue(myString, ',', 3); 67 68firstVal = l.toFloat(); 69secondVal = m.toFloat(); 70thirdVal = n.toFloat(); 71forthVal = o.toFloat(); 72 73 myString = ""; 74// end new code 75 } 76 } 77 78} 79 80void sensorvalue1() 81{ 82float sdata = firstVal; 83 // You can send any value at any time. 84 // Please don't send more that 10 values per second. 85 Blynk.virtualWrite(V2, sdata); 86 87} 88void sensorvalue2() 89{ 90float sdata = secondVal; 91 // You can send any value at any time. 92 // Please don't send more that 10 values per second. 93 Blynk.virtualWrite(V3, sdata); 94 95} 96 97void sensorvalue3() 98{ 99float sdata = thirdVal; 100 // You can send any value at any time. 101 // Please don't send more that 10 values per second. 102 Blynk.virtualWrite(V4, sdata); 103 104} 105void sensorvalue4() 106{ 107float sdata = forthVal; 108 // You can send any value at any time. 109 // Please don't send more that 10 values per second. 110 Blynk.virtualWrite(V5, sdata); 111 112} 113 114String getValue(String data, char separator, int index) 115{ 116 int found = 0; 117 int strIndex[] = { 0, -1 }; 118 int maxIndex = data.length() - 1; 119 120 for (int i = 0; i <= maxIndex && found <= index; i++) { 121 if (data.charAt(i) == separator || i == maxIndex) { 122 found++; 123 strIndex[0] = strIndex[1] + 1; 124 strIndex[1] = (i == maxIndex) ? i+1 : i; 125 } 126 } 127 return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; 128} 129
Code for Arduino
arduino
1#include <SoftwareSerial.h> 2#include <Wire.h> 3SoftwareSerial nodemcu(2,3); 4#include 5 <Adafruit_INA219.h> 6#include <Adafruit_SSD1306.h> 7#include <SPI.h> 8long 9 int data; 10 11#define OLED_RESET 4 12Adafruit_SSD1306 display(OLED_RESET); 13Adafruit_INA219 14 ina219; 15 16unsigned long previousMillis = 0; 17unsigned long interval = 100; 18float 19 shuntvoltage = 0; 20float busvoltage = 0; 21float current_mA = 0; 22float loadvoltage 23 = 0; 24float energy = 0; 25 26float sdata1 = 0; 27float sdata2 = 0; 28float 29 sdata3 = 0; 30float sdata4 = 0; 31 32String cdata; 33 34void setup() 35{ 36Serial.begin(9600); 37 38nodemcu.begin(9600); 39 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); 40 ina219.begin(); 41} 42 43 44void 45 loop() 46{ 47 unsigned long currentMillis = millis(); 48 if (currentMillis 49 - previousMillis >= interval) 50 { 51 previousMillis = currentMillis; 52 53 ina219values(); 54 displaydata(); 55 } 56if(nodemcu.available() == 0 57 ) 58{ 59 sdata1 = loadvoltage; 60 sdata2 = current_mA; 61 sdata3 = 62 loadvoltage * current_mA; 63 sdata4 = energy; 64 65 66 cdata = cdata 67 + sdata1+","+sdata2+","+sdata3+","+sdata4; 68 Serial.println(cdata); 69 70 nodemcu.println(cdata); 71 delay(1000); 72 cdata = ""; 73} 74} 75 76 77void 78 displaydata() { 79 display.clearDisplay(); 80 display.setTextColor(WHITE); 81 82 display.setTextSize(1); 83 display.setCursor(0, 0); 84 display.println(loadvoltage); 85 86 display.setCursor(31, 0); 87 display.println("V"); 88 display.setCursor(62, 89 0); 90 display.setCursor(75, 0); 91 display.println(current_mA); 92 display.setCursor(110, 93 0); 94 display.println("mA"); 95 display.setCursor(0, 6); 96 display.println("--------------------"); 97 98 99 100 display.setCursor(101, 14); 101 display.println("A"); 102 103 display.setCursor(108, 17); 104 display.println("b"); 105 display.setCursor(114, 106 20); 107 display.println("i"); 108 display.setCursor(120, 23); 109 display.println("d"); 110 111 112 113 114 display.setCursor(0, 13); 115 display.println(loadvoltage * current_mA); 116 117 display.setCursor(57, 13); 118 display.println("mW"); 119 display.setCursor(0, 120 23); 121 display.println(energy); 122 display.setCursor(57, 23); 123 display.println("mWh"); 124 125 display.display(); 126} 127 128void ina219values() { 129 shuntvoltage = ina219.getShuntVoltage_mV(); 130 131 busvoltage = ina219.getBusVoltage_V(); 132 current_mA = ina219.getCurrent_mA(); 133 134 loadvoltage = busvoltage + (shuntvoltage / 1000); 135 energy = energy + loadvoltage 136 * current_mA / 3600; 137} 138
Code for Nodemcu
arduino
1#define BLYNK_PRINT Serial 2 3 4#include <ESP8266WiFi.h> 5#include 6 <BlynkSimpleEsp8266.h> 7#include <SoftwareSerial.h> 8#include <SimpleTimer.h> 9 10char 11 auth[] = "nSC5994bDxGyYpYM4ds3sFeKoDSHBdKz"; 12 13// Your WiFi credentials. 14// 15 Set password to "" for open networks. 16char ssid[] = "ABID"; 17char pass[] 18 = "8901234567"; 19 20SimpleTimer timer; 21 22String myString; // complete 23 message from arduino, which consistors of snesors data 24char rdata; // received 25 charactors 26 27float firstVal, secondVal,thirdVal,forthVal; // 28// This function 29 sends Arduino's up time every second to Virtual Pin (1). 30// In the app, Widget's 31 reading frequency should be set to PUSH. This means 32// that you define how often 33 to send data to Blynk App. 34void myTimerEvent() 35{ 36 // You can send any 37 value at any time. 38 // Please don't send more that 10 values per second. 39 40 Blynk.virtualWrite(V1, millis() / 1000); 41 42} 43 44 45 46void setup() 47{ 48 49 // Debug console 50 Serial.begin(9600); 51 52 Blynk.begin(auth, ssid, pass); 53 54 55 timer.setInterval(1000L,sensorvalue1); 56 timer.setInterval(1000L,sensorvalue2); 57 58 timer.setInterval(1000L,sensorvalue3); 59 timer.setInterval(1000L,sensorvalue4); 60} 61 62void 63 loop() 64{ 65 if (Serial.available() == 0 ) 66 { 67 Blynk.run(); 68 timer.run(); 69 // Initiates BlynkTimer 70 } 71 72 if (Serial.available() > 0 ) 73 { 74 75 rdata = Serial.read(); 76 myString = myString+ rdata; 77 // Serial.print(rdata); 78 79 if( rdata == '\ 80') 81 { 82String l = getValue(myString, ',', 0); 83String 84 m = getValue(myString, ',', 1); 85String n = getValue(myString, ',', 2); 86String 87 o = getValue(myString, ',', 3); 88 89firstVal = l.toFloat(); 90secondVal = m.toFloat(); 91thirdVal 92 = n.toFloat(); 93forthVal = o.toFloat(); 94 95 myString = ""; 96// end new 97 code 98 } 99 } 100 101} 102 103void sensorvalue1() 104{ 105float sdata = firstVal; 106 107 // You can send any value at any time. 108 // Please don't send more that 10 109 values per second. 110 Blynk.virtualWrite(V2, sdata); 111 112} 113void sensorvalue2() 114{ 115float 116 sdata = secondVal; 117 // You can send any value at any time. 118 // Please don't 119 send more that 10 values per second. 120 Blynk.virtualWrite(V3, sdata); 121 122} 123 124void 125 sensorvalue3() 126{ 127float sdata = thirdVal; 128 // You can send any value at 129 any time. 130 // Please don't send more that 10 values per second. 131 Blynk.virtualWrite(V4, 132 sdata); 133 134} 135void sensorvalue4() 136{ 137float sdata = forthVal; 138 // 139 You can send any value at any time. 140 // Please don't send more that 10 values 141 per second. 142 Blynk.virtualWrite(V5, sdata); 143 144} 145 146String getValue(String 147 data, char separator, int index) 148{ 149 int found = 0; 150 int strIndex[] 151 = { 0, -1 }; 152 int maxIndex = data.length() - 1; 153 154 for (int i = 0; 155 i <= maxIndex && found <= index; i++) { 156 if (data.charAt(i) == separator 157 || i == maxIndex) { 158 found++; 159 strIndex[0] = strIndex[1] 160 + 1; 161 strIndex[1] = (i == maxIndex) ? i+1 : i; 162 } 163 } 164 165 return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; 166} 167
Downloadable files
powe_energy_bb_jeWHOYckNG.png
powe_energy_bb_jeWHOYckNG.png
Comments
Only logged in users can leave comments
abid_hossain
0 Followers
•0 Projects
Table of contents
Intro
6
0