muizenval

Observe mouse traps remotely
Log | Files | Refs

modem.ino (3439B)


      1 #include "include/config.h"
      2 #include "include/modem.h"
      3 
      4 
      5 void sara_modem::init() {
      6 	pinMode(modemPowerPin, OUTPUT);		 // Put voltage on the nb-iot module
      7 	pinMode(modemVoltagePin, OUTPUT);	 // Switch module voltage
      8 	pinMode(modemEnablePin, OUTPUT);	 // Set state to active
      9 
     10 	digitalWrite(modemPowerPin, HIGH);
     11 	digitalWrite(modemVoltagePin, LOW);
     12 	digitalWrite(modemEnablePin, HIGH);
     13 	modemSerial.begin(modemBaud);
     14 
     15 	while (!modemSerial)
     16 		;
     17 
     18 	// -*- module initialization -*-
     19 	// usbSerial.print(prefixInfo "waiting for module to start up");
     20 	for (;;) {
     21 		// usbSerial.print('.');
     22 		modemSerial.write("AT\r\n");
     23 		delay(1000);
     24 		if (modemSerial.available())
     25 			break;
     26 	}
     27 
     28 	while (modemSerial.available())	   // clear cache
     29 		modemSerial.read();
     30 	// usbSerial.println();
     31 }
     32 
     33 sara_modem::command_status sara_modem::send(const char* request, char* response, command_flags flags) {
     34 	char   line[lineBuffer];
     35 	size_t lineLen;
     36 	char   buf;
     37 
     38 	bool silent		  = flags & COMMAND_SILENT,
     39 		 block		  = flags & COMMAND_BLOCK,
     40 		 ignore		  = flags & COMMAND_IGNORE,
     41 		 event_handle = flags & COMMAND_EVENT;
     42 
     43 	if (response)
     44 		response[0] = '\0';
     45 
     46 	unsigned long start = millis(),
     47 				  now;
     48 
     49 	modemSerial.write(request);
     50 	modemSerial.write("\r\n");
     51 	modemSerial.flush();
     52 
     53 	if (blockDebug && block && !silent) {
     54 		// usbSerial.print(prefixDebug "command '");
     55 		// usbSerial.print(request);
     56 		// usbSerial.println("' is blocking");
     57 	}
     58 
     59 	for (;;) {
     60 		lineLen = 0;
     61 		for (;;) {
     62 			while (!modemSerial.available()) {
     63 				now = millis();
     64 				if (ignore && now - start > ignoreDelay * 1000) {
     65 					if (commandDebug && !silent) {
     66 						// usbSerial.print(prefixDebug "command '");
     67 						// usbSerial.print(request);
     68 						// usbSerial.println("' succeed (ignoring response)");
     69 					}
     70 					return COMMAND_TIMEOUT;
     71 				} else if (!ignore && !block && now - start > commandTimeout * 1000) {
     72 					if (commandDebug && !silent) {
     73 						// usbSerial.print(prefixWarn "command '");
     74 						// usbSerial.print(request);
     75 						// usbSerial.println("' timed out");
     76 					}
     77 					return COMMAND_TIMEOUT;
     78 				}
     79 			}
     80 			buf = modemSerial.read();
     81 			if (buf == '\r')
     82 				continue;
     83 			if (buf == '\n')
     84 				break;
     85 			line[lineLen++] = buf;
     86 		}
     87 		line[lineLen] = '\0';
     88 
     89 		if (String(line) == "OK") {
     90 			if (commandDebug && !silent) {
     91 				// usbSerial.print(prefixDebug "command '");
     92 				// usbSerial.print(request);
     93 				// usbSerial.println("' succeed");
     94 			}
     95 			return COMMAND_OK;
     96 		} else if (strstr(line, "ERROR")) {
     97 			if (commandDebug && !silent) {
     98 				// usbSerial.print(prefixError "command '");
     99 				// usbSerial.print(request);
    100 				// usbSerial.println("' failed");
    101 			}
    102 			return COMMAND_ERROR;
    103 		} else if (event_handle && line[0] == '+') {
    104 			if (eventDebug && !silent) {
    105 				// usbSerial.print(prefixEvent "event '");
    106 				// usbSerial.print(line);
    107 				// usbSerial.println(" caused'");
    108 			}
    109 		} else if (line[0] != '\0' && strcmp(request, line)) {
    110 			if (lineDebug && !silent) {
    111 				// usbSerial.print(prefixLine);
    112 				// usbSerial.print(request);
    113 				// usbSerial.print(" -> '");
    114 				// usbSerial.print(line);
    115 				// usbSerial.println("'");
    116 			}
    117 
    118 			if (response) {
    119 				if (response[0] != '\0')	// check if not empty string
    120 					strcat(response, "\n");
    121 				strcat(response, line);
    122 			}
    123 		}
    124 	}
    125 	delay(commandDelay * 1000);	   // wait 0.1 sec
    126 }
    127 
    128 sara_modem::command_status sara_modem::send(const char* request, command_flags flags) {
    129 	return send(request, NULL, flags);
    130 }