繼今年三月BLE+ATMEGA328P
After ble+atmega328p board in this March.
我把線路改進之後
I re-designed the circuit.
也把程式弄好,包含了APP
Also finished the code , including APP.
雖然還有一些小小的傳輸問題
Although there are some problems in the transmission.
只能等有空再改改了
I just can programming while I have spare time
最終目的是想放在環控上面,例如室內燈控或藍芽AC插座之類的
The finally purpose I want designed in an environment controller products, for example lighting controller or AC socket on BLE.
介紹一下主板:
Introduce the main board components:
主板共有2個主要元件,一是藍芽HM10,二是ATMEGA328P的MCU
There are two components in the main board, one is HM-10 Bluetooth, the other one is ATMEGA328P MCU.
照片如下:
The photos as follows:
主板程式的部分如下方所示,另外請注意,這程式是用ARDUINO IDE 1.0.5的版本,
The Main board's code as follows, please pay attention, this code is from ARDUINO IDE version 1.0.5
IDE必需使用1.0.5這個版本,不然其它版本遇到UART DISABLE好像會RESET,
You need have to use IDE 1.0.5 this version, otherwise you will find problems in UART disable reset in the others IDE version.
編譯好之後然後找出ARDUINO的 HEX 檔,用ISP下載到主板
After compiler find the hex file and download to the main board.(use ISP to download)
//HM10 AT-COMMAND TEST//
//Baudrate=9600
String TPassword="Ta0001e";
String RPassword="Ra0001e";
String Rsetcmmd="ResetServer";
//#define PINB *(unsigned char *)0x0023 /* Input pins */
//#define DDRB *(unsigned char *)0x0024 /* Data direction register */
//#define PORTB *(unsigned char *)0x0025 /* Data register */
char IOstatus[5]="0000";//
String CompareIO="";
unsigned char SRI_rx[]="";
byte PBread;
char ledPin0 = 8; //PORTB0
char ledPin1 = 9; //PORTB1
int ledPin2 = 10; //PORTB2
int ledPin3 = 11; //PORTB3
int ledPinD0 = 4; //PORTB0
int ledPinD1 = 5; //PORTB1
int ledPinD2 = 6; //PORTB2
int ledPinD3 = 7; //PORTB3
void(* resetFunc) (void) = 0;
///////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600); // open the serial port at 9600 bps:
pinMode(12, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(ledPin0, INPUT);
pinMode(ledPin1, INPUT);
pinMode(ledPin2, INPUT);
pinMode(ledPin3, INPUT);
}
void loop()
{
char incomingByte,kk,mm,led_status;
unsigned char ioread;
String Rxstring,RxPass ="";
//////SEND IO STATUS///////////////////
ioread=0;
for(;;)
{
IOstatus[0]='0';
if(digitalRead(4)==1)
IOstatus[0]='1';
IOstatus[1]='0';
if(digitalRead(5)==1)
IOstatus[1]='1';
IOstatus[2]='0';
if(digitalRead(6)==1)
IOstatus[2]='1';
IOstatus[3]='0';
if(digitalRead(7)==1)
IOstatus[3]='1';
CompareIO=IOstatus;
Serial.print(TPassword+CompareIO); // prints a label
Sendstatus:
delay(1000);
/////////////////////////
while (Serial.available() > 0)
{
incomingByte = Serial.read();
Rxstring+=incomingByte;
incomingByte=0;
}
//Serial.print(Rxstring);
//Serial.print("\r\n");
if(Rxstring=="")
break;
if(Rxstring==(RPassword+CompareIO))
{
led_status=0;
digitalWrite(12, HIGH);
Rxstring="";
RxPass="";
mm=0;
while(1)
{
while (Serial.available() > 0)
{
incomingByte = Serial.read();
SRI_rx[mm]=incomingByte;
Rxstring+=incomingByte;
incomingByte=0;
mm+=1;
if(mm==11)
{
led_status=1;
Serial.end();
}
}
if(led_status==1)
{
led_status=0;
mm=0;
Serial.begin(9600);
if(Rxstring==Rsetcmmd)
{
Rxstring="";
asm volatile (" jmp 0");
}
Rxstring="";
if(SRI_rx[0]=='R' && SRI_rx[1]=='a' && SRI_rx[2]=='0' && SRI_rx[3]=='0' && SRI_rx[4]=='0' && SRI_rx[5]=='1' && SRI_rx[6]=='e')
{
switch (SRI_rx[10]) {
case '1':
digitalWrite(7, HIGH);
break;
case '0':
digitalWrite(7, LOW);
break;
}
switch (SRI_rx[9]) {
case '1':
digitalWrite(6, HIGH);
break;
case '0':
digitalWrite(6, LOW);
break;
}
switch (SRI_rx[8]) {
case '1':
digitalWrite(5, HIGH);
break;
case '0':
digitalWrite(5, LOW);
break;
}
switch (SRI_rx[7]) {
case '1':
digitalWrite(4, HIGH);
break;
case '0':
digitalWrite(4, LOW);
break;
}
Serial.print("Ta0001e");
Serial.write(SRI_rx[7]);
Serial.write(SRI_rx[8]);
Serial.write(SRI_rx[9]);
Serial.write(SRI_rx[10]);
delay(300);
}
}
delay(200);
}
}
//delay(500);
//delay(500);
Rxstring="";
}//end for(;;)
}//END LOOP
底下是APP介面,這次的APP設計成可讀取目前主板的 I/O是ON 或OFF 狀態
The follows photo is APP interface , I have designed read the Main Board's status through the APP, if IO is ON(high) or OFF(low).