Advertisement
GastonFontenla

Untitled

Aug 14th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Management;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using System.IO.Ports;
  12. using System.Threading;
  13.  
  14. namespace PlantIoT
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         public bool arduinoConnected;
  19.        
  20.         public Form1()
  21.         {
  22.             InitializeComponent();
  23.         }
  24.  
  25.         private void Form1_Load(object sender, EventArgs e)
  26.         {
  27.             arduinoConnected = false;
  28.             abrirSerial();
  29.         }
  30.  
  31.         private int abrirSerial()
  32.         {
  33.             puerto.Text = "";
  34.             string portNum = "";
  35.             string port = "";
  36.             serialPort1.Close();
  37.             foreach (string ports in SerialPort.GetPortNames())
  38.             {
  39.                 portNum = ports.ToString();
  40.                 puerto.Text = portNum;
  41.                 port = portNum;
  42.             }
  43.             try
  44.             {
  45.                 serialPort1.PortName = (port);
  46.                 serialPort1.BaudRate = 9600;
  47.                 serialPort1.DataBits = 8;
  48.                 serialPort1.Parity = Parity.None;
  49.                 serialPort1.StopBits = StopBits.One;
  50.                 serialPort1.Handshake = Handshake.None;
  51.                 serialPort1.Encoding = System.Text.Encoding.Default;
  52.                 arduinoConnected = true;
  53.                 return 1;
  54.             }
  55.             catch
  56.             {
  57.                 errorLabel.Text = "HumeData no está conectado";
  58.             }
  59.             arduinoConnected = false;
  60.             return 0;
  61.         }
  62.  
  63.         private void enviarDatos_Click(object sender, EventArgs e)
  64.         {
  65.             string ssid = wifiSSID.Text;
  66.             string pass = wifiPASS.Text;
  67.             string conn = connectionString.Text;
  68.  
  69.             if(ssid == "" || pass == "" || conn == "")
  70.             {
  71.                 errorLabel.Text = "Hay campos vacíos";
  72.                 return;
  73.             }
  74.             else
  75.             {
  76.                 errorLabel.Text = "";
  77.             }
  78.  
  79.             abrirSerial();
  80.  
  81.             if (arduinoConnected == true)
  82.             {
  83.                 //HostName=PoloUnlamIoT.azure-devices.net;DeviceId=IoTTest;SharedAccessKey=wzGd/5ThNoXwz7jAOHdrNUqxwuVRbm63XNETWaMUjuQ=
  84.                 string data = "<SSID>" + ssid + "</SSID>" +
  85.                               "<PASS>" + pass + "</PASS>" +
  86.                               "<CONN>" + conn + "</CONN>";
  87.                 serialPort1.Open();
  88.                 serialPort1.Write("B" + data + "*");
  89.                 Thread.Sleep(2000);
  90.                 serialPort1.Close();
  91.                 errorLabel.Text = "HumeData ha sido configurado";
  92.             }
  93.             else
  94.             {
  95.                 errorLabel.Text = "HumeData no está conectado";
  96.             }
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement