ĐẠI HỌC QUỐC GIA TP.HCM TRƯỜNG ĐẠI HỌC BÁCH KHOA KHOA ĐIỆN – ĐIỆN TỬ BÁO CÁO ĐỒ ÁN 1 ĐỀ TÀI: GỬI TIN NHẮN LÊN WEB SERVER THÔNG QUA GPRS VÀ PHÂN LOẠI.. Dịch vụ GPRS cung cấp: - Dịch vụ ti
Trang 1ĐẠI HỌC QUỐC GIA TP.HCM TRƯỜNG ĐẠI HỌC BÁCH KHOA
KHOA ĐIỆN – ĐIỆN TỬ
BÁO CÁO ĐỒ ÁN 1
ĐỀ TÀI: GỬI TIN NHẮN LÊN WEB SERVER
THÔNG QUA GPRS VÀ PHÂN LOẠI.
Giảng viên: TS Võ Quế Sơn
Trang 2MỤC LỤC
Trang 3I CƠ SỞ LÝ THUYẾT:
General Packet Radio Service (GPRS) là dịch vụ vô tuyến gói tổng hợp được phát triển trên nền tảng công nghệ thông tin di động toàn cầu GSM Công nghệ GPRS hay còn biết đến với mạng di động thế hệ 2.5G (trong khi GSM đc xem như là mạng 2G).
GPRS cung cấp dữ liệu ở tốc độ từ 56 đến 114 kbps.
Dịch vụ GPRS cung cấp:
- Dịch vụ tin nhắn đa phương tiện (MMS)
- Push to Talk
- Ứng dụng Internet dành cho Thiết bị thông minh (WAP)
GPRS có thể được dùng như sóng mang SMS Nếu SMS trên GPRS được dùng, tốc độ truyền tải SMS có thể đạt đến 30 tin nhắn SMS một phút Tốc độ nhanh hơn nhiều so với SMS thông thường trên GSM, tốc độ truyền tải chỉ khoảng 6 đến 10 tin SMS một phút.
2 TẬP LỆNH AT:
Tập lệnh AT (Attention command) là tập lệnh chuẩn được hỗ trợ bởi hầu hết các thiết bị di động như điện thoại di động, GSM modem mà
có hỗ trợ gửi và nhận tin nhắn tin nhắn dưới dạng SMS (Short
Message Service) và điều khiển cuộc gọi Ví dụ một số lệnh AT như: AT+CMGS, AT+ CMGL,
Tên đầy đủ của ASP.NET là Active Server Pages.NET (.NET ở đây
là NET framework) là công nghệ phát triển các ứng dụng trên nền web, thế hệ kế tiếp của ASP Một số thành phần quan trọng trong một
dự án về ASP.NET :
Default.aspx: nơi thiết kế giao diện cho website, có 3 chế độ là design (cho phép sửa chữa nội dung web một cách trực quan), source (hiện thị mã nguồn html, tại đây người dùng có thể soạn thảo trực tiếp lên các thẻ html), split (cho phép xem trang web đồng thời ở cả 2 chế độ trên).
Default.aspx.cs: chứa mã nguồn viết bằng C#
Global.asax: chúng ta sử dụng tập tin này để khai báo các biến toàn cục và phản ứng với các sự kiện toàn cục.
Trang 4 Web.config: tập tin dạng XML chứa cấu hình cho ứng dụng ASP.NET nó bao gồm các cấu hình về an ninh, quản lý trạng thái, quản lý bộ nhớ,
Khi gửi một tin nhắn về số điện thoại trong SIM điện thoại đặt trên module SIM900 thì tin nhắn sẽ được giữ lại Nhấn SW1 trên kit TIVA
để tiến hành gửi chuỗi dữ liệu tin nhắn vừa được gửi đến SIM lên server web vào file Text.txt Sau đó file Text sẽ dựa vào cú pháp của tin nhắn mà phân loại vào những ô hiển thị phù hợp.
UART
GPRS
Trang 5II CÁC THÀNH PHẦN CỦA DỰ ÁN:
tính
năng nổi bật của SIM900:
SIM900 hoạt động trên 2 băng tần 900 và 1800MHz
Đế Sim Card tích hợp trên board
Tích hợp nguồn xung 3A, với 3 chế độ nguồn 3.3V/4V/4.5V
Trang 6 Tích hợp ngõ ra Speaker, MIC
Kích thước nhỏ gọn 47mm x 34.29 mm
GSM Module Sim900 là một sản phẩm do AT-COM phát triển nhằm giúp
người sử dụng có thể khai thác các tính năng của Sim900 một cách dễ dàng Board hỗ trợ khá đầy các ngõ ra của Module Sim900 ( RS232, Audio, ADC, VRTC, PWM, I2C ) Dễ kết nối kết nối với các dòng vi điều khiển như PIC, AVR, ARM, Arduino để phát triển các ứng dụng điều khiển, giám sát qua môi trường mạng GSM, GPRS Ngõ ra RS232 và UART giúp giao tiếp máy tính và lập trình cho Module Sim900 thông qua tập lệnh AT COMMAND
Tập lệnh AT COMMAND cho việc gửi dữ liệu lên web server thông qua GPRS:
AT+CGATT? [Kiểm tra GPRS có kích hoạt hay chưa]
[Phản hồi là "+CGATT: 1" chỉ ra rằng GPRS đã được kích hoạt]
AT+CIPSHUT [Đóng IP]
[Phản hồi: "SHUT OK"]
AT+CIPSTATUS [Kiểm tra IP đã được khởi tạo hay chưa]
[Phản hồi: "STATE: IP INITIAL"]
AT+CIPMUX=0 [Cài đặt chế độ kết nối]
[Phản hồi: "OK" là cài đặt thành công]
AT+CGDCONT=1,"IP","m-wap";
[Phản hồi là "OK", có thể xác nhận bằng lệnh "AT+CGDCONT?"]
AT+CSTT="m-wap","mms","mms";
[Phản hồi:"OK"]
AT+CIICR [Kết nối đến nhà cung cấp dịch vụ, có thể mất vài giây]
[Phản hồi: "OK"]
AT+CIFSR [Lấy địa chỉ IP cuả module]
[Phản hồi: "IP address" đăng ký cho module]
AT+CIPSTART="TCP","trandaolong.somee.com","80" [Bắt đầu kết nối, TCP, tên miền, port]
Trang 7[Phản hồi: "OK" và sau đó "CONNECT OK"]
AT+CIPSEND [Yêu cầu gửi dữ liệu]
[Phản hồi lại ký tự “>” để xác nhận bạn có thể gửi dữ liệu]
GET http://www.vusihung.somee.com/Default.asp?Data=123456
[Sau đó gửi byte 0x0A(Ctrl+Enter) và 0x1A(Ctrl+Z) để tiến hành gửi dữ liệu]
[Module sẽ xác nhận bằng phản hồi: "SEND OK"]
AT+CIPSHUT [Ngắt kết nối hiện tại]
[Phản hồi: "SHUT OK"]
Những đoạn lệnh AT trên sẽ được gửi từ chip ARM Cortex M4 thông qua UART cho module Sim900.
Tiva Lauchpad là phiên bản nâng cấp của kit Stellaris Lauchpad hiện
đã ngừng sản xuất, có giá thành thấp và ít tiêu hao năng lượng, được
Trang 8sản xuất bởi tập đoàn Texas Instruments Tiva lauchpad mang đầy đủ tính năng của kit Stellaris Lauchpad đồng thời nâng cấp thêm 1 số ngoại vi và sửa những lỗi có trên phiên bản Stellaris Lauchpad cũ.
Ta sẽ dùng Kit này để đọc chuỗi dữ liệu từ trong module SIM900 và truyền lên web server thông qua GPRS Code cho Kit như sau:
#include"inc/hw_ints.h"
#include<stdbool.h>
#include<stdint.h>
#include"inc/hw_memmap.h"
#include"driverlib/pin_map.h"
#include"inc/hw_types.h"
#include"driverlib/sysctl.h"
#include"driverlib/gpio.h"
#include"utils/uartstdio.h"
#include"driverlib/uart.h"
#include"driverlib/interrupt.h"
#include"driverlib/pin_map.h"
int sms[8000]; int i=0; int a=0; int b=0;
//=======================================================
====================//
void UART1IntHandler( void)
{
unsigned long ulStatus;
ulStatus = UARTIntStatus(UART1_BASE, true); //get interrupt status
UARTIntClear(UART1_BASE, ulStatus); //clear the asserted interrupts
while(UARTCharsAvail(UART1_BASE))
{
sms[i]=UARTCharGet(UART1_BASE); //luu chuoi tin nhan vao mang sms[]
if((((sms[i])=='D')&&((sms[i-1])=='S')&&((sms[i-2])=='U')&&((sms[i-3])=='C')))
{
a=1; //Neu dung la tin nhan ta se cho a=1
} i++;
} }
//=======================================================
=================================================
void ConfigureUARTB( void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART1);
GPIOPinConfigure(GPIO_PB0_U1RX);
GPIOPinConfigure(GPIO_PB1_U1TX);
GPIOPinTypeUART(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);
UARTClockSourceSet(UART1_BASE, UART_CLOCK_PIOSC);
UARTStdioConfig(1, 115200, 16000000);//baurate 115200
}
//=======================================================
==================================================
Trang 9int main( void)
{
SysCtlClockSet(SYSCTL_SYSDIV_2_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|
SYSCTL_OSC_MAIN);//tan so 80Mhz
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1);
ConfigureUARTB(); //Goi chuong trinh con
IntMasterEnable(); //enable processor interrupts
IntEnable(INT_UART1); //enable the UART interrupt
UARTIntEnable(UART1_BASE, UART_INT_RX | UART_INT_RT);
UARTprintf("AT\n");
SysCtlDelay(SysCtlClockGet()/3);
UARTCharPut(UART1_BASE, 26);
while(1)
{
/*
Bien a dung de kiem soat gui hay khong gui tin nhan len sever
Neu ki tu gui ve co chua CUSD thi do chinh la tin nhan
Nhan duoc tin nhan ta se cho a=1 de cho phep gui tin nhan len sever
*/
if(a==1)
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1,0x02);
// Bat led do bao hieu dang gui
SysCtlDelay(SysCtlClockGet()/15);
//Delay 0,2s
UARTprintf("AT+CIPSHUT\n");
//Ngat ket noi trc khi ket noi
SysCtlDelay(SysCtlClockGet()/15);
//Delay 0,2s
UARTprintf("AT+CIPSTART=\"TCP\",\"www.vusihung.somee.com\",\"80\"\n");
//Ket noi toi sever
SysCtlDelay(SysCtlClockGet()/3);
//Delay 1s
UARTprintf("AT+CIPSEND\n");
//Gui voi noi dung:
//Noi dung gui di
for (b=0; b<i; b++)
{
UARTprintf("%c", sms[b]);
}
UARTprintf("%c \n", sms[i]);
//Ket thuc noi dung
SysCtlDelay(SysCtlClockGet()/15);
//Delay 0,2s
Trang 10UARTCharPut(UART1_BASE, 26);
// Enter gui di
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1,0x00);
// tat led do bao hieu qua trinh gui key thuc
i=0;
} }
}
Kết nối Module Sim900 và Kit TIVA: nối chân TXD của SIM900 với
chân PC6 (RXD) của KIT, chân RXD của SIM900 với chân PC7(TXD) của KIT, kết nối GND chung cho KIT và module sau đó lấy nguồn 4.5V
từ module cấp cho KIT vào chân VBUS.
a) Code giao diện dùng mã HTML (nằm trong file Default.aspx):
<!—Các khai báo ban đầu cho chương trình >
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Do an 1</title>
<style type="text/css">
.canhgiua {
margin-left:auto;
margin-right:auto;
} #form1 { height: 565px;
}
</style>
</head>
<body style="background-color: #ccff00">
<form id="form1" runat="server">
<asp:Panel ID="Panel1" runat="server" CssClass="canhgiua" Width="1000px"
Height="180px">
<! Tạo tựa để cho trang web: RECEIVE DATA FROM GPRS MODULE >
Trang 11<div>
<asp:Label ID="lbtitle" Text="<h1>SPECIFY MESSAGES</h1>"
runat="server" style="color: #0000CC; text-align: center"
</asp:Label>
</div>
<! Tạo logo BK >
<asp:Panel ID="Panel2" runat="server" CssClass="canhgiua" Width="500px"
Height="142px">
<asp:Image ID="Image1" runat="server" style="text-align: center" ImageUrl="~/bk.png" /> </asp:Panel>
<! Tạo khung hiển thị dữ liệu và nút nhấn A01,A02,A03,A04 >
<asp:Label ID="Label2" runat="server" style="text-align: center" Width="153px"
BorderColor="Red" BorderStyle="Double" Height="33px" Font-Bold="True"
Font-Size="X-Large" ForeColor="Red"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="A01" BackColor="#FFFF66" BorderColor="White"
BorderStyle="Outset" Font-Bold="True" Font-Size="Large" />
<br />
<asp:Label ID="Label3" runat="server" style="text-align: center" Width="153px"
BorderColor="Red" BorderStyle="Double" Height="33px" Font-Bold="True"
Font-Size="X-Large" ForeColor="Red"></asp:Label>
<br />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="A02" BackColor="#FFFF66" BorderColor="White"
BorderStyle="Outset" Font-Bold="True" Font-Size="Large" />
<br />
</asp:Panel>
<asp:Panel ID="Panel4" runat="server" CssClass="canhgiua" Width="600px"
Height="143px">
<br />
<asp:Label ID="Label5" runat="server" style="text-align: center" Width="153px"
BorderColor="Red" BorderStyle="Double" Height="33px" Font-Bold="True"
Font-Size="X-Large" ForeColor="Red"></asp:Label>
<br />
<input type="submit" name="Button2" value="A03" id="Button2"
style="background-color:#FFFF66;border-color:White;border-style:Outset;font-size:Large;font-weight:bold;">
<br />
<asp:Label ID="Label6" runat="server" style="text-align: center" Width="153px"
BorderColor="Red" BorderStyle="Double" Height="33px" Font-Bold="True"
Font-Size="X-Large" ForeColor="Red"></asp:Label>
<br />
<input type="submit" name="Button3" value="A04" id="Button3"
style="background-color:#FFFF66;border-color:White;border-style:Outset;font-size:Large;font-weight:bold;">
Trang 12<br />
</asp:Panel>
<br />
<asp:Panel ID="Panel3" runat="server" CssClass="canhgiua" Width="143px"
Height="100px">
</asp:Panel>
<br />
<! Tạo nút nhấn Delete >
<input type="submit" name="Button4" value="Delete" id="Button4"
style="background-color:#FFFFFF;border-color:White;border-style:Outset;font-size:Large;font-weight:bold;">
<! Tạo dòng chữ chạy:"UNIVERSITY OF TECHNOLOGY " >
<marquee
style="font-family: 'Times New Roman', Times, serif; font-size: xx-large; font-weight: 700; color:
#FF3300; margin-top: 0px;">
UNIVERSITY OF TECHNOLOGY
</marquee></form>
</body>
</html>
b) Mã nguồn C# cho các tương tác giữa client và server (trong file Default.aspx.cs):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
publicpartialclass_Default : System.Web.UI.Page
{
// Khởi tạo hàm readFile
privateint readFile(string file)
{
try
{
StreamReader reader = newStreamReader(file);
string content = reader.ReadLine();
reader.Close();
return int.Parse(content);
}
catch
{
return 1;
}
}
// Khởi tạo chức năng cho nút nhấn A01 trên trang web
Trang 13protectedvoid Button1_Click(object sender, EventArgs e) {
int DATA;
string pathfile = Server.MapPath(Request.ApplicationPath + "\\Text.txt"); DATA = readFile(pathfile);
Label2.Text = DATA.ToString();
}
// Khởi tạo chức năng cho nút nhấn DELETE REPORT trên trang web
protectedvoid Button2_Click(object sender, EventArgs e)
string pathfile = Server.MapPath(Request.ApplicationPath + "\\Report.txt"); StreamWriter stream = newStreamWriter(pathfile,false);
stream.Write("");
stream.Close();
}
c) Mã nguồn cho các sự kiện toàn cục (trong file Global.asax):
<%@Application Language=VBDebug=true %>
<scriptrunat="server">
Sub Application_BeginRequest(ByVal Sender asObject, ByVal E asEventArgs)
Dim data_string, file_System, file_path, file, report_path, report, report_system
data_string = Request.QueryString("Data")
if data_string <> ""then
file_System = CreateObject("Scripting.FileSystemObject")
report_system = CreateObject("Scripting.FileSystemObject") report_path = Server.MapPath("Report.txt")
file_path = Server.MapPath("Text.txt")
'Xóa nội dung file Text.txt
Dim stream AsNew IO.StreamWriter(file_path, False) stream.Write("")
stream.Close()
'Ghi data_string vào file Text.txt
file = file_System.OpenTextFile(file_path, 8, True) file.WriteLine(data_string)
file.Close() file = Nothing
file_System = Nothing EndIf
EndSub
</script>
Cách triển khai ứng dụng ASP.net trên somee.com:
Để đăng ký web hosting NET miễn phí vào trang http://somee.com/ Trong mục FREE NET HOSTING chọn Learn More như hình dưới.