SHUBHAM NARKHEDE
Published

Smart Grid Power System 2.0

# smart_grid_2.0 Smart Grid Power System 2.0 -An up-gradation to the present system

IntermediateShowcase (no instructions)16
Smart Grid Power System 2.0

Story

Read more

Code

grid.ino

Arduino
upload it on NodeMCU for current sensing and sending it to the cloud.
#include <ESP8266WebServer.h>
#include <WiFiClientSecure.h>
#include <WiFiClient.h>
#include <ESP8266WiFi.h>

#include <ThingSpeak.h>



#include <Wire.h>
#include <Adafruit_ADS1015.h>

Adafruit_ADS1015 ads;     /* Use thi for the 12-bit version */

double offsetI;
double filteredI;
double sqI,sumI;
int16_t sampleI;
double Irms;
char ssid[] = "ssid";
char pass[] = "pass";
WiFiClient client;
unsigned long myChannelNumber = 7****; //Your Channel Number (Without Brackets)
 
const char* myWriteAPIKey = "*********"; //Your Write API Key
unsigned long ChannelNumber = ******; //Your Channel Number (Without Brackets)
 
const char* WriteAPIKey = "***********"; //Your Write API Key
double calcIrms(unsigned int Number_of_Samples)
{
   
  /* Be sure to update this value based on the IC and the gain settings! */
  double multiplier =0.05F;    /* ADS1115 @ +/- 4.096V gain (16-bit results) */
  for (unsigned int n = 0; n < Number_of_Samples; n++)
  {
    sampleI = ads.readADC_Differential_0_1();
    //Serial.println(sampleI);
   // delay(100);
    

    // Digital low pass filter extracts the 2.5 V or 1.65 V dc offset, 
  //  then subtract this - signal is now centered on 0 counts.
    offsetI = (offsetI + (sampleI-offsetI)/1024);
    
    filteredI = sampleI - offsetI;
    //filteredI = sampleI * multiplier;

    // Root-mean-square method current
    // 1) square current values
    sqI = filteredI * filteredI;
    // 2) sum 
    sumI += sqI;
  }
  
  
  Irms = sqrt(sumI / Number_of_Samples)*multiplier; 

  //Reset accumulators
  sumI = 0;
//--------------------------------------------------------------------------------------       
 
  return Irms;
}


const int sub=D3;
const int dom=D4 ;
const int street=D5 ;
const int motor1=D6 ;
const int motor2=D7 ;

void setup() {
  
Serial.begin(115200);
  ads.setGain(GAIN_TWO);        // 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV
  ads.begin();
  
  // Connect to WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid,pass);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
   Serial.print("connected: ");
  Serial.println(WiFi.localIP());
 
 
 
ThingSpeak.begin(client);
 
  // Setup GPIO
// relay pin is set as output
  pinMode(sub,INPUT);
  pinMode(dom,INPUT);
  pinMode(street,INPUT);
  pinMode(motor1,INPUT);
  pinMode(motor2,INPUT);
  
 

}   

  double p1=10;  
  double p2=10;
  double p3=10;
  double p4=8;
  double p5=50;
  
// loop() runs continuously, it's our infinite loop.
void loop(){
   
    
  double current = calcIrms(1024);
  delay(500);
   double power =(250*current); // w
   double p=p1-p2 ;
   double ps_dom=p2-p3 ;
   ThingSpeak.setField( 1, (long)p);
   ThingSpeak.setField( 3, (long)ps_dom);
   ThingSpeak.writeFields(ChannelNumber,WriteAPIKey); //Update in ThingSpeaK
   delay(5000);
   
   if(digitalRead(sub) == LOW){
  ThingSpeak.setField( 1, (long)power);
ThingSpeak.setField( 2, (long)p2);
ThingSpeak.setField( 3, (long)p3);
ThingSpeak.setField( 6, (long)p4);
ThingSpeak.setField( 7, (long)p5);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
Serial.println("make it any");
delay(5000); // ThingSpeak will only accept updates every 15 seconds.
  
  
  p1=power ;
  
   }
   else if(digitalRead(dom) == LOW){
    ThingSpeak.setField( 1, (long)p1);
ThingSpeak.setField( 2, (long)power);
ThingSpeak.setField( 3, (long)p3);
ThingSpeak.setField( 6, (long)p4);
ThingSpeak.setField( 7, (long)p5);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
Serial.println("make it anyD");
delay(5000); // ThingSpeak will only accept updates every 15 seconds.
     p2=power ;
   }
   else if(digitalRead(street) == LOW){
    ThingSpeak.setField( 1, (long)p1);
ThingSpeak.setField( 2, (long)p2);
ThingSpeak.setField( 3, (long)power);
ThingSpeak.setField( 6, (long)p4);
ThingSpeak.setField( 7, (long)p5);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
Serial.println("make it anyS");

delay(5000); // ThingSpeak will only accept updates every 15 seconds.
     p3=power ;
   }
  else if(digitalRead(motor1) == LOW){
    ThingSpeak.setField( 1, (long)p1);
ThingSpeak.setField( 2, (long)p2);
ThingSpeak.setField( 3, (long)p3);
ThingSpeak.setField( 6, (long)power);
ThingSpeak.setField( 7, (long)p5);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
Serial.println("make it anyM1");

delay(5000); // ThingSpeak will only accept updates every 15 seconds.
     p4=power ;
   }
   else if(digitalRead(motor2) == LOW){
    ThingSpeak.setField( 1, (long)p1);
ThingSpeak.setField( 2, (long)p2);
ThingSpeak.setField( 3, (long)p3);
ThingSpeak.setField( 6, (long)p4);
ThingSpeak.setField( 7, (long)power);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
Serial.println("make it anyM2");

delay(20000); // ThingSpeak will only accept updates every 15 seconds.
     p5=power ;
   }
   else{
    Serial.println("no measurement");
   }
  }

switch

Arduino
upload it on Arduino for load distribution
#include <Wire.h>
#include <Adafruit_ADS1015.h>

Adafruit_ADS1015 ads;     /* Use thi for the 12-bit version */

double offsetI;
double filteredI;
double sqI,sumI;
int16_t sampleI;
double Irms;

double calcIrms(unsigned int Number_of_Samples)
{
   
  /* Be sure to update this value based on the IC and the gain settings! */
  double multiplier =0.05F;    /* ADS1115 @ +/- 4.096V gain (16-bit results) */
  for (unsigned int n = 0; n < Number_of_Samples; n++)
  {
    sampleI = ads.readADC_Differential_0_1();
    //Serial.println(sampleI);
   // delay(100);
    

    // Digital low pass filter extracts the 2.5 V or 1.65 V dc offset, 
  //  then subtract this - signal is now centered on 0 counts.
    offsetI = (offsetI + (sampleI-offsetI)/1024);
    
    filteredI = sampleI - offsetI;
    //filteredI = sampleI * multiplier;

    // Root-mean-square method current
    // 1) square current values
    sqI = filteredI * filteredI;
    // 2) sum 
    sumI += sqI;
  }
  
  
  Irms = sqrt(sumI / Number_of_Samples)*multiplier; 

  //Reset accumulators
  sumI = 0;
//--------------------------------------------------------------------------------------       
 
  return Irms;
}


const int more_posi=8;
const int more_nega=9 ;
const int shut=10 ;


void setup() {
  
Serial.begin(9600);
  ads.setGain(GAIN_TWO);        // 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV
  ads.begin();
  
 
  // Setup GPIO
// relay pin is set as output
  pinMode(more_posi,OUTPUT);
  pinMode(more_nega,OUTPUT);
  pinMode(shut,OUTPUT);
   digitalWrite(more_posi,LOW);
     digitalWrite(more_nega,LOW);
     digitalWrite(shut,LOW); 
  
 

}   

// loop() runs continuously, it's our infinite loop.
void loop(){
   
    
  double current = calcIrms(1024);
  delay(500);
   double power =(250*current); // w
  Serial.println(power);
  if(power > 50.00)
  {
    digitalWrite(more_posi,HIGH);
     digitalWrite(more_nega,HIGH);
     digitalWrite(shut,LOW); 
  }
  else if(power < 1.00)
  {
   digitalWrite(shut,HIGH); 
   digitalWrite(more_posi,LOW);
     digitalWrite(more_nega,LOW);
  }
  else{
    digitalWrite(shut,LOW); 
   digitalWrite(more_posi,LOW);
     digitalWrite(more_nega,LOW);
  }
}

rank_database.py

Python
Run this for generating database and also don't forget to create your .db file ie database in your system and then run this code replacing the name of the database of yours.
import pandas as pd
import sqlite3

# stock ticker symbol
url1 = "https://api.thingspeak.com/channels/73*****/fields/6.csv?api_key=*******"
url2 = "https://api.thingspeak.com/channels/738300/fields/7.csv?api_key=********"


# import data with pandas
data = pd.read_csv(url1)
data1 = pd.read_csv(url2)
req_data = str(max(data['field6']))
req_data1 = str(max(data1['field7']))
reqq_data = [req_data,req_data1]
appliance_name = ["Shakti Pumps","Bhakti Pumps"]

con=None
cursor=None
for i in range(2):
    try:
                
        con=sqlite3.connect("your_database_name.db")
        sql = "insert into appliances(Appliance_Name,Power) values('%s','%s')"
        args = (appliance_name[i],reqq_data[i])
        cursor = con.cursor()
        cursor.execute(sql % args)
        con.commit()
        print(cursor.rowcount , "Records inserted")
    except Exception as e:
        print("Add issue",e)
        con.rollback()
    finally:
        if cursor is not None:
                cursor.close()
        if con is not None:
                con.close();

while True:
    opt = int(input("1 View Record,2 Exit"))
    if opt == 1:
        con = None
        cursor = None
        try:
                        con = sqlite3.connect("your_database_name.db")
                        sql = "select distinct Appliance_Name,Power from appliances order by (Power) asc"
                        cursor = con.cursor()
                        cursor.execute(sql)
                        data = cursor.fetchall()
                        df = pd.read_sql_query(sql, con)
                        #df = pd.DataFrame(df1, columns= ['Appliance Name','Power(Watt)'])
                        export_excel = df.to_excel (r'mention the directory to save the excel database here\Appliance_Rankings.xlsx', index = None, header=True)
                        for d in data:
                                
                                print(d[0],d[1])
        except Exception as e:
                        print("select issue",e)
        finally:
                        if cursor is not None:
                                cursor.close()
                        if con is not None:
                                con.close();
    elif opt == 2:
                break
    else:
                print("Invalid Option")
    

Credits

SHUBHAM NARKHEDE
1 project • 0 followers

Comments