Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. Arduino und RestfulAPI

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    Arduino und RestfulAPI

    This topic has been deleted. Only users with topic management privileges can see it.
    • Thisoft
      Thisoft last edited by

      Hallo zusammen,

      ich habe einen Arduino der mir mittels eines Ultraschallsensors einen Füllstand misst. Diesen möchte ich jetzt gern an ioBroker übermitteln und wollte dafür die RestfulAPI verwenden. Allerdings scheitere ich bisher daran wie ich das im Arduino-Code schreiben muss 😞 Hier mein momentaner Versuch:

      #include <ArduinoHttpClient.h>
      
      EthernetClient client;
      HttpClient http = HttpClient(client,IPString,8087); 
      String contentType = "application/x-www-form-urlencoded";       
      String postData;   
         
      postData = "?value=" + IPString;
      http.post("/set/javascript.1.Regenwasser.RegentankDuino.IP", contentType, postData);
              int statusCode = http.responseStatusCode();
              String response = http.responseBody();
      
              Serial.print("Status code: ");
              Serial.println(statusCode);
              Serial.print("Response: ");
              Serial.println(response);
      
      

      Ich erhalte da aber immer nur einen StatusCode -2 zurück. Habe auch schon verschiedene Varianten im POST und auch mal mit GET versucht und komme nicht drauf was dieser ArduinoHttpClient da von mir haben will.
      Wäre toll wenn mir da jemand weiterhelfen könnte.

      Danke schon mal.

      M 1 Reply Last reply Reply Quote 0
      • M
        MCU @Thisoft last edited by

        @thisoft Was steht denn im IPString?

        Thisoft 1 Reply Last reply Reply Quote 0
        • Thisoft
          Thisoft @MCU last edited by

          @mcu Im IPString steht die IP-Adresse - ist doch aber eigentlich auch egal solange eben irgend welche Zeichen drinstehen - oder?

          String IPString = "192.168.137.30";
          
          M 1 Reply Last reply Reply Quote 0
          • M
            MCU @Thisoft last edited by MCU

            @thisoft Wenn ich es richtig verstehe, möchtest du die IP von deinem Ardunio an ioBroker senden über simpleApi?
            https://github.com/amcewen/HttpClient/blob/master/examples/SimpleHttpExample/SimpleHttpExample.ino
            Nach dem Beispiel würde es ausreichen, wenn http.get genutzt wird (192.168.178.80 Beispiel musst du mit Deiner ioBroker-IP ersetzen)

            EthernetClient client;
            HttpClient http(client); 
            String postData;   
            String IPString = '192.168.178.30';
            postData = "?value=" + IPString;
            err = http.get('192.168.178.80:8087', '/set/javascript.1.Regenwasser.RegentankDuino.IP'+postData);
            if (err = 0)
            {
              Serial.print("Connect ok Data send");
            }
            else
            {
             Serial.print("Connect failed: ");
             Serial.println(err);
            }
            http.stop();
            
            Thisoft 1 Reply Last reply Reply Quote 0
            • Thisoft
              Thisoft @MCU last edited by

              @mcu Ja das verstehst du richtig. Wobei die IP jetzt nur erstmal zum Testen ist.
              Du hast mir auf jeden Fall erstmal weitergeholfen indem der von dir verlinkte HttpClient mir schon mal wesentlich besser zusagt als der "ArduinoHttpClient" den ich da bisher im Einsatz hatte.
              Im Prinzip klappt das wie in deinem Beispiel angegeben. Allerdings bekomme ich immer nur eine -1 in err, also "keine Verbindung" wenn ich das richtig sehe. Die Ethernet-Verbindung steht aber, zumindest kann ich über den EthernetClient erfolgreich eine MQTT-Verbindung aufbauen. Ebenso hab ich den API-Aufrufstring erfolgreich im Browser getestet.
              Ich bin im Moment etwas ratlos und mach für heute erstmal Schluss. Vielleicht habe ich ja morgen den Geistesblitz... oder du siehst meinen Fehler... 🙂

              M 1 Reply Last reply Reply Quote 0
              • M
                MCU @Thisoft last edited by MCU

                @thisoft lt. Beschreibung:

                int get(const char* aServerName, uint16_t aServerPort, const char* aURLPath, 
                            const char* aUserAgent =NULL)
                

                versuch mal den get anders:

                EthernetClient client;
                HttpClient http(client); 
                String postData;   
                String IPString = '192.168.178.30';
                postData = "?value=" + IPString;
                err = http.get('192.168.178.80',8087, '/set/javascript.1.Regenwasser.RegentankDuino.IP'+postData,'');
                if (err = 0)
                {
                  Serial.print("Connect ok Data send");
                }
                else
                {
                 Serial.print("Connect failed: ");
                 Serial.println(err);
                }
                http.stop();
                
                

                Mit WiFi hab ich es hinbekommen, EthernetClient nicht im Einsatz.
                Hab es nur mit #include <ESP8266WiFi.h> gemacht auf einem D1 Mini.

                #include <ESP8266WiFi.h>
                const char ssid[] = "DeineWLANSSID";
                const char pass[] = "DeinWLANPasswort";
                int werteCount = 0;
                //IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
                IPAddress server(192, 168, 178, 89); // ioBroker
                //char server[] = "www.google.com";    // name address for Google (using DNS)
                
                WiFiClient client;
                
                void printWiFiStatus() {
                  Serial.print("SSID: ");
                  Serial.println(WiFi.SSID());
                  IPAddress ip = WiFi.localIP();
                  Serial.print("IP Address: ");
                  Serial.println(ip);
                  long rssi = WiFi.RSSI();
                  Serial.print("signal strength (RSSI):");
                  Serial.print(rssi);
                  Serial.println(" dBm");
                }
                
                
                void setup() {
                  Serial.begin(115200);
                  WiFi.begin(ssid, pass);
                  while (WiFi.status() != WL_CONNECTED) {
                      Serial.print(".");
                      delay(1000);
                    }
                  Serial.println("Connected to wifi");
                  printWiFiStatus();
                
                  Serial.println("\nStarting connection to server...");
                  if (client.connect(server, 8087)) {
                    Serial.println("connected to server");
                    // Make a HTTP request:
                    client.println("GET /set/0_userdata.0.Power1?value=20 HTTP/1.1");
                    client.println("Connection: close");
                    client.println();
                    client.stop();
                  }
                }
                
                void loop() {
                  delay(10000);
                  if (client.connect(server, 8087)) {
                    Serial.println("connected to server");
                    // Make a HTTP request:
                    werteCount++;
                    //String Senden ="GET /set/0_userdata.0.Power1?value=" + werteCount + " HTTP/1.1";
                    //sprintf(senden,"GET /set/0_userdata.0.Power1?value=%i HTTP/1.1",werteCount);
                    String Senden = "GET /set/0_userdata.0.Power1?value=" + String(werteCount) + " HTTP/1.1";
                    client.println(Senden);
                    //client.println("Host: www.google.com");
                    client.println("Connection: close");
                    client.println();
                    Serial.println("Daten wurde gesendet");
                    client.stop();
                  }
                }
                
                Thisoft 1 Reply Last reply Reply Quote 0
                • Thisoft
                  Thisoft @MCU last edited by

                  @mcu Vielen Dank für Deine Bemühungen!

                  Ich hatte gestern nur wenig Zeit, aber dein erster Vorschlag hat so auch nicht funktioniert.

                  Der Weg mit dem ESP-WiFi dürfte funktionieren. Das habe ich mit einem ESP auch schon mal so realisiert. Allerdings will ich bei diesem Projekt explizit einen Arduino mit LAN-Verbindung verwenden da ich den LAN-Anschluss dort zur Verfügung habe und WLAN soweit möglich vermeiden möchte.

                  Das kann doch eigentlich gar nicht so schwierig sein - vermutlich übersehe ich nur irgend eine Kleinigkeit...

                  M 1 Reply Last reply Reply Quote 0
                  • M
                    MCU @Thisoft last edited by

                    @thisoft Hier noch ein anderes Beispiel

                    /*
                     * Created by ArduinoGetStarted.com
                     *
                     * This example code is in the public domain
                     *
                     * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-http-request
                     */
                    
                    #include <SPI.h>
                    #include <Ethernet.h>
                    
                    // replace the MAC address below by the MAC address printed on a sticker on the Arduino Shield 2
                    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
                    
                    EthernetClient client;
                    
                    int    HTTP_PORT   = 8087;
                    String HTTP_METHOD = "GET";
                    char   HOST_NAME[] = "192.168.178.80";
                    String PATH_NAME   = "/set/0_userdata.0.Power0";
                    String queryString = "?value=70";;
                    
                    void setup() {
                      Serial.begin(9600);
                    
                      // initialize the Ethernet shield using DHCP:
                      if (Ethernet.begin(mac) == 0) {
                        Serial.println("Failed to obtaining an IP address using DHCP");
                        while(true);
                      }
                    
                      // connect to web server on port 80:
                      if(client.connect(HOST_NAME, HTTP_PORT)) {
                        // if connected:
                        Serial.println("Connected to server");
                        // make a HTTP request:
                        // send HTTP header
                        client.println(HTTP_METHOD + " " + PATH_NAME + queryString + " HTTP/1.1");
                        client.println("Host: " + String(HOST_NAME));
                        client.println("Connection: close");
                        client.println(); // end HTTP header
                    
                        // send HTTP body
                        //client.println(queryString);
                    
                        while(client.connected()) {
                          if(client.available()){
                            // read an incoming byte from the server and print it to serial monitor:
                            char c = client.read();
                            Serial.print(c);
                          }
                        }
                    
                        // the server's disconnected, stop the client:
                        client.stop();
                        Serial.println();
                        Serial.println("disconnected");
                      } else {// if not connected:
                        Serial.println("connection failed");
                      }
                    }
                    
                    void loop() {
                    
                    }
                    
                    
                    Thisoft 1 Reply Last reply Reply Quote 1
                    • Thisoft
                      Thisoft @MCU last edited by

                      @mcu Super, das funktioniert 🙂 Du bist mein Held! Danke! Und was mir am Besten daran gefällt ist dass man keinerlei zusätzliche Lib o.ä. braucht.

                      Allerdings kommt da jetzt gleich meine nächste Frage ;-), kann man jetzt über die RestfulAPI auch was an den Arduino senden? Also so dass man innerhalb des "if(client.available())" darauf reagieren könnte?

                      M 1 Reply Last reply Reply Quote 0
                      • M
                        MCU @Thisoft last edited by

                        @thisoft Reicht es nicht, wenn du im loop mit getPlainvalue abfragst "Soll ich was holen?"
                        Oder halt direkt die Werte abfragen, die du holen willst.

                        https://github.com/ioBroker/ioBroker.simple-api

                        Thisoft 1 Reply Last reply Reply Quote 0
                        • Thisoft
                          Thisoft @MCU last edited by

                          @mcu Ja, du hast schon recht, das reicht auch.

                          Vielen Dank nochmal.

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post

                          Support us

                          ioBroker
                          Community Adapters
                          Donate

                          746
                          Online

                          31.8k
                          Users

                          80.0k
                          Topics

                          1.3m
                          Posts

                          communication
                          2
                          11
                          546
                          Loading More Posts
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes
                          Reply
                          • Reply as topic
                          Log in to reply
                          Community
                          Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                          The ioBroker Community 2014-2023
                          logo