1
votes

I have an ASP.Net page for login/register operations. What I'm trying to do is show the user name on LCD when the user logs in. The hardware I'm using is LCD Keypad Shield not just LCD if that matters. And the lovely Arduino UNO.

C# side

I tried storing the username in a char array and send to arduino one by one but the Serial.Write() gives error if I don't give a string to it. I wanted to send the whole name at once then, but Serial.Read() seems to be reading one at a time.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.IO.Ports;
using System.Text;
using System.ComponentModel;
using System.Windows;

namespace EComm
{
    public partial class Login : System.Web.UI.Page
    {
        SerialPort sp;
        protected void Page_Load(object sender, EventArgs e)
        {
            sp = new SerialPort();
            sp.PortName = "COM13";
            sp.BaudRate = 9600;
            txtPass.TextMode = TextBoxMode.Password;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DBDataContext db = new DBDataContext();
            db.Connection.Open();
            var users = from allusers in db.Users select allusers;
            foreach (User _user in users)
            {
                if (txtUser.Text == _user.UserName.Trim())
                    if (txtPass.Text == _user.UserPassword.Trim())
                    {
                        Session["User"] = _user.UserName;
                        String outgoing = Session["User"].ToString();
                        for (int i = 0; i < outgoing.Length; i++)
                        {
                            sp.Open();
                            sp.Write(outgoing[i].ToString());
                            sp.Close();
                        }
                        Response.Redirect("Default.aspx");
                    }
            }
        }

Arduino side

#include <LiquidCrystal.h>
char usrName[10];
char incomingChar;
byte index=0;

LiquidCrystal lcd(4,5,6,7,8,9);
int baud = 9600; 
int x=0;
int y=0;

void setup()
{
 lcd.begin(16,2);
 lcd.setCursor(x,y); 
 Serial.begin(baud);
}

void loop()
{
    while(Serial.available()>0){
    if(index<10){
      y=1;
      incomingChar=Serial.read();
      usrName[index]=incomingChar;
      lcd.setCursor(x,y);
      lcd.print(usrName[index]);
      index++;
      x++;
    }
}
}

Both codes do not give any errors or warnings. When I upload the program to Arduino and run the login page, I am succesfully redirected to the page stated but nothing shows up on the LCD.

Actually this is what I see after I login to the site. I have no idea why there are white cells, they just show up when I plug in the board. But when I upload an example program for keypad shield those cells just turn to normal.

enter image description here

enter image description here

1
Are you able to print anything to your LCD at all? You should try to isolate the problem better. Pleasy verify that outgoing[i].ToString() is not empty, and that it is indeed being received at the Arduino side. If this is the case, you have a problem with the way you interact with your LCD or a hardware issue.Stian Sandve
Yes I can print to LCD with no problem. I debugged the C# side. When I check outgoing I see it has the username entered in the textbox but when I check outgoing.[i].ToString() I still see the whole username. Shouldn't it be the first letter of it instead?ardatosun
If you meant if I got any output on LCD after logging in, yes i have 3 weird characters on first 3 columns on the first row which is still weird because I set the row value to 1 to print it on the 2nd row.ardatosun

1 Answers

1
votes

I found out that the order of the pin numbers mattered. Changed the LiquidCrystal lcd(4,5,6,7,8,9); line to LiquidCrystal lcd(8,9,4,5,6,7); I can see the desired output now and there are no white cells on startup too.