Simple url request / need help please


#1

Hello @all

i want only a very very simple code for:

when i get a high signal maybe on D0 then send a GET request to a url like this:

192.168.1.253:98/rawCmd?schalte=192

but i only find online some code that s for sending a message to the pushover client. but i need not so much code … the core is connected to my wifi router and only when D0 is switched from LOW to HIGH the program must make a GET Request to a url not more. Maybe someone can help me out :wink:

I post my test code so you can maybe see whats wrong or can say something

regards from Germany

Marc

// test-shys-tempsender-01


 int incomingByte;      

 byte serverIP[] = {192, 168, 2, 254 };                                 // Ip Adresse des pushempfängers


 const char * DEVID1 = "24";                              // "bei PushingBox.com Scenario code erstellen und hier einfügen" (test).
 const char * DEVID2 = "vCF39F9B6E79B3FA";                              // "bei PushingBox.com Scenario code erstellen und hier einfügen" (Es regnet).
 const char * DEVID3 = "v4AF3166D1527B8F";                              // "bei PushingBox.com Scenario code erstellen und hier einfügen" (Temperatur mehr als 30,00 Grad).

 
 
 
 const int buttonPin = D0;                                              //  Pin an dem der Button angeschlossen ist. Pin über 4,7kOhm auf GND. pin -> auf 3,3v wechselt.
                                                                        //  Wird über Relais gesteuert. 
 boolean DEBUG = true;

 int LED = D7;                                                          //  LED zeigt Status an. (nachricht gesendet oder nicht. Led blau Pin_"D7").
 const char * serverName = "http://192.168.2.254/signalInput.php?sensorId=12346&sensorWert=23";                        //  PushingBox API URL

 int buttonState = 0;                                                   //  Variable zu lesen des Pin_Status.

 TCPClient client;
 

 void setup() {
   
    Serial1.begin(9600);

    pinMode(LED, OUTPUT);                                               // LED als Output festlegen.
    
    pinMode(buttonPin, INPUT);                                          // buttonPin als Eingang festlegen.

    
    delay(1000);                                                        // Pause (1 Sekunde).

    
    }

 void loop() {

 buttonState = digitalRead(buttonPin);                                  // Pin Status lesen.

  if (buttonState == HIGH) {
      
                      sendToPushingBox(DEVID1);                         // auslösen des oben declarierten ersten Scenarios. 


    delay(60000);                                                       // Pause um Fehlarlarme zu vermeiden.
      
  }
  
  else {
      
      
   }
   
   if (Serial1.available() > 0) {

    incomingByte = Serial1.read();

     if (incomingByte == 'R') {
         
         sendToPushingBox(DEVID2);
         
         delay(60000);
         
         
        
    } 
    
    
     if (incomingByte == 'T') {
         
         sendToPushingBox(DEVID3);
         
         delay(60000);
        
    } 
              
   }         
         
 }


 void sendToPushingBox(const char * devid) {
   
   
    digitalWrite(LED, HIGH);                                             //  LED einschalten
    

    client.stop();
    
    if(DEBUG){
  }
  
    if (client.connect(serverIP, 80)) {                                  // Bitte diese Zeile für die Verwendung mit der Fritzbox verwenden! 

   //  if (client.connect(serverName, 80)) {                             // standart server aufruf. ( bei verwendung von Fritzbox auskommentieren). !
        
    if(DEBUG){
  }
  
    client.print("GET /http://192.168.2.254/signalInput.php?sensorId=12346&sensorWert=33");
    client.print(devid);
  //  client.println(" HTTP/1.1");
   // client.print("Host: ");
   // client.println(serverName);
   // client.println("User-Agent: Spark");
   // client.println();
    client.flush();
    
    if(DEBUG) {
      
       
        }
        
        digitalWrite(LED, LOW);                                          // LED ausschalten.
        
    } else {
      
        digitalWrite(LED, HIGH);                                         // LED einschalten, falls LED dauerhaft an ist war die letzte Verbindung fehlerhaft!
        
        if(DEBUG){
      }
    }
  
}

#2

Just as a side note HTTP requests need to be terminated with \r\n - hence the final println() instruction.

Also you are already connected to the server, so I’d omit /http://192.168.2.254.

You can also look here :wink:


#3

Thank you ScruffR for your answer :wink:

can you show me maybe a little example code for explain what you mean with ( \r\n - hence the final )

thank you very much :wink:


#4

The linked thread features a sample already.


#5

i know but this is wrong too , nothing worked :frowning: i tested the code you told me in the sample (link) nothing worked… then i test other code like this:

#include <string.h>
// #define LIB_DOMAIN "192.168.2.254/signalInput.php?sensorId=12346&sensorWert=30"


TCPClient client;



void setup() {
    
}

void loop() {
   // client.connect(LIB_DOMAIN, 80);
    client.println("GET /status HTTP/1.0");
    client.println("Host:192.168.2.254/signalInput.php?sensorId=12346&sensorWert=30" );
    client.println("Content-Length: 0");
    client.println();
    client.flush();

  delay(5000);
  
}

but i have no luck :frowning:

i don’t know whats the problem

regards from Germany


#6

How about this?

TCPClient client;

IPAddress serverIP(192, 168, 2, 254);  

void setup() {
    client.connect(serverIP, 80);
    client.println("GET /signalInput.php?sensorId=12346&sensorWert=30 HTTP/1.0");
    client.println("Host: 192.168.2.254");
    client.println("Content-Length: 0");
    client.println();
    client.flush();
}

If you want to do this frequently you need to take care not to use up all the free sockets by just connecting and never closing the connection.

From Austria :wink:


#7

Thank you very much :wink: it worked but sadly only one time now the spark core is flashing blue the green then red and nothing worked again :frowning: i have tried to upload it again but after the upload the same issue

regards from Germany


#8

You should be able to revert to a working device via CLI and DFU Mode

particle flash --usb tinker

#9

It is worth noting that setup is only called once and that you may not be able to remain perpetually connected to the server. You might want to also start utilizing client.stop() to disconnect or utilize if (!client.connected()) { /* connect here */ } to re-establish the connection when you’ve been disconnected.

Are you using a battery? If so, you probably don’t want to remain connected and may want to make other considerations.


#10

Exactly, hence

The snippet above is not as a best practice sample but only proof of concept (based on the not working code with minimal corrections).