ESP8266 : Arduino Mega Virtueller Server
ESP8266 : Arduino Mega Virtueller Server
Heute schließen wir mal ein ESP8266 WiFi Modul an den Arduino Mega an, übertragen eine Variable ins WWW und bekommen eine Antwort zurück.
Weiter unten im Test findet Ihr auch einen Code für ein ESP8266-12(E) Modul ohne Arduino für die Arduino IDE 1.6.9.
Alles nichts besonderes, dazu gibt es viele Beispiele.
Aber 99% aller Beispiele berücksichtigen nicht eine Virtuelle Server Farm hinter einer einzigen IP Adresse, mit Diesen kann man sich nämlich nicht einfach mit den normalen Beispielen die so im Netz rumschwirren verbinden !
Leider sind aber viele von euch gemietete Domains auf einem Virtuellen Server hinter einer einzigen IP gehostet .
Dazu erstmal der Anschlussplan :
Das weiter unten aufgeführte Programm verbindet euren ESP8266 mit eurem Router, ruft eine PHP Datei die sich im Verzeichnis von www.arduinoclub.de befindet auf, überträgt eine Variable zur PHP Datei und die PHP Datei sendet die Variable an euch zurück.
Zur Info : Das PHP Programm :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php /* * ESP8266 receive data and send back * Ralf Bohnen, 2013 * This example code is in the public domain. */ if (isset($_GET['a'])) { //FILTER INPUT / PHP Version >= 5.2 $a = filter_input(INPUT_GET, 'a', FILTER_SANITIZE_STRING); //you never do this : $a = $_GET['a']; //is not save } else { $a = "empty Var"; } echo("#######################\n"); echo("ARDUINO CLUB HERE !\n"); echo("Receive :".$a."\n"); echo("#######################\n"); ?> |
Das Programm für den MEGA :
Die Baudrate für den ESP8266 ist hier auf 115200 eingestellt, wenn Ihr die Bautrate eures ESP8266 einstellen wollt könnt Ihr ja mal hier reinschauen : ESP8266 : Bautrate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
/* * ESP8266 connect to Virtual Host * Ralf Bohnen, 2013 * This example code is in the public domain. */ /* ESP8266 Version : 0018000902-AI03 This program is for the MEGA or other devices with more than one serial outputs. It makes a connection from a ESP8266 network device with an access point, then it sends and receives something. WARNING ! Do never connect the 3.3V pin from the ESP8266 with the 3.3V pin of the MEGA because: ------------------------------------------------------------------------------------- MEGA description : - 3V3. A 3.3 volt supply generated by the on-board regulator. Maximum current draw is 50 mA. ------------------------------------------------------------------------------------- SUPPOSE AN EXTERNAL POWER SUPPLY WITH MINIMUM 400mA OUTPUT !!! */ String SIID = "YOUR SIID"; String PASSWORD = "YOUR PASSWORD"; void setup() { Serial.begin(115200); //FROM MEGA TO PC delay(200); Serial3.begin(115200); //FROM MEGA TO ESP8266 delay(200); if (!testESP8266()) { if (!resetESP8266 ()) {stopProgram ();} } Serial.println("VERSION: "+ getVersion() + "\n"); setMode("3"); connectAP (SIID, PASSWORD); } //END setup() void loop() { if (getConnectedState()) { update ("1234567890", "www.arduinoclub.de"); } delay(30000); } void update(String send, String IP) { if (AT("AT+CIPSTART", "=\"TCP\",\"" + IP + "\",80", 10000)) { //Build Header String cmd = "GET /esp8266.php?a=" + send + " HTTP/1.0\r\n"; cmd+= "Host: " + IP + "\r\n"; cmd+= "Connection: close\r\n"; cmd+= "Cache-Control: no-cache\r\n"; cmd+= "\r\n"; if (AT("AT+CIPSEND=", String(cmd.length()), 20000)) { String get; Serial3.print(cmd); while (Serial3.available() > 0) { get = Serial3.readString(); } Serial.println (get); Serial.println("-------------------------------------------------------"); Serial3.println("AT+CIPCLOSE"); } } } String getVersion() { Serial3.println("AT+GMR"); Serial.print("AT+GMR"); String get; Serial3.flush(); Serial3.readStringUntil('\r\n'); String Version = Serial3.readStringUntil('\r\n'); get = Serial3.readString(); if (get.endsWith("OK\r\n")) {return Version;} Serial.println("...ERROR"); return "Error"; } bool getNetwork(String SIID) { Serial3.setTimeout(15000); Serial3.println("AT+CWLAP"); Serial.print ("AT+CWLAP [" + SIID + "]"); String get = Serial3.readString(); Serial.println(get); if (get.indexOf(SIID) == -1) {Serial.println ("...NETWORK UNREACHABLE"); return false;} Serial.println ("...NETWORK ACCESSIBLE"); return true; } bool connectAP (String siid, String password) { String cmd="\""; cmd+=siid; cmd+="\",\""; cmd+=password; cmd+="\""; if (AT("AT+CWJAP=", cmd, 30000)) {return true;} return false; } bool getConnectedState () { if (AT("AT+CWJAP?", "", 10000)) {return true;} return false; } bool testESP8266() { if (AT("AT","", 2000)) {return true;} return false; } bool setMode(String m) { if (AT("AT+CWMODE=", m, 5000)) {return true;} return false; } bool resetESP8266 () { if (AT("AT+RST", "", 10000)) {return true;} return false; } bool AT(String at, String param, long timeout) { Serial3.println(at + param); Serial.print(at + param); long TimeOut; bool result = false; TimeOut = millis() + timeout; while (true) { if (millis() > TimeOut) {Serial.println("TIME OUT"); break;} //Serial3.flush(); if (Serial3.available() > 0) { String get = Serial3.readStringUntil('\r\n'); //Serial.println(get); //AT if (at.equals("AT")) { if (get.indexOf("OK") != -1) { result = true; break;} } //AT+RST if (at.equals("AT+RST")) { if (get.indexOf("ready") != -1) { result = true; break;} } //AT+CWMODE=param if (at.equals("AT+CWMODE=")) { if (get.indexOf("OK") != -1) { result = true; break;} if (get.indexOf("no change") != -1) { result = true; break;} } //AT+CWJAP? if (at.equals("AT+CWJAP?")) { if (get.indexOf("+CWJAP:\""+ SIID + "\"") != -1) { result = true; break;} } //AT+CWJAP if (at.equals("AT+CWJAP=")) { if (get.indexOf("OK") != -1) { result = true; break;} } //----------- if (at.equals("AT+CIPSTART")) { if (get.indexOf("Linked") != -1) { result = true; break;} } if (at.equals("AT+CIPSEND=")) { if (get.indexOf(">") != -1) { result = true; delay(1000); break;} } } } if (result) {Serial.println("...TRUE"); } else {Serial.println("...FALSE");} Serial.println("-------------------------------------------------------"); return result; } void stopProgram () { Serial.println ("PROGRAM STOPPED...CHECK WIRING AND PRESS RESET "); while(true) {delay(1000);} } |
In Zeile 50 rufen wir die selbst erstellte update Funktion auf in der wir dem Programm den Wert der Variable „send“ und die IP/Host Adresse mitteilen.
In Zeile 57 holt sich das ESP8266 WiFi Modul dann die IP Adresse vom DNS Server.
In den Zeilen 60 bis 64 bilden wir den HTTP Header für die Anfrage, dabei ist die Zeile 61 am wichtigsten, denn damit teilen wir dem Virtuellen Server Array mit auf welchen Virtuellen Server wir denn nun hinter der IP Adresse wollen.
Das Ergebnis im Video :
Hier haben wir noch den Code wenn man mit der zu diesem Zeitpunkt aktuelle IDE 1.6.9 arbeitet.
Aufgespielt haben wir den Code auf ein ESP8266-12E Modul :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
/* Arduino IDE 1.6.9 ESP8266 : Virtual Server Created 15 May 2016 by Ralf Bohnen - www.Arduinoclub.de This example code is in the public domain. */ #include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> #include <ESP8266HTTPClient.h> const char WiFiSSID[] = "SSID"; # your Router SSID const char WiFiPSK[] = "PW"; # your Router Password #define DBG_OUTPUT_PORT Serial ESP8266WiFiMulti WiFiMulti; bool isConnected(long timeOutSec) { timeOutSec = timeOutSec * 1000; int z = 0; while (WiFiMulti.run() != WL_CONNECTED) { delay(200); DBG_OUTPUT_PORT.print("."); if (z == timeOutSec / 200) { return false; } z++; } return true; } void setup() { DBG_OUTPUT_PORT.begin(76400); DBG_OUTPUT_PORT.setDebugOutput(true); DBG_OUTPUT_PORT.println(F("ArduinoClub-Virtual Server")); WiFiMulti.addAP(WiFiSSID, WiFiPSK); /* add setup code here */ } void sendToClient(String data) { HTTPClient http; String url = "http://"; url += "www.arduinoclub.de/esp8266.php?a="; url += data; http.begin(url); //HTTP int httpCode = http.GET(); if (httpCode > 0) { // HTTP header has been send and Server response header has been handled DBG_OUTPUT_PORT.printf("[HTTP] GET... code: %d\n", httpCode); // file found at server if (httpCode == HTTP_CODE_OK) { String payload = http.getString(); DBG_OUTPUT_PORT.println(payload); } } else { DBG_OUTPUT_PORT.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); } void loop() { if (isConnected(30)) { sendToClient("123458780"); } delay(20000); /* add main program code here */ } |
Ok das war es schon für heute.
Euer Arduino-Club Team
I love looking through an article that will make people
think. Also, thanks for allowing for me to comment!
Good post. I’m experiencing some of these issues as well..
Bekomme beim Aufruf von http://www.arduinoclub immer diesen Fehler:
AT+CIPSTART=“TCP“,“www.arduinoclub.de“,80TIME OUT
…FALSE
aLLES ANDERE VORHER FUNKTINIERT; das Modul ist im Router zu sehen
In ein paar Tagen wird dieser Beitrag hier in Teilen ersetzt b.z.w. ergänzt, ich hoffe das der neue Beitrag zu dem Thema dein Problem lösen wird.