dramaquinn

Arduino web server

Oct 6th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <LiquidCrystal_I2C.h>
  2. #include <Wire.h>
  3. int _GSM_TXPIN_ = 10;
  4. int _GSM_RXPIN_ = 11;
  5. #include "SIM900.h"
  6. #include <SoftwareSerial.h>
  7. #include "inetGSM.h"
  8.  
  9. LiquidCrystal_I2C lcd(0x27, 20, 4);
  10. InetGSM inet;
  11.  
  12. int sensor = 52;
  13. int count = 0;
  14. int lastSensorState = 0;
  15. boolean started = false;
  16.  
  17. void setup() {
  18.   pinMode(sensor, INPUT);
  19.   Serial.begin(9600);
  20.   delay(2000);
  21.   Serial.println("Start LCD");
  22.   lcd.init();
  23.   lcd.backlight();
  24.   lcd.clear();
  25.   lcd.print("Starting...");
  26.   //
  27.   if (gsm.begin(4800)) {
  28.     Serial.println("\nstatus=READY");
  29.     started = true;
  30.   } else Serial.println("\nstatus=IDLE");
  31.   lcd.clear();
  32.   lcd.print("status=READY");
  33.   if (started) {
  34.     //viettel v-internet / mobi m-wap
  35.     if (inet.attachGPRS("m-wap", "mms", "mms")) {
  36.       Serial.println("status=ATTACHED");
  37.       lcd.clear();
  38.       lcd.print("status=ATTACHED");
  39.     }
  40.     else Serial.println("status=ERROR");
  41.     delay(1000);
  42.  
  43.     //Read IP address.
  44.     gsm.SimpleWriteln("AT+CIFSR");
  45.     count = updateServer();
  46.   }
  47.   updateLCD(count);
  48. }
  49.  
  50. void loop() {
  51.   int sensorState = digitalRead(sensor);
  52.   if (sensorState != lastSensorState) {
  53.     if (sensorState == HIGH) {
  54.       Serial.println("Sensor on");
  55.       count++;
  56.       updateLCD(count);
  57.       updateServer();
  58.     } else {
  59.       Serial.println("Sensor off");
  60.     }
  61.   }
  62.   lastSensorState = sensorState;
  63.   delay(50);
  64. }
  65.  
  66. void updateLCD(int count) {
  67.   lcd.clear();
  68.   lcd.print("so nguoi");
  69.   lcd.setCursor(0, 1);
  70.   lcd.print(count);
  71. }
  72.  
  73. int updateServer() {
  74.   char res[250];
  75.   int numdata = 0;
  76.   numdata = inet.httpPOST("mayruatay.ga", 80, "/update", "", res, 255);
  77.   Serial.println("SENT");
  78.   delay(500);
  79.   //Print the results.
  80. //  Serial.print("Respond length: ");
  81. //  Serial.print(numdata);
  82. //  Serial.print(" | Data received: ");
  83. //  Serial.println(res);
  84.   return atoi(res);
  85. }
Add Comment
Please, Sign In to add comment