Advertisement
M0n5t3r

Growbox

Jan 26th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.96 KB | None | 0 0
  1.  
  2.  
  3. #include <Wire.h>
  4. #include <LCD.h>
  5. #include <LiquidCrystal_I2C.h>>
  6. #include "DHT.h"
  7. #include "RTClib.h"
  8.  
  9.  
  10. #define DHTPIN 5
  11. #define DHTTYPE DHT21  
  12. DHT dht(DHTPIN, DHTTYPE);
  13.  
  14. #define TRIGGER_PIN 13
  15. #define ECHO_PIN 12
  16. #define USONIC_DIV 58.0
  17. #define MEASURE_SAMPLE_DELAY 5
  18. #define MEASURE_SAMPLES 25
  19. #define MEASURE_DELAY 250
  20.  
  21.  
  22. #define trigPin 13
  23. #define echoPin 12
  24.  
  25. LiquidCrystal_I2C  lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified module
  26.  
  27. RTC_DS1307 rtc;
  28.  
  29. String ssid ="Visnet";
  30. String password="vinvisje";
  31.  
  32. String data;
  33. String server = "www.sweetcaffeine.nl"; // www.example.com
  34. String uri = "/growbox/getfile.php";// our example is /Serial2post.php
  35.  
  36. int sensorPin = A0;
  37.  
  38.  
  39. //reset the Wifi module
  40.  
  41. void reset() {
  42.   Serial2.println("AT+RST");
  43.   delay(1000);
  44.   if(Serial2.find("OK") ) Serial.println("Module Reset");
  45.   Serial.println("Reset OK");
  46. }
  47.  
  48. //Try to connect 10 times, than try again :P
  49.  
  50. boolean checkfor(char* value)
  51. {
  52.   int i;
  53.   for(i = 1; i < 11; i++)
  54.   {
  55.     delay(2000);
  56.     if(Serial2.find(value)) {
  57.       Serial.println("Found");
  58.       Serial.println(value);
  59.       return true;
  60.     }
  61.     Serial.println ("try: ") + Serial.println(i);
  62.   }
  63.   return false;
  64. }
  65.  
  66. //Connect to Wifi Network
  67.  
  68. void connectWifi() {
  69.   lcd.clear();
  70.   lcd.setCursor(0,0);
  71.  
  72.   String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"";
  73.   delay(200);
  74.   Serial2.println(cmd);
  75.   Serial.println("ERROR");
  76.   lcd.print("Error");
  77.   if(!checkfor("OK")) {
  78.     Serial.println("Cannot connect to wifi");
  79.       lcd.setCursor(0,1);
  80.     lcd.print("Couldn't connect to wifi");
  81.     connectWifi();
  82.   }
  83.   lcd.clear();
  84.   lcd.setCursor(0,0);
  85.   lcd.print("Connected");
  86.   Serial.println("Connected!");
  87.   Serial.println("printing cmd:" + cmd);
  88.   lcd.setCursor(0,2);
  89.   lcd.print("printing cmd:" + cmd);
  90. }
  91.  
  92. //Send Header+Package to database
  93.  
  94. void httppost() {
  95.   Serial2.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.
  96.   if( Serial2.find("OK")) {
  97.     Serial.println("TCP connection ready");
  98.   }
  99.   delay(1000);
  100.   String postRequest =
  101.  
  102.   "POST " + uri + " HTTP/1.0\r\n" +
  103.   "Host: " + server + "\r\n" +
  104.   "Accept: *" + "/" + "*\r\n" +
  105.   "Content-Length: " + data.length() + "\r\n" +
  106.   "Content-Type: application/x-www-form-urlencoded\r\n" +
  107.   "\r\n" + data;
  108.   String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.
  109.  
  110.   Serial2.print(sendCmd);
  111.   Serial2.println(postRequest.length() );
  112.   delay(500);
  113.  
  114.   if(Serial2.find(">")) { Serial.println("Sending.."); Serial2.print(postRequest);
  115.   if( Serial2.find("SEND OK")) {
  116.     Serial.println("Packet sent");
  117.     Serial.println(data);
  118.     delay(5000000);
  119.       while (Serial2.available()) {
  120.       String tmpRSerial2 = Serial2.readString();
  121.       Serial.println(tmpRSerial2);
  122.       }
  123.    
  124.     Serial2.println("AT+CIPCLOSE");
  125.    
  126.     }
  127.  
  128.   }
  129. }
  130.  
  131. void screenupdate(){
  132.  
  133. }
  134.  
  135. void setup() {
  136.   Serial.begin(115200);
  137.  
  138.  
  139.   lcd.setBacklightPin(3,POSITIVE);
  140.   lcd.setBacklight(HIGH); // NOTE: You can turn the backlight off by setting it to LOW instead of HIGH
  141.   lcd.begin(20, 4);
  142.   lcd.clear();
  143.   pinMode(TRIGGER_PIN, OUTPUT);
  144.   pinMode(ECHO_PIN, INPUT);
  145.  
  146.   // Reset the trigger pin and wait a half a second
  147.   digitalWrite(TRIGGER_PIN, LOW);
  148.   delayMicroseconds(500);
  149.  
  150.   lcd.setCursor(0,0);
  151.   lcd.print("GROWBOX - Vincent Venhuizen");
  152.   Serial.println("GROWBOX_V4");
  153.   lcd.setCursor(0,1);
  154.   lcd.print("Starting reset");
  155.   Serial.println("Starting reset");
  156.   reset();
  157.   lcd.setCursor(0,2);
  158.   lcd.print("Connecting to wifi");
  159.   Serial.println("Connecting Wifi");
  160.   connectWifi();
  161.  
  162.   Serial.begin(115200);
  163.   if (! rtc.begin()) {
  164.     Serial.println("Couldn't find RTC");
  165.     lcd.clear();
  166.     lcd.setCursor(0,0);
  167.     lcd.print("Couldn't find RTC");
  168.     while (1);
  169.   }
  170. }
  171.  
  172. void loop () {
  173.   data = "password=growbox&temperature=TEST&time=TEST&humidity=TEST&groundmoisture=TEST";
  174.   httppost();
  175.   delay(100);
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement