Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <SoftwareSerial.h>
- #include <Wire.h>
- #include <Adafruit_LIS3DH.h>
- #include <U8glib.h>
- #define SIM800_TX_PIN 8
- #define SIM800_RX_PIN 7
- #define TOUCH_PIN 6
- #define VIBRATION_PIN 5
- SoftwareSerial serialSIM800(SIM800_TX_PIN, SIM800_RX_PIN);
- Adafruit_LIS3DH accelerometer = Adafruit_LIS3DH();
- U8GLIB_SH1106_128X64 display(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_FAST);
- int displayTextValue = -128;
- void readSIM800Serial() {
- while (serialSIM800.available()) {
- Serial.write(serialSIM800.read());
- }
- Serial.println();
- }
- String executeCommand(String command, unsigned long delayMs) {
- while (serialSIM800.available()) {
- serialSIM800.flush();
- }
- serialSIM800.println(command.c_str());
- delay(delayMs);
- String response = "";
- while (serialSIM800.available()) {
- response.concat(serialSIM800.readString());
- delay(10);
- }
- response.remove(0, command.length());
- response.trim();
- return response;
- }
- void sendMessage() {
- serialSIM800.flush();
- serialSIM800.println("AT+CSQ");
- readSIM800Serial();
- serialSIM800.println("AT+CMGF=1");
- delay(1000);
- Serial.print("\t [*] AT+CMGF=1 --> ");
- readSIM800Serial();
- String number = "509161630";
- serialSIM800.println("AT+CMGS=\"" + number + "\"\r");
- Serial.print("[*] AT+CMGS=1 --> ");
- readSIM800Serial();
- delay(1000);
- String SMS = String("Test arduino");
- serialSIM800.println(SMS);
- delay(100);
- serialSIM800.println((char) 26);
- Serial.print("[*] Send message --> ");
- readSIM800Serial();
- Serial.println("[*] After message");
- }
- boolean communicationInitialized = false;
- void initInternetCommunication() {
- serialSIM800.flush();
- Serial.print("Initializing network connection... ");
- Serial.println(executeCommand("AT", 300));
- Serial.println(executeCommand("AT+SAPBR=3, 1, \"APN\", \"internet\"", 1000));
- Serial.println(executeCommand("AT+SAPBR=1, 1", 1000));
- Serial.println(executeCommand("AT+HTTPINIT", 1000));
- communicationInitialized = true;
- Serial.println("SUCCESS");
- }
- String getDataFromServer() {
- serialSIM800.flush();
- if (!communicationInitialized) {
- Serial.println("Error, sim800l not initialized!");
- return "";
- }
- Serial.println(executeCommand("AT+HTTPPARA=\"CID\", 1", 300));
- Serial.println(executeCommand("AT+HTTPPARA=\"URL\", \"http://httpbin.org/get\"", 300));
- Serial.println(executeCommand("AT+HTTPACTION=0", 2000));
- String result = executeCommand("AT+HTTPREAD", 2000);
- Serial.println(result);
- return result;
- }
- void sendDataToServer() {
- serialSIM800.flush();
- if (!communicationInitialized) {
- Serial.println("Error, sim800l not initialized!");
- return;
- }
- Serial.println(executeCommand("AT+HTTPPARA=\"URL\", \"http://httpbin.org/post\"", 300));
- Serial.println(executeCommand("AT+HTTPPARA=\"CONTENT\", \"application/x-www-form-urlencoded\"", 300));
- Serial.println(executeCommand("AT+HTTPDATA=8, 2000", 300));
- Serial.println(executeCommand(String(millis()), 1000));
- Serial.println(executeCommand("AT+HTTPACTION=1", 500));
- }
- void setup() {
- pinMode(2, OUTPUT);
- digitalWrite(2, HIGH);
- pinMode(3, OUTPUT);
- digitalWrite(3, HIGH);
- delay(2000);
- digitalWrite(3, LOW);
- pinMode(4, OUTPUT);
- digitalWrite(4, HIGH);
- Serial.begin(9600);
- pinMode(TOUCH_PIN, INPUT);
- pinMode(VIBRATION_PIN, OUTPUT);
- pinMode(13, OUTPUT);
- digitalWrite(13, LOW);
- serialSIM800.begin(9600);
- delay(1000);
- if (!accelerometer.begin()) {
- Serial.println("Couldn't find LIS3DH!");
- }
- accelerometer.setRange(LIS3DH_RANGE_4_G);
- display.setColorIndex(1);
- display.setFont(u8g_font_unifont);
- display.begin();
- Serial.print("Initializing...");
- String result = "";
- while (result[9] != '1') {
- result = executeCommand("AT+CREG?", 100);
- delay(200);
- }
- Serial.println(" SUCCESS!");
- initInternetCommunication();
- digitalWrite(13, HIGH);
- }
- int counter = 0;
- void loop() {
- display.firstPage();
- accelerometer.read();
- // Serial.print("X: "); Serial.print(accelerometer.x);
- // Serial.print(" \tY: "); Serial.print(accelerometer.y);
- // Serial.print(" \tZ: "); Serial.println(accelerometer.z);
- // Serial.print("HeartBeat: ");
- // Serial.println(analogRead(A0));
- String dataValue = "";
- if (digitalRead(TOUCH_PIN) == HIGH) {
- analogWrite(VIBRATION_PIN, 255);
- delay(300);
- analogWrite(VIBRATION_PIN, 0);
- dataValue = getDataFromServer();
- // sendDataToServer();
- // Serial.println("======= Returned value: ");
- // dataValue.trim();
- // Serial.println(dataValue);
- // Serial.println("======= End");
- }
- displayTextValue++;
- if (displayTextValue > 130) {
- displayTextValue = -128;
- }
- do {
- display.drawStr(24, 24, "Limfy v0.1");
- if (dataValue == "") {
- display.drawStr(displayTextValue, 48, "In development");
- } else {
- display.drawStr(displayTextValue, 48, dataValue.c_str());
- }
- } while (display.nextPage());
- if (dataValue != "") {
- delay(3000);
- }
- delay(70);
- // delay(2000);
- // analogWrite(VIBRATION_PIN, 30);
- // delay(300);
- // analogWrite(VIBRATION_PIN, 0);
- // sendDataToServer();
- // counter++;
- // Serial.print("COUNTER ==> ");
- // Serial.println(counter);
- // analogWrite(VIBRATION_PIN, 30);
- // delay(300);
- // analogWrite(VIBRATION_PIN, 0);
- }
Add Comment
Please, Sign In to add comment