Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface mbed-rtos
Fork of Telemetrie_eth_h2m by
Revision 0:c0179f2ad295, committed 2015-05-02
- Comitter:
- HMFK03LST1
- Date:
- Sat May 02 11:02:37 2015 +0000
- Child:
- 1:2df3958877f2
- Commit message:
- 1.0
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Sat May 02 11:02:37 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/HMFK03LST1/code/EthernetInterface/#7ffdbf314299
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Telemetry.cpp Sat May 02 11:02:37 2015 +0000
@@ -0,0 +1,307 @@
+/*------------------------------------------------*/
+/*Autor: Sebastian Hauzenberger */
+/*------------------------------------------------*/
+
+#include "Telemetry.h"
+
+//LEDs definieren
+#ifdef LED
+DigitalOut ledserial(LED1);
+DigitalOut ledeth(LED2);
+DigitalOut ledsock(LED3);
+DigitalOut ledwfa(LED4);
+#endif
+
+//Serielle Schnittstelle definieren
+#ifdef DEBUG
+Serial serial(USBTX, USBRX);
+#endif
+
+//Konstruktoren
+EthernetInterface eth;
+TCPSocketConnection sock_tcp;
+UDPSocket sock_udp;
+
+
+//Globale Variable
+bool InitSucceed = false;
+
+Telemetry::Telemetry()
+{
+
+}
+
+#ifdef DEBUG
+void Telemetry::InitUSBSerialConnection()
+{
+ serial.baud(115200);
+
+ #ifdef LED
+ ledserial = 1;
+ #endif
+}
+#endif
+
+//Funktion überladen. Ohne Parameter DHCP und 10 Sekunden Timeout. Mit Parameter kein DHCP und 3 Sekunden Timeout
+bool Telemetry::InitEthernetConnection()
+{
+ bool ReturnValue = false;
+ #ifdef DEBUG
+ serial.printf("Initalisiere LAN Verbindung mit DHCP\r\n\r\n");
+ #endif
+
+ //Schnittstelle nur einmal initialisieren, sonst gibt es Fehler!
+ if (!InitSucceed)
+ {
+ if (eth.init()==0) //Init Interface
+ {
+ InitSucceed = true;
+ ReturnValue = true;
+ }
+ }
+
+ //Nur wenn Initialisierung erfolgreich war!
+ if (InitSucceed)
+ {
+ #ifdef DEBUG
+ serial.printf("Verbinde\r\n\r\n");
+ #endif
+
+ if (eth.connect(10000)==0) //CONNECT
+ {
+ #ifdef DEBUG
+ serial.printf("IP Adresse: %s\r\n\r\n", eth.getIPAddress());
+ #endif
+
+ #ifdef LED
+ ledeth = 1;
+ #endif
+
+ ReturnValue = true;
+ }
+ else
+ {
+ ReturnValue = false;
+ }
+ }
+
+ #ifdef DEBUG
+ if (ReturnValue == false)
+ {
+ serial.printf("Fehlgeschlagen!");
+ }
+ #endif
+
+ return ReturnValue;
+}
+
+bool Telemetry::InitEthernetConnection(const char* IPAdress, const char* SubNetMask, const char* GateWay)
+{
+ bool ReturnValue = false;
+ #ifdef DEBUG
+ serial.printf("Initalisiere LAN Verbindung ohne DHCP\r\n\r\n");
+ serial.printf("IP: %s - GateWay: %s - SubNetMask: %s\r\n\r\n",IPAdress, GateWay, SubNetMask);
+ #endif
+
+ //Schnittstelle nur einmal initialisieren, sonst gibt es Fehler!
+ if (!InitSucceed)
+ {
+ if (eth.init(IPAdress, SubNetMask, GateWay)==0) //Init Interface
+ {
+ InitSucceed = true;
+ ReturnValue = true;
+ }
+ }
+
+ //Nur wenn Initialisierung erfolgreich war!
+ if (InitSucceed)
+ {
+ #ifdef DEBUG
+ serial.printf("Verbinde\r\n\r\n");
+ #endif
+
+ if (eth.connect(3000)==0) //CONNECT
+ {
+ #ifdef DEBUG
+ serial.printf("IP Adresse: %s\r\n\r\n", eth.getIPAddress());
+ #endif
+
+ #ifdef LED
+ ledeth = 1;
+ #endif
+
+ ReturnValue = true;
+ }
+ else
+ {
+ ReturnValue = false;
+ }
+ }
+
+ #ifdef DEBUG
+ if (ReturnValue == false)
+ {
+ serial.printf("Fehlgeschlagen!");
+ }
+ #endif
+
+ return ReturnValue;
+}
+
+
+void Telemetry::CloseEthernetConnection()
+{
+ eth.disconnect();
+
+ #ifdef DEBUG
+ serial.printf("LAN Verbindung geschlossen\r\n\r\n");
+ #endif
+
+ #ifdef LED
+ ledeth = 0;
+ #endif
+}
+
+void Telemetry::ConnectSocket_tcp(char* Host)
+{
+ sock_tcp.connect(Host, 80);
+
+ #ifdef DEBUG
+ serial.printf("TCP Socket geoeffnet.\r\n\r\n");
+ #endif
+
+ #ifdef LED
+ ledsock = 1;
+ #endif
+}
+
+void Telemetry::ConnectSocket_udp()
+{
+ sock_udp.init();
+
+ #ifdef DEBUG
+ serial.printf("UDP Socket geoeffnet.\r\n\r\n");
+ #endif
+
+}
+
+void Telemetry::CloseSocket()
+{
+ sock_tcp.close();
+ sock_udp.close();
+
+ #ifdef DEBUG
+ serial.printf("TCP/UDP Socket geschlossen.\r\n\r\n");
+ #endif
+
+ #ifdef LED
+ ledsock = 0;
+ #endif
+}
+
+
+void Telemetry::TransmitDataoverUDP(char *Host, int Port, string Daten)
+{
+ Endpoint data_server;
+ data_server.set_address(Host, Port);
+//Umwandeln in char*
+ const char *DataBuf = Daten.c_str();
+ char DataPaket[Daten.length()];
+ strcpy(DataPaket,DataBuf);
+
+ #ifdef DEBUG
+ serial.printf("----\r\n%s----\r\n\r\n",DataPaket);
+ serial.printf("Sende Paket UDP.\r\n\r\n");
+ #endif
+
+ sock_udp.sendTo(data_server, DataPaket, sizeof(DataPaket));
+
+ #ifdef DEBUG
+ serial.printf("UDP Paket gesendet.\r\n\r\n");
+ #endif
+}
+
+void Telemetry::TransmitDataoverTCP(char *Host, string Daten, string Username, string Passwort)
+{
+ #ifdef LED
+ ledwfa = 1;
+ #endif
+
+ //Datenpaket schnüren
+ string DATEN = "Username=" + Username + "&Passwort=" + Passwort + "&Paket=" + Daten;
+
+ string POST = "POST /H2MClient/h2m_client.php HTTP/1.1\r\n";
+
+ string HostString = Host;
+ string HOST = "Host: " + HostString + "\r\n";
+
+ string CONTENTTYPE = "Content-Type: application/x-www-form-urlencoded\r\n";
+
+ string Length;
+ stringstream convert;
+ convert << DATEN.length();
+ Length = convert.str();
+ string CONTENTLENGTH = "Content-Length: " + Length + "\r\n\r\n";
+
+
+ string datenpaket = POST + HOST + CONTENTTYPE + CONTENTLENGTH + DATEN + "\r\n";
+
+ //Umwandeln in char*
+ const char *DataBuf = datenpaket.c_str();
+ char DataPaket[datenpaket.length()];
+ strcpy(DataPaket,DataBuf);
+
+ #ifdef DEBUG
+ serial.printf("----\r\n%s----\r\n\r\n",DataPaket);
+ serial.printf("Sende Paket.\r\n\r\n");
+ #endif
+
+ sock_tcp.send_all(DataPaket, sizeof(DataPaket)-1);
+
+ #ifdef DEBUG
+ serial.printf("Paket gesendet.\r\n\r\n");
+ #endif
+
+ #ifdef LED
+ ledwfa = 0;
+ #endif
+}
+
+void Telemetry::ReadAnswerandTransmitoverSerial()
+{
+ char buffer[300];
+ int ret;
+ while (true)
+ {
+ ret = sock_tcp.receive(buffer, sizeof(buffer)-1);
+ if (ret <= 0)
+ break;
+ buffer[ret] = '\0';
+
+ #ifdef DEBUG
+ serial.printf("Received %d chars from server:\n%s\n", ret, buffer);
+ #endif
+ }
+ #ifdef LED
+ ledwfa = 0;
+ #endif
+}
+
+string Telemetry::ReadAnswer()
+{
+ char buffer[300];
+ int ret;
+ while (true)
+ {
+ ret = sock_tcp.receive(buffer, sizeof(buffer)-1);
+ if (ret <= 0)
+ break;
+ buffer[ret] = '\0';
+ }
+
+ #ifdef LED
+ ledwfa = 0;
+ #endif
+
+ return buffer;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Telemetry.h Sat May 02 11:02:37 2015 +0000
@@ -0,0 +1,48 @@
+/*------------------------------------------------*/
+/*Define LED: */
+/* LEDSerial = LED1 */
+/* LEDEthernet = LED2 */
+/* LEDSOCKET = LED3 */
+/* LEDWAITFORANSWER = LED4 */
+/* */
+/*Define DEBUG: */
+/* Ausgabe über Serielle Schnittstelle */
+/* (USBRX/USBTX) */
+/* */
+/*Autor: Sebastian Hauzenberger */
+/*------------------------------------------------*/
+
+#ifndef TELEMETRY
+#define TELEMETRY
+
+//#define LED
+#define DEBUG
+
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include <string>
+#include <sstream>
+
+class Telemetry
+{
+ public:
+ Telemetry();
+ #ifdef DEBUG
+ void InitUSBSerialConnection();
+ #endif
+
+ bool InitEthernetConnection();
+ bool InitEthernetConnection(const char* IPAdress, const char* SubNetMask, const char* GateWay);
+ void CloseEthernetConnection();
+
+ void ConnectSocket_tcp(char* Host);
+ void ConnectSocket_udp();
+ void CloseSocket();
+
+ void TransmitDataoverUDP(char *Host, int Port, string Daten);
+ void TransmitDataoverTCP(char *Host, string Daten, string Username, string Passwort);
+ void ReadAnswerandTransmitoverSerial();
+ string ReadAnswer();
+};
+
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Sat May 02 11:02:37 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/HMFK03LST1/code/mbed-rtos/#993a398629de
