Samstag, 14. Mai 2016

NRF24L01+ USB Adapter UART Adapter

Abstract

I buyed this Modul because I tried different Communication Systems. In this Post I try to collect the informations about it, because documentation is very  low.

Parameters and commands:


1. The number of bytes in a single transmission valid: 1-31 bytes.
The first 0 byte system reserved for each transmitted packet length statistics; for example serial port to send "123" (ASCII code, 3 bytes), the actual transfer when the first byte 0 to 3, the receiving end of the actual should be handled to determine the packet length byte received under this number.
 
2. Module Baud Rate: 4800,9600,14400,19200,38400,57600,115200.
Covers common baud rate, (factory default 9600)
Baud rate modification command: send ASCII code [AT + BAUD = x] (x is 1,2,3,4,5,6,7 4800,9600,14400,19200,38400,57600,115200 the corresponding baud)

Such as: Modify the baud rate to 115200, the serial debugging assistant sending ASCII [AT + BAUD = 7], the system replies:
Baud rate setting success !!
Baud Rate: 115200
At this baud rate of 115200, serial debugging assistant need to switch to 115 200 to communicate with the module.
Note: The command must be in uppercase letters!

3.NRF24L01 module transmission rate: 1Mbps, 2Mbps (factory default 1Mbps)

Transmission rate setting command: send ASCII code [AT + RATE = x] (x 1,2 respectively 1Mbps, the transmission rate of 2Mbps)

Such as: modify the transmission rate of 2Mbps, then sending ASCII serial debugging assistant [AT + RATE = 2], the system replies:
Transmission rate setting success !!

Transmit power: 0dBm
Transfer rate: 2Mbps
Low-noise amplifier gain: open

4, NRF24L01 module address setting: 5 address, local address and the receiver address matches 0 (factory default 0xFF, 0xFF, 0xFF, 0xFF, 0xFF)

Address Set command: Send ASCII code [AT + ADD = 0x ??, 0x ??, 0x ??, 0x ??, 0x ??] (0x ?? address you want to set a comma, the comma must be in English half-width )

Such as: change the address for 0x11,0x22,0x33,0x44,0x55, then sending ASCII serial debugging assistant [AT + ADD = 0x11,0x22,0x33,0x44,0x55], the system replies:

Address Setting Success !!

Local Address: 0x11,0x22,0x33,0x44,0x55
Receiving end address 0: 0x11,0x22,0x33,0x44,0x55

5, System Information inquiry:
Query: sending ASCII [AT?], The system replies:

AT?
System Information:
Baud Rate: 115200
Local Address: 0x11,0x22,0x33,0x44,0x55
Receiving end address 0: 0x11,0x22,0x33,0x44,0x55
Transmit power: 0dBm
Transfer rate: 2Mbps
Low-noise amplifier gain: open

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) {


  }
}