User Tools

Site Tools


haxx:projects:sbc:piserverclient

PiServer/PiClient C/C# communications

About

This is an about page for using the current Pi Server. It is using the C GPIO lib WiringPi.

I have tested this with components from the Adeept Ultimate Starter Kit

PiServer

To compile:

gcc -o PiServer PiServer.c -lwiringPi

The Pi Server currently accepts input for pins 0 - 3

The server expects input from one client in the format of (pin, high/low)

Pins are referenced by letter… 0 = A; 1 = B; 2 = C; 3 = D

examples: AH; CL; BH;

IP address will need to be configured for personal use

Contact

Feel free to contact me with any questions

ynniv1178@gmail.com

Code

Source and download follows (click the PiServer.c link at the top of the code block to download):

PiServer.c
//Pi server
 
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <wiringPi.h>
 
#define Pin0 0 
#define Pin1 1
#define Pin2 2 
#define Pin3 3 
 
int initializeGPIO ()
{
    printf ("Initializing GPIO!\n");
    if (wiringPiSetup () == -1)
    {
        printf ("Setup wiringPi failed!\n");
        return (-1);
    }
 
    pinMode (Pin0, OUTPUT);
    pinMode (Pin1, OUTPUT);
    pinMode (Pin2, OUTPUT);
    pinMode (Pin3, OUTPUT);
 
    printf ("GPIO Initialized!\n");
    return 0;
}	
 
//This is the important function for controlling the PI pins
void setPin (char Pin, char OnOff)
{
    int PinNumber;
 
    if (Pin == 'A')
    {
        PinNumber = Pin0;
    }
 
    if (Pin == 'B')
    {
	PinNumber = Pin1;
    }
 
    if (Pin == 'C')
    {
	PinNumber = Pin2;
    }
 
    if (Pin == 'D')
    {
	PinNumber = Pin3;
    }
 
 
    if (OnOff == 'H')
    {
	digitalWrite (PinNumber, HIGH);
    }
 
    if (OnOff == 'L')
    {
	digitalWrite (PinNumber, LOW);
    }
}
 
void runServer ()
{
 
    printf ("Initializing Server!\n");
    int udpSocket, nBytes;
    char buffer[1024];
    struct sockaddr_in serverAddr;
    struct sockaddr_storage serverStorage;
    socklen_t addr_size;
 
    udpSocket = socket (PF_INET, SOCK_DGRAM, 0);
 
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons (8001);
    serverAddr.sin_addr.s_addr = inet_addr ("192.168.0.11");
    memset (serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  
 
    bind (udpSocket, (struct sockaddr *) &serverAddr, sizeof (serverAddr));
 
    addr_size = sizeof (serverStorage);
 
    printf ("Running Server!\n");
    int Running = 1;
    while (Running == 1)
    {
        nBytes = recvfrom (udpSocket, buffer, 1024, 0, (struct sockaddr *)&serverStorage, &addr_size);
 
        for (int i = 0;i < nBytes -1; i++)
            buffer[i] = toupper (buffer[i]);     
 
        setPin (buffer[0], buffer[1]);
    }
}
 
int main ()
{    
    int gpioCheck = initializeGPIO();
    if (gpioCheck == -1)
        return -1;
 
    runServer ();
 
    return (0);
}

PiClient

CSharpPiClient.cst
//This is a simple C# client
//The server is currently configured to accept one message at a time
//The server is expecting/using chars it seems to work with less hassle
//The client is currently configured to send one message at a time
 
//To send a message enter AA
 
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
 
class Program
{
	static void Main(string[] args)
	{
	    Boolean done = false;
	    Boolean exceptionThrown = false;
 
	    Socket sendingSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 
	    //You will want to know the IP address of your PI
	    IPAddress sendToAddress = IPAddress.Parse("192.168.0.11");
	    //Pick a port 8001 is usually fine
	    IPEndPoint sendingEndPoint = new IPEndPoint(sendToAddress, 8001);
	    Console.WriteLine("Enter text to broadcast via UDP.");
	    Console.WriteLine("Enter a blank line to exit the program.");
 
	    while (!done)
	    {
		Console.WriteLine("Enter text to send, blank line to quit");
		string textToSend = Console.ReadLine();
		if (textToSend.Length == 0)
		{
		    done = true;
		}
		else
		{	
		//This may be the only "odd" or unexpected thing
		//
		byte[] sendBuffer = new byte[505];
		sendBuffer[0] = Convert.ToByte(textToSend[0]);
		sendBuffer[1] = Convert.ToByte(textToSend[1]);
 
		Console.WriteLine("Sending to address: {0} port: {1}", sendingEndPoint.Address, sendingEndPoint.Port );
		try
		{
		    sendingSocket.SendTo(sendBuffer, sendingEndPoint);
		}
		catch (Exception sendException )
		{
		    exceptionThrown = true;
		    Console.WriteLine(" Exception {0}", sendException.Message);
		}
 
		if (exceptionThrown == false)
		{
		    Console.WriteLine("Message has been sent to the broadcast address");
		}
		else
		{
		    exceptionThrown = false;
                    Console.WriteLine("The exception indicates the message was not sent.");
                }
            }
	} 
    }
}
haxx/projects/sbc/piserverclient.txt · Last modified: 2018/02/18 05:00 by wedge