Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
RAGUL M 1560912Anshul Shajijoshua johnsonC P ADARSH
Published © CC0

Comptroller Box: IoT Based Smart Switchboard

Primarily a possible approach of a smart switchboard to control/operate any traditional electrical gadgets remotely using a web application.

IntermediateShowcase (no instructions)Over 30 days3,938
Comptroller Box: IoT Based Smart Switchboard

Things used in this project

Hardware components

WIZ750SR
WIZnet WIZ750SR
×1
Arduino UNO
Arduino UNO
×1
WIZwiki-W7500
WIZnet WIZwiki-W7500
×1
General Purpose Dual Op-Amp
Texas Instruments General Purpose Dual Op-Amp
×1
Resistor 10k ohm
Resistor 10k ohm
×2
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Relay (generic)
×4
Transformer
×1
Inverter
×1
Multipin plug board
×1
Temperature Sensor
Temperature Sensor
×1
network router
×1
Digital display board
×1

Software apps and online services

Mbed OS
ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

WIZ750SR CONNECTION TO ARDUINO UNO

Comptroller and WIZ Boards connection

Workflow

Code

Connecting the database with Arduino:

PHP
<?php
   
    $dbusername = "arduino"; 
    
    $dbpassword = "arduinotest";
    
    $server = "localhost"; 

    $dbconnect = mysql_pconnect($server, $dbusername, $dbpassword);
    $dbselect = mysql_select_db("test",$dbconnect);

    $sql = "INSERT INTO test.sensor (value) VALUES ('".$_GET["value"]."')";    

    mysql_query($sql);

?>

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
 
// Enter the IP address for Arduino, as mentioned we will use 192.168.0.16
// Be careful to use , insetead of . when you enter the address here
IPAddress ip(192,168,0,16);

int photocellPin = 0;  // Analog input pin on Arduino we connected the SIG pin from sensor
int photocellReading;  // Here we will place our reading

char server[] = "192.168.0.11"; // IMPORTANT: As we are using XAMPP we had to find out the IP address of the computer and put it here.

// Initialize the Ethernet server library
EthernetClient client;

void setup() {
 
  // Serial.begin starts the serial connection between computer and Arduino
  Serial.begin(9600);
 
  // start the Ethernet connection
  Ethernet.begin(mac, ip);
    
}

void loop() {
 
  photocellReading = analogRead(photocellPin); // Fill the sensorReading with the information from sensor
 
  // Connect to the server (your computer or web page)  
  if (client.connect(server, 80)) {
    client.print("GET /write_data.php?"); // This
    client.print("value="); // This
    client.print(photocellReading); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
    client.println(" HTTP/1.1"); // Part of the GET request
    client.println("Host: 192.168.0.11"); // IMPORTANT: As we are using XAMPP we had to find out the IP address of the computer and put it here.
    client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
    client.println(); // Empty line
    client.println(); // Empty line
    client.stop();    // Closing connection to server

  }

  else {
    // If Arduino can't connect to the server (your computer or web page)
    Serial.println("--> connection failed\n");
  }
 
  // the server is given some time to receive the data and store it say 10 seconds. Be advised when delaying. Using a short delay might lead the server unable to capture data because of Arduino transmitting new data too soon.
  delay(10000);
}

LED operations

PHP
<?php
$onoroff = $_GET["state"];
$textfile = "LEDstate.txt"; 
 
$fileLocation = "$textfile";
$fh = fopen($fileLocation, 'w   ') or die("Something went wrong!"); 

$stringToWrite = "$onoroff"; 
fwrite($fh, $stringToWrite); 
fclose($fh); 
 
header("Location: index.html");
?>

Wiz750SR Connectivity Source Code

C/C++
#include "mbed.h"
#include "EthernetInterface.h"
#include "Adafruit_SSD1306.h"
#include "HTTPClient.h"


#if defined(TARGET_WIZWIKI_W7500)||defined(TARGET_WIZWIKI_W7500P)
    uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x53, 0xAE, 0x90};
#endif
Serial pc(USBTX, USBRX);
Serial device(D1,D0);
EthernetInterface eth;

// Declare TCP Connection Class
TCPSocketConnection sock;

int main() {
   int val;
   int i=0;
   char c;
   
char D[1000];
   
   int phy_link;
    printf("Wait a second...\r\n");

   eth.init(mac_addr);     //Use DHCP

printf("Check Ethernet Link\r\n");
    /*while(1) //Wait link up
    {
        if(eth.link() == true) 
            break;
    }*/
    printf("Link up\r\n");

    eth.connect();
    
      printf("IP Address is %s\r\n\r\n", eth.getIPAddress());
        printf("MASK Address is %s\r\n\r\n", eth.getNetworkMask());
        printf("GATEWAY Address is %s\r\n\r\n", eth.getGateway());
        printf("MAC Address is %s\r\n\r\n", eth.getMACAddress());
        while(1){
             while(device.readable())
        {   
        
            D[i]=device.getc();
           // char c= device.getc();
            pc.printf("%c",D[i]);
            c = D[i];
            i++;
            
            
        }
        val = int(c - 48);
        //printf("%d",val);
        // TCP socket connect to openweather server 
        //TCPSocketConnection sock;
        char send_data[1024];
        int CDS_data=val;
        sock.connect("api.thingspeak.com", 80);
        
         sprintf(send_data, "GET https://api.thingspeak.com/update?api_key=WO0QPMWOHQ7HTZMM&field1=%d HTTP/1.0\n\n",CDS_data);
        // GET method, to request weather forecast  
       // char http_cmd[] = "GET /update?api_key=0Q8S3KCULT7GUDVR&field1=2000 HTTP/1.0\n\n";
        
           
        sock.send_all(send_data, sizeof(send_data)-1);
        
        // get data into buffer
        char buffer[2048];
        int ret;
        while (true) {
            ret = sock.receive(buffer, sizeof(buffer)-1);
            if (ret <= 0)
                break;
            buffer[ret] = '\0';
            printf("Received %d chars from server: %s\n", ret, buffer);     
        }
        printf("\r\n\r\n");
      }
}

WIZwiki-W7500 Source Code

C/C++
#include "mbed.h"
#include "EthernetInterface.h"
#include "FsHandler.h"
#include "HTTPServer.h"
#include "SDFileSystem.h"
#include "SoftwareSerial.h"


SoftwareSerial mySerial(10,11);//RX, TX

#if defined(TARGET_WIZWIKI_W7500)||defined(TARGET_WIZWIKI_W7500P)
    //Choose one of file system.
    SDFileSystem local(SD_MOSI, SD_MISO, SD_CLK, SD_SEL, "local");//PB_3, PB_2, PB_1, PB_0
    //LocalFileSystem local("local");
#endif

#if defined(TARGET_WIZWIKI_W7500)||defined(TARGET_WIZWIKI_W7500P)
    uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0xFF, 0xFF, 0x02};
#endif

// Declare Ethernet Class
EthernetInterface eth;

// Declare HTTP Server Class
HTTPServer  svr;

// Set Server Network
char ip_addr[] = "169.254.80.156";
char subnet_mask[] = "255.255.255.0";
char gateway_addr[] = "192.168.240.1";


int main()
{
    HTTPFsRequestHandler::mount_eth(&eth);
    HTTPFsRequestHandler::mount("/local/", "/");
    svr.addHandler<HTTPFsRequestHandler>("/");

#if defined(TARGET_WIZWIKI_W7500)||defined(TARGET_WIZWIKI_W7500P)
    
    #ifdef DHCP
        eth.init(mac_addr); //Use DHCP
    #else
        eth.init(mac_addr, ip_addr, subnet_mask, gateway_addr); //Not Use DHCP
    #endif

#else

    #ifdef DHCP
        eth.init(); //Use DHCP
    #else
        eth.init(ip_addr, subnet_mask, gateway_addr); //Not Use DHCP
    #endif

#endif

    printf("Check Ethernet Link\r\n");
    while(1) //Wait link up
    {
        if(eth.link() == true) 
            break;
    }
    printf("Link up\r\n");

    eth.connect();
    printf("Server IP Address is %s\r\n", eth.getIPAddress());

    if (!svr.start(80, &eth)) {

        error("Server not starting !");
        exit(0);
    }

    while(1) {
        svr.poll();
    }
    
}

Credits

Anshul Shaji

Posted by RAGUL M 1560912

Comments