Thanks for Visiting My Blog...
I will also do freelance projects : contact me via
anilkunchalaece@gmail.com
void setup(){
Serial.begin(9600);
pinMode(9,OUTPUT);
}
void loop()
{
if(Serial.available())
{
String inChar = Serial.readString();
if(inChar == "ON")
{
digitalWrite(9,HIGH);
Serial.println("LED is On...");
}else if(inChar == "OFF")
{
digitalWrite(9,LOW);
Serial.println("LED is Off...");
}else if(0 > inChar.toInt() < 1024)
{
analogWrite(9,inChar.toInt());
Serial.print("Led Brightness Value\t");
Serial.println(inChar.toInt());
}
}//end of available
}//end of loop
Python Code for the Raspberry Pi
import RPi.GPIO as GPIO
import time
import sys
import serial
from pubnub import Pubnub
ser = serial.Serial('/dev/ttyACM1',9600)
GPIO.setmode (GPIO.BCM)
LED_PIN = 4
GPIO.setup(LED_PIN,GPIO.OUT)
pubnub = Pubnub(publish_key='pub-c-9e022950-208f-49aa-9873-c560add30b41', subscribe_key='sub-c-9ddf7ad2-cfec-11e5-b522-0619f8945a4f')
channel = 'anil'
def _callback(m, channel):
print(m)
ser.write(m['led'])
def _error(m):
print(m)
pubnub.subscribe(channels=channel, callback=_callback, error=_error)
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Getting data from a sensor</title>
</head>
<body>
<header>
<h1>Control LED from Web Interface</h1>
<p>ON and OFF and Brightness Control</p>
</header>
<section id="main" role="main">
<button id="ledOn">LED ON!</button>
<button id="ledOff">LED OFF!</button>
</section>
<section>
<form id="form1">
Enter the Brightness Value <input type="text" id="inputValue">
<input type="submit" value="submit">
</form>
</section>
<footer>
Done By Kunchala Anil.. Using PubNub Data Streams
</footer>
<!-- including the latest PubNub JavaScript SDK -->
<script src="http://cdn.pubnub.com/pubnub-3.7.1.min.js"></script>
<script>
(function() {
// DOM
var buttonOn = document.querySelector('#ledOn');
var buttonOff= document.querySelector('#ledOff');
// This is the channel name you are subscribing in remote-led.py
var channel = 'anil';
// Init - Get your own keys at admin.pubnub.com
var p = PUBNUB.init({
subscribe_key: 'sub-c-9ddf7ad2-cfec-11e5-b522-0619f8945a4f',
publish_key: 'pub-c-9e022950-208f-49aa-9873-c560add30b41'
});
// Sending data
function sendValue() {
p.publish({
channel : channel,
message : {led : document.getElementById("inputValue").value}
});
}
function lightOn() {
p.publish({
channel : channel,
message : {led : "ON"}
});
}
function lightOff() {
p.publish({
channel : channel,
message : {led : "OFF"}
});
}
// Click event
buttonOn.addEventListener("click",lightOn);
buttonOff.addEventListener("click",lightOff)
document.getElementById("form1").addEventListener("submit",sendValue);
})();
</script>
</body>
</html>
You can Download The Source Code In My GitHub Repositories Here