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/espduinocd 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) {
}
}
Keine Kommentare:
Kommentar veröffentlichen