Tobiahao

Arduino example

Oct 22nd, 2019
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.77 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <SoftwareSerial.h>
  3. #include <Wire.h>
  4. #include <Adafruit_LIS3DH.h>
  5. #include <U8glib.h>
  6.  
  7. #define SIM800_TX_PIN 8
  8. #define SIM800_RX_PIN 7
  9. #define TOUCH_PIN 6
  10. #define VIBRATION_PIN 5
  11.  
  12. SoftwareSerial serialSIM800(SIM800_TX_PIN, SIM800_RX_PIN);
  13. Adafruit_LIS3DH accelerometer = Adafruit_LIS3DH();
  14. U8GLIB_SH1106_128X64 display(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_FAST);
  15. int displayTextValue = -128;
  16.  
  17. void readSIM800Serial() {
  18.     while (serialSIM800.available()) {
  19.         Serial.write(serialSIM800.read());
  20.     }
  21.     Serial.println();
  22. }
  23.  
  24. String executeCommand(String command, unsigned long delayMs) {
  25.     while (serialSIM800.available()) {
  26.         serialSIM800.flush();
  27.     }
  28.  
  29.     serialSIM800.println(command.c_str());
  30.    
  31.     delay(delayMs);
  32.  
  33.     String response = "";
  34.     while (serialSIM800.available()) {
  35.         response.concat(serialSIM800.readString());
  36.         delay(10);
  37.     }
  38.  
  39.     response.remove(0, command.length());
  40.     response.trim();
  41.  
  42.     return response;
  43. }
  44.  
  45. void sendMessage() {
  46.     serialSIM800.flush();
  47.    
  48.     serialSIM800.println("AT+CSQ");
  49.     readSIM800Serial();
  50.  
  51.     serialSIM800.println("AT+CMGF=1");
  52.     delay(1000);
  53.     Serial.print("\t [*] AT+CMGF=1 --> ");
  54.     readSIM800Serial();
  55.  
  56.     String number = "509161630";
  57.     serialSIM800.println("AT+CMGS=\"" + number + "\"\r");
  58.     Serial.print("[*] AT+CMGS=1 --> ");
  59.     readSIM800Serial();
  60.     delay(1000);
  61.  
  62.     String SMS = String("Test arduino");
  63.     serialSIM800.println(SMS);
  64.  
  65.     delay(100);
  66.     serialSIM800.println((char) 26);
  67.  
  68.     Serial.print("[*] Send message --> ");
  69.     readSIM800Serial();
  70.  
  71.     Serial.println("[*] After message");
  72. }
  73.  
  74. boolean communicationInitialized = false;
  75.  
  76. void initInternetCommunication() {
  77.     serialSIM800.flush();
  78.  
  79.     Serial.print("Initializing network connection... ");
  80.     Serial.println(executeCommand("AT", 300));
  81.     Serial.println(executeCommand("AT+SAPBR=3, 1, \"APN\", \"internet\"", 1000));
  82.     Serial.println(executeCommand("AT+SAPBR=1, 1", 1000));
  83.  
  84.     Serial.println(executeCommand("AT+HTTPINIT", 1000));
  85.  
  86.     communicationInitialized = true;
  87.     Serial.println("SUCCESS");
  88. }
  89.  
  90. String getDataFromServer() {
  91.     serialSIM800.flush();
  92.  
  93.     if (!communicationInitialized) {
  94.         Serial.println("Error, sim800l not initialized!");
  95.         return "";
  96.     }
  97.    
  98.     Serial.println(executeCommand("AT+HTTPPARA=\"CID\", 1", 300));
  99.     Serial.println(executeCommand("AT+HTTPPARA=\"URL\", \"http://httpbin.org/get\"", 300));
  100.     Serial.println(executeCommand("AT+HTTPACTION=0", 2000));
  101.    
  102.     String result = executeCommand("AT+HTTPREAD", 2000);
  103.     Serial.println(result);
  104.  
  105.     return result;
  106. }
  107.  
  108. void sendDataToServer() {
  109.     serialSIM800.flush();
  110.  
  111.     if (!communicationInitialized) {
  112.         Serial.println("Error, sim800l not initialized!");
  113.         return;
  114.     }
  115.  
  116.     Serial.println(executeCommand("AT+HTTPPARA=\"URL\", \"http://httpbin.org/post\"", 300));
  117.     Serial.println(executeCommand("AT+HTTPPARA=\"CONTENT\", \"application/x-www-form-urlencoded\"", 300));
  118.     Serial.println(executeCommand("AT+HTTPDATA=8, 2000", 300));
  119.     Serial.println(executeCommand(String(millis()), 1000));
  120.    
  121.     Serial.println(executeCommand("AT+HTTPACTION=1", 500));
  122. }
  123.  
  124. void setup() {
  125.     pinMode(2, OUTPUT);
  126.     digitalWrite(2, HIGH);
  127.  
  128.     pinMode(3, OUTPUT);
  129.     digitalWrite(3, HIGH);
  130.     delay(2000);
  131.     digitalWrite(3, LOW);
  132.  
  133.     pinMode(4, OUTPUT);
  134.     digitalWrite(4, HIGH);
  135.  
  136.     Serial.begin(9600);
  137.     pinMode(TOUCH_PIN, INPUT);
  138.     pinMode(VIBRATION_PIN, OUTPUT);
  139.  
  140.     pinMode(13, OUTPUT);
  141.     digitalWrite(13, LOW);
  142.  
  143.     serialSIM800.begin(9600);
  144.     delay(1000);
  145.  
  146.     if (!accelerometer.begin()) {
  147.         Serial.println("Couldn't find LIS3DH!");
  148.     }
  149.     accelerometer.setRange(LIS3DH_RANGE_4_G);
  150.  
  151.     display.setColorIndex(1);
  152.     display.setFont(u8g_font_unifont);
  153.     display.begin();
  154.    
  155.     Serial.print("Initializing...");
  156.     String result = "";
  157.     while (result[9] != '1') {
  158.         result = executeCommand("AT+CREG?", 100);
  159.         delay(200);
  160.     }
  161.    
  162.     Serial.println(" SUCCESS!");
  163.     initInternetCommunication();
  164.     digitalWrite(13, HIGH);
  165. }
  166.  
  167. int counter = 0;
  168. void loop() {
  169.     display.firstPage();
  170.  
  171.     accelerometer.read();
  172.     // Serial.print("X:  "); Serial.print(accelerometer.x);
  173.     // Serial.print("  \tY:  "); Serial.print(accelerometer.y);
  174.     // Serial.print("  \tZ:  "); Serial.println(accelerometer.z);
  175.     // Serial.print("HeartBeat: ");
  176.     // Serial.println(analogRead(A0));
  177.     String dataValue = "";
  178.  
  179.     if (digitalRead(TOUCH_PIN) == HIGH) {
  180.         analogWrite(VIBRATION_PIN, 255);
  181.         delay(300);
  182.         analogWrite(VIBRATION_PIN, 0);
  183.  
  184.         dataValue = getDataFromServer();
  185.         // sendDataToServer();
  186.        
  187.         // Serial.println("======= Returned value: ");
  188.         // dataValue.trim();
  189.         // Serial.println(dataValue);
  190.         // Serial.println("======= End");
  191.     }
  192.  
  193.     displayTextValue++;
  194.     if (displayTextValue > 130) {
  195.         displayTextValue = -128;
  196.     }
  197.  
  198.     do {
  199.         display.drawStr(24, 24, "Limfy v0.1");
  200.         if (dataValue == "") {
  201.             display.drawStr(displayTextValue, 48, "In development");
  202.         } else {
  203.             display.drawStr(displayTextValue, 48, dataValue.c_str());
  204.         }
  205.     } while (display.nextPage());
  206.  
  207.     if (dataValue != "") {
  208.         delay(3000);
  209.     }
  210.     delay(70);
  211.  
  212.     // delay(2000);
  213.    
  214.     // analogWrite(VIBRATION_PIN, 30);
  215.     // delay(300);
  216.     // analogWrite(VIBRATION_PIN, 0);
  217.  
  218.     // sendDataToServer();
  219.     // counter++;
  220.     // Serial.print("COUNTER ==> ");
  221.     // Serial.println(counter);
  222.    
  223.     // analogWrite(VIBRATION_PIN, 30);
  224.     // delay(300);
  225.     // analogWrite(VIBRATION_PIN, 0);
  226. }
Add Comment
Please, Sign In to add comment