2019年12月31日 星期二

STM32 on Arduino IDE



Arduino 1.8.13 
//-------------------------------------------------------------------------------------------------------------------

Arduino STM32 Nucleo-64 開發環境設定 

https://moon-half.info/p/2373

https://raw.githubusercontent.com/stm32duino/BoardManagerFiles/master/STM32/package_stm_index.json


https://dl.espressif.com/dl/package_esp32_index.json,http://arduino.esp8266.com/stable/package_esp8266com_index.json,https://raw.githubusercontent.com/stm32duino/BoardManagerFiles/master/STM32/package_stm_index.json



Tools -->  Board:   --->  Boards Manager  

search :  stm32f1  

Install  STM32F1xx/ .. ... ..



ST Link SWD Upload ..........

https://www.st.com/en/development-tools/stm32cubeprog.html

install   STM32CubeProgrammer



Serial Com Port upload ......
FTDI  Pin to STM32 Board pin

5V     ----------   5V
Gnd  ----------   Gnd
Rx    ----------   A9
Tx    ----------   A10   

Tools --> Upload method:  Serial

Tools -->  select Port : 


Pin  Boot0  --> 1  

RESET Board 
Sketch Upload  .....

電源不穩可能會上傳失敗


https://www.st.com/en/microcontrollers/stm32f103.html?querycriteria=productId=LN1565


https://www.st.com/resource/en/datasheet/stm32f103rc.pdf


https://translate.google.com/translate?hl=zh-TW&sl=zh-CN&u=http://www.makerlab.me/guides/2/articles/8&prev=search






//============================================================
Arduino IDE 1.8.4   or  other

//------- only STM32F1  --------------------------------------------------------------------------
File  ->  Preferences--> Additional Board Manager URLs:

http://dan.drown.org/stm32duino/package_STM32duino_index.json     --> OK


//--------keep ESP32 & ESP8266 ----------------------------------------------------------------------------------
ESP8266
ESP32
STM32

https://dl.espressif.com/dl/package_esp32_index.json,http://arduino.esp8266.com/stable/package_esp8266com_index.json,http://dan.drown.org/stm32duino/package_STM32duino_index.json



選擇不同板子在編譯時所佔記憶體容量的差異性






2019年2月16日 星期六

String 逗號分隔範例 strtok_r & String



//  strtok_r_comma_split   


//  String strKey1 = (char*)keyData;                                                       // 字元陣列 to 串流  keyData  to  String

//  strKey1.toCharArray((char*)keyData, strKey1.length() + 1);        //  串流 to 字元陣列  string   to  keyData.

/*
   http://forum.arduino.cc/index.php?topic=186687.0

*/
// strtok_r_comma_split_testOK

//------------------ EEPROM  結構 -------------------------------------------------------------
struct espConf {
  char wifiSsid[20];
  char wifiPass[20];
  char iotKey[16];
  char set_1[4];
  char set_2[4];
};

espConf config_1 = {  "1100",  "74sjfekjtrhe",  "------!","63", "31" };

String adminConfString = "2100,wsderf4321543d,IoT_Write_KEY_16,70,4";

void setup () {
  Serial.begin (115200);
delay(200);
Serial.println("---------read   struct ------------");
Serial.println((char*)config_1.wifiSsid);
Serial.println((char*)config_1.wifiPass);
Serial.println((char*)config_1.iotKey);
Serial.println((char*)config_1.set_1);
Serial.println((char*)config_1.set_2);
Serial.println();
delay(200);




  int strLng = adminConfString.length() + 1;
  char sz[strLng];
  adminConfString.toCharArray(sz, strLng);                        // Convert string to array.  串流  轉 字元陣列sz


  char *str;
  // char sz[] = "2100,poiuytrewqpoi,IoT_Write_KEY_16,70,4";      // char array
  char *p = sz;                                                   // char array
  static int i = 0;
  String g0, g1, g2;
  String g3, g4;
  while (( str = strtok_r(p, ",", &p)) != NULL) {                  // NULL就可以了,代表我没有数据给你,你就用剩下的数据就可以了。
    if (i == 0) g0 = str;
    if (i == 1) g1 = str;
    if (i == 2) g2 = str;
    if (i == 3) g3 = str;
    if (i == 4) g4 = str;
    i++;
    Serial.println(str);     // String
  }
  int c_g3 = g3.toInt();                       // String to int
  int c_g4 = g4.toInt();                       // String to int
  Serial.print(g0); Serial.print(",");          // ptint String g0
  Serial.print(g1); Serial.print(",");          // ptint String g1
  Serial.print(g2); Serial.print(",");          // ptint String g2
  Serial.print(c_g3, DEC); Serial.print(",");   // ptint int c_g3
  Serial.print(c_g4, DEC); Serial.println(" "); // ptint int c_g4

  g0.toCharArray(config_1.wifiSsid, g0.length() + 1); // Convert string to array.  串流  轉 字元陣列
  g1.toCharArray(config_1.wifiPass, g1.length() + 1); // Convert string to array.  串流  轉 字元陣列
  g2.toCharArray(config_1.iotKey,   g2.length() + 1); // Convert string to array.  串流  轉 字元陣列
  g3.toCharArray(config_1.set_1,    g3.length() + 1); // Convert string to array.  串流  轉 字元陣列
  g4.toCharArray(config_1.set_2,    g4.length() + 1); // Convert string to array.  串流  轉 字元陣列
//  config_1.set_1 = c_g3;
//  config_1.set_2 = c_g4;



Serial.println("---------write  struct ------------");
Serial.println((char*)config_1.wifiSsid);
Serial.println((char*)config_1.wifiPass);
Serial.println((char*)config_1.iotKey);
Serial.println((char*)config_1.set_1);
Serial.println((char*)config_1.set_2);
Serial.println();
delay(200);

}


//  String strKey1 = (char*)keyData;                            // 字元陣列 to 串流  keyData  to  String
//  strKey1.toCharArray((char*)keyData, strKey1.length() + 1);  //  串流 to 字元陣列  string   to  keyData.


void loop () { }






//  String_comma_split   

https://stackoverflow.com/questions/11068450/arduino-c-language-parsing-string-with-delimiter-input-through-serial-interfa



String myString = ",daqwete0,rec23123ipt,weiqweght,timadsdasde,dasdasdasdasdte,";

// the setup routine runs once when you press reset:
void setup() {
  Serial.begin(115200);

  int Index1 = myString.indexOf(',');
  int Index2 = myString.indexOf(',', Index1 + 1);
  int Index3 = myString.indexOf(',', Index2 + 1);
  int Index4 = myString.indexOf(',', Index3 + 1);
  int Index5 = myString.indexOf(',', Index4 + 1);
  int Index6 = myString.indexOf(',', Index5 + 1);
  String Value_1 = myString.substring(Index1 + 1, Index2);
  String Value_2 = myString.substring(Index2 + 1, Index3);
  String Value_3 = myString.substring(Index3 + 1, Index4);
  String Value_4 = myString.substring(Index4 + 1, Index5);
  String Value_5 = myString.substring(Index5 + 1, Index6);
  //Serial.println(Index1);

  Serial.println(Value_1);
  Serial.println(Value_2);
  Serial.println(Value_3);
  Serial.println(Value_4);
  Serial.println(Value_5);

}


// the loop routine runs over and over again forever:
void loop() {

}


2019年2月8日 星期五

CharArray_Write EEPROM & Read EEPROM

/*
   2019-02-08 test OK by yulie
*/

char WifiName[] = "jigu";                               // Wifi name
char WifiPass[] = "0000";                               // Wifi password
//char WifiName[] = "M8_1";                             // Wifi name
//char WifiPass[] = "poiuytrewqpoi";                    // Wifi password


#include <EEPROM.h>

void setup() {
  Serial.begin(115200);
  //  EEPROM.begin(2048); // only for  ESP Chip


  //------------ write EEPROM to WifiName --------------------------------------------------------
  //  saveChar( WifiName, sizeof(WifiName), 0x10);  //將 WifiName 寫入EEPROM 0x10位置

  //------------ write EEPROM to WifiPass ----------------------------------------------------
  //  saveChar( WifiPass, sizeof(WifiPass), 0x20);  //將 WifiName 寫入EEPROM 0x20位置

  Serial.print("OLD_SSID: ");   Serial.write(WifiName, sizeof(WifiName)); Serial.println();
  Serial.print("OLD_PASS: ");   Serial.write(WifiPass, sizeof(WifiPass)); Serial.println();

  Serial.println("Read SSID & PASS  form EEPROM ... ");

  //------------ read EEPROM to WifiName --------------------------------------------------------
  String strName = (char*)readChar(0x10);                         // 將0x10讀到的字元陣列轉換成串流(strName)
  uint8_t lenN = strName.length();                                 // 取得串流長度
  char WifiName[lenN];                                          //字元陣列長度重設
  for (word i = 0; i < lenN; i++)WifiName[i] = strName.charAt(i);  // 將strName串流轉換成WifiName字元陣列
  Serial.print("New_SSID: "); Serial.write(WifiName, sizeof(WifiName)); Serial.println();

  //------------- read EEPROM to WifiPass -------------------------------------------------------
  String strPass = (char*)readChar(0x20);                         // 將0x20讀到的字元陣列轉換成串流(strPass)
  uint8_t lenP = strPass.length();                                // 取得串流長度
  char WifiPass[lenP];                                         //字元陣列長度重設
  for (word i = 0; i < lenP; i++)WifiPass[i] = strPass.charAt(i); // 將strPass串流轉換成WifiPass字元陣列
  Serial.print("New_PASS: "); Serial.write(WifiPass, sizeof(WifiPass)); Serial.println();

}

//----------------------- read EEPROM char array function return -----------
char* readChar (word addr) {
  uint8_t len = EEPROM.read(addr);                           // 讀取串流長度
  static  char buf[32];                                         // max len 32 char
  addr = addr + 1 ;   len = len + 1;
  for (word i = 0; i < len; i++) buf[i] = EEPROM.read(addr + i);
  //  Serial.write(buf, sizeof(buf)); Serial.println();
  return buf;
}


//---------- char array function return demo -----------
char* foo () {
  static char buf [100] = "But does it get goat's blood out?";
  return buf;
}  // end of foo




  //<< ========== write char[] to EEPROM =============================================
void saveChar(char charName[], byte lenIn, word addr) {  //Ex: saveChar( WifiName, sizeof(WifiName), 0x00);
  uint8_t len = lenIn - 1;        // 取得串流長度
  EEPROM.write(addr, len);     // 紀錄串流長度
  delay(5);                       // write takes 3.3 ms to complete
  addr = addr + 1 ;   len = len + 1;
  for (word i = 0; i < len; i++) {
    EEPROM.write(addr + i, charName[i]);
    delay(5); // write takes 3.3 ms to complete
  }
  delay(200);
//  Serial.print("write char: "); Serial.write(charName, len);      // 顯示字元陣列內容
//  Serial.print("  len: ");    Serial.print(len);
  Serial.print("  address : 0x"); Serial.print(addr, HEX); Serial.print(" ~ 0x"); Serial.println(addr + len, HEX);
  //  EEPROM.commit(); // Buffer SAVE to flase  (ESP8266 Only)
  //  delay(200);
}

/*
  //<< ========== read char[] form EEPROM =============================================
  byte readChar(char charName[],  word addr) {           //Ex:   readChar( WifiName, 0x00);
  uint8_t len = EEPROM.read(addr);                           // 讀取串流長度
  Serial.print("read  len: ");    Serial.println(len);
  addr = addr + 1 ;   len = len + 1;
  for (word i = 0; i < len; i++) {
    charName[i] = EEPROM.read(addr + i);
    delay(5);
  }
  delay(20);
  Serial.print("read char: "); Serial.write(charName, len);      // 顯示字元陣列內容
  Serial.print("  len: ");    Serial.print(len);
  Serial.print("  address : 0x"); Serial.print(addr, HEX); Serial.print(" ~ 0x"); Serial.println(addr + len, HEX);
  //  EEPROM.commit(); // Buffer SAVE to flase  (ESP8266 Only)
  //  delay(200);
  len = len - 1;
  Serial.print(" return len: "); Serial.println(len);
  return len  ;
  }
*/

void loop() {}

String_Write EEPROM & Read EEPROM

/*
   Circuits4you.com
   Reading and Writing String to EEPROM Example Code
   Oct 2018
   https://circuits4you.com/2018/10/16/arduino-reading-and-writing-string-to-eeprom/

   佔用空間為字串長度 + 1 ,需注意
*/

//String key1 = "1000000000";
//String key2 = "20000000-";
//String key3 = "300000000--";

String key1 = "FAPOUNY1BRB3HIH2";
String key2 = "G1RZYYND9TV636UF";
String key3 = "GT34WTYAZ6E0HGK7";

#include <EEPROM.h>

//void writeString(char add,String data);
//String read_String(char add);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);


  //------------ write EEPROM to WifiName --------------------------------------------------------
  writeString(0xA0, key1);  //Address 0xA0 and String type key1 (char array +1 )
  writeString(0xB1, key2);  //Address 0xB0 and String type key2 (char array +1 )
  writeString(0xC2, key3);  //Address 0xC0 and String type key3 (char array +1 )


  //------------ read EEPROM to WifiName --------------------------------------------------------
  key1 = read_String(0xA0);   // read Address 0xA0 to String key1
  key2 = read_String(0xB1);   // read Address 0xA0 to String key1
  key3 = read_String(0xC2);   // read Address 0xA0 to String key1

  Serial.print("Read Data key1:");  Serial.println(key1);
  Serial.print("Read Data key2:");  Serial.println(key2);
  Serial.print("Read Data key3:");  Serial.println(key3);


  /*
    //-----write -----------------------
    String data = "Hello World";
    Serial.print("Writing Data:");
    Serial.println(data);
    writeString(10, data);  //Address 10 and String type data
    delay(10);


    //-----read -----------------------
    String recivedData;
    recivedData = read_String(10);
    Serial.print("Read Data:");
    Serial.println(recivedData);
    delay(1000);
  */

  // 串流String 內容固定長度方式
  //  Serial.println(key1); Serial.println(key2); Serial.println(key3);
  //Serial.println(key4); Serial.println(key5); Serial.println(key6);
  // saveKey(key1,0x80);  // 0x80 ~ 0x8F  (16 Byte)
  // saveKey(key2,0x90);  // 0x90 ~ 0x9F  (16 Byte)
  // saveKey(key3,0xA0);  // 0xA0 ~ 0xAF  (16 Byte)

  //  readKey(key1, 0x80); // 0x80 ~ 0x8F  (16 Byte)
  //  readKey(key2, 0x90); // 0x90 ~ 0x9F  (16 Byte)
  //  readKey(key3, 0xA0); // 0xA0 ~ 0xAF  (16 Byte)      // 將key3 從 EEPROM帶回來


}





void loop() {
  // put your main code here, to run repeatedly:

}


//<< ==== EEPROM  write_String    =============================================
void writeString(byte addr, String data) {
  int _size = data.length();
  int i;
  for (i = 0; i < _size; i++) {
    EEPROM.write(addr + i, data[i]);
  }
  EEPROM.write(addr + _size, '\0'); //Add termination null character for String Data 加入空白結束字符
//  Serial.print("write char: "); Serial.write(data, _size);      // 顯示字元陣列內容
//  Serial.print("  len: ");    Serial.print(_size);
  Serial.print("write address : 0x"); Serial.print(addr, HEX); Serial.print(" ~ 0x"); Serial.println(addr + _size , HEX);
}


//<< ==== EEPROM  read_String    =============================================
String read_String(byte addr) {
  int i;
  char data[126];                     //Max 126 Bytes
  int len = 0;
  unsigned char k;
  k = EEPROM.read(addr);
  while (k != '\0' && len < 500) {    //Read until null character 讀取直到null字符
    k = EEPROM.read(addr + len);
    data[len] = k;
    len++;
  }
  data[len] = '\0';                   // 先找出(while)  空白結束字符(\0),帶入 string 長度(len)
  return String(data);
}


  //<< ========== save key String to EEPROM =============================================
byte saveKey(String strKey, word addr) {
  String keystr = String((String)strKey);
  uint8_t len = keystr.length();                               // 自動取得串流長度
  uint8_t keyData[len];
  //  for(word i=0;i < len;i++)keyData[i] = keystr.charAt(i);    // 將keystr串流轉換成keyData字元陣列
  for (word i = 0; i < len; i++) {
    keyData[i] = keystr.charAt(i);                                //來源為串流才使用將(串流)轉換成 (字元陣列)
    EEPROM.write(addr + i, keyData[i]);
  }
  Serial.write(keyData, len);                                  // 顯示字元陣列內容
  Serial.print("  len="); Serial.println(len);
  //  EEPROM.commit(); // Buffer SAVE to flase  (ESP8266 Only)
  delay(200);
}


  //<< ========== read key EEPROM to Stringg   ==========================================
byte readKey(String &strKey, word addr) {                          // &將 EEPROM read帶回來的值 寫回帶進來的變數key_
  uint8_t len = strKey.length();                                   // 串流長度
  uint8_t keyData[len];
  for (word i = 0; i < len; i++)keyData[i] = EEPROM.read(addr + i);
  Serial.write(keyData, len); delay(100); Serial.println(" ");      // 顯示字元陣列內容
  String strKey1 = (char*)keyData;                                 // uint8_t    將字元陣列keyData  轉換成串流keystr
  strKey = strKey1;
}