2016/06/22

Install MQTT Broker Mosquitto on Raspberry Pi

I have installed the MQTT Broker Mosquitto on my Raspberry Pi Model B+ with Raspbian Jessie pre-installed following the procedure described below a few days ago. The version of Mosquitto installed is 1.4.9 supporting both MQTT protocol 3.1 and 3.1.1.

It is stated on module page of  NodeMCU documentation website ( https://nodemcu.readthedocs.io/en/master/en/modules/mqtt/ )that:

"The client adheres to version 3.1.1 of the MQTT protocol. Make sure that your broker supports and is correctly configured for version 3.1.1. The client is backwards incompatible with brokers running MQTT 3.1."

1. Update your packages:

sudo apt-get update
sudo apt-get upgrade

2. To use the new repository, import the repository package signing key:

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
sudo rm mosquitto-repo.gpg.key

3. Make the repository available to apt:

cd /etc/apt/sources.list.d/

4. Install the packages list for your version of Raspbian.

For Wheezy (old version)

sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list

For Jessie (latest version)

sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list

5. Update apt information and install Mosquitto:

sudo apt-get update
sudo apt-get install mosquitto mosquitto-clients

Additional Board Manager URLs For Arduino IDE


Here's my collection of additional board manager URLs for Arduino IDE version 1.6.4 or greater - don't use 1.6.2!


ESP8266

http://arduino.esp8266.com/stable/package_esp8266com_index.json

MediaTek LinkIt Smart 7688 DUO

http://download.labs.mediatek.com/package_mtk_linkit_smart_7688_test_index.json

MediaTek LinkIt One

http://download.labs.mediatek.com/package_mtk_linkitone_index.json

Realtek Ameba

https://github.com/Ameba8195/Arduino/raw/master/release/package_realtek.com_ameba_index.json

2016/05/09

Arduino Sketch Writing MakerSwitch Tail Settings to ATMEL AT24C02 I2C EEPROM


Wiring

1. AT24C02.P01 - GND
2. AT24C02.P02 - GND
3. AT24C02.P03 - GND
4. AT24C02.P04 - GND
5. AT24C02.P05 - ArduinoUno.A4
6. AT24C02.P06 - ArduinoUno.A5
7. AT24C02.P07 - GND
8. AT24C02.P08 - 5V

Sketch

// Modified by Befun Hung on May 9, 2016
//Write setting of MakerSwitch to ATMEL AT24C02
// Address 0-7 contains ID number, address 8 contains address storing switch status value '0' or '1'
// Modified by Befun Hung on May 2, 2016 
// For writing string to ATMEL AT24C02 I2C EEPROM from address 0 once and display all values in the EEPROM on serial monitor once
// The ATMEL AT24C02 I2C EEPROM with 256 bytes (2K bits) need only one byte for addressing is suitable for storing short text message such as id and ip configuration
//
// ** Original comment in simplified Chinese, the original sketch write in EEPROM numbers equal to it's address, read the values and display on serial monitor repeatedly 
//直接给你个24c02简单的程序吧  不用附加库  别的带附加库也可以
//byte 换为int也行, 多数据,大于255的数据自己for循环存吧,double float论坛有教程
#include <Wire.h>

#define colPerLine 8
// define idLen according the length of id value needed
#define idLen 8
char id[idLen+1] = "25610249";


int i = 0;
int val; 

void Writebyte(byte DeviceAddress, byte DataAddress, byte Data)
{
  int rData = Data;
  Wire.beginTransmission(DeviceAddress);
  Wire.write(DataAddress);
  Wire.write(rData);
  Wire.endTransmission();
  delay(10);
}

byte Readbyte(int DeviceAddress, byte DataAddress)
{
  byte rdata = 0xFF;
  Wire.beginTransmission(DeviceAddress);
  Wire.write(DataAddress);
  Wire.endTransmission();
  Wire.requestFrom(DeviceAddress, 1);
  delay(10);
  if (Wire.available())
  {
    rdata = Wire.read();
  }
  delay(10);
  return rdata;
}

void setup()
{
  Wire.begin();
  Serial.begin(115200);
  
  for (i=0;i<256;i++) {
    if (i< idLen) {
       Writebyte(0x50,i,id[i]);
    }
    else {
      // comment out the following line if values in the address greater than idLen not to be overrided with 0
      Writebyte(0x50,i,0);
    }
  }
  
  // Address 8 contains address storing switch status value '0' or '1'
  Writebyte(0x50,8,255);
  
  for (i=0;i<256;i++) {
    if ((i%colPerLine) == 0) Serial.println("");
    val = Readbyte(0x50,i);
    Serial.print(val);
    Serial.print(" ");
  }
}

void loop()
{
    // if (i > 0xFF)
    // {
    //   i = 0x00;
    // }
    // Writebyte(0x50, i, i);
    // val = Readbyte(0x50, i);
    // i++;
    // Serial.print(val);
    // Serial.print(" ");
    // if ((i%8) == 0) Serial.println("");
}

2016/05/02

Writing String To ATMEL AT24C02 256 Bytes (2K bits) I2C EEPROM

Sometimes I need to store default data. Storing the data in external I2C EEPROM is suitable than in ATMEGA328P built-in EEPROM when you want to change the data values.

For ATMEGA328P built-in EEPROM, you have to upload a sketch to change the default data and re-upload the sketch for use.

For external I2C EEPROM, you just unplug the IC from IC socket and replace a new one with default data you need. Uploading sketches are not needed. And the chip is cheap, it costs only 0.03 USD.

Wiring

1. AT24C02.P01 - GND
2. AT24C02.P02 - GND
3. AT24C02.P03 - GND
4. AT24C02.P04 - GND
5. AT24C02.P05 - ArduinoUno.A4
6. AT24C02.P06 - ArduinoUno.A5
7. AT24C02.P07 - GND
8. AT24C02.P08 - 5V

Sketch

// Modified by Befun Hung on May 2, 2016 
// For writing string to ATMEL AT24C02 I2C EEPROM from address 0 once and display all values in the EEPROM on serial monitor once
// The ATMEL AT24C02 I2C EEPROM with 256 bytes (2K bits) need only one byte for addressing is suitable for storing short text message such as id and ip configuration
//
// ** Original comment in simplified Chinese, the original sketch write in EEPROM numbers equal to it's address, read the values and display on serial monitor repeatedly 
//直接给你个24c02简单的程序吧  不用附加库  别的带附加库也可以
//byte 换为int也行, 多数据,大于255的数据自己for循环存吧,double float论坛有教程
#include <Wire.h>

#define colPerLine 8
// define idLen according the length of id value needed
#define idLen 8
char id[idLen+1] = "25613804";


int i = 0;
int val; 

void Writebyte(byte DeviceAddress, byte DataAddress, byte Data)
{
  int rData = Data;
  Wire.beginTransmission(DeviceAddress);
  Wire.write(DataAddress);
  Wire.write(rData);
  Wire.endTransmission();
  delay(10);
}

byte Readbyte(int DeviceAddress, byte DataAddress)
{
  byte rdata = 0xFF;
  Wire.beginTransmission(DeviceAddress);
  Wire.write(DataAddress);
  Wire.endTransmission();
  Wire.requestFrom(DeviceAddress, 1);
  delay(10);
  if (Wire.available())
  {
    rdata = Wire.read();
  }
  delay(10);
  return rdata;
}

void setup()
{
  Wire.begin();
  Serial.begin(115200);
  
  for (i=0;i<256;i++) {
    if (i< idLen) {
       Writebyte(0x50,i,id[i]);
    }
    else {
      // comment out the following line if values in the address greater than idLen not to be overrided with 0
      Writebyte(0x50,i,0);
    }
  }
  
  for (i=0;i<256;i++) {
    if ((i%colPerLine) == 0) Serial.println("");
    val = Readbyte(0x50,i);
    Serial.print(val);
    Serial.print(" ");
  }
}

void loop()
{
    // if (i > 0xFF)
    // {
    //   i = 0x00;
    // }
    // Writebyte(0x50, i, i);
    // val = Readbyte(0x50, i);
    // i++;
    // Serial.print(val);
    // Serial.print(" ");
    // if ((i%8) == 0) Serial.println("");
}

2016/04/29

RF Remote

Code:

// Modified by Befun Hung on Apr. 29, 2016
// 1. Use RadioHead Library
// 2. Max Length of Digits is 20 Characters.
// 3. Numbers followed by '*' to Switch On, '#' to Switch Off
//    Example: 23456789* To Swith On Device 23456789, 23456789# To Switch Off Device 23456789
// Modified by Befun Hung on Oct. 28, 2014   
// Blink led after sending message to notice user 5 times

#include <Keypad.h>
#include <RH_ASK.h>
#include <SPI.h>

RH_ASK driver;

#define maxLength 20
#define keyOn '*'
#define keyOff '#'

const byte ROWS = 4; // four rows
const byte COLS = 3; // three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {5, 4, 3, 2};
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = {8, 7, 6};    // connect to the column pinouts of the keypad
char keyins[maxLength+1] = "";
int i=0, j=0, sendTimes=5;
int led=13;

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() {
  Serial.begin(115200);
  if (!driver.init()) Serial.println("init failed");
  Serial.println("Ready!");
  pinMode(led, OUTPUT);
  digitalWrite(led, HIGH);
  delay(20);
  digitalWrite(led, LOW);
  delay(20);
}
  
void loop() {
    char key = keypad.getKey();
    if (key) {
      Serial.print(key);
      keyins[i] = key;
      i++;
      if (key == keyOn || key == keyOff) {
        for (j=0;j<sendTimes;j++) {
          Serial.println();
          send(keyins);
        }
        digitalWrite(led, HIGH);
        delay(20);
        digitalWrite(led, LOW);
        delay(20);
        i = 0;
      } 
    }
}

void send(char *message)
{
  driver.send((uint8_t *) message, strlen(message));
  driver.waitPacketSent();
}

2016/04/13

Burning Optiboot Bootloader to Arduino Pro Mini Using Uno R3 with ArduinoISP

This is the memorandum recording the procedure I use burning Optiboot bootloader to Arduino Pro Mini.

The procedure:

1. Upload ArduinoISP sketch to Uno R3 as any general sketch.
2. Connect Arduino Pro Mini RST to Uno R3 D10, Vcc to Vcc, Gnd to Gnd, D11 to D11, D12 to D12 and D13 to D13 respectly.
3. Select Board ->  Arduino Uno, Programmer -> Arduino as ISP, Burn Bootloader from Tools menu. Wait for a minute or so, the status bar of Arduino IDE will show "Done burning bootloader.".

That's it.

No need to download Optiboot hex file from any website, to compile optiboot from source or to edit board.txt, Arduino Uno use Optiboot as its default bootloader and the hex file is include in the Arduino IDE ( the Arduino IDE I use is 1.0.5-r2). The parameters set for Uno in board.txt is suitable for all boards using ATMEGA328P-PU or ATMEGA328P-AU microcontroller.

I burn Optiboot to Arduino Pro Mini following the procedure above and upload the example blink sketch to the Arduino Pro Mini by selecting Tools -> Board -> Arduino Uno repeatly and successfully.

If Arduino Uno R3 with ArduinoISP uploaded is used, skip to step 2 directly. Step 2 can be done before or after plugging USB type A connector to computer.

Following the procedure, I can minimize the needed size of board and bytes occupied by bootloader and maximize the functions the bootloader provides such as watchdog timer.

2016/01/13

6-digit 7-segment LED Counter Display

I need a 6-digit 7-segment LED module to display time and found Sparkfun has an example sketch for 4-digit 7-segment LED display module. So I just modify the example sketch to meet my requirement. The photos taken cannot display actual image saw by human eyes.

Photos



Parts

Arduino Uno R3
6-digit 7-segment LED Display Module KYX-3662BS
Some Wires

LED Module Pinouts


Sketch

/*
 6-13-2011
 Spark Fun Electronics 2011
 Nathan Seidle

 Original used for 4-digit 7-segment LED module
 Modified by Befun Hung on Jan. 13, 2016 to Extend to 6-digit

 This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).

 4 digit 7 segment display:
 http://www.sparkfun.com/products/9483
 Datasheet: 
 http://www.sparkfun.com/datasheets/Components/LED/7-Segment/YSD-439AR6B-35.pdf

 This is an example of how to drive a 7 segment LED display from an ATmega without the use of current limiting resistors.
 This technique is very common but requires some knowledge of electronics - you do run the risk of dumping too 
 much current through the segments and burning out parts of the display. If you use the stock code you should be ok, but 
 be careful editing the brightness values.

 This code should work with all colors (red, blue, yellow, green) but the brightness will vary from one color to the next
 because the forward voltage drop of each color is different. This code was written and calibrated for the red color.

 This code will work with most Arduinos but you may want to re-route some of the pins.

 7 segments
 6 digits
 1 colon
 =
 14 pins required for full control 
 */

// LED module used in this example is KYX-3662BS which is common anode 
int digit1 = 11; // 6-digit 7-segment LED pin 1
int digit2 = 10; // 6-digit 7-segment LED pin 2
int digit3 = 9;  // 6-digit 7-segment LED pin 3
int digit4 = 6;  // 6-digit 7-segment LED pin 4
int digit5 = 2;  // 6-digit 7-segment LED pin 5
int digit6 = 12; // 6-digit 7-segment LED pin 6

//Pin mapping from Arduino to the ATmega DIP28 if you need it
//http://www.arduino.cc/en/Hacking/PinMapping
int segA = A1; // 6-digit 7-segment LED pin 14
int segB = 3;  // 6-digit 7-segment LED pin 13
int segC = 4;  // 6-digit 7-segment LED pin 12
int segD = 5;  // 6-digit 7-segment LED pin 11
int segE = A0; // 6-digit 7-segment LED pin 10
int segF = 7;  // 6-digit 7-segment LED pin  9
int segG = 8;  // 6-digit 7-segment LED pin  8

void setup() {                
  pinMode(segA, OUTPUT);
  pinMode(segB, OUTPUT);
  pinMode(segC, OUTPUT);
  pinMode(segD, OUTPUT);
  pinMode(segE, OUTPUT);
  pinMode(segF, OUTPUT);
  pinMode(segG, OUTPUT);

  pinMode(digit1, OUTPUT);
  pinMode(digit2, OUTPUT);
  pinMode(digit3, OUTPUT);
  pinMode(digit4, OUTPUT);
  pinMode(digit5, OUTPUT);
  pinMode(digit6, OUTPUT);
  
  pinMode(13, OUTPUT);
}

void loop() {
  
  //long startTime = millis();

  displayNumber(millis()/1000, 6);
  displayNumber(millis()/100000, 4);
  displayNumber(millis()/10000000, 2);

  //while( (millis() - startTime) < 2000) {
  //displayNumber(1217);
  //}
  //delay(1000);  
}

//Given a number, we display 10:22
//After running through the 4 numbers, the display is left turned off

//Display brightness
//Each digit is on for a certain amount of microseconds
//Then it is off until we have reached a total of 20ms for the function call
//Let's assume each digit is on for 1000us
//If each digit is on for 1ms, there are 4 digits, so the display is off for 16ms.
//That's a ratio of 1ms to 16ms or 6.25% on time (PWM).
//Let's define a variable called brightness that varies from:
//5000 blindingly bright (15.7mA current draw per digit)
//2000 shockingly bright (11.4mA current draw per digit)
//1000 pretty bright (5.9mA)
//500 normal (3mA)
//200 dim but readable (1.4mA)
//50 dim but readable (0.56mA)
//5 dim but readable (0.31mA)
//1 dim but readable in dark (0.28mA)

// displayNumber() has added i parameter to display only 2 digit on specific position
void displayNumber(int toDisplay, int i) {
#define DISPLAY_BRIGHTNESS  500

#define DIGIT_ON  HIGH
#define DIGIT_OFF  LOW

  long beginTime = millis();

  for(int digit = i ; digit > i-2 ; digit--) {
    // digitalWrite(13, HIGH);

    //Turn on a digit for a short amount of time
    switch(digit) {
    // case 0:
      // digitalWrite(13, HIGH);
      // break;
    case 1:
      digitalWrite(digit1, DIGIT_ON);
      break;
    case 2:
      digitalWrite(digit2, DIGIT_ON);
      break;
    case 3:
      digitalWrite(digit3, DIGIT_ON);
      break;
    case 4:
      digitalWrite(digit4, DIGIT_ON);
      break;
    case 5:
      digitalWrite(digit5, DIGIT_ON);
      break;
    case 6:
      digitalWrite(digit6, DIGIT_ON);
      break;
    }

    //Turn on the right segments for this digit
    lightNumber(toDisplay % 10);
    toDisplay /= 10;

    delayMicroseconds(DISPLAY_BRIGHTNESS); //Display this digit for a fraction of a second (between 1us and 5000us, 500 is pretty good)

    //Turn off all segments
    lightNumber(10); 

    //Turn off all digits
    digitalWrite(digit1, DIGIT_OFF);
    digitalWrite(digit2, DIGIT_OFF);
    digitalWrite(digit3, DIGIT_OFF);
    digitalWrite(digit4, DIGIT_OFF);
    digitalWrite(digit5, DIGIT_OFF);
    digitalWrite(digit6, DIGIT_OFF);
    digitalWrite(13, LOW);
  }

  while( (millis() - beginTime) < 10) ; //Wait for 20ms to pass before we paint the display again
}

//Given a number, turns on those segments
//If number == 10, then turn off number
void lightNumber(int numberToDisplay) {

#define SEGMENT_ON  LOW
#define SEGMENT_OFF HIGH

  switch (numberToDisplay){

  case 0:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case 1:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case 2:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 3:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 4:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 5:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 6:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 7:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;

  case 8:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_ON);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 9:
    digitalWrite(segA, SEGMENT_ON);
    digitalWrite(segB, SEGMENT_ON);
    digitalWrite(segC, SEGMENT_ON);
    digitalWrite(segD, SEGMENT_ON);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_ON);
    digitalWrite(segG, SEGMENT_ON);
    break;

  case 10:
    digitalWrite(segA, SEGMENT_OFF);
    digitalWrite(segB, SEGMENT_OFF);
    digitalWrite(segC, SEGMENT_OFF);
    digitalWrite(segD, SEGMENT_OFF);
    digitalWrite(segE, SEGMENT_OFF);
    digitalWrite(segF, SEGMENT_OFF);
    digitalWrite(segG, SEGMENT_OFF);
    break;
  }
}