Joined: 27/01/2013(UTC) Posts: 3 Location: Dortmund
|
Hi there, I have a similar project, I send messages directly to my CS2 via WLAN and an ESP and am looking for a solution how I can control locomotives with a throttle. Could this also be done with your solution? Could you adapt your sketch so that it sends it directly to the CS2? Do you have some instructions for the hardware and the sketch, can I buy that? Here is an example of my solution. Quote:#include <ESP8266WiFi.h> #include <WiFiUdp.h>
const char* ssid = "xxxxxxxx"; const char* password = "yyyyyyy";
char * HostName = "192.168.67.141"; // Zieladdr const uint16_t HostPort = 15731; // Zielport const uint16_t LocalPort = 15730;
WiFiUDP udp;
bool sendMessage(const byte * data) { if(udp.beginPacket(HostName,HostPort)) { Serial.println("beginPacket ok"); }else { Serial.println("beginPacket err"); }
udp.write(data, 13); Serial.println(" Bytes writen"); if(udp.endPacket()) { Serial.println("endPacket ok"); }else { Serial.println("endPacket err"); } } void setup(void) { Serial.begin(115200); Serial.println(""); Serial.println("setup begin"); if (WiFi.mode(WIFI_STA)) { Serial.println("to AP ok"); } else { Serial.println("to AP err"); } while (! WiFi.begin(SSID, PASSWORD)) { Serial.print(".") ; yield(); } Serial.println("WiFi begin ok");
if(WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.printf("Could not connect to %s\n", SSID); while(1) delay(500); // Brutaler Halt } while (! udp.begin(LocalPort) ) { Serial.print("+") ; yield(); } Serial.println("UDP ok"); Serial.print("setup end\r\n"); } void loop(void) { static unsigned long timestamp; byte can1[13] = {0x00, 0x23, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00}; // Besetzt Sende CAN message Adresse 1 an byte can2[13] = {0x00, 0x23, 0x03, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00}; // Besetzt Sende CAN message Adresse 1 aus
//byte can1[13] = {0x00, 0x16, 0x03, 0x00, 0x06, 0x00, 0x00, 0x38, 0x00, 0x01, 0x01, 0x00, 0x00}; // Weichen Befehl Sende CAN message //byte can2[13] = {0x00, 0x16, 0x03, 0x00, 0x06, 0x00, 0x00, 0x38, 0x00, 0x00, 0x01, 0x00, 0x00}; // Weichen Befehl Sende CAN message
byte can3[13] = {0x00, 0x16, 0x03, 0x00, 0x06, 0x00, 0x00, 0x38, 0x01, 0x01, 0x01, 0x00, 0x00}; // Weichen Befehl Sende CAN message byte can4[13] = {0x00, 0x16, 0x03, 0x00, 0x06, 0x00, 0x00, 0x38, 0x01, 0x00, 0x01, 0x00, 0x00}; // Weichen Befehl Sende CAN message
if(millis() - timestamp > 10) { sendMessage(can1); timestamp = millis(); delay(10000); sendMessage(can2); timestamp = millis(); delay(10000); sendMessage(can3); timestamp = millis(); delay(10000); sendMessage(can4); timestamp = millis(); delay(10000); } }
Thank you for a short answer Olaf
|