#include #include #include #define SERIAL2_TX_D6 12 #define SERIAL2_RX_D7 13 #define SERIAL_DELAY 200 #define DEBUG_D1 5 #define ONBOARD_LED 16 #define ERROR_BLINK_DELAY 200 const char* ssid = "gutmetmetihmseindings"; const char* wifipassword = "321meins"; const uint16_t serverport = 666; IPAddress local_IP(192,168,0,1); IPAddress gateway(0,0,0,0); IPAddress subnet(255,255,255,0); WiFiServer server(serverport); WiFiClient client; SoftwareSerial myPort; bool debugMode = true; // false; bool okSIM = true; bool ledOn = false; void determineDebugMode() { pinMode(DEBUG_D1, INPUT_PULLUP); pinMode(ONBOARD_LED, OUTPUT); digitalWrite(ONBOARD_LED, LOW); delay(100); if (digitalRead(DEBUG_D1) == LOW) debugMode = true; } void setupAP() { if (debugMode) { WiFi.softAPConfig(local_IP, gateway, subnet); WiFi.softAP(ssid, wifipassword); } } void setupSoftSerial() { // SIM 800L comm myPort.begin(9600, SWSERIAL_8N1, SERIAL2_RX_D7, SERIAL2_TX_D6, false); if (!myPort) while (true) delay(1000); } void setupDebugServer() { if (debugMode) { server.begin(); do client = server.available(); while (!client); } } String sendCmd(String cmd) { myPort.println(cmd); delay(SERIAL_DELAY); return myPort.readString(); } bool sendCmdAndExpect(String cmd, String expected) { String response = sendCmd(cmd); if (expected != NULL && expected != "") return response.indexOf(expected) > -1; else return true; } void checkSIMModule() { if (!debugMode) { okSIM &= sendCmdAndExpect("ATI", "SIM800"); okSIM &= sendCmdAndExpect("AT+CREG?", "+CREG: 0,1"); String resp = sendCmd("AT+CSQ"); int first = resp.indexOf(":"); int last = resp.indexOf(","); if (first > -1 && last > -1 && (last-1) > first) { String tmp = resp.substring(first+1, last); tmp.trim(); int val = tmp.toInt(); okSIM &= (val > 10); } } } void setup() { determineDebugMode(); setupAP(); setupSoftSerial(); setupDebugServer(); checkSIMModule(); } void loop() { if (debugMode) { while (client.available() == 0) {} String cmd = client.readString(); cmd.trim(); myPort.println(cmd); delay(333); client.println(myPort.readString()); } else if (!okSIM) { ledOn = !ledOn; digitalWrite(ONBOARD_LED, ledOn); delay(ERROR_BLINK_DELAY); } else { } }