I came across an iphone app where you can control the gpio pins on a raspberry pi. I am now trying to replicate this in a html format so that the gpio pins on a raspberry pi are controllable from a web page too and not just an iphone app.
the server side code is as follows
from twisted.inteet.protocol import Protocol, Factory
from twisted.inteet import reactor
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(7, GPIO.OUT) ## Setup GPIO Pin 7 to OUT
class RaspberryLight(Protocol):
def coectionMade(self):
#self.transport.write("""coected""")
self.factory.clients.append(self)
print "clients are ", self.factory.clients
def coectionLost(self, reason):
print "coection lost ", self.factory.clients
self.factory.clients.remove(self)
def dataReceived(self, data):
msg = ""
if (data == 'P7H'):
msg = "Pin 7 is now High"
GPIO.output(7, True)
elif (data == 'P7L'):
msg = "Pin 7 is now Low"
GPIO.output(7, False)
print msg
factory = Factory()
factory.protocol = RaspberryLight
factory.clients = []
reactor.listenTCP(5000, factory)
print "RaspberryLight server started"
reactor.run()
the iphone app client side of the code is as follows (viewcontroller.m)
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self initNetworkCommunication];
//used so that when you minimize the app it inits the the network again.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationEnteredForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
_lightToggle.selectedSegmentIndex = 1;
_logo.layer.coerRadius = _logo.frame.size.width / 2;
_logo.clipsToBounds = YES;
_logo.layer.borderColor = [[UIColor whiteColor] CGColor];
_logo.layer.borderWidth = 6;
// CAGradientLayer *gradient = [CAGradientLayer layer];
// gradient.frame = self.view.bounds;
// gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:(2/255.0f) green:(160/255.0f) blue:(224/255.0f) alpha:1] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
// [self.view.layer insertSublayer:gradient atIndex:0];
self.view.backgroundColor = [UIColor colorWithRed:(2/255.0f) green:(160/255.0f) blue:(224/255.0f) alpha:1];
}
- (void)applicationEnteredForeground:(NSNotification *)notification {
[self initNetworkCommunication];
}
- (void)didReceiveMemoryWaing
{
[super didReceiveMemoryWaing];
// Dispose of any resources that can be recreated.
}
- (void)initNetworkCommunication {
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.0.117", 5000, &readStream, &writeStream);
_inputStream = (NSInputStream *)CFBridgingRelease(readStream);
_outputStream = (NSOutputStream *)CFBridgingRelease(writeStream);
[_inputStream setDelegate:self];
[_outputStream setDelegate:self];
[_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[_inputStream open];
[_outputStream open];
}
- (IBAction)ToggleLight:(id)sender {
UISegmentedControl *button = ((UISegmentedControl*)sender);
long tag = button.tag;
NSString *response = [NSString stringWithFormat:@"P%ld%@", tag , button.selectedSegmentIndex?@"H" : @"L"];
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
[_outputStream write:[data bytes] maxLength:[data length]];
}
- (IBAction)shutdown:(id)sender {
NSData *data = [[NSData alloc] initWithData:[@"shutdown" dataUsingEncoding:NSASCIIStringEncoding]];
[_outputStream write:[data bytes] maxLength:[data length]];
}
- (IBAction)reboot:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"REBOOT?" message:@"Are you sure you want to reboot?" delegate:self cancelButtonTitle:@"OH.. Hell No!" otherButtonTitles:@"I said REBOOT!", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(buttonIndex == 1){
NSData *data = [[NSData alloc] initWithData:[@"reboot" dataUsingEncoding:NSASCIIStringEncoding]];
[_outputStream write:[data bytes] maxLength:[data length]];
NSLog(@"YES %@",data);
}
}
- (IBAction)reset:(id)sender {
[self initNetworkCommunication];
}
@end
if, for example i would like to tu gpio pin 7 high or low, the command needs to be/contain "P7H" and "P7L" respectively, however, looking at this function
def dataReceived(self, data):
msg = ""
if (data == 'P7H'):
msg = "Pin 7 is now High"
GPIO.output(7, True)
elif (data == 'P7L'):
msg = "Pin 7 is now Low"
GPIO.output(7, False)
print msg
My question is what exactly is the format in which data should be sent? Since the arguments are "(self, data)" I cant just send "P7H" or "P7L" as the response I get is "undefined"
Assuming i want to simply tu pin 7 on, what would the command be? and what exactly is the format of "self" and what does it mean?
