wget helper in automation?

9 years 1 month ago - 9 years 1 month ago #621 by Xavier
Hi,
Is it a way to send a wget command through automation? For me, it would be an easy way to send SMS alert for free.
My French provider (Free Mobile) offers an API that permit to send myself an SMS from the shell :
wget --ca-certificate=/usr/share/ca-certificates/freemobile/RapidSSLCABundle.crt -O /dev/null "https://smsapi.free-mobile.fr/sendmsg?user=xxxxxx&pass=yyyyyy&msg=RPi speaking..."
Response is :
--2015-02-25 20:40:02--  https://smsapi.free-mobile.fr/sendmsg?user=xxxxxx&pass=yyyyyy&msg=RPi%20speaking...
Résolution de smsapi.free-mobile.fr (smsapi.free-mobile.fr)... 212.27.40.200
Connexion vers smsapi.free-mobile.fr (smsapi.free-mobile.fr)|212.27.40.200|:443...connecté.
requête HTTP transmise, en attente de la réponse...200 OK
Longueur: non spécifié
Sauvegarde en : «/dev/null»
   [ <=>                                                          ] 0           --.-K/s   ds 0s
2015-02-25 20:40:29 (0,00 B/s) - «/dev/null» sauvegardé [0]
As you can see the wget returns only when SMS is sent, here it last 30s.
Is it a problem?

Please Log in or Create an account to join the conversation.

9 years 1 month ago #628 by EasyIoT
Replied by EasyIoT on topic wget helper in automation?

Xavier wrote: Hi,
Is it a way to send a wget command through automation? For me, it would be an easy way to send SMS alert for free.
My French provider (Free Mobile) offers an API that permit to send myself an SMS from the shell :

wget --ca-certificate=/usr/share/ca-certificates/freemobile/RapidSSLCABundle.crt -O /dev/null "https://smsapi.free-mobile.fr/sendmsg?user=xxxxxx&pass=yyyyyy&msg=RPi speaking..."
Response is :
--2015-02-25 20:40:02--  https://smsapi.free-mobile.fr/sendmsg?user=xxxxxx&pass=yyyyyy&msg=RPi%20speaking...
Résolution de smsapi.free-mobile.fr (smsapi.free-mobile.fr)... 212.27.40.200
Connexion vers smsapi.free-mobile.fr (smsapi.free-mobile.fr)|212.27.40.200|:443...connecté.
requête HTTP transmise, en attente de la réponse...200 OK
Longueur: non spécifié
Sauvegarde en : «/dev/null»
   [ <=>                                                          ] 0           --.-K/s   ds 0s
2015-02-25 20:40:29 (0,00 B/s) - «/dev/null» sauvegardé [0]
As you can see the wget returns only when SMS is sent, here it last 30s.
Is it a problem?


Use HttpGet in program. Se example how to control ESP directly with TCP - its very similar.
http://iot-playground.com/forum/suggestion-box/93-add-tcp-relay-node-esp8266-only-needed?start=6#608
The following user(s) said Thank You: Xavier

Please Log in or Create an account to join the conversation.

9 years 1 month ago #639 by Xavier
Replied by Xavier on topic wget helper in automation?
Hi,
Here is my script. It seems to compile OK but it doesn't work. I even suspect it makes silently crash EasyIot so that I have to restart the server when an error occurs. Do you see something wrong?
const String testNode = "N2S0";
const float maxValue = 65;
const float minValue = 45;
const String hiMsg = "RPi Alert.\r";
const String loMsg = "Alert closed.\r";

const String FreeID = "xxxxxxx";
const String FreePW = "yyyyyyy";
    
/* 
  ====================================================================
  This code is running one time when program is enabled
  ====================================================================
*/
public void Setup()
{
  // Gets a NumberFormatInfo associated with the en-US culture.
  System.Globalization.NumberFormatInfo nfi = new System.Globalization.CultureInfo( "en-US", false ).NumberFormat;

  EventHelper.ModuleChangedHandler((o, m, p) =>
  {
    Console.WriteLine("\n => Alerte: " + m.Domain +" "+ m.Address + " in program id "+ Program.ProgramId.ToString()+ " property "+ p.Property + " value " + p.Value);

    if (m.Domain == "MySensors" && m.Address == testNode && p.Property == "Sensor.Humidity" && p.Value != "NaN")
    {
        float fValue = Convert.ToSingle(p.Value,nfi);    
//      float fValue = float.Parse(p.Value.Replace('.',','));
        if (fValue >= maxValue)
	      SendSMS(hiMsg);
  	    else if (fValue <= minValue)
          SendSMS(loMsg);
    }
    return true;
  });
}

/*
  ====================================================================
  This code is running periodicaly when program is enabled. 
  Cron job determine running period.
  ====================================================================
*/
public void Run()
{
}

/*
  ====================================================================
  This function send a SMS using SMSAPI from Free mobile.
  The RapidSSLCABundle.crt certificat (used by Free mobile) must be
  installed first for the https request to complete.  
  ====================================================================
*/
private void SendSMS(String message)
{
  string msgURL = "https://smsapi.free-mobile.fr/sendmsg?user="+FreeID+"&pass="+FreePW+"&msg="+message;

  Console.WriteLine("Send SMS :" + message + "/n -> Status=" );

  try
  {	
	System.Net.WebRequest request = System.Net.WebRequest.Create(msgURL);
    request.Method = System.Net.WebRequestMethods.Http.Head;
    System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
    Console.WriteLine(response.StatusCode);
*/
    // Close everything.
    response.Close();
  }
  catch(Exception e)
  {
    Console.WriteLine(e.StackTrace);
  }
//  return 0;
}

Please Log in or Create an account to join the conversation.

9 years 1 month ago #642 by EasyIoT
Replied by EasyIoT on topic wget helper in automation?

Xavier wrote: Hi,
Here is my script. It seems to compile OK but it doesn't work. I even suspect it makes silently crash EasyIot so that I have to restart the server when an error occurs. Do you see something wrong?

const String testNode = "N2S0";
const float maxValue = 65;
const float minValue = 45;
const String hiMsg = "RPi Alert.\r";
const String loMsg = "Alert closed.\r";

const String FreeID = "xxxxxxx";
const String FreePW = "yyyyyyy";
    
/* 
  ====================================================================
  This code is running one time when program is enabled
  ====================================================================
*/
public void Setup()
{
  // Gets a NumberFormatInfo associated with the en-US culture.
  System.Globalization.NumberFormatInfo nfi = new System.Globalization.CultureInfo( "en-US", false ).NumberFormat;

  EventHelper.ModuleChangedHandler((o, m, p) =>
  {
    Console.WriteLine("\n => Alerte: " + m.Domain +" "+ m.Address + " in program id "+ Program.ProgramId.ToString()+ " property "+ p.Property + " value " + p.Value);

    if (m.Domain == "MySensors" && m.Address == testNode && p.Property == "Sensor.Humidity" && p.Value != "NaN")
    {
        float fValue = Convert.ToSingle(p.Value,nfi);    
//      float fValue = float.Parse(p.Value.Replace('.',','));
        if (fValue >= maxValue)
	      SendSMS(hiMsg);
  	    else if (fValue <= minValue)
          SendSMS(loMsg);
    }
    return true;
  });
}

/*
  ====================================================================
  This code is running periodicaly when program is enabled. 
  Cron job determine running period.
  ====================================================================
*/
public void Run()
{
}

/*
  ====================================================================
  This function send a SMS using SMSAPI from Free mobile.
  The RapidSSLCABundle.crt certificat (used by Free mobile) must be
  installed first for the https request to complete.  
  ====================================================================
*/
private void SendSMS(String message)
{
  string msgURL = "https://smsapi.free-mobile.fr/sendmsg?user="+FreeID+"&pass="+FreePW+"&msg="+message;

  Console.WriteLine("Send SMS :" + message + "/n -> Status=" );

  try
  {	
	System.Net.WebRequest request = System.Net.WebRequest.Create(msgURL);
    request.Method = System.Net.WebRequestMethods.Http.Head;
    System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
    Console.WriteLine(response.StatusCode);
*/
    // Close everything.
    response.Close();
  }
  catch(Exception e)
  {
    Console.WriteLine(e.StackTrace);
  }
//  return 0;
}


In current beta Automation can crash EasyIoT so put code in try catch. I fixed that in my development version and fix will be included in next release. Use console mode and console output to debug your program

Please Log in or Create an account to join the conversation.

9 years 1 month ago - 9 years 1 month ago #649 by Xavier
Replied by Xavier on topic wget helper in automation?

EasyIoT wrote: Use console mode and console output to debug your program

It took me a while to understand this sentence :pinch: .
I didn't understand neither why I get no debug messages in the systemlogger.log...
Then I remember that I make EasyIoT run as a service, so no more stdout display.
When runing as an App (as you suggest), I get this:
sudo mono EasyIoT.exe
2015-02-27T22:13:18.1279240+01:00       INFO    System                          System started
2015-02-27T22:13:24.5143530+01:00       DEBUG   MySensors                               Mysensors Start driver.
2015-02-27T22:13:24.9956220+01:00       DEBUG   MySensors                               Read: 2-2-0 s=0,c=1,t=1,pt=7,l=5:62.5
2015-02-27T22:13:25.3717880+01:00       INFO    MySensors       N2S0    Sensor.Humidity 62.5    -
=> Alerte: MySensors N2S0 in program id 16 property Sensor.Humidity value 62.6
2015-02-27T22:14:28.7547280+01:00       DEBUG   MySensors                               Read: 2-2-0 s=1,c=1,t=0,pt=7,l=5:20.3
2015-02-27T22:14:28.7621220+01:00       INFO    MySensors       N2S1    Sensor.Temperature      20.3    -
2015-02-27T22:14:28.7907270+01:00       DEBUG   MySensors                               Read: 2-2-0 s=2,c=1,t=1,pt=7,l=5:61.4
2015-02-27T22:14:28.7979410+01:00       INFO    MySensors       N2S2    Sensor.Humidity 61.4    -
2015-02-27T22:14:28.8217310+01:00       DEBUG   MySensors                               Read: 2-2-0 s=3,c=1,t=0,pt=7,l=5:20.0
2015-02-27T22:14:28.8287340+01:00       INFO    MySensors       N2S3    Sensor.Temperature      20.0    -

Unhandled Exception:
System.FormatException: Input string was not in the correct format
  at System.Double.Parse (System.String s, NumberStyles style, IFormatProvider provider) [0x00000] in <filename unknown>:0
  at System.Single.Parse (System.String s, IFormatProvider provider) [0x00000] in <filename unknown>:0
  at System.Convert.ToSingle (System.String value, IFormatProvider provider) [0x00000] in <filename unknown>:0
  at EasyIoT.Automation.ScriptingInstance+<Setup>c__AnonStorey0.<>m__0 (System.Object o, GenericDriver.Module m, GenericDriver.DriverPropertyChangedData p) [0x00000] in <filename unknown>:0
  at EasyIoT.Automation.AutomationEngine.ProcessEvent (System.Object source, GenericDriver.Module m, GenericDriver.DriverPropertyChangedData obj) [0x00000] in <filename unknown>:0
  at EasyIoT.EasyIoTService.processInternalEvent (System.Object eventData) [0x00000] in <filename unknown>:0
OK! I'm no more in the dark, I see that my attempt to convert string to float fails at the first occurrence, because in France the decimal separator is a coma, not a dot a standard parse() don't work . And my NumberFormatInfo argument seems not taken into account.
Nota:
Timestamps tell that it takes between 30 - 60s for automation task to startup.

Please Log in or Create an account to join the conversation.

9 years 1 month ago #678 by EasyIoT
Replied by EasyIoT on topic wget helper in automation?
You can try p.DecimalValue property to read numeric value.

Please Log in or Create an account to join the conversation.

Time to create page: 0.309 seconds

Forum latest

  • No posts to display.