INFO: Dieses Forum nutzt Cookies...
Cookies sind für den Betrieb des Forums unverzichtbar. Mit der Nutzung des Forums erklärst Du dich damit einverstanden, dass wir Cookies verwenden.

Es wird in jedem Fall ein Cookie gesetzt um diesen Hinweis nicht mehr zu erhalten. Desweiteren setzen wir Google Adsense und Google Analytics ein.

Antwort schreiben 
 
Themabewertung:
  • 2 Bewertungen - 4 im Durchschnitt
  • 1
  • 2
  • 3
  • 4
  • 5
Mit dem Pin 8 und 9 vom Yun werden zwei LED wechselseitig blinken über http
02.12.2015, 12:26 (Dieser Beitrag wurde zuletzt bearbeitet: 02.12.2015 19:32 von arduinofan.)
Beitrag #1
Mit dem Pin 8 und 9 vom Yun werden zwei LED wechselseitig blinken über http
Hallo guten Tag.
Mit diesem FreeBasicprogramm werden vom Notebook aus der Pin 8 und 9 als Blinklicht geschaltet im Wechsel am Yun mit http:
------------------------------
httppost("192.168.240.1","data/put/ledhigh/10","")
Sleep 100,1
httppost("192.168.240.1","data/put/ledhigh/01","")
Sleep 100,1
------------------------------

Freebasicprogramm:
Code:
#Include "win/winsock2.bi"
#Include "windows.bi"
#Include "fbgfx.bi"
#include "vbcompat.bi"
Using FB

Dim Shared As String http_text

#DEFINE RECVBUFFLEN 16384
#Define newline CHR(13,10)

Sub InitWinsock Constructor
    '' init winsock
    DIM wsaData AS WSAData
    IF( WSAStartup( MAKEWORD( 1, 1 ), @wsaData ) <> 0 ) THEN
        PRINT "Error: WSAStartup failed"
        END 1
    END If
End SUB

SUB ExitWinsock Destructor
    WSACleanup
End SUB

FUNCTION httpget(server AS STRING, path AS STRING, hadd AS STRING = "") AS STRING
    DIM IP AS INTEGER
    DIM ia AS in_addr
    DIM s AS SOCKET
    DIM hostentry AS hostent PTR
    DIM sendbuffer AS STRING
    DIM recvbuffer AS ZSTRING * RECVBUFFLEN+1
    DIM bytes AS INTEGER
    DIM sa AS sockaddr_in
    DIM in AS STRING
    ia.S_addr = inet_addr( server )
    IF ( ia.S_addr = INADDR_NONE ) THEN
        hostentry = gethostbyname( server )
        IF ( hostentry = 0 ) THEN
            RETURN "IP couldn't be resolved!"
        END IF
        IP = *CAST( INTEGER PTR, *hostentry->h_addr_list )
    ELSE
        IP = ia.S_addr
    END IF
    s = opensocket( AF_INET, SOCK_STREAM, IPPROTO_TCP )
    IF( s = 0 ) THEN
        RETURN "Socket couldn't be opened."
    END IF
    sa.sin_port         = htons( 80 )
    sa.sin_family       = AF_INET
    sa.sin_addr.S_addr  = ip
    IF ( connect( s, CAST( PSOCKADDR, @sa ), LEN( sa )) = SOCKET_ERROR ) THEN
        closesocket( s )
        RETURN "Couldn't connect to host"
    END IF
    sendBuffer = "GET /" + path + " HTTP/1.0" + NEWLINE + _
    "Host: " + server + NEWLINE + _
    "Connection: close" + NEWLINE + _
    hadd + _
    NEWLINE
    IF( send( s, sendBuffer, LEN( sendBuffer ), 0 ) = SOCKET_ERROR ) THEN
        closesocket( s )
        RETURN "Couldn't send request"
    END IF
    DO
        bytes = recv( s, recvBuffer, RECVBUFFLEN, 0 )
        IF( bytes <= 0 ) THEN
            EXIT DO
        END IF
        recvbuffer[bytes] = 0
        in += recvbuffer
    LOOP
    shutdown( s, 2 )
    closesocket( s )
    RETURN in
END FUNCTION


FUNCTION httppost(server AS STRING, path AS STRING, toPost AS STRING, hadd AS STRING = "") AS STRING
    DIM IP AS INTEGER
    DIM ia AS in_addr
    DIM s AS SOCKET
    DIM hostentry AS hostent PTR
    DIM sendbuffer AS STRING
    DIM recvbuffer AS ZSTRING * RECVBUFFLEN+1
    DIM bytes AS INTEGER
    DIM sa AS sockaddr_in
    DIM in AS STRING

    ia.S_addr = inet_addr( server )
    IF ( ia.S_addr = INADDR_NONE ) THEN
        hostentry = gethostbyname( server )
        IF ( hostentry = 0 ) THEN
            RETURN "IP couldn't be resolved!"
        END IF
        IP = *CAST( INTEGER PTR, *hostentry->h_addr_list )
    ELSE
        IP = ia.S_addr
    END IF
    s = opensocket( AF_INET, SOCK_STREAM, IPPROTO_TCP )
    IF( s = 0 ) THEN
        RETURN "Socket couldn't be opened."
    END IF
    sa.sin_port         = htons( 80 )
    sa.sin_family       = AF_INET
    sa.sin_addr.S_addr  = ip
    IF ( connect( s, CAST( PSOCKADDR, @sa ), LEN( sa )) = SOCKET_ERROR ) THEN
        closesocket( s )
        RETURN "Couldn't connect to host"
    END IF
    sendBuffer = "POST /" + path + " HTTP/1.0" + NEWLINE + _
    "Host: " + server + NEWLINE + _
    "Content-Type: application/x-www-form-urlencoded" + NEWLINE + _
    "Content-Length: " + STR(LEN(toPost)) + NEWLINE + _
    "Connection: close" + NEWLINE + _
    hadd + _
    NEWLINE + _
    toPost + NEWLINE
    IF( send( s, sendBuffer, LEN( sendBuffer ), 0 ) = SOCKET_ERROR ) THEN
        closesocket( s )
        RETURN "Couldn't send request"
    END IF
    DO
        bytes = recv( s, recvBuffer, RECVBUFFLEN, 0 )
        IF( bytes <= 0 ) THEN
            EXIT DO
        END IF
        recvbuffer[bytes] = 0
        in += recvbuffer
    LOOP
    shutdown( s, 2 )
    closesocket( s )
    RETURN in
END Function

ScreenRes 400,500,32
ScreenControl SET_WINDOW_POS, 500, 40

Do
   If MULTIKEY(FB.SC_ESCAPE) Then
       End
   EndIf
   httppost("192.168.240.1","data/put/ledhigh/10","")
   Sleep 100,1
   httppost("192.168.240.1","data/put/ledhigh/01","")
   Sleep 100,1
   Cls
Loop

End

Yun-Programm:
Buffer[0] ist für Pin8 und Buffer[1] ist für Pin9.
Code:
#include <Bridge.h>

char buffer[16];

void setup() {
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  Bridge.begin();
}

void loop() {
  Bridge.get("ledhigh", buffer, 2);
  if (buffer[0] == '1') {
    digitalWrite(8, HIGH);
  } else {
    digitalWrite(8, LOW);
  }

  if (buffer[1] == '1') {
    digitalWrite(9, HIGH);
  } else {
    digitalWrite(9, LOW);
  }
}


Angehängte Datei(en) Thumbnail(s)
   
Alle Beiträge dieses Benutzers finden
Diese Nachricht in einer Antwort zitieren
08.12.2015, 15:03 (Dieser Beitrag wurde zuletzt bearbeitet: 08.12.2015 16:38 von arduinofan.)
Beitrag #2
RE: Mit dem Pin 8 und 9 vom Yun werden zwei LED wechselseitig blinken über http
Hier wird eine Zahl übertragen vom Yun an das Freebasicprogramm. Die Zahl wird immer 1 erhöht und angezeigt im Screen.
-------------------------------------
http_text=httpget("192.168.240.1","data/get/analog0","")
-------------------------------------

Im Freebasicprogramm ändert sich immer nur die Do/Loop-Schleife.

Freebasicprogramm:
Code:
#Include "win/winsock2.bi"
#Include "windows.bi"
#Include "fbgfx.bi"
#include "vbcompat.bi"
Using FB

Dim Shared As String http_text

#DEFINE RECVBUFFLEN 16384
#Define newline CHR(13,10)

Sub InitWinsock Constructor
    '' init winsock
    DIM wsaData AS WSAData
    IF( WSAStartup( MAKEWORD( 1, 1 ), @wsaData ) <> 0 ) THEN
        PRINT "Error: WSAStartup failed"
        END 1
    END If
End SUB

SUB ExitWinsock Destructor
    WSACleanup
End SUB

FUNCTION httpget(server AS STRING, path AS STRING, hadd AS STRING = "") AS STRING
    DIM IP AS INTEGER
    DIM ia AS in_addr
    DIM s AS SOCKET
    DIM hostentry AS hostent PTR
    DIM sendbuffer AS STRING
    DIM recvbuffer AS ZSTRING * RECVBUFFLEN+1
    DIM bytes AS INTEGER
    DIM sa AS sockaddr_in
    DIM in AS STRING
    ia.S_addr = inet_addr( server )
    IF ( ia.S_addr = INADDR_NONE ) THEN
        hostentry = gethostbyname( server )
        IF ( hostentry = 0 ) THEN
            RETURN "IP couldn't be resolved!"
        END IF
        IP = *CAST( INTEGER PTR, *hostentry->h_addr_list )
    ELSE
        IP = ia.S_addr
    END IF
    s = opensocket( AF_INET, SOCK_STREAM, IPPROTO_TCP )
    IF( s = 0 ) THEN
        RETURN "Socket couldn't be opened."
    END IF
    sa.sin_port         = htons( 80 )
    sa.sin_family       = AF_INET
    sa.sin_addr.S_addr  = ip
    IF ( connect( s, CAST( PSOCKADDR, @sa ), LEN( sa )) = SOCKET_ERROR ) THEN
        closesocket( s )
        RETURN "Couldn't connect to host"
    END IF
    sendBuffer = "GET /" + path + " HTTP/1.0" + NEWLINE + _
    "Host: " + server + NEWLINE + _
    "Connection: close" + NEWLINE + _
    hadd + _
    NEWLINE
    IF( send( s, sendBuffer, LEN( sendBuffer ), 0 ) = SOCKET_ERROR ) THEN
        closesocket( s )
        RETURN "Couldn't send request"
    END IF
    DO
        bytes = recv( s, recvBuffer, RECVBUFFLEN, 0 )
        IF( bytes <= 0 ) THEN
            EXIT DO
        END IF
        recvbuffer[bytes] = 0
        in += recvbuffer
    LOOP
    shutdown( s, 2 )
    closesocket( s )
    RETURN in
END FUNCTION


FUNCTION httppost(server AS STRING, path AS STRING, toPost AS STRING, hadd AS STRING = "") AS STRING
    DIM IP AS INTEGER
    DIM ia AS in_addr
    DIM s AS SOCKET
    DIM hostentry AS hostent PTR
    DIM sendbuffer AS STRING
    DIM recvbuffer AS ZSTRING * RECVBUFFLEN+1
    DIM bytes AS INTEGER
    DIM sa AS sockaddr_in
    DIM in AS STRING

    ia.S_addr = inet_addr( server )
    IF ( ia.S_addr = INADDR_NONE ) THEN
        hostentry = gethostbyname( server )
        IF ( hostentry = 0 ) THEN
            RETURN "IP couldn't be resolved!"
        END IF
        IP = *CAST( INTEGER PTR, *hostentry->h_addr_list )
    ELSE
        IP = ia.S_addr
    END IF
    s = opensocket( AF_INET, SOCK_STREAM, IPPROTO_TCP )
    IF( s = 0 ) THEN
        RETURN "Socket couldn't be opened."
    END IF
    sa.sin_port         = htons( 80 )
    sa.sin_family       = AF_INET
    sa.sin_addr.S_addr  = ip
    IF ( connect( s, CAST( PSOCKADDR, @sa ), LEN( sa )) = SOCKET_ERROR ) THEN
        closesocket( s )
        RETURN "Couldn't connect to host"
    END IF
    sendBuffer = "POST /" + path + " HTTP/1.0" + NEWLINE + _
    "Host: " + server + NEWLINE + _
    "Content-Type: application/x-www-form-urlencoded" + NEWLINE + _
    "Content-Length: " + STR(LEN(toPost)) + NEWLINE + _
    "Connection: close" + NEWLINE + _
    hadd + _
    NEWLINE + _
    toPost + NEWLINE
    IF( send( s, sendBuffer, LEN( sendBuffer ), 0 ) = SOCKET_ERROR ) THEN
        closesocket( s )
        RETURN "Couldn't send request"
    END IF
    DO
        bytes = recv( s, recvBuffer, RECVBUFFLEN, 0 )
        IF( bytes <= 0 ) THEN
            EXIT DO
        END IF
        recvbuffer[bytes] = 0
        in += recvbuffer
    LOOP
    shutdown( s, 2 )
    closesocket( s )
    RETURN in
END Function

ScreenRes 400,500,32
ScreenControl SET_WINDOW_POS, 500, 40

Do
   If MULTIKEY(FB.SC_ESCAPE) Then
       End
   EndIf
   http_text=httpget("192.168.240.1","data/get/analog0","")
   PRINT http_text
   Sleep 100
Loop

End

Yun-Programm :
Code:
#include <Bridge.h>

int zahl;

void setup() {
  pinMode(A0, INPUT);
  Bridge.begin();
}

void loop() {
  zahladd();
  Bridge.put("analog0", String(zahl,DEC));
  delay(100);
}

void zahladd() {
  zahl=zahl+1;
}
Alle Beiträge dieses Benutzers finden
Diese Nachricht in einer Antwort zitieren
Antwort schreiben 


Möglicherweise verwandte Themen...
Thema: Verfasser Antworten: Ansichten: Letzter Beitrag
  Zwei identische I2C sensoren an einem Bus Karli 19 747 02.04.2018 22:13
Letzter Beitrag: Harry
  UDP Paket über UART senden (nicht SPI) Wampo 9 540 04.03.2018 18:49
Letzter Beitrag: Tommy56
  HTTP-Server lesen / SIM800l AT+HTTPREAD uk1408 1 220 22.02.2018 19:25
Letzter Beitrag: georg01
  Variablenname über Schleife deklarieren golden_eagle 9 446 12.02.2018 22:36
Letzter Beitrag: golden_eagle
  Led über ethernet schalten dahans 2 328 06.02.2018 20:01
Letzter Beitrag: hotsystems
  Servo über Transistor Harry 21 1.586 29.01.2018 19:30
Letzter Beitrag: hotsystems
  Code muss um 73Bytes reduziert werden! Wie? torsten_156 13 945 25.01.2018 21:56
Letzter Beitrag: torsten_156
  Float über I2C zwischen 2 Arduinos UNO Marduino_UNO 12 3.715 13.01.2018 11:54
Letzter Beitrag: hotsystems
  2 Befehle über Serielle Schnittstelle interpretieren phischmi 14 936 04.12.2017 08:52
Letzter Beitrag: phischmi
  Taster 1 mal drücken = LED 2 mal blinken oder aufblitzen gordolino 19 2.227 03.11.2017 15:14
Letzter Beitrag: ardu_arne

Gehe zu:


Benutzer, die gerade dieses Thema anschauen: 1 Gast/Gäste