Skip to content

Instantly share code, notes, and snippets.

@jasonhejna
Last active February 13, 2016 06:53
Show Gist options
  • Save jasonhejna/59fd227606769df5e601 to your computer and use it in GitHub Desktop.
Save jasonhejna/59fd227606769df5e601 to your computer and use it in GitHub Desktop.
ATtiny85 code for serial communication with the MG2639 cellular modem
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
// Timing Characteristics All constants in this section defined in milliseconds
#define POWER_PULSE_DURATION 2000 // 2-5s pulse required to turn on/off
#define MODULE_OFF_TIME 10000 // Time the module takes to turn off
#define COMMAND_RESPONSE_TIME 500 // Command response timeout on UART
#define MODULE_WARM_UP_TIME 3000 // Time between on and ready
const char* gprs_open = "+ZPPPOPEN:CONNECTED";
unsigned int gprs_open_len = 18;
unsigned int start_line = 0;
unsigned int end_line = 0;
unsigned int i = 0;
unsigned int j = 0;
unsigned long seconds = 0;
unsigned int loop_count = 0;
unsigned int mhz = 16000;
char serial_buff[47];
//char serial_eval[47];
//unsigned int byte_count = 0;
//char serial_out[20];
//connection
char ipSetupCmd[33] = "\0";
//emset(ipSetupCmd, "\0", 33);
const char TCP_SETUP[] = "AT+ZIPSETUP"; //at?
unsigned int channel = 0;
//IPAddress serverIP(54, 88, 179, 111);//54.88.179.111
unsigned int port = 8080;
const char getRequest1[] = "GET / HTTP/1.1";
const char getRequest2[] = "Host: 54.88.179.111:8080";
const char getRequest3[] = "message-body: {\"device_id\":\"0123456789\",\"api_key\":\"9a8b7c6d5e4f3g2h1i\"}";
const char getRequest4[] = "\n\n";
void setup() {
delay(5000);
// Open serial communication
Serial.begin(9600);
//Serial.println("Enter Serial to Start");
//while (!Serial) {
// ;
//}
// initialize pins
pinMode(7, OUTPUT);
digitalWrite(7, HIGH);
delay(POWER_PULSE_DURATION);
digitalWrite(7, LOW);
delay(MODULE_WARM_UP_TIME);
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
}
void loop() {
if ( mySerial.available() > 0 ) {
mySerial.readBytesUntil('\0', serial_buff, 47);
//Serial.write(serial_buff);
for ( i = 0; i < 47; i++ ) {
if ( serial_buff[i] == '+' ) {
start_line = i;
}
if ( serial_buff[i] == '\n' ) {
end_line = i;
for ( j = start_line; j < end_line; j++ ) {
Serial.write(serial_buff[j]);
}
}
}
}
if ( loop_count % mhz == 0 ) {
seconds++;
Serial.println(seconds);
loop_count = 0;
switch (seconds) {
case 9:
mySerial.println("AT+CIMI?");
break;
case 12:
mySerial.println("A/");
break;
case 15:
mySerial.println("AT+GSN");
break;
case 18:
// grrp open
mySerial.println("AT+ZPPPOPEN");
break;
case 23:
sprintf(ipSetupCmd, "%s=%d,%d.%d.%d.%d,%d", TCP_SETUP, channel, 54, 88, 179, 111, port);
mySerial.println(ipSetupCmd);
break;
case 25:
mySerial.println(getRequest1);
mySerial.println(getRequest2);
mySerial.println(getRequest3);
mySerial.println(getRequest4);//problem is with the println, and or the variable getRequest
break;
case 33:
// turn off
digitalWrite(7, HIGH);
delay(POWER_PULSE_DURATION);
digitalWrite(7, LOW);
break;
}
}
loop_count++;
//if (Serial.available()) {
// mySerial.println(Serial.read());
//}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment