نمایش نتایج: از 1 به 3 از 3

موضوع: کد اس ام اس کنترلر

  1. #1
    Junior Member
    تاریخ عضویت
    Feb 2017
    نوشته ها
    22

    کد اس ام اس کنترلر

    سلام اساتید محترم اگه ممکنه بفرمایید این کد رو میشه برای اس ام اس کنترلر استفاده کرد برای کنترل رله ها وسنسورهاوتعریف شماره کاربر وگرفتن وضعیت ورمزگذاری برروی دستگاه یا خیر اخه توی سایت هیچ کد کاملی ندیدم که بشه برای اس ام اس کنترلر کامل استفاده کرد:
    [ this is my code]
    #define _SS_MAX_RX_BUFF 256 //RX BUFFER SIZE
    #include
    char inchar; // Will hold the incoming character from the GSM shield
    SoftwareSerial SIM900(4, 6);

    int Buzzer = 13;
    int x = 0;
    int y = 0;
    float z =0;
    int RIP_sensor = 12;
    int Room2 = 11 ;
    int Room1 = 7 ;
    int Home_light = 8;
    int water_sensor = 0;
    int Gas_sensor = 0;
    float Tem_sensor = 0;
    float temp =0.0;
    String textForGAS ;
    String textForRIP ;
    String textForWATER;
    String textForTAM ;

    void setup()
    {
    Serial.begin(19200);
    // set up the digital pins to control
    pinMode(RIP_sensor, INPUT);
    pinMode(Buzzer, OUTPUT);
    pinMode(Room1, OUTPUT);
    pinMode(Room2, OUTPUT);
    pinMode(Home_light, OUTPUT);
    digitalWrite(Buzzer, LOW);
    digitalWrite(Room1, LOW);
    digitalWrite(Room2, LOW);
    digitalWrite(Home_light, LOW);

    // wake up the GSM shield
    SIM900.begin(19200);
    delay(20000); // give time to log on to network.
    SIM900.print(“AT+CMGF=1r”); // set SMS mode to text
    delay(100);
    SIM900.print(“AT+CNMI=2,2,0,0,0r”);
    // blurt out contents of new SMS upon receipt to the GSM shield’s serial out
    delay(100);
    Serial.println(“Ready…”);
    }

    void sendSMS(String message)
    {
    SIM900.print(“AT+CMGF=1r”);
    delay(100);
    SIM900.println(“AT + CMGS = “+967737704074″”);
    delay(100);
    SIM900.println(message);
    delay(100);
    SIM900.println((char)26);
    delay(100);
    SIM900.println();
    delay(5000);
    }

    void loop()
    {
    //If a character comes in from the cellular module…
    if (SIM900.available())
    {
    inchar = SIM900.read();
    Serial.println(inchar);
    if (inchar == ‘#’)
    {
    delay(10);

    inchar = SIM900.read();
    if (inchar == ‘a’)
    {
    delay(10);
    inchar = SIM900.read();
    if (inchar == ‘0’)
    {
    digitalWrite(Home_light, HIGH);
    }
    else if (inchar == ‘1’)
    {
    digitalWrite(Home_light, LOW);
    }
    delay(10);
    inchar = SIM900.read();
    if (inchar == ‘b’)
    {
    inchar = SIM900.read();
    if (inchar == ‘0’)
    {
    digitalWrite(Room1, HIGH);
    }
    else if (inchar == ‘1’)
    {
    digitalWrite(Room1, LOW);
    }
    delay(10);
    inchar = SIM900.read();
    if (inchar == ‘c’)
    {
    inchar = SIM900.read();
    if (inchar == ‘0’)
    {
    digitalWrite(Room2, HIGH);
    }
    else if (inchar == ‘1’)
    {
    digitalWrite(Room2, LOW);
    }
    delay(10);
    }
    }
    SIM900.println(“AT+CMGD=1,4”); // delete all SMS
    }
    }
    }

    // put your main code here, to run repeatedly:
    Gas_sensor = (analogRead(A0));
    water_sensor = (analogRead(A1));
    Tem_sensor = (analogRead(A2));
    temp = Tem_sensor * 0.48828125;
    //program for GAS sensor
    if (temp > 70.0)
    {
    textForTAM = “Alarm ! The degree of Tamperture is :n “;
    textForTAM.concat(temp);
    textForTAM = textForTAM +”C”;
    digitalWrite(Buzzer, HIGH);
    Serial.println(textForTAM);
    sendSMS(textForGAS);
    delay(1000);
    do {
    z = (analogRead(A0));
    } while (z >= 141.312);
    }
    else {
    digitalWrite(Buzzer, LOW);
    delay(1000);
    }

    //program for GAS sensor
    if (Gas_sensor > 500)
    {
    textForGAS = “Alarm ! there is Gas by rate :n “;
    textForGAS.concat(Gas_sensor);
    digitalWrite(Buzzer, HIGH);
    Serial.println(textForGAS);
    sendSMS(textForGAS);
    delay(1000);
    do {
    x = (analogRead(A0));
    } while (x >= 499);
    }
    else {
    digitalWrite(Buzzer, LOW);
    delay(1000);
    }

    //program for Water sensor
    if (water_sensor > 830)
    {
    digitalWrite(Buzzer, HIGH);
    delay(60);
    digitalWrite(Buzzer, LOW);
    delay(40);
    textForWATER = “Warnning ! n Tank is Empty ! tourn on the Dynamo “;
    textForWATER.concat(water_sensor);
    Serial.println(textForWATER);
    sendSMS(textForWATER);
    delay(1000);
    do {
    y = (analogRead(A1));
    } while (y >= 829);
    }

    else if (water_sensor < 175 )
    {
    digitalWrite(Buzzer, HIGH);
    delay(60);
    digitalWrite(Buzzer, LOW);
    delay(40);
    textForWATER = "Warrning ! n Tank is Full ! tourn off the Dynamo ";
    textForWATER.concat(water_sensor);
    Serial.println(textForWATER);
    sendSMS(textForWATER);
    delay(1000);
    do {
    y = (analogRead(A1));
    }
    while (y <= 176);
    }

    else {
    digitalWrite(Buzzer, LOW);
    delay(1000);
    }
    }
    thank you

    [/CODE][/CODE]

  2. #2
    Junior Member
    تاریخ عضویت
    Feb 2017
    نوشته ها
    22
    یا این کدها:
    #include <SoftwareSerial.h>


    // EN: String buffer for the GPRS shield message
    String msg = String("");
    // EN: Set to 1 when the next GPRS shield message will contains the SMS message
    int SmsContentFlag = 0;
    //control pins of relay.
    int relay_a=4;
    int relay_b=5;
    int relay_c=6;
    int relay_d=7;

    // EN: Code PIN of the SIM card (if applied)
    //String SIM_PIN_CODE = String( "XXXX" );

    void setup()
    {
    Serial.begin(19200); // the GPRS baud rate
    // Initialize PINs
    pinMode( 4, OUTPUT );
    pinMode( 5, OUTPUT );
    pinMode( 6, OUTPUT );
    pinMode( 7, OUTPUT );
    digitalWrite( 4, LOW );
    digitalWrite( 5, LOW );
    digitalWrite( 6, LOW );
    digitalWrite( 7, LOW );
    Serial.println( "AT+CMGF=1" );
    delay(200);
    }

    void loop()
    {
    char SerialInByte;
    if(Serial.available())
    {
    SerialInByte = (unsigned char)Serial.read();
    delay(5);

    // -------------------------------------------------------------------
    // EN: Program also listen to the GPRS shield message.
    // -------------------------------------------------------------------
    // EN: If the message ends with <CR> then process the message
    if( SerialInByte == 13 ){
    // EN: Store the char into the message buffer
    ProcessGprsMsg();
    }
    if( SerialInByte == 10 ){
    // EN: Skip Line feed
    }
    else {
    // EN: store the current character in the message string buffer
    msg += String(SerialInByte);
    }
    }
    }
    // EN: Make action based on the content of the SMS.
    // Notice than SMS content is the result of the processing of several GPRS shield messages.
    void ProcessSms( String sms ){

    if( sms.indexOf("ona") >= 0 ){
    digitalWrite( relay_a, HIGH );
    }
    if( sms.indexOf("onb") >= 0 ){
    digitalWrite( relay_b, HIGH );
    }
    if( sms.indexOf("onc") >= 0 ){
    digitalWrite( relay_c, HIGH );
    }
    if( sms.indexOf("ond") >= 0 ){
    digitalWrite( relay_d, HIGH );
    }
    if( sms.indexOf("offa") >= 0 ){
    digitalWrite( relay_a, LOW );
    }
    if( sms.indexOf("offb") >= 0 ){
    digitalWrite( relay_b, LOW );
    }
    if( sms.indexOf("offc") >= 0 ){
    digitalWrite( relay_c, LOW );
    }
    if( sms.indexOf("offd") >= 0 ){
    digitalWrite( relay_d, LOW );
    }
    }
    // EN: Request Text Mode for SMS messaging
    void GprsTextModeSMS(){
    Serial.println( "AT+CMGF=1" );
    }

    void GprsReadSmsStore( String SmsStorePos ){
    Serial.print( "AT+CMGR=" );
    Serial.println( SmsStorePos );
    }

    // EN: Clear the GPRS shield message buffer
    void ClearGprsMsg(){
    msg = "";
    }

    // EN: interpret the GPRS shield message and act appropiately
    void ProcessGprsMsg() {
    if( msg.indexOf( "Call Ready" ) >= 0 ){
    // Serial.println( "*** GPRS Shield registered on Mobile Network ***" );
    GprsTextModeSMS();
    }

    // EN: unsolicited message received when getting a SMS message
    if( msg.indexOf( "+CMTI" ) >= 0 ){
    // Serial.println( "*** SMS Received ***" );
    // EN: Look for the coma in the full message (+CMTI: "SM",6)
    // In the sample, the SMS is stored at position 6
    int iPos = msg.indexOf( "," );
    String SmsStorePos = msg.substring( iPos+1 );
    // Serial.print( "SMS stored at " );
    // Serial.println( SmsStorePos );
    // EN: Ask to read the SMS store
    GprsReadSmsStore( SmsStorePos );
    }

    // EN: SMS store readed through UART (result of GprsReadSmsStore request)
    if( msg.indexOf( "+CMGR:" ) >= 0 ){
    // EN: Next message will contains the BODY of SMS
    SmsContentFlag = 1;
    // EN: Following lines are essentiel to not clear the flag!
    ClearGprsMsg();
    return;
    }

    // EN: +CMGR message just before indicate that the following GRPS Shield message
    // (this message) will contains the SMS body
    if( SmsContentFlag == 1 ){
    // Serial.println( "*** SMS MESSAGE CONTENT ***" );
    // Serial.println( msg );
    // Serial.println( "*** END OF SMS MESSAGE ***" );
    ProcessSms( msg );
    }

    ClearGprsMsg();
    // EN: Always clear the flag
    SmsContentFlag = 0;
    }

  3. #3
    Junior Member
    تاریخ عضویت
    Feb 2017
    نوشته ها
    22
    یا این یکی:

    #include <String.h>
    unsigned char SigQ[50];
    unsigned char SigQ1[5]={'a','a','a','a','a'};
    int SIGQ=0;

    void setup()
    {
    Serial1.begin(115200); // the GPRS baud rate
    Serial.begin(115200); // the GPRS baud rate
    delay(500);
    // mySerial.println("AT+CPIN?"); //get the signal Quality
    // delay(100);
    // pinMode(9, OUTPUT);
    // digitalWrite(9,LOW);
    // delay(1000);
    // digitalWrite(9,HIGH);
    // delay(1500);
    // digitalWrite(9,LOW);

    //************************************************** *****
    //GetSignalQuality();
    //************************************************** *****
    for(int x=0;x<20;x++)
    {

    GetSignalQuality();
    delay(800);
    for(int i=0;i<26;i++)
    {
    if(SigQ[i]==58)
    {
    int j=0;
    int k=0;
    i++;i++;
    while((SigQ[i+j])!=44)
    {
    if((SigQ[i+j]>='0'&&SigQ[i+j]<='9')) //>=48 <=57
    {
    SigQ1[j]=SigQ[i+j];
    }
    j++; //j=1
    }
    SIGQ=SigQ1[0]-'0';

    if(j==2)
    {
    SIGQ=SIGQ*10+SigQ1[1]-'0';
    }
    Serial.println("");
    Serial.print("SIGQ:");
    Serial.println(SIGQ);
    }
    }
    if ( Serial1.available())
    Serial.write( Serial1.read());
    if (SIGQ>=10)
    {
    break;
    }
    if(x==20)
    {
    Serial.print("The Signal Quality is poor!");
    }
    }
    delay(2000);
    DialVoiceCall();
    if ( Serial1.available())
    Serial.write( Serial1.read());
    }

    void loop()
    {
    //after start up the program, you can using terminal to connect the serial of gprs shield,
    //if you input 't' in the terminal, the program will execute SendTextMessage(), it will show how to send a sms message,
    //if input 'd' in the terminal, it will execute DialVoiceCall(), etc.
    // GetSignalQuality();
    delay(500);
    if (Serial.available())
    switch(Serial.read())
    {
    case 't':
    SendTextMessage();
    break;
    case 'd':
    DialVoiceCall();
    break;
    case 'h':
    SubmitHttpRequest();
    break;
    case 's':
    Send2Pachube();
    break;
    case 'q':
    GetSignalQuality();
    break;
    }
    if ( Serial1.available())
    Serial.write( Serial1.read());
    }

    ///SendTextMessage()
    ///this function is to send a sms message
    void SendTextMessage()
    {
    Serial1.print("AT+CMGF=1\r"); //Because we want to send the SMS in text mode
    delay(100);
    Serial1.println("AT + CMGS = \"+8613016490443\"");//send sms message, be careful need to add a country code before the cellphone number
    delay(100);
    Serial1.println("GSM test message!");//the content of the message
    delay(100);
    Serial1.println((char)26);//the ASCII code of the ctrl+z is 26
    delay(100);
    Serial1.println();
    }

    ///DialVoiceCall
    ///this function is to dial a voice call
    void DialVoiceCall()
    {
    Serial1.println("AT+SNFS=0");
    delay(100);
    Serial1.println("ATDxxxxxxxxxx;");//dial the number
    //mySerial.println("ATD + +8613826558615;");//dial the number
    delay(100);
    Serial1.println();
    }

    ///SubmitHttpRequest()
    ///this function is submit a http request
    ///attention:the time of delay is very important, it must be set enough
    void SubmitHttpRequest()
    {
    Serial1.println("AT+CSQ");
    delay(100);

    ShowSerialData();// this code is to show the data from gprs shield, in order to easily see the process of how the gprs shield submit a http request, and the following is for this purpose too.

    Serial1.println("AT+CGATT?");
    delay(100);

    ShowSerialData();

    Serial1.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\" ");//setting the SAPBR, the connection type is using gprs
    delay(1000);

    ShowSerialData();

    Serial1.println("AT+SAPBR=3,1,\"APN\",\"CMNET\"");//setting the APN, the second need you fill in your local apn server
    delay(4000);

    ShowSerialData();

    Serial1.println("AT+SAPBR=1,1");//setting the SAPBR, for detail you can refer to the AT command mamual
    delay(2000);

    ShowSerialData();

    Serial1.println("AT+HTTPINIT"); //init the HTTP request

    delay(2000);
    ShowSerialData();

    Serial1.println("AT+HTTPPARA=\"URL\",\"www.google. com.hk\"");// setting the httppara, the second parameter is the website you want to access
    delay(1000);

    ShowSerialData();

    Serial1.println("AT+HTTPACTION=0");//submit the request
    delay(10000);//the delay is very important, the delay time is base on the return from the website, if the return datas are very large, the time required longer.
    //while(!mySerial.available());

    ShowSerialData();

    Serial1.println("AT+HTTPREAD");// read the data from the website you access
    delay(300);

    ShowSerialData();

    Serial1.println("");
    delay(100);
    }

    ///send2Pachube()///
    ///this function is to send the sensor data to the pachube, you can see the new value in the pachube after execute this function///
    void Send2Pachube()
    {
    Serial1.println("AT+CGATT?");
    delay(100);

    ShowSerialData();

    Serial1.println("AT+CSTT=\"CMNET\"");//start task and setting the APN,
    delay(1000);

    ShowSerialData();

    Serial1.println("AT+CIICR");//bring up wireless connection
    delay(300);

    ShowSerialData();

    Serial1.println("AT+CIFSR");//get local IP adress
    delay(2000);

    ShowSerialData();

    Serial1.println("AT+CIPSPRT=0");
    delay(3000);

    ShowSerialData();

    Serial1.println("AT+CIPSTART=\"tcp\",\"api.cosm.co m\",\"8081\"");//start up the connection
    delay(2000);

    ShowSerialData();

    Serial1.println("AT+CIPSEND");//begin send data to remote server
    delay(4000);
    ShowSerialData();
    String humidity = "1031";//these 4 line code are imitate the real sensor data, because the demo did't add other sensor, so using 4 string variable to replace.
    String moisture = "1242";//you can replace these four variable to the real sensor data in your project
    String temperature = "30";//
    String barometer = "60.56";//
    Serial1.print("{\"method\": \"put\",\"resource\": \"/feeds/42742/\",\"params\"");//here is the feed you apply from pachube
    delay(500);
    ShowSerialData();
    Serial1.print(": {},\"headers\": {\"X-PachubeApiKey\":");//in here, you should replace your pachubeapikey
    delay(500);
    ShowSerialData();
    Serial1.print(" \"_cXwr5LE8qW4a296O-cDwOUvfddFer5pGmaRigPsiO0");//pachubeapikey
    delay(500);
    ShowSerialData();
    Serial1.print("jEB9OjK-W6vej56j9ItaSlIac-hgbQjxExuveD95yc8BttXc");//pachubeapikey
    delay(500);
    ShowSerialData();
    Serial1.print("Z7_seZqLVjeCOmNbEXUva45t6FL8AxOcuNS sQS\"},\"body\":");
    delay(500);
    ShowSerialData();
    Serial1.print(" {\"version\": \"1.0.0\",\"datastreams\": ");
    delay(500);
    ShowSerialData();
    Serial1.println("[{\"id\": \"01\",\"current_value\": \"" + barometer + "\"},");
    delay(500);
    ShowSerialData();
    Serial1.println("{\"id\": \"02\",\"current_value\": \"" + humidity + "\"},");
    delay(500);
    ShowSerialData();
    Serial1.println("{\"id\": \"03\",\"current_value\": \"" + moisture + "\"},");
    delay(500);
    ShowSerialData();
    Serial1.println("{\"id\": \"04\",\"current_value\": \"" + temperature + "\"}]},\"token\": \"lee\"}");


    delay(500);
    ShowSerialData();

    Serial1.println((char)26);//sending
    delay(5000);//waitting for reply, important! the time is base on the condition of internet
    Serial1.println();

    ShowSerialData();

    Serial1.println("AT+CIPCLOSE");//close the connection
    delay(100);
    ShowSerialData();
    }
    //************************************************** *****
    //GetSignalQuality();
    //************************************************** *****

    void GetSignalQuality()
    {

    Serial1.println("AT+CSQ"); //get the signal Quality
    delay(100);
    int k=0;
    while( Serial1.available()!=0)
    {
    SigQ[k]= Serial1.read();
    Serial.write(SigQ[k]);
    k+=1;
    }
    }

    void ShowSerialData()
    {
    while( Serial1.available()!=0)
    Serial.write( Serial1.read());
    }

مجوز های ارسال و ویرایش

  • شما نمیتوانید موضوع جدیدی ارسال کنید
  • شما امکان ارسال پاسخ را ندارید
  • شما نمیتوانید فایل پیوست کنید.
  • شما نمیتوانید پست های خود را ویرایش کنید
  •  

SEO by vBSEO