Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include "RTClib.h"
- #include <ESP8266WiFi.h>
- #include <ESP8266WebServer.h>
- #include <TimedAction.h>
- #include <Button.h>
- #include <SPI.h>
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #define OLED_RESET 0
- Adafruit_SSD1306 display(OLED_RESET);
- int inPin = D8;
- String userName = "KRZYSIEK";
- String sensorName = "ARDUINO Z KUCHNI";
- const char* serverAddress = "192.168.1.103";
- const int httpPort = 8080;
- Button button = Button(inPin, BUTTON_PULLUP);
- #define NUMFLAKES 10
- #define XPOS 0
- #define YPOS 1
- #define DELTAY 2
- char timeStamp [25] = "";
- #define LOGO16_GLCD_HEIGHT 16
- #define LOGO16_GLCD_WIDTH 16
- static const unsigned char PROGMEM logo16_glcd_bmp[] =
- { B00000000, B11000000,
- B00000001, B11000000,
- B00000001, B11000000,
- B00000011, B11100000,
- B11110011, B11100000,
- B11111110, B11111000,
- B01111110, B11111111,
- B00110011, B10011111,
- B00011111, B11111100,
- B00001101, B01110000,
- B00011011, B10100000,
- B00111111, B11100000,
- B00111111, B11110000,
- B01111100, B11110000,
- B01110000, B01110000,
- B00000000, B00110000 };
- #if (SSD1306_LCDHEIGHT != 32)
- #error("Height incorrect, please fix Adafruit_SSD1306.h!");
- #endif
- String lastmeasure = "";
- ESP8266WebServer server(80);
- RTC_DS3231 rtc;
- const char* ssid = "cokolwiek";
- const char* password = "qwe123zxcasd";
- char daysOfTheWeek[7][12] = {"Niedziela", "Ponie", "Wtorek", "Środa", "Czwartek", "Piątek", "Sobota"};
- const char* handlebody = "null";
- DateTime now;
- void checkTime() {
- now = rtc.now();
- Serial.print(now.year(), DEC);
- Serial.print('/');
- Serial.print(now.month(), DEC);
- Serial.print('/');
- Serial.print(now.day(), DEC);
- Serial.print(" (");
- Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
- Serial.print(") ");
- Serial.print(now.hour() + 2, DEC);
- Serial.print(':');
- Serial.print(now.minute(), DEC);
- Serial.print(':');
- Serial.print(now.second(), DEC);
- Serial.println();
- sprintf(timeStamp, "%02d:%02d:%02d", now.hour()+2, now.minute(), now.second());
- }
- void handleServer() {
- server.handleClient();
- }
- void printLastMeasure() {
- now = rtc.now();
- display.display();
- display.setTextSize(2.5);
- display.setTextColor(WHITE);
- display.setCursor(0,0);
- display.clearDisplay();
- display.println(timeStamp);
- Serial.println(timeStamp);
- }
- TimedAction serverAction = TimedAction(1000, handleServer);
- TimedAction checkTimeAction = TimedAction(10000, checkTime);
- TimedAction lcdDisplayAction = TimedAction(10000, printLastMeasure);
- void setup () {
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
- Serial.begin(9600);
- WiFi.begin(ssid, password); //Connect to the WiFi network
- while (WiFi.status() != WL_CONNECTED) { //Wait for connection
- delay(500);
- Serial.println("Waiting to connect...");
- }
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP()); //Print the local IP
- server.on("/time", handleBody); //Associate the handler function to the path
- server.begin(); //Start the server
- Serial.println("Server listening");
- #ifndef ESP8266
- while (!Serial); // for Leonardo/Micro/Zero
- #endif
- delay(3000); // wait for console opening
- if (! rtc.begin()) {
- Serial.println("Couldn't find RTC");
- while (1);
- }
- if (rtc.lostPower()) {
- Serial.println("RTC lost power, lets set the time!");
- // following line sets the RTC to the date & time this sketch was compiled
- rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- // This line sets the RTC with an explicit date & time, for example to set
- // January 21, 2014 at 3am you would call:
- rtc.adjust(DateTime(2000, 9, 11, 10, 00, 00));
- }
- }
- void loop () {
- serverAction.check();
- checkTimeAction.check();
- lcdDisplayAction.check();
- if (button.uniquePress()) {
- sendRequest();
- }
- }
- void handleBody() { //Handler for the body path
- if (server.hasArg("timestamp")== false){ //Check if body received
- server.send(200, "text/plain", "nie doszło mordo");
- return;
- }
- int timestamp = server.arg("timestamp").toInt();
- rtc.adjust(timestamp);
- server.send(200, "text/plain", "doszlo mordo");
- Serial.print("CHANGED TIME TO: ");
- Serial.println(timestamp);
- }
- void sendRequest() {
- WiFiClient client;
- if (!client.connect(serverAddress, httpPort)) {
- Serial.println("connection failed");
- return;
- }
- String data = "{ \"userName\": \"Leszek\", \"sensorName\": \"arduino kitchen\", \"timeStamp\": \"" + (String) now.unixtime() + "\" }";
- char c[140];
- data.toCharArray(c, 140);
- // Serial.println((String )now.unixtime());
- // Serial.println(c);
- Serial.print("Requesting POST: ");
- // Send request to the server:
- client.println("POST /lunchtime HTTP/1.1");
- client.println("Host: 192.168.1.103:8080");
- client.println("Content-Type: application/json");
- client.print("Content-Length: ");
- client.println(strlen(c));// number of bytes in the payload
- client.println();
- client.println(c);
- delay(500); // Can be changed
- Serial.println("closing connection");
- delay(5000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement