|
| 1 | +/* |
| 2 | + Description: Press btnB init to test SIMcard,signal,GPRS. Press btnC get host ip and test ping website. |
| 3 | +*/ |
| 4 | +#include <M5Stack.h> |
| 5 | +#define RX_PIN 16 |
| 6 | +#define TX_PIN 17 |
| 7 | +#define RESET_PIN 5 //Module reset resistance is not soldered. if necessary, weld it yourself. |
| 8 | + |
| 9 | +void header(const char *string){ |
| 10 | + M5.Lcd.setTextSize(1); |
| 11 | + M5.Lcd.setTextColor(WHITE, BLUE); |
| 12 | + M5.Lcd.fillRect(0, 0, 320, 30, BLUE); |
| 13 | + M5.Lcd.setTextDatum(TC_DATUM); |
| 14 | + M5.Lcd.drawString(string, 160, 3, 4); |
| 15 | + M5.Lcd.drawString("SIGNAL-REFRESH",160, 220, 2); |
| 16 | + M5.Lcd.drawString("GET-IP&PING",270, 220, 2); |
| 17 | + M5.Lcd.setCursor(180, 65, 2); |
| 18 | + M5.Lcd.setTextColor(WHITE, BLACK); |
| 19 | + M5.Lcd.print("Support USB Serial"); |
| 20 | + M5.Lcd.setCursor(150, 85, 2); |
| 21 | + M5.Lcd.print("AT Command Pass-through"); |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +String _readSerial(uint32_t timeout) |
| 26 | +{ |
| 27 | + uint64_t timeOld = millis(); |
| 28 | + while (!Serial2.available() && !(millis() > timeOld + timeout)) |
| 29 | + { |
| 30 | + delay(13); |
| 31 | + } |
| 32 | + String str; |
| 33 | + while(Serial2.available()) |
| 34 | + { |
| 35 | + if (Serial2.available()>0) |
| 36 | + { |
| 37 | + str += (char) Serial2.read(); |
| 38 | + } |
| 39 | + } |
| 40 | + Serial.print(str); |
| 41 | + return str; |
| 42 | +} |
| 43 | + |
| 44 | +void simcard_test(){ |
| 45 | + Serial2.print(F("AT+CPIN?\r")); |
| 46 | + String simcard_status = _readSerial(3000); |
| 47 | + Serial.print(simcard_status); |
| 48 | + M5.Lcd.setCursor(0, 40, 2); |
| 49 | + if(simcard_status == ""){ |
| 50 | + M5.Lcd.setTextColor(WHITE, RED); |
| 51 | + M5.Lcd.print("No SIM Card"); |
| 52 | + }else if(simcard_status.indexOf("READY")!= -1){ |
| 53 | + M5.Lcd.setTextColor(WHITE, 0x03E0); |
| 54 | + M5.Lcd.print("SIM Card OK"); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +void signal_test(){ |
| 59 | + String csq_data; |
| 60 | + M5.Lcd.setCursor(0, 65, 2); |
| 61 | + M5.Lcd.setTextColor(WHITE, RED); |
| 62 | + M5.Lcd.print("Not Signal"); |
| 63 | + M5.Lcd.setTextColor(WHITE, 0x03E0); |
| 64 | + do{ |
| 65 | + M5.Lcd.print("."); |
| 66 | + delay(500); |
| 67 | + Serial2.print(F("AT+CSQ\r")); |
| 68 | + csq_data = _readSerial(3000); |
| 69 | + }while(csq_data.indexOf("+CSQ: 0,0")!= -1); |
| 70 | + M5.Lcd.setCursor(0, 65, 2); |
| 71 | + M5.Lcd.print(csq_data.substring(csq_data.indexOf("+CSQ:"),csq_data.indexOf("OK"))); |
| 72 | +} |
| 73 | + |
| 74 | +void GPRS_init(){ |
| 75 | + Serial2.print(F("AT+CIPSHUT\r")); |
| 76 | + String init_data = _readSerial(4000); |
| 77 | + Serial2.print(F("AT+CSTT=\"CMNET\"\r")); |
| 78 | + init_data = _readSerial(4000); |
| 79 | + Serial2.print(F("AT+CIICR\r")); |
| 80 | + init_data = _readSerial(4000); |
| 81 | + Serial.print(init_data); |
| 82 | + M5.Lcd.setCursor(0, 90, 2); |
| 83 | + if((init_data.indexOf("ERROR")!= -1)||(init_data.indexOf("DEACT")!= -1)||(init_data == "")){ |
| 84 | + M5.Lcd.setTextColor(WHITE, RED); |
| 85 | + M5.Lcd.print("NOT"); |
| 86 | + }else{ |
| 87 | + M5.Lcd.setTextColor(WHITE, 0x03E0); |
| 88 | + M5.Lcd.print("GPRS OK"); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +void ping_test(){ |
| 93 | + String ping_data; |
| 94 | + Serial2.print(F("AT+CIFSR\r")); |
| 95 | + M5.Lcd.setCursor(0, 115, 2); |
| 96 | + M5.Lcd.setTextColor(WHITE, YELLOW); |
| 97 | + M5.Lcd.print("PINGING..."); |
| 98 | + ping_data = _readSerial(4000); |
| 99 | + M5.Lcd.setCursor(0, 115, 2); |
| 100 | + if((ping_data.indexOf("ERROR")!= -1)||(ping_data == "")){ |
| 101 | + M5.Lcd.setTextColor(WHITE, RED); |
| 102 | + M5.Lcd.print("PING NOT"); |
| 103 | + }else{ |
| 104 | + M5.Lcd.setTextColor(WHITE, 0x03E0); |
| 105 | + M5.Lcd.print(ping_data); |
| 106 | + Serial2.print(F("AT+CIPPING=\"www.baidu.com\"\r")); |
| 107 | + delay(1000); |
| 108 | + ping_data = _readSerial(4000); |
| 109 | + M5.Lcd.setCursor(0, 160, 1); |
| 110 | + M5.Lcd.print(ping_data); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +void setup() { |
| 115 | + M5.begin(); |
| 116 | + header("SIM800L Factory Test"); |
| 117 | + Serial2.begin(115200, SERIAL_8N1, 16, 17); |
| 118 | + delay(1000); |
| 119 | + simcard_test(); |
| 120 | + signal_test(); |
| 121 | + GPRS_init(); |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | +void loop(){ |
| 126 | + //AT instruction write |
| 127 | + if(Serial.available()){ |
| 128 | + Serial2.write(Serial.read()); |
| 129 | + } |
| 130 | + //AT instruction result |
| 131 | + if(Serial2.available()){ |
| 132 | + Serial.write(Serial2.read()); |
| 133 | + } |
| 134 | + //test SIMcard,signal,GPRS |
| 135 | + if(M5.BtnB.wasPressed()){ |
| 136 | + M5.Lcd.fillRect(0, 30, 150, 190, BLACK); |
| 137 | + simcard_test(); |
| 138 | + signal_test(); |
| 139 | + GPRS_init(); |
| 140 | + } |
| 141 | + //get host ip and test ping website. |
| 142 | + if(M5.BtnC.wasPressed()){ |
| 143 | + ping_test(); |
| 144 | + } |
| 145 | + M5.update(); |
| 146 | +} |
0 commit comments