firmware per leggere dalla seriale della Nucleo
serverReciveTethering.txt@1:a3481c2fb440, 2017-02-27 (annotated)
- Committer:
- divui
- Date:
- Mon Feb 27 12:48:48 2017 +0000
- Revision:
- 1:a3481c2fb440
- Child:
- 2:bc0caa30735b
Interfaccia seriale - webserver tramite WeMosD1 (o generico esp8266)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
divui | 1:a3481c2fb440 | 1 | // file .ino da caricare tramite Arduino IDE |
divui | 1:a3481c2fb440 | 2 | |
divui | 1:a3481c2fb440 | 3 | /* |
divui | 1:a3481c2fb440 | 4 | * Per poter programmare ESP8266 o simili tramite Ide arduino bisogna eseguire le seguenti operazioni: |
divui | 1:a3481c2fb440 | 5 | * 1) Aprire ArduinIDE le "File - Impostazioni" |
divui | 1:a3481c2fb440 | 6 | * 2) Inserire "http://arduino.esp8266.com/stable/package_esp8266com_index.json" nel riquadro URL aggiuntive... |
divui | 1:a3481c2fb440 | 7 | * 3) Arpire "Strumenti - Scheda - Gestore schede - cercare 'ESP8266' e scaricare il pacchetto" |
divui | 1:a3481c2fb440 | 8 | * 4) Selezionare WeMos D1 (Retired) |
divui | 1:a3481c2fb440 | 9 | * |
divui | 1:a3481c2fb440 | 10 | * |
divui | 1:a3481c2fb440 | 11 | * I driver WEMOS D1 mini |
divui | 1:a3481c2fb440 | 12 | * https://www.wemos.cc/tutorial/get-started-arduino.html |
divui | 1:a3481c2fb440 | 13 | * |
divui | 1:a3481c2fb440 | 14 | * |
divui | 1:a3481c2fb440 | 15 | * |
divui | 1:a3481c2fb440 | 16 | * Collegamenti |
divui | 1:a3481c2fb440 | 17 | * |
divui | 1:a3481c2fb440 | 18 | * WeMOS ---> Nucleo |
divui | 1:a3481c2fb440 | 19 | * 3v3 ---> 3v3 |
divui | 1:a3481c2fb440 | 20 | * G ---> GND |
divui | 1:a3481c2fb440 | 21 | * TX ---> D8 |
divui | 1:a3481c2fb440 | 22 | * |
divui | 1:a3481c2fb440 | 23 | * |
divui | 1:a3481c2fb440 | 24 | * |
divui | 1:a3481c2fb440 | 25 | * |
divui | 1:a3481c2fb440 | 26 | * In MBED bisogna definire la seriale: |
divui | 1:a3481c2fb440 | 27 | * Serial esp(PA_9,PA_10); { PA_9 - TX - (D8) ---- PA_10 - RX - (D2) } |
divui | 1:a3481c2fb440 | 28 | * |
divui | 1:a3481c2fb440 | 29 | * La stringa trasmessa deve essere di tipo: |
divui | 1:a3481c2fb440 | 30 | * esp.printf("%s$", "Stringa che trasmetto"); |
divui | 1:a3481c2fb440 | 31 | * |
divui | 1:a3481c2fb440 | 32 | * Se voglio trasmettere un paramentro (number) intero ad esempio: |
divui | 1:a3481c2fb440 | 33 | * esp.printf("%s-%d$", "Value",number); |
divui | 1:a3481c2fb440 | 34 | */ |
divui | 1:a3481c2fb440 | 35 | |
divui | 1:a3481c2fb440 | 36 | |
divui | 1:a3481c2fb440 | 37 | |
divui | 1:a3481c2fb440 | 38 | #include <ESP8266WiFi.h> |
divui | 1:a3481c2fb440 | 39 | #include <WiFiClient.h> |
divui | 1:a3481c2fb440 | 40 | #include <ESP8266WebServer.h> |
divui | 1:a3481c2fb440 | 41 | #include <ESP8266mDNS.h> |
divui | 1:a3481c2fb440 | 42 | |
divui | 1:a3481c2fb440 | 43 | |
divui | 1:a3481c2fb440 | 44 | #define ACCESS_POINT_NAME "ESP_Serial_Monitor" |
divui | 1:a3481c2fb440 | 45 | #define ACCESS_POINT_PASSWORD "12345678" |
divui | 1:a3481c2fb440 | 46 | |
divui | 1:a3481c2fb440 | 47 | const char* ssid = "AndroidAP";// |
divui | 1:a3481c2fb440 | 48 | const char* password = "12345678";// |
divui | 1:a3481c2fb440 | 49 | |
divui | 1:a3481c2fb440 | 50 | String precedente=""; |
divui | 1:a3481c2fb440 | 51 | String attuale; |
divui | 1:a3481c2fb440 | 52 | |
divui | 1:a3481c2fb440 | 53 | int count=0; |
divui | 1:a3481c2fb440 | 54 | |
divui | 1:a3481c2fb440 | 55 | ESP8266WebServer server(80); |
divui | 1:a3481c2fb440 | 56 | |
divui | 1:a3481c2fb440 | 57 | const int led = 13; |
divui | 1:a3481c2fb440 | 58 | |
divui | 1:a3481c2fb440 | 59 | void handleRoot() { |
divui | 1:a3481c2fb440 | 60 | digitalWrite(led, 1); |
divui | 1:a3481c2fb440 | 61 | |
divui | 1:a3481c2fb440 | 62 | |
divui | 1:a3481c2fb440 | 63 | attuale=Serial.readString(); |
divui | 1:a3481c2fb440 | 64 | |
divui | 1:a3481c2fb440 | 65 | //per ordinalre la stringa ricevuta in maniera decrescente |
divui | 1:a3481c2fb440 | 66 | Serial.println(attuale); |
divui | 1:a3481c2fb440 | 67 | int from=-1; |
divui | 1:a3481c2fb440 | 68 | while(1){ |
divui | 1:a3481c2fb440 | 69 | int to=from+1; |
divui | 1:a3481c2fb440 | 70 | from=attuale.indexOf("$",(to+1)); |
divui | 1:a3481c2fb440 | 71 | Serial.print("from: ");Serial.println(from); |
divui | 1:a3481c2fb440 | 72 | if (from==-1) break; |
divui | 1:a3481c2fb440 | 73 | Serial.print("Stringa: ");Serial.println(attuale.substring(to,from)); |
divui | 1:a3481c2fb440 | 74 | precedente=attuale.substring(to,from)+ "<br>" + precedente.substring(0, precedente.indexOf("<br>", 500)); |
divui | 1:a3481c2fb440 | 75 | } |
divui | 1:a3481c2fb440 | 76 | |
divui | 1:a3481c2fb440 | 77 | String content = "<html><head>"; |
divui | 1:a3481c2fb440 | 78 | content += "<meta http-equiv='refresh' content='0.1'></head>"; |
divui | 1:a3481c2fb440 | 79 | content += "<body>"; |
divui | 1:a3481c2fb440 | 80 | //content += attuale; |
divui | 1:a3481c2fb440 | 81 | content += precedente; |
divui | 1:a3481c2fb440 | 82 | content += "</body></html>"; |
divui | 1:a3481c2fb440 | 83 | server.send(200, "text/html", content); |
divui | 1:a3481c2fb440 | 84 | |
divui | 1:a3481c2fb440 | 85 | /* attuale.substring(0,(attuale.lastIndexOf ("<br>")-1)); |
divui | 1:a3481c2fb440 | 86 | precedente=attuale + precedente.substring(0, precedente.indexOf("<br>", 500)); |
divui | 1:a3481c2fb440 | 87 | */ |
divui | 1:a3481c2fb440 | 88 | Serial.println("scrivo sulla pagina web"); |
divui | 1:a3481c2fb440 | 89 | |
divui | 1:a3481c2fb440 | 90 | digitalWrite(led, 0); |
divui | 1:a3481c2fb440 | 91 | } |
divui | 1:a3481c2fb440 | 92 | |
divui | 1:a3481c2fb440 | 93 | void handleNotFound(){ |
divui | 1:a3481c2fb440 | 94 | digitalWrite(led, 1); |
divui | 1:a3481c2fb440 | 95 | String message = "File Not Found\n\n"; |
divui | 1:a3481c2fb440 | 96 | message += "URI: "; |
divui | 1:a3481c2fb440 | 97 | message += server.uri(); |
divui | 1:a3481c2fb440 | 98 | message += "\nMethod: "; |
divui | 1:a3481c2fb440 | 99 | message += (server.method() == HTTP_GET)?"GET":"POST"; |
divui | 1:a3481c2fb440 | 100 | message += "\nArguments: "; |
divui | 1:a3481c2fb440 | 101 | message += server.args(); |
divui | 1:a3481c2fb440 | 102 | message += "\n"; |
divui | 1:a3481c2fb440 | 103 | for (uint8_t i=0; i<server.args(); i++){ |
divui | 1:a3481c2fb440 | 104 | message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; |
divui | 1:a3481c2fb440 | 105 | } |
divui | 1:a3481c2fb440 | 106 | server.send(404, "text/plain", message); |
divui | 1:a3481c2fb440 | 107 | digitalWrite(led, 0); |
divui | 1:a3481c2fb440 | 108 | } |
divui | 1:a3481c2fb440 | 109 | |
divui | 1:a3481c2fb440 | 110 | void setup(void){ |
divui | 1:a3481c2fb440 | 111 | pinMode(led, OUTPUT); |
divui | 1:a3481c2fb440 | 112 | digitalWrite(led, 0); |
divui | 1:a3481c2fb440 | 113 | Serial.begin(115200); |
divui | 1:a3481c2fb440 | 114 | Serial.begin(115200); |
divui | 1:a3481c2fb440 | 115 | |
divui | 1:a3481c2fb440 | 116 | |
divui | 1:a3481c2fb440 | 117 | WiFi.begin(ssid, password); |
divui | 1:a3481c2fb440 | 118 | Serial.println(""); |
divui | 1:a3481c2fb440 | 119 | |
divui | 1:a3481c2fb440 | 120 | // Wait for connection |
divui | 1:a3481c2fb440 | 121 | while (WiFi.status() != WL_CONNECTED) { |
divui | 1:a3481c2fb440 | 122 | delay(500); |
divui | 1:a3481c2fb440 | 123 | Serial.print("."); |
divui | 1:a3481c2fb440 | 124 | } |
divui | 1:a3481c2fb440 | 125 | Serial.println(""); |
divui | 1:a3481c2fb440 | 126 | Serial.print("Connected to "); |
divui | 1:a3481c2fb440 | 127 | Serial.println(ssid); |
divui | 1:a3481c2fb440 | 128 | Serial.print("IP address: "); |
divui | 1:a3481c2fb440 | 129 | Serial.println(WiFi.localIP()); |
divui | 1:a3481c2fb440 | 130 | Serial.println(); |
divui | 1:a3481c2fb440 | 131 | Serial.println(WiFi.localIP()); |
divui | 1:a3481c2fb440 | 132 | |
divui | 1:a3481c2fb440 | 133 | |
divui | 1:a3481c2fb440 | 134 | if (MDNS.begin("esp8266")) { |
divui | 1:a3481c2fb440 | 135 | Serial.println("MDNS responder started"); |
divui | 1:a3481c2fb440 | 136 | } |
divui | 1:a3481c2fb440 | 137 | |
divui | 1:a3481c2fb440 | 138 | /* AP MODE |
divui | 1:a3481c2fb440 | 139 | WiFi.mode(WIFI_AP_STA); |
divui | 1:a3481c2fb440 | 140 | WiFi.softAP( ACCESS_POINT_NAME , ACCESS_POINT_PASSWORD); |
divui | 1:a3481c2fb440 | 141 | |
divui | 1:a3481c2fb440 | 142 | Serial.print("IP address: "); |
divui | 1:a3481c2fb440 | 143 | Serial.println(WiFi.localIP()); |
divui | 1:a3481c2fb440 | 144 | */ |
divui | 1:a3481c2fb440 | 145 | server.on("/", handleRoot); |
divui | 1:a3481c2fb440 | 146 | |
divui | 1:a3481c2fb440 | 147 | server.on("/inline", [](){ |
divui | 1:a3481c2fb440 | 148 | server.send(200, "text/plain", "this works as well"); |
divui | 1:a3481c2fb440 | 149 | }); |
divui | 1:a3481c2fb440 | 150 | |
divui | 1:a3481c2fb440 | 151 | server.onNotFound(handleNotFound); |
divui | 1:a3481c2fb440 | 152 | |
divui | 1:a3481c2fb440 | 153 | server.begin(); |
divui | 1:a3481c2fb440 | 154 | Serial.println("HTTP server started"); |
divui | 1:a3481c2fb440 | 155 | } |
divui | 1:a3481c2fb440 | 156 | |
divui | 1:a3481c2fb440 | 157 | void loop(void){ |
divui | 1:a3481c2fb440 | 158 | server.handleClient(); |
divui | 1:a3481c2fb440 | 159 | |
divui | 1:a3481c2fb440 | 160 | delay(500); |
divui | 1:a3481c2fb440 | 161 | Serial.print("&Ciao$"); |
divui | 1:a3481c2fb440 | 162 | |
divui | 1:a3481c2fb440 | 163 | } |