Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Netduino send Twilio SMS #2

Closed
Glavin001 opened this issue May 14, 2015 · 3 comments
Closed

Netduino send Twilio SMS #2

Glavin001 opened this issue May 14, 2015 · 3 comments
Assignees

Comments

@Glavin001
Copy link
Owner

http://stackoverflow.com/questions/23394895/send-message-using-a-webrequest-and-twilio

@Glavin001
Copy link
Owner Author

See http://forums.netduino.com/index.php?/topic/6799-https-support-on-n2/

The Netduino's .NET does not support SSL.
So we cannot send requests to Twilio!
So what we do is: create a small Node.js server and all it does is receive the request in HTTP and then creates a Twilio request to HTTPS (SSL)

@Glavin001 Glavin001 self-assigned this May 14, 2015
@Glavin001
Copy link
Owner Author

My attempted Twilio code:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using System.Text;

namespace AuthNet
{
    public class Program
    {
        public static void Main()
        {
            // write your code here
            var Interface = Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0];
            Interface.EnableDynamicDns();
            Interface.EnableDhcp();
            Debug.Print("IPAddress: " + Interface.IPAddress);
            foreach (var d in Interface.DnsAddresses)
                Debug.Print("DNS: " + d);

            Debug.Print("Started");

            string _Host = "api.twilio.com";
            var Entry = Dns.GetHostEntry(_Host);
            var _Address = new IPAddress(Entry.AddressList[0].GetAddressBytes());
            var _Port = 443;

            var accountSid = "";
            var authToken = "";

            var s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPHostEntry ipHostInfo = Dns.GetHostEntry(_Host);
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPEndPoint endPoint = new IPEndPoint(ipAddress, _Port);
            //var EndPoint = new IPEndPoint(_Address, 80);
            s.Connect(endPoint);

            var url = "http://api.twilio.com/2010-04-01/Accounts/" + accountSid + "/SMS/Messages.json";
            //var text = KeyPadIndexToChar[(int)KeyCode].ToString();
            //string text = "\"" + _ID.Substring(0, 2) + " " + _Result + "\""; // +Result;
            string body = "From=+12356&To=+12323432&Body=testing";

            var databuilder = new StringBuilder();
            databuilder.AppendLine("POST " + url + " HTTP/1.1");
            databuilder.AppendLine("Accept: application/json");
            databuilder.AppendLine("Content-Type: application/x-www-form-urlencoded; charset=utf-8");
            //databuilder.AppendLine("Content-Type: text/plain; charset=utf-8");
            databuilder.AppendLine("Host: " + _Host);
            databuilder.AppendLine("Content-Length: " + body.Length);
            string credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes(accountSid + ":" + authToken));
            databuilder.AppendLine("Authorization: Basic " + credentials);
            databuilder.AppendLine("Connection: Keep-Alive");
            databuilder.AppendLine("");
            databuilder.AppendLine(body);

            var databytes = Encoding.UTF8.GetBytes(databuilder.ToString());

            System.Net.Security

            Debug.Print("Sending...");
            var numbytessend = s.Send(databytes);
            Debug.Print("Sent");

            var buff = new byte[10000];
            var numbytesread = s.Receive(buff, 0, 10000, SocketFlags.None);
            if (numbytesread > 0)
            {
                var chars = Encoding.UTF8.GetChars(buff, 0, numbytesread);
                Debug.Print("Result: " + new String(chars));
            }


            var LED = new OutputPort(Pins.ONBOARD_LED, false);

            while (true)
            {
                LED.Write(true);
                Thread.Sleep(200);
                LED.Write(false);
                Thread.Sleep(100);
            }
        }

    }
}

@Glavin001
Copy link
Owner Author

11269436_10206578346127647_1708633744_n

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant