Category: Programming

Twilio Test Application in Node js

First things first – if you have not yet installed Node.js on your system, you can download an installer for your platform from nodejs.org

Next, install the Twilio module using npm, in the same directory as “Twilio.js”

[sudo] npm install twilio
var twilio = require('twilio');
var client = new twilio.RestClient('TWILIO_ACCOUNT_SID', 'TWILIO_AUTH_TOKEN');
 
// REST client will handle authentication and response serialzation for you.
client.sms.messages.create({
 to:'+16512223344',
 from:'TWILIO_NUMBER',
 body:'Hello World'
}, function(error, message) {
 if (!error) {
 // information about the text messsage you just sent:
 console.log('Success! The SID for this SMS message is:');
 console.log(message.sid);
 
 console.log('Message sent on:');
 console.log(message.dateCreated);
 } else {
 console.log('Oops!This number is invalid.');
 }
});

To run this code, return to the terminal and run

node Twilio.js

 

How to Create a Proxy Layer Between MQTT Broker & AWS IOT Broker

Create Proxylayer.js file and paste the following code into it:

var mqtt = require('mqtt'), url = require('url');
var client = mqtt.connect('mqtt://localhost:1883',
{
username: '<username>',
password: '<password>'
});

console.log("Connected to MQTT Broker:- localhost” + client.toString());
var awsIot = require('aws-iot-device-sdk');
var device = awsIot.device({

keyPath:  Certificate key file path,
certPath: Certificate file path,
caPath:   Certificate root file path,
clientId: AWS Thing Name,
region:   AWS IoT Broker region,
});

device.on('connect', function ()
{
console.log("Connected to AWS IoT Broker:- " + device.toString());
});

client.on('connect', function()
{
//subscribe to a topic (#)
client.subscribe('#', function ()
{
client.on('message', function (topic, message, packet) {
console.log("Received :-" + message + " on " + topic);
device.publish(topic, message);
console.log("Sent :-" + message + " on " + topic);
});
});
});

 

How to Create a TCP Client Server Socket example using Java

*****************************TCPSERVER***********************

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

class TCPServer {
 public static void main(String argv[]) throws Exception {
 ServerSocket ourFirstSocket = new ServerSocket(port);
 while (true) {

Socket connectionSocket = ourFirstSocket.accept();
 BufferedReader messagesFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
 String clientSentence = messagesFromClient.readLine();
 System.out.println("Received: " + clientSentence);
 }
 }
}

*****************************TCPCLIENT***********************

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.*;
class TCPClient {
public static void main(String agrs[]) throws Exception {
Socket clientsocket = new Socket("localhost", port);
DataOutputStream outToServer = new DataOutputStream(clientsocket.getOutputStream());
BufferedReader infromUser = new BufferedReader(new InputStreamReader(System.in));
String sentence = //infromUser.readLine();
outToServer.writeBytes(sentence + '\n');

clientsocket.close();
}
}