Search code examples
c#serverraspberry-piuwphome-automation

Handling gpio value using localhost


i am making a Universal windows Application for home automation, the app UI and hardware interfacing is ready and working i'm using raspberry pi 2 to serve as gpio and i plan to use the same app on my windows phone to trigger the ON/OFF request over same local network

the GPIO Hardware interfacing is done and is working

i'm stuck at a point "How do i communicate between my phone app and raspberry pi?" they are connected to same wifi network i am a C#/.Net person, and any solution available over internet is for Python/Java.


Solution

  • There are several solutions for your question but none will involve only C# on the raspberry side. What I did (is one of the solutions available but you can choose to use pyton, for example) was to use Node.JS and run a server on a raspberry pi and connect to that server (it could be any type of server TCP server, telnet, web, etc.) and connect from C# to that server and then interact with your GPIO normally. There are libraries in Node.JS (https://www.npmjs.com/package/pi-gpio) to control GPIO so you can write code to handle a connection from your C# app and when connected turn on/off a certain GPIO.

    Something like the following will create a TCP server on the raspberry pi and from your C# program you will need to connect to that IP (raspberry IP) and port (1337) and do whatever you want to do.

    It is worth the time it will take learn Node.JS (it won't be complicated if you already know C# but it will take time to get used to the syntaxis but you can do so much after you know how it works).

    var net = require('net'); 
    
    var server = net.createServer(function(socket) {
        socket.write('Echo server\r\n');
        socket.pipe(socket);
    });
    
    server.listen(1337, '127.0.0.1');