Carlos Andres Galvez Calderon
Created March 18, 2016 © GPL3+

UPS IoT

designed to compensate a solar energy source, protecting the system attached to it by switching the source to comercial at the right time.

IntermediateShowcase (no instructions)5 hours2,921
UPS IoT

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
×1
Relay (generic)
×1
resistor
×1
1N4007 – High Voltage, High Current Rated Diode
1N4007 – High Voltage, High Current Rated Diode
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Story

Read more

Schematics

3.3v to 5v

This 3.3v to 5v circuit is connected from arduino digital pin to relay control pin to make it properly work.

Protection circuit and Voltage divider (10:1)

Protection circuit and Voltage divider (10:1), to read analog voltages from 0 to 33V

Arduino wiring diagram

It is the wiring diagram of how the MKR1000 interact with the instrumentation. Reading analog voltages and making things happen trough relays

Code

UPS IoT

Arduino
It is the main program that is running on the arduino
#include <WiFi101.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiSSLClient.h>
#include <WiFiUdp.h>

/*
 * This example is modified from the original file 
 * https://github.com/arduino-libraries/WiFi101/blob/master/examples/SimpleWebServerWiFi/SimpleWebServerWiFi.ino
 By Carlos Andres Galvez Calderon Mar/2016
 */
#include <SPI.h>
#include <WiFi101.h>

char ssid[] = "";      //  your network SSID (name)
char pass[] = "";      // your network password

int keyIndex = 0;      // your network key Index number (needed only for WEP)
int ledpin = 6;
int k1 = 7;
int k3 = 8;

String tipo = "auto";
String alim = "Panel";

//Time
unsigned long currentMicros;
unsigned long previousMicros;
unsigned long elapsedTime;

byte hundredths;
byte tenths;
byte secondsOnes;
byte oldSecondsOnes;
byte secondsTens;
byte minutesOnes = 0;
byte minutesTens = 0;
byte hoursOnes = 0;
byte hoursTens = 0;
int conteo = 0;

//AnalogRead 1
long past=0;
long actu=0;
float vsis = 14;
float cuen=0;
int tot=0;
float prom=0;
float vsist=14;

//AnalogRead 2
float vsis2 = 14;
float cuen2=0;
float prom2=0;
float vsist2=14;

int status = WL_IDLE_STATUS;
WiFiServer server(80);





void setup() {
  Serial.begin(9600);           // initialize serial communication
  
  pinMode(ledpin, OUTPUT);      // set the pin mode
  pinMode(k1, OUTPUT);
  pinMode(k3, OUTPUT);
  
  // Check for the presence of the shield
  Serial.print("WiFi101 shield: ");
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("NOT PRESENT");
    return; // don't continue
  }
  Serial.println("DETECTED");
  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    digitalWrite(ledpin, LOW);
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);
    digitalWrite(ledpin, HIGH);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status
  digitalWrite(ledpin, HIGH);
  past=millis();
}





void loop() {
  //AnalogRead
  AnlgRead();  

  //auto/manual change control
  automanchange();

  //Server power ON
  servOn();   
 }





void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}





void AnlgRead() {
  actu=millis();
  if((actu-past)>=10){ //One sample per 0.01 seg
    vsis = analogRead(A3);
    vsis = vsis *33;  //
    vsis=vsis/1023;   // Convertion factor
    vsis=vsis+(vsis*1.89/100); //Compensation operation
    cuen=cuen+vsis;            //Fill a total variable

    vsis2 = analogRead(A3);
    vsis2 = vsis2 *33;
    vsis2=vsis2/1023;
    vsis2=vsis2+(vsis2*1.89/100);
    cuen2=cuen2+vsis2;
    
    tot=tot+1;     //Make the count of the samples
    past=actu;
  }
  if(tot>=100){    // if the samples are equal or more of 100 then
    prom=cuen/tot; // make the average of the samples
    prom2=cuen2/tot;
    tot=0;
    cuen=0;
    cuen2=0;
    vsist=prom;
    vsist2=prom2;
  }
}





void automanchange(){
  if (tipo == "auto"){ 
   if (conteo==1){     //if the mode set is auto and the trigger is up then stert the count
    currentMicros = micros();

  // how long's it been?
  elapsedTime = currentMicros - previousMicros;
  if ( elapsedTime >=10000UL){  // 0.01 second passed? Update the timers
    elapsedTime = 0;
    previousMicros  = previousMicros + 10000UL;
    hundredths = hundredths+1;
    if (hundredths == 10){
      hundredths = 0;
      tenths = tenths +1;
      if (tenths == 10){
        tenths = 0;
        secondsOnes = secondsOnes + 1;
        if (secondsOnes == 10){
          secondsOnes = 0;
          secondsTens = secondsTens +1;
          if (secondsTens == 6){ 
            secondsTens = 0;
            minutesOnes =  minutesOnes + 1;
            if (minutesOnes == 10){
              minutesOnes = 0;
              minutesTens = minutesTens +1;
              if (minutesTens == 6){
                minutesTens = 0;
                hoursOnes = hoursOnes +1;
                if (hoursOnes == 10){
                  hoursOnes = 0;
                  hoursTens = hoursTens =1;
                  if (hoursOnes == 4 && hoursTens ==2){
                    hoursOnes = 0;
                    hoursTens = 0;
                  }
                }
              } // minutesTens rollover check
            } // minutesOnes rollover check
          } // secondsTens rollover check
        } // secondsOnes rollover check
      } // tenths rollover check
    } // hundredths rollover check
  } // hundredths passing check

  if (hoursOnes == 0 && hoursTens ==1) { //after 10 hours, the system come back to solar source
    
    digitalWrite(k1, LOW);
    conteo=0;

    minutesOnes = 0;
    minutesTens = 0;
    hoursOnes = 0;
    hoursTens = 0;
    secondsTens = 0;
    secondsOnes = 0;        
    alim="Panel";
   }  
  }
  else {
    if (vsist<12) { //If the system have a voltage source under 12v (trigger) the source change to comercial current
    digitalWrite(k1, HIGH);       
    conteo=1;
    currentMicros = micros();
    previousMicros = currentMicros;
    alim="Corriente comercial";
   }
  }  
 }
}





void servOn (){
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");  // the connection will be closed after completion of the response
            client.println("Refresh: 30");  // refresh the page automatically every 30 sec          
            client.println();
                        
            client.println("<!DOCTYPE HTML>");
            client.println("<html>");
          
            client.print("<img src='http://i1075.photobucket.com/albums/w432/carlos_and/descarga_zpsevu35lll.png'>");
            client.print("<br><br><h1>Horqueta station</h1>");          
            client.print("<br><br>The actual power source is: ");
            client.print(alim);
            client.print("<br>The change of the power source is: ");
            client.print(tipo);
            client.print("<br><br>The voltage in of the system is: ");
            client.print(vsist);
            client.print(" +- 0.05 V");
            client.print("<br><br>The voltage of the comercial current is: ");
            client.print(vsist2);
            client.print(" +- 0.05 V");
          
            client.print("<br><br>The total amount of time that the batteries have been charging is: ");
            client.print (hoursTens);
            client.print(hoursOnes);
            client.print(":");
            client.print(minutesTens);
            client.print(minutesOnes);
            client.print(":");
            client.print(secondsTens);          
            client.print(secondsOnes);
                   
            client.print("<br><br><br>Click <a href=\"/E\">here</a> to make the source change 'auto' <br>");
            client.print("Click <a href=\"/T\">here</a> to make the source change 'manual' <br>");
            client.print("Click <a href=\"/H\">here</a> to make use of the comercial current<br>");
            client.print("Click <a href=\"/L\">here</a> to make use of the solar panel<br>");
            client.print("Click <a href=\"/K\">here</a> to power on the relay 1<br>");
            client.print("Click <a href=\"/U\">here</a> to power off the relay 1<br>");
          
            client.println("</html>");
            break;            
            }
            else {      // if you got a newline, then clear currentLine:
              currentLine = "";
            }
          }
          else if (c != '\r') {    // if you got anything else but a carriage return character,
            currentLine += c;      // add it to the end of the currentLine
          }        
          if (currentLine.endsWith("GET /E")) {   // GET /E Make the source change 'auto'
            tipo="auto";          
          }
          if (currentLine.endsWith("GET /T")) {   // GET /T Make the source change 'manual'
            tipo="manual";
          }        
          if (currentLine.endsWith("GET /H")) {   // GET /H Make use of the comercial current
            if(tipo=="manual"){
            digitalWrite(k1, HIGH);               
            alim="Corriente comercial";
            conteo=1;
            }
          }
          if (currentLine.endsWith("GET /L")) {   // GET /L Make use of the solar panel
            if(tipo=="manual"){
            digitalWrite(k1, LOW);               
            alim="Panel";
            conteo=0;
            minutesOnes = 0;
            minutesTens = 0;
            hoursOnes = 0;
            hoursTens = 0;
            secondsTens = 0;
            secondsOnes = 0;
            }              
          }        
          if (currentLine.endsWith("GET /K")) {   // GET /K power on the relay 1
            digitalWrite(k3, LOW);                
          }
          if (currentLine.endsWith("GET /U")) {   // GET /U power off the relay 1
            digitalWrite(k3, HIGH);                
          }        
      }   
    }
      // close the connection:
      client.stop();
      Serial.println("client disonnected");
   }
}

UPS_IoT

Universal Windows App (UWA) for Uninterruptible power supply (UPS), proved on Arduino UNO through serial communication

Credits

Carlos Andres Galvez Calderon

Carlos Andres Galvez Calderon

1 project • 1 follower
Electronics enthusiast Engineering student Universidad del Valle Cali, Colombia

Comments