티스토리 뷰

728x90

1. 개요

  

  EEPROM을 이용하면 보드내 변수를 동적으로 사용할 수 있다. 이때 Serial 통신의 메시지를 통해 EEPROM에 저장되는 데이터를 읽고, 쓰며, 이를 활용하여 Setting을 만들어 보자.





2. Source Code

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <EEPROM.h>
#define EEP_DATA_LEN 100
 
const String ModeCommand = String("AT");
const String SSIDCommand = String("AT+SSID");
const String PASSWDCommand = String("AT+PASSWD");
const String BROKERCommand = String("AT+BROKER");
const String ENDCommand = String ("AT+END");
const String CommnadLine = "("+ SSIDCommand + ", " + PASSWDCommand + ", " + BROKERCommand + ", " + ENDCommand +")";
 
bool mode = false;
String mSSID, mPASSWD, mBROKER;
 
void EEPread(){
    int startAt,endAt;
    String readData = "";
    char temp;
    for (int i = 0; i < EEP_DATA_LEN; ++i)
    {
        temp = char(EEPROM.read(i));
        if(temp == '!')
            break;
        readData += temp;
    }
   
    endAt = readData.indexOf('/',0);
    mSSID = readData.substring(0,endAt);
     
    startAt = endAt+1;
    endAt = readData.indexOf('/',startAt);
    mPASSWD = readData.substring(startAt,endAt);
     
    startAt = endAt+1;
    endAt = readData.indexOf('/',startAt);
    mBROKER = readData.substring(startAt,endAt);
}
 
void EEPwrite(){
    String writeData = mSSID + "/" + mPASSWD + "/" + mBROKER + "!";
 
    for (int i = 0; i < EEP_DATA_LEN; ++i) {
        EEPROM.write(i, 0); //clearing
    }
   // EEPROM.commit();  //esp8266
     
    for (int i = 0; i < writeData.length(); ++i)
    {
        EEPROM.write(i, writeData[i]);
    }
     
   // EEPROM.commit();  //esp8266
}
 
void printSettingValue(){
    Serial.println("Current Setting Value");
    Serial.println("SSID : " + mSSID);
    Serial.println("SSID's Passwd : " + mPASSWD);
    Serial.println("Broker IP : " + mBROKER);
}
 
void setup() {
    //Serial.begin(9600); 
    Serial.begin(115200);
    //EEPROM.begin();      //Arduino Board(ex.UNO) need this command.But other Board may not need this command
    EEPread();
     
    for(int i=0;i<100;i++){
        if(Serial.available()){
            String str = Serial.readString();
            if(str.equals(ModeCommand)){
                Serial.println(str);
                Serial.println("OK\n");
                printSettingValue();
                Serial.println("\nInput Command" + CommnadLine + "\n");
                mode = true;
                break;
            }
        }
        delay(100);
    }
}
 
void loop() {
    if(mode){
        if(Serial.available()){
            String str = Serial.readString();
            if(str.equals(SSIDCommand)){
                Serial.println(str);
                Serial.println("Input New SSID!");
                while(1){
                    if(Serial.available()){
                        mSSID = Serial.readString();
                        Serial.print("New SSID is '");
                        Serial.print(mSSID);
                        Serial.println("'");
                        EEPwrite();
                        Serial.println("\nInput Command" + CommnadLine + "\n");
                        break;
                    }
                }
            }else if(str.equals(PASSWDCommand)){
                Serial.println(str);
                Serial.println("Input New SSID's Passwd!");
                while(1){
                    if(Serial.available()){
                        mPASSWD = Serial.readString();
                        Serial.print("New SSID's Passwd is '");
                        Serial.print(mPASSWD);
                        Serial.println("'");
                        EEPwrite();
                        Serial.println("\nInput Command" + CommnadLine + "\n");
                        break;
                    }
                }
            }else if(str.equals(BROKERCommand)){
                Serial.println(str);
                Serial.println("Input New Broker IP!");
                while(1){
                    if(Serial.available()){
                        mBROKER = Serial.readString();
                        Serial.print("New Broker IP is '");
                        Serial.print(mBROKER);
                        Serial.println("'");
                        EEPwrite();
                        Serial.println("\nInput Command" + CommnadLine + "\n");
                        break;
                    }
                }
            }else if(str.equals(ENDCommand)){
                Serial.println(str);
                printSettingValue();
                Serial.print("\nSetting Value Saved and END!!");
            }else{
                Serial.println(str);
                Serial.print("'");
                Serial.print(str);
                Serial.print("'");
                Serial.println("is not Command"+CommnadLine);
            }
        }
    }else{
        //Here Loop Program
    }
}

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함