WLAN/WIFI-Modul ESP8266

nero76

Moderator
Idee:

Wifi-Modul: ESP8266
Der Arduino-Code müsste nur geringfügig für das Modul ergänzt werden. Die pfodApp am Handy kann auch per TCP/IP mit dem Wifi-Modul verbinden. Dort bleibt also alles beim alten.

Diese Lösung wurde noch nicht gebaut und getestet, wäre aber so umgesetzt wohl am einfachsten.

http://www.mikrocontroller.net/articles/ESP8266
 
Hallo

Ich wollte den Mähroboter mit einem WLan Router verbinden und so von zu Hause sehen was der Mäher im Garten macht.
Im Garten habe ich ein kleinen Server mit einem Raspberry Pi der Wetterdaten ins Internet stellt. http://garten.in-arnstadt.de/weewx
Darüber wollte ich dann auch die Einstellungen und Infos abrufen.

Ich habe noch so ein Wifi Modul (s2w-m02).
Würde es in Verbindung einer kleinen SD-Karte damit auch gehen ?

MfG André
 
@Andre: hatte vorhin das falsche Modul angegeben - hier korrigiert (bitte dort den Thread zum WLAN weiterlaufenlassen): http://www.ardumower.de/index.php/d...c-bluetooth-handy/503-wlan-wifi-modul-esp8266
Wenn man nicht die pfodApp (über WLAN, also TCP/IP) nutzen möchte, könnte man auch ein kleines Javascript-Programm (z.B. am Server oder direkt am Roboter) schreiben welches das pfodApp-Protokoll implementiert, und auf diese Weise (per Web-Browser) die Daten abrufen. Kann da gerne behilflich sein.


Code:
/* ====== ESP8266 Demo ======
 * Print out analog values
 * (Updated Dec 14, 2014)
 * ==========================
 *
 * Change SSID and PASS to match your WiFi settings.
 * The IP address is displayed to soft serial upon successful connection.
 *
 * Ray Wang @ Rayshobby LLC
 * [URL]http://rayshobby.net/?p=9734[/URL]  */

// comment this part out if not using LCD debug

#define BUFFER_SIZE 512

#define SSID  "your_ssid"      // change this to match your WiFi SSID
#define PASS  "your_password"  // change this to match your WiFi password
#define PORT  "8080"           // using port 8080 by default

char buffer[BUFFER_SIZE];

/*
// If using Software Serial for debug
// Use the definitions below
//#include <SoftwareSerial.h>
//SoftwareSerial dbg(7,8);  // use pins 7, 8 for software serial 
//#define esp Serial
*/

// If your MCU has dual USARTs (e.g. ATmega644)
// Use the definitions below
#define dbg Serial    // use Serial for debug
#define esp Serial1   // use Serial1 to talk to esp8266

// By default we are looking for OKrn
char OKrn[] = "OKrn";
byte wait_for_esp_response(int timeout, char* term=OKrn) {
  unsigned long t=millis();
  bool found=false;
  int i=0;
  int len=strlen(term);
  // wait for at most timeout milliseconds
  // or if OKrn is found
  while(millis()<t+timeout) {
    if(esp.available()) {
      buffer[i++]=esp.read();
      if(i>=len) {
        if(strncmp(buffer+i-len, term, len)==0) {
          found=true;
          break;
        }
      }
    }
  }
  buffer[i]=0;
  dbg.print(buffer);
  return found;
}

void setup() {

  // assume esp8266 operates at 115200 baud rate
  // change if necessary to match your modules' baud rate
  esp.begin(115200);
  
  dbg.begin(9600);
  dbg.println("begin.");
    
  setupWiFi();

  // print device IP address
  dbg.print("device ip addr:");
  esp.println("AT+CIFSR");
  wait_for_esp_response(1000);
}

bool read_till_eol() {
  static int i=0;
  if(esp.available()) {
    buffer[i++]=esp.read();
    if(i==BUFFER_SIZE)  i=0;
    if(i>1 && buffer[i-2]==13 && buffer[i-1]==10) {
      buffer[i]=0;
      i=0;
      dbg.print(buffer);
      return true;
    }
  }
  return false;
}

void loop() {
  int ch_id, packet_len;
  char *pb;  
  if(read_till_eol()) {
    if(strncmp(buffer, "+IPD,", 5)==0) {
      // request: +IPD,ch,len:data
      sscanf(buffer+5, "%d,%d", &ch_id, &packet_len);
      if (packet_len > 0) {
        // read serial until packet_len character received
        // start from :
        pb = buffer+5;
        while(*pb!=':') pb++;
        pb++;
        if (strncmp(pb, "GET /", 5) == 0) {
          wait_for_esp_response(1000);
          dbg.println("-> serve homepage");
          serve_homepage(ch_id);
        }
      }
    }
  }
}

void serve_homepage(int ch_id) {
  String header = "HTTP/1.1 200 OKrnContent-Type: text/htmlrnConnection: closernRefresh: 5rn";

  String content="";
  // output the value of each analog input pin
  for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
    int sensorReading = analogRead(analogChannel);
    content += "analog input ";
    content += analogChannel;
    content += " is ";
    content += sensorReading;
    content += "<br />n";       
  }

  header += "Content-Length:";
  header += (int)(content.length());
  header += "rnrn";
  esp.print("AT+CIPSEND=");
  esp.print(ch_id);
  esp.print(",");
  esp.println(header.length()+content.length());
  if(wait_for_esp_response(2000, "> ")) {
    esp.print(header);
    esp.print(content);
  } else {
    esp.print("AT+CIPCLOSE=");
    esp.println(ch_id);
  }
}


void setupWiFi() {
  // try empty AT command
  esp.println("AT");
  wait_for_esp_response(1000);

  // set mode 1 (client)
  esp.println("AT+CWMODE=1");
  wait_for_esp_response(1000);  

  // reset WiFi module
  esp.print("AT+RSTrn");
  wait_for_esp_response(1500);
  delay(3000);
 
  // join AP
  esp.print("AT+CWJAP="");
  esp.print(SSID);
  esp.print("","");
  esp.print(PASS);
  esp.println(""");
  // this may take a while, so wait for 5 seconds
  wait_for_esp_response(5000);
  
  esp.println("AT+CIPSTO=30");  
  wait_for_esp_response(1000);

  // start server
  esp.println("AT+CIPMUX=1");
  wait_for_esp_response(1000);
  
  esp.print("AT+CIPSERVER=1,"); // turn on TCP service
  esp.println(PORT);
  wait_for_esp_response(1000);
  
    
}
 
My understanding is that there the wifi module is not out-of-the-bos supported in the cuttent Ardumower firmware? Perhaps somewhere is a different branch?

Thanks,
Frederic
 
Managed to have it connect to my WLAN B)
It seems not difficult to make it work with Ardumower/pfod, so I suppose somebody has the required code laying around.... otherwise I would be very happy to work on it.

Frederic
 
I have been fighting with the wifi module (needed newer firmware) and my development environment (ecipse) but I am making good progress now B)

Less trivial than I was thinking at first though... :blush:

Frederic
 
Sure! Will perform a pull request when ready.

I am working in https [EDIT: I abandoned this approach] ://github.com/FredericG-BE/ardumower/tree/esp8266_adandoned

For now I only pushed additional test-code.

Frederic
 
Unfortunately I managed to destroy my wifi module, it's getting burning hot. :oops:
I suppose I did it by temporarily remove the 3.3 V while the Arduino was still powered.
Will order a new one (or perhaps an additional spare :unsure: )

Frederic
 
While waiting for a new ESP8266 module, I am wondering if it would not be better to work on the ESP8266 module code instead. The default ESP8266 module implements the AT command set, but I believe the ESP code could be updated to act more the way Bluetooth module does. Here seems an example, if I understand everything correctly, where a simple webserver runs on the ESP8266 module:
https://github.com/dzindra/esphttpd
perhaps start from this code to made a server that automatically connects to a preconfigured AP and accept TCP connections on port 8080 and route the data to the serial port...

Frederic
 
This looks interesting.
https://github.com/esp8266/Arduino
It seems it allows to program the ESP8266 as if it where a an Arduino board with wifi shield. B) This would allow us to implement the connecting to wifi and managing the connection with pfod in the ESP8266 and hide this from the ardumower Adruino.

My replacement wifi modules are in transit...

Frederic
 
Success !!! B)

I was able to replace the standard ESP8266 firmware with code that automatically connects to my wifi, accepts connections from pfod and transparently routes data data to the serial port. For Ardumower it looks like a bluetooth module :)

I need to make a mechanism to allow to configure the wifi parameters, clenaup and document.

Frederic
 
Hello Frederic,

Congrats! Great work - I really like the approach being able to re-use pfodApp with the Wifi module (pfodApp is already able to use a Wifi connection instead of Bluetooth, ArduRemote will be soon too...).

Once your work is documented and available, I will try it out too. After tested successfully, we can add add it to the Ardumower Wiki and add any missing documentation/apps/links etc.

Keep up the good work!

Regards,
Alexander
 
OK, I am "ready". I could improve a few things here and there, but ok... B)

I documented everything here: https://github.com/FredericG-BE/ardumower/wiki/Using-ESP8266-WIFI-module-on-Ardumower
All code is in https://github.com/FredericG-BE/ardumower/tree/esp8266 (not the branch I mentioned before because I abandoned that first approach)

If you feel like giving it a try for yourself, let us know how it goes.

I did not test a lot yet. For example, I don't know what happens when the connection to the Access Point is lost and if the connection will be established again when coming in range again and so on. But I will look into that.

Frederic
 
Hello Frederic,

Nice work! I'll test it. One question: considering that there are many pitfalls involved when re-flashing the stock firmware, maybe we should try to use the default firmware instead? As you can see I did some Wifi connection programming, and a simple HTTP server already (code under Ardumower Github folder tests/esp8266).

On the pfodApp side, we have byte-level control over the sent data (TCP/IP), and on the ESP module side, we could parse the AT-strings to build the raw TCP/IP received strings...

What do you think?

Regards,
Alexander
 
Hi Alexander,

Yes I have experimented with the test you wrote and at first I continued that route. That work is still here: https://github.com/FredericG-BE/ardumower/tree/esp8266_abandoned
I was making a driver that has the methods of Serial and under the hood do all the AT commands. See https://github.com/FredericG-BE/ardumower/blob/esp8266_abandoned/ardumower/esp8266.h https://github.com/FredericG-BE/ardumower/blob/esp8266_abandoned/ardumower/esp8266.cpp

I had it almost working but got a bad feeling about it:
[ul]
[li]It was complicated[/li]
[li]I was afraid that the driver could block the Ardumower code sometimes for too long, causing problems in PID controllers, sluggish reactions, ... To make the driver more asynchronous I would have to make the code even more complicated [/li]
[li]It seemed to me the ping-ponging with AT commands to result in slow communication. [/li]
[li]I was afraid that it would take a lot of work to get everything robust [/li]
[/ul]

I started thinking about other approaches and found out that writing code inside the ESP8266 is not difficult at all.

I don't think one needs to be afraid to flash new firmware in the ESP8266 devices. The original code to work with AT commands is freely available, so you can always go back. Anyway, when I started working with the AT approach, I quickly realized that the devices don't ship with the latest code and that it is best to upgrade to the latest vermware, which I did. Updating the code looks perhaps a bit intimidating at first, but is good to run the latest code anyway and you can always go back as you don't touch the bootloader.

Frederic
 
Are there concrete plans to make ArduRemote WIFI compatible? I must say I don't like pfod too much; I find the UI very counterintuitive.

Frederic
 
I have discovered that in some cases the serial port of the ESP8266 experiences an overrun and so does not transfers the complete pfod message to the phone resulting in a timeout. Especially the Perimeter Settings menu seems very heavy; I did not use this one before.

I am working on it.

Frederic
 
Oben