Posts mit dem Label Arduino werden angezeigt. Alle Posts anzeigen
Posts mit dem Label Arduino werden angezeigt. Alle Posts anzeigen

Donnerstag, 5. Mai 2016

Testing ESPDuino

Testing ESPDuino 


I found a Firmware for my ESP8266, Espduino and It sound that this is what I looked for.

    https://github.com/tuanpmt/espduino

Espduino is a library that use the ESP8266 only for communication, It has 2 parts. The EPS8266 Firmware and the Arduino Library which allows you to use MQTT and WIFI.

Internally the library use SLIP to communicate with the ESP8266.

Goal of my project is to use MQTT via WIFI together with a Arduino Board of course on a very cheap basis.


Download Software

    git clone https://github.com/tuanpmt/espduino
    cd espduino

Flashing

First I did is to connect the ESP8266-01 via a FTDI232 to the USB Port.
With the esptool.py Tool it was easy to flash the Firmware to the ESP.
Remember to connect GPIO 0 to GND before Flashing and the power the ESP8266.

esp8266/tools/esptool.py -p /dev/ttyUSB0 write_flash 0x00000 esp8266/release/0x00000.bin 0x40000 esp8266/release/0x40000.bin

Test

My first try was to use an UNO together with Software Serial where the ESP was connected.
This failed! Many parallels Messages bring the System to crash.
I started 5 Publishers with send 1 Message per second. After around 5 Minutes it hangs.
So I use a Ardurino MEGA with more Serial Ports and this worked.

The  Sketch

 
#include <espduino.h>
#include <mqtt.h>



ESP esp(&Serial1, &Serial, 4);
MQTT mqtt(&esp);
boolean wifiConnected = false;

int i=0;

void wifiCb(void* response)
{
  uint32_t status;
  RESPONSE res(response);

  if(res.getArgc() == 1) {
    res.popArgs((uint8_t*)&status, 4);
    if(status == STATION_GOT_IP) {
      Serial.println("WIFI CONNECTED");
      mqtt.connect("192.168.1.50", 1883, false);
      wifiConnected = true;
      //or mqtt.connect("host", 1883); /*without security ssl*/
    } else {
      wifiConnected = false;
      mqtt.disconnect();
    }
    
  }
}

void mqttConnected(void* response)
{
  Serial.println("Connected");
  mqtt.subscribe("/test"); //or mqtt.subscribe("topic"); /*with qos = 0*/

  mqtt.publish("/test1", "AAAAAAAAAAAAAAAAAAAAa");

}
void mqttDisconnected(void* response)
{
  Serial.println("Disconnect");
}
void mqttData(void* response)
{
  RESPONSE res(response);

  Serial.print("Received: topic=");
  String topic = res.popString();
  Serial.println(topic);

  Serial.print("data=");
  String data = res.popString();
  Serial.println(data);

}
void mqttPublished(void* response)
{

}
void setup() {
  Serial1.begin(19200);

  Serial.begin(19200);

  esp.enable();
  delay(500);
  esp.reset();
  delay(500);

  while(!esp.ready()){
    Serial.println(".");
    delay(500);
  }

  Serial.println("ARDUINO: setup mqtt client");
  if(!mqtt.begin("MQTT_HOST", "MQTT_USER", "MQTT_PASSWORD", 120, 1)) {
    Serial.println("ARDUINO: fail to setup mqtt");
    while(1);
  }


  Serial.println("ARDUINO: setup mqtt lwt");
  mqtt.lwt("/lwt", "offline", 0, 0); 

/*setup mqtt events */
  mqtt.connectedCb.attach(&mqttConnected);
  mqtt.disconnectedCb.attach(&mqttDisconnected);
  mqtt.publishedCb.attach(&mqttPublished);
  mqtt.dataCb.attach(&mqttData);

  /*setup wifi*/
  Serial.println("ARDUINO: setup wifi");
  esp.wifiCb.attach(&wifiCb);

  esp.wifiConnect("WIFI_STATION_ID","WIFI_PASSWORD");


  Serial.println("ARDUINO: system started");
}

void loop() {
  esp.process();
  if(wifiConnected) {


  }
}

Montag, 26. Januar 2015

ESP8266-01 mit NodeMCU (LUA Firmware)

Nach einigen Fehlschlägen mit meinem Arduino und ESP8266 habe ich mich entschlossen das Konzept umzuändern.
Zuerst wollte ich den ESP nur als Wifi Modul nutzen und dem Arduino die Kontrolle übernehmen lassen. Das Problem das ich hatte war das die Serielleverbindung immer Probleme machte. Es ich vermute stark das die BAUD die bei fast allen Firmwares auf 115200 Baud war die Probleme verursachte.

Da ich jetzt  einen FTDI (USB TTL Adapter) bekommen hatte, konnte ich endlich auch die Firmware upgraden. Das Cloud Update ging mit der Version noch nicht.

Mit dem FTDI hatte ich auch keine Probleme mehr mit der Baudrate und der ESP lief ganz stabil. Trotzdem habe ich das teil geflashed jedoch die BAUD Rate konnte ich immer noch nicht einstellen.

Parallel darauf bin ich jedoch auf eine LUA Firmware gestoßen NodeMCU die auch sehr interessant klingt und auch meine bedenken bezüglich WebServer und Arduino zerstreuen.

Neues Ziel ist den Arduino einfach nur als Slave zu dem ESP zu nutzen und Kommandos per Serial Konsole weiterzugeben. Vorteilhaft an dem NodeMCU ist auch das es mit 9600 Baud arbeitet.


NodeMCU - https://github.com/nodemcu/nodemcu-firmware
ESP Tool zum Flashen - https://github.com/themadinventor/esptool


Kommando zum Flashen:

sudo ./esptool.py --port /dev/ttyUSB0 write_flash 0x000000 ../nodemcu-firmware/pre_build/latest/nodemcu_latest.bin

Wichtig ist das GPIO-0 mit GND verbunden ist. Auch musste ich hin und wieder das Teil reseten, weil es zu "Failed to connect" Fehlern gekommen ist.

Ein kleines Script zum Verbinden

> print(wifi.sta.getip())
nil

> wifi.setmode(wifi.STATION)
> wifi.sta.config("SSID","password")
> print(wifi.sta.getip())

192.168.1.112

Sieht soweit alles ganz gut aus :)