Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <LCD.h>
- #include <LiquidCrystal_I2C.h>>
- #include "DHT.h"
- #include "RTClib.h"
- #define DHTPIN 5
- #define DHTTYPE DHT21
- DHT dht(DHTPIN, DHTTYPE);
- #define TRIGGER_PIN 13
- #define ECHO_PIN 12
- #define USONIC_DIV 58.0
- #define MEASURE_SAMPLE_DELAY 5
- #define MEASURE_SAMPLES 25
- #define MEASURE_DELAY 250
- #define trigPin 13
- #define echoPin 12
- LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified module
- RTC_DS1307 rtc;
- String ssid ="Visnet";
- String password="vinvisje";
- String data;
- String server = "www.sweetcaffeine.nl"; // www.example.com
- String uri = "/growbox/getfile.php";// our example is /Serial2post.php
- int sensorPin = A0;
- //reset the Wifi module
- void reset() {
- Serial2.println("AT+RST");
- delay(1000);
- if(Serial2.find("OK") ) Serial.println("Module Reset");
- Serial.println("Reset OK");
- }
- //Try to connect 10 times, than try again :P
- boolean checkfor(char* value)
- {
- int i;
- for(i = 1; i < 11; i++)
- {
- delay(2000);
- if(Serial2.find(value)) {
- Serial.println("Found");
- Serial.println(value);
- return true;
- }
- Serial.println ("try: ") + Serial.println(i);
- }
- return false;
- }
- //Connect to Wifi Network
- void connectWifi() {
- lcd.clear();
- lcd.setCursor(0,0);
- String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"";
- delay(200);
- Serial2.println(cmd);
- Serial.println("ERROR");
- lcd.print("Error");
- if(!checkfor("OK")) {
- Serial.println("Cannot connect to wifi");
- lcd.setCursor(0,1);
- lcd.print("Couldn't connect to wifi");
- connectWifi();
- }
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Connected");
- Serial.println("Connected!");
- Serial.println("printing cmd:" + cmd);
- lcd.setCursor(0,2);
- lcd.print("printing cmd:" + cmd);
- }
- //Send Header+Package to database
- void httppost() {
- Serial2.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.
- if( Serial2.find("OK")) {
- Serial.println("TCP connection ready");
- }
- delay(1000);
- String postRequest =
- "POST " + uri + " HTTP/1.0\r\n" +
- "Host: " + server + "\r\n" +
- "Accept: *" + "/" + "*\r\n" +
- "Content-Length: " + data.length() + "\r\n" +
- "Content-Type: application/x-www-form-urlencoded\r\n" +
- "\r\n" + data;
- String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.
- Serial2.print(sendCmd);
- Serial2.println(postRequest.length() );
- delay(500);
- if(Serial2.find(">")) { Serial.println("Sending.."); Serial2.print(postRequest);
- if( Serial2.find("SEND OK")) {
- Serial.println("Packet sent");
- Serial.println(data);
- delay(5000000);
- while (Serial2.available()) {
- String tmpRSerial2 = Serial2.readString();
- Serial.println(tmpRSerial2);
- }
- Serial2.println("AT+CIPCLOSE");
- }
- }
- }
- void screenupdate(){
- }
- void setup() {
- Serial.begin(115200);
- lcd.setBacklightPin(3,POSITIVE);
- lcd.setBacklight(HIGH); // NOTE: You can turn the backlight off by setting it to LOW instead of HIGH
- lcd.begin(20, 4);
- lcd.clear();
- pinMode(TRIGGER_PIN, OUTPUT);
- pinMode(ECHO_PIN, INPUT);
- // Reset the trigger pin and wait a half a second
- digitalWrite(TRIGGER_PIN, LOW);
- delayMicroseconds(500);
- lcd.setCursor(0,0);
- lcd.print("GROWBOX - Vincent Venhuizen");
- Serial.println("GROWBOX_V4");
- lcd.setCursor(0,1);
- lcd.print("Starting reset");
- Serial.println("Starting reset");
- reset();
- lcd.setCursor(0,2);
- lcd.print("Connecting to wifi");
- Serial.println("Connecting Wifi");
- connectWifi();
- Serial.begin(115200);
- if (! rtc.begin()) {
- Serial.println("Couldn't find RTC");
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("Couldn't find RTC");
- while (1);
- }
- }
- void loop () {
- data = "password=growbox&temperature=TEST&time=TEST&humidity=TEST&groundmoisture=TEST";
- httppost();
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement