Beispiel #1
0
        static void ModemInitTimer(object state)
        {
            // If we don't detect "Call Ready" from the modem, set the baud rate to the correct setting
            seeedStudioGSM gprs = (seeedStudioGSM)state;

            gprs.SIM900_SetBaudRate(gsmbaudrate);
            InitTimeout = true;
        }
Beispiel #2
0
        static void Cellular_thread()
        {
            seeedStudioGSM gprs = new seeedStudioGSM(gsmcomport, gsmbaudrate);

            seeedStudioGSM.SIM900_TogglePower();                                // If this actually powers down the modem we catch the power down message and TogglePower again through the DataHandler
            Timer ModemInitTimeout = new Timer(ModemInitTimer, gprs, 30000, 0); // Timeout for modem to return "Call Ready". If it doesn't we assume the modem is set to the wrong baud rate and send baud rate command

            int    requestedSwitch;
            string requestedAction;
            bool   WhitelistExists      = false;
            bool   MasterCellExists     = false;
            bool   MasterPasswordExists = false;
            string MasterPassword       = "";

            string[] Whitelist  = { "" };
            string   MasterCell = "";

#if (XBEE)
            XBee xbee = new XBee(XBeeComport, XBeeBaudRate);
#endif

#if (LCD)
            lcdMessageLine1 = System.Text.Encoding.UTF8.GetBytes("  Powering up");
            lcdMessageLine2 = System.Text.Encoding.UTF8.GetBytes("     Modem");
#endif

            while (InitTimeout == false && seeedStudioGSM.ModemReady == false)
            {
                Thread.Sleep(100); // Wait for either the modem to report "Call Ready" or for 30 seconds to pass
            }

            ModemInitTimeout.Dispose(); // Dispose of timer even if never fired.

            #if (LCD)
            lcdMessageLine1 = System.Text.Encoding.UTF8.GetBytes("  Initializing");
            lcdMessageLine2 = System.Text.Encoding.UTF8.GetBytes("     Modem");
            #endif

            gprs.SIM900_FirmwareVersion();
            gprs.SIM900_SignalQuality();
            gprs.SIM900_SetTime();

            // Display signal quality on LCD
#if (LCD)
            lcdMessageLine1 = System.Text.Encoding.UTF8.GetBytes("Signal Quality:");
#endif
            // Excellent Signal
            if ((seeedStudioGSM.SignalStrength >= 20) && (seeedStudioGSM.SignalStrength <= 31))
            {
                Debug.Print("Signal: Excellent");
#if (LCD)
                lcdMessageLine2 = System.Text.Encoding.UTF8.GetBytes("   Excellent");
#endif
            }
            // Good Signal
            else if ((seeedStudioGSM.SignalStrength >= 13) && (seeedStudioGSM.SignalStrength <= 19))
            {
                Debug.Print("Signal: Good");
#if (LCD)
                lcdMessageLine2 = System.Text.Encoding.UTF8.GetBytes("      Good");
#endif
            }
            // Poor Signal
            else if ((seeedStudioGSM.SignalStrength >= 0) && (seeedStudioGSM.SignalStrength <= 12))
            {
                Debug.Print("Signal: Poor");
#if (LCD)
                lcdMessageLine2 = System.Text.Encoding.UTF8.GetBytes("      Poor");
#endif
            }
            // No Signal
            else if (seeedStudioGSM.SignalStrength == 99)
            {
                Debug.Print("No Signal");
#if (LCD)
                lcdMessageLine2 = System.Text.Encoding.UTF8.GetBytes("      None");
#endif
            }
            else
            {
                Debug.Print("Unknown Signal");
#if (LCD)
                lcdMessageLine2 = System.Text.Encoding.UTF8.GetBytes("     Unknown");
#endif
            }

            gprs.InitializeSMS();
            gprs.DeleteAllSMS();

            // File containing Cellphone number to send initialization SMS too
            if (File.Exists(@"SD\settings\MasterCell.txt"))
            {
                MasterCell       = FileTools.ReadString("settings\\MasterCell.txt").TrimEnd(';');
                MasterCellExists = true;
            }
            // File containing Master password to allow whitelist additions
            if (File.Exists(@"SD\settings\MasterPassword.txt"))
            {
                MasterPassword       = FileTools.ReadString("settings\\MasterPassword.txt").TrimEnd(';');
                MasterPasswordExists = true;
            }
            // File containing cellphone numbers to accept commands from, all others are ignored, splitting on + sign since we get a single string back from the file read
            if (File.Exists(@"SD\settings\Whitelist.txt"))
            {
                Whitelist       = FileTools.ReadString("settings\\Whitelist.txt").Split(';');
                WhitelistExists = true;
            }
            // Send SMS to default number saying we are up!
            if (MasterCellExists)
            {
                //gprs.SendSMS(MasterCell, "Remote switch controller operational at " + DateTime.Now.ToString());
            }

            while (true)
            {
                if (seeedStudioGSM.LastMessage > 0)
                {
                    gprs.ReadSMS(seeedStudioGSM.LastMessage);
                    gprs.DeleteAllSMS();
                    if (File.Exists(@"SD\Temp\SMS.cmd"))
                    {
                        string   ReplySMS = "";
                        string[] command  = FileTools.ReadString("Temp\\SMS.cmd").Split(';');
                        File.Delete(@"SD\Temp\SMS.cmd");
                        Debug.Print("Commands: " + command[0] + "   " + command[1]);

                        if (WhitelistExists && MasterCellExists && MasterPasswordExists) // We have a whitelist, master phone and password otherwise first message is new master phone
                        {
                            if (CheckNumberWhitelist(command[0], Whitelist))             // Make sure incoming message was sent from allowed number
                            {
                                if (command[1].Substring(0, 8).ToUpper() == "PASSWORD" && MasterCell == command[0])
                                {
                                    byte[] master = SHA.computeSHA1(System.Text.Encoding.UTF8.GetBytes(command[1].Trim().Substring(9))); //Encode Master password
                                    MasterPassword = "";                                                                                 // Clear variable before calculating new password
                                    foreach (byte b in master)
                                    {
                                        MasterPassword = MasterPassword + b.ToString();
                                    }
                                    File.Delete(@"SD\settings\MasterPassword.txt");
                                    FileTools.New("MasterPassword.txt", "settings", MasterPassword); // Encode master password and add to whitelist
                                    gprs.SendSMS(command[0], "New password " + command[1].Substring(9) + " saved");
                                }
#if (WEB)
                                else if (command[1].ToUpper() == "SHOWIP" && MasterCell == command[0]) // Return current IP if requested
                                {
                                    gprs.SendSMS(command[0], "IP Address: " + Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IPAddress);
                                }
#endif
                                else
                                {
                                    if (command[1].Trim().ToUpper().Substring(0, command[1].Trim().Length - 1) == "ALL")
                                    {
                                        requestedSwitch = 0;
                                    }
                                    else
                                    {
                                        requestedSwitch = int.Parse(command[1].Trim().ToUpper().Substring(0, command[1].Trim().Length - 1));
                                    }

                                    requestedAction = command[1].Trim().ToUpper().Substring(command[1].Trim().Length - 1, 1);

                                    if (requestedAction == "+")
                                    {
                                        if (requestedSwitch == 0) // 0 = ALL
                                        {
                                            for (int i = 0; i < NumSwitches; i++)
                                            {
                                                Relay.On(i + 1);
                                                SWState[i] = "On  ";
                                            }
#if (XBEE)
                                            for (int i = 0; i < NumXbeeSwitches; i++)
                                            {
                                                xbee.SetDigitalOutput(XBeeAddress[i], XBeeCommand.ADio0Configuration, true);
                                                SWState[i + 4] = "On  ";
                                            }
#endif
                                            ReplySMS = DateTime.Now.ToString() + ": All switches turned On.";
                                        }
                                        else if (requestedSwitch <= 4) // Turn on numbered switch
                                        {
                                            Relay.On(requestedSwitch);
                                            ReplySMS = DateTime.Now.ToString() + ": Switch " + requestedSwitch + " was turned On";
                                            SWState[requestedSwitch - 1] = "On  ";
                                        }
                                        else if (requestedSwitch > 4) // Turn on Xbee numbered switch
                                        {
                                            xbee.SetDigitalOutput(XBeeAddress[requestedSwitch - 5], XBeeCommand.ADio0Configuration, true);

                                            Debug.Print("Xbee: " + XBeeAddress[requestedSwitch - 5]);

                                            ReplySMS = DateTime.Now.ToString() + ": Xbee Switch " + requestedSwitch + " was turned On";
                                            SWState[requestedSwitch - 1] = "On  ";
                                        }
                                        else
                                        {
                                            ReplySMS = DateTime.Now.ToString() + ": Error turning On Switch " + requestedSwitch;
                                        }
                                    }
                                    else if (requestedAction == "-")
                                    {
                                        if (requestedSwitch == 0)
                                        {
                                            for (int i = 0; i < NumSwitches; i++)
                                            {
                                                Relay.Off(i + 1);
                                                SWState[i] = "Off ";
                                            }
#if (XBEE)
                                            for (int i = 0; i < NumXbeeSwitches; i++)
                                            {
                                                xbee.SetDigitalOutput(XBeeAddress[i], XBeeCommand.ADio0Configuration, false);
                                                SWState[i + 4] = "Off ";
                                            }
#endif
                                            ReplySMS = DateTime.Now.ToString() + ": All switches turned Off.";
                                        }
                                        else if (requestedSwitch <= 4) // Turn off numbered switch
                                        {
                                            Relay.Off(requestedSwitch);
                                            ReplySMS = DateTime.Now.ToString() + ": Switch " + requestedSwitch + " was turned Off";
                                            SWState[requestedSwitch - 1] = "Off ";
                                        }
#if (XBEE)
                                        else if (requestedSwitch > 4) // Turn off Xbee numbered switch
                                        {
                                            xbee.SetDigitalOutput(XBeeAddress[requestedSwitch - 5], XBeeCommand.ADio0Configuration, false);

                                            Debug.Print("Xbee: " + XBeeAddress[requestedSwitch - 5]);

                                            ReplySMS = DateTime.Now.ToString() + ": Xbee Switch " + requestedSwitch + " was turned Off";
                                            SWState[requestedSwitch - 1] = "Off ";
                                        }
#endif
                                        else
                                        {
                                            ReplySMS = DateTime.Now.ToString() + ": Error turning Off Switch " + requestedSwitch;
                                        }
                                    }
                                    else if (requestedAction == "?")
                                    {
                                        if (Relay.State(requestedSwitch))
                                        {
                                            ReplySMS = DateTime.Now.ToString() + ": Switch " + requestedSwitch + " is On";
                                        }
                                        else
                                        {
                                            ReplySMS = DateTime.Now.ToString() + ": Switch " + requestedSwitch + " is Off";
                                        }
                                    }
                                    else
                                    {
                                        ReplySMS = "";
                                        Debug.Print(DateTime.Now.ToString() + ": Unknown Command: " + command[1] + " from " + command[0]);
                                        gprs.SendSMS(command[0], DateTime.Now.ToString() + ": Unknown command from " + command[0] + ": " + command[1]);
                                    }
                                    if (ReplySMS.Length > 0)
                                    {
                                        gprs.SendSMS(command[0], ReplySMS);
                                    }
                                }
                            }
                            else
                            {
                                string GivenPassword = "";

                                byte[] master = SHA.computeSHA1(System.Text.Encoding.UTF8.GetBytes(command[1])); //Encode given Master password
                                foreach (byte b in master)
                                {
                                    GivenPassword = GivenPassword + b.ToString();
                                }
                                Debug.Print("Given: " + GivenPassword + " Master: " + MasterPassword);
                                if (GivenPassword == MasterPassword)
                                {
                                    FileTools.Add("Whitelist.txt", "settings", command[0]); // Add new phone to whitelist
                                    Whitelist = FileTools.ReadString("settings\\Whitelist.txt").Split(';');
                                    gprs.SendSMS(command[0], "You have been added to the whitelist");
                                }
                                else
                                {
                                    Debug.Print(command[0] + " not in whitelist, message ignored");
                                }
                            }
                        }
                        else // No whitelist file, incoming message is from new master
                        {
                            if (command[1].IndexOf(" ") == -1) // Checks if supplied password contains a space
                            {
                                FileTools.New("MasterCell.txt", "settings", command[0]); // Add master number
                                MasterCell       = FileTools.ReadString("settings\\MasterCell.txt").TrimEnd(';');
                                MasterCellExists = true;

                                FileTools.New("Whitelist.txt", "settings", command[0]); // Add new master to whitelist
                                Whitelist       = FileTools.ReadString("settings\\Whitelist.txt").Split(';');
                                WhitelistExists = true;

                                byte[] master = SHA.computeSHA1(System.Text.Encoding.UTF8.GetBytes(command[1])); //Encode Master password
                                foreach (byte b in master)
                                {
                                    MasterPassword = MasterPassword + b.ToString();
                                }
                                Debug.Print("Master Password: "******"MasterPassword.txt", "settings", MasterPassword); // Add encoded password to file
                                MasterPassword       = FileTools.ReadString("settings\\MasterPassword.txt").TrimEnd(';');
                                MasterPasswordExists = true;

                                gprs.SendSMS(command[0], "New master phone set, password " + command[1] + " saved");
                            }
                            else
                            {
                                gprs.SendSMS(command[0], "Passwords can not contain spaces, try again");
                            }
                        }
                    }
                }
#if (LCD)
                lcdMessageLine1 = System.Text.Encoding.UTF8.GetBytes("SW1:" + SWState[0] + "SW2:" + SWState[1]);
                lcdMessageLine2 = System.Text.Encoding.UTF8.GetBytes("SW3:" + SWState[2] + "SW4:" + SWState[3]);
#endif
                Thread.Sleep(1000);
            }
        }