Advertisement
Guest User

Untitled

a guest
Jul 5th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.23 KB | None | 0 0
  1. #include <Wire.h>
  2. #include "RTClib.h"
  3. #include <ESP8266WiFi.h>
  4. #include <ESP8266WebServer.h>
  5. #include <TimedAction.h>
  6. #include <Button.h>
  7. #include <SPI.h>
  8. #include <Wire.h>
  9. #include <Adafruit_GFX.h>
  10. #include <Adafruit_SSD1306.h>
  11.  
  12. #define OLED_RESET 0
  13. Adafruit_SSD1306 display(OLED_RESET);
  14. int inPin = D8;
  15.  
  16. String userName = "KRZYSIEK";
  17. String sensorName = "ARDUINO Z KUCHNI";
  18. const char* serverAddress = "192.168.1.103";
  19. const int httpPort = 8080;
  20.  
  21.  
  22.  
  23.  
  24. Button button = Button(inPin, BUTTON_PULLUP);
  25.  
  26. #define NUMFLAKES 10
  27. #define XPOS 0
  28. #define YPOS 1
  29. #define DELTAY 2
  30.  
  31. char timeStamp [25] = "";
  32.  
  33. #define LOGO16_GLCD_HEIGHT 16
  34. #define LOGO16_GLCD_WIDTH  16
  35. static const unsigned char PROGMEM logo16_glcd_bmp[] =
  36. { B00000000, B11000000,
  37.   B00000001, B11000000,
  38.   B00000001, B11000000,
  39.   B00000011, B11100000,
  40.   B11110011, B11100000,
  41.   B11111110, B11111000,
  42.   B01111110, B11111111,
  43.   B00110011, B10011111,
  44.   B00011111, B11111100,
  45.   B00001101, B01110000,
  46.   B00011011, B10100000,
  47.   B00111111, B11100000,
  48.   B00111111, B11110000,
  49.   B01111100, B11110000,
  50.   B01110000, B01110000,
  51.   B00000000, B00110000 };
  52.  
  53. #if (SSD1306_LCDHEIGHT != 32)
  54. #error("Height incorrect, please fix Adafruit_SSD1306.h!");
  55. #endif
  56.  
  57. String lastmeasure = "";
  58.  
  59. ESP8266WebServer server(80);
  60. RTC_DS3231 rtc;
  61. const char* ssid = "cokolwiek";
  62. const char* password =  "qwe123zxcasd";
  63.  
  64. char daysOfTheWeek[7][12] = {"Niedziela", "Ponie", "Wtorek", "Środa", "Czwartek", "Piątek", "Sobota"};
  65.  
  66. const char*  handlebody = "null";
  67.  
  68. DateTime now;
  69.  
  70. void checkTime() {
  71.   now = rtc.now();
  72.  
  73.   Serial.print(now.year(), DEC);
  74.   Serial.print('/');
  75.   Serial.print(now.month(), DEC);
  76.   Serial.print('/');
  77.   Serial.print(now.day(), DEC);
  78.   Serial.print(" (");
  79.   Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  80.   Serial.print(") ");
  81.   Serial.print(now.hour() + 2, DEC);
  82.   Serial.print(':');
  83.   Serial.print(now.minute(), DEC);
  84.   Serial.print(':');
  85.   Serial.print(now.second(), DEC);
  86.   Serial.println();
  87.  
  88.  
  89.   sprintf(timeStamp, "%02d:%02d:%02d", now.hour()+2, now.minute(), now.second());
  90.  
  91. }
  92.  
  93. void handleServer() {
  94.   server.handleClient();
  95. }
  96.  
  97. void printLastMeasure() {
  98. now = rtc.now();
  99.   display.display();
  100.   display.setTextSize(2.5);
  101.   display.setTextColor(WHITE);
  102.   display.setCursor(0,0);
  103.   display.clearDisplay();
  104.   display.println(timeStamp);
  105.   Serial.println(timeStamp);
  106. }
  107.  
  108.  
  109. TimedAction serverAction = TimedAction(1000, handleServer);
  110. TimedAction checkTimeAction = TimedAction(10000, checkTime);
  111. TimedAction lcdDisplayAction = TimedAction(10000, printLastMeasure);
  112.  
  113. void setup () {
  114.  
  115.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)
  116.  
  117.  
  118.   Serial.begin(9600);
  119.  
  120.     WiFi.begin(ssid, password);  //Connect to the WiFi network
  121.     while (WiFi.status() != WL_CONNECTED) {  //Wait for connection
  122.  
  123.         delay(500);
  124.         Serial.println("Waiting to connect...");
  125.  
  126.     }
  127.     Serial.print("IP address: ");
  128.     Serial.println(WiFi.localIP());  //Print the local IP
  129.  
  130.     server.on("/time", handleBody); //Associate the handler function to the path
  131.  
  132.     server.begin(); //Start the server
  133.     Serial.println("Server listening");
  134.  
  135. #ifndef ESP8266
  136.   while (!Serial); // for Leonardo/Micro/Zero
  137. #endif
  138.  
  139.  
  140.  
  141.   delay(3000); // wait for console opening
  142.  
  143.   if (! rtc.begin()) {
  144.     Serial.println("Couldn't find RTC");
  145.     while (1);
  146.   }
  147.  
  148.   if (rtc.lostPower()) {
  149.     Serial.println("RTC lost power, lets set the time!");
  150.     // following line sets the RTC to the date & time this sketch was compiled
  151.     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  152.     // This line sets the RTC with an explicit date & time, for example to set
  153.     // January 21, 2014 at 3am you would call:
  154.      rtc.adjust(DateTime(2000, 9, 11, 10, 00, 00));
  155.  
  156. }
  157.  
  158. }
  159. void loop () {
  160.   serverAction.check();
  161.   checkTimeAction.check();
  162.   lcdDisplayAction.check();
  163.  
  164.  
  165.   if (button.uniquePress()) {
  166.  
  167.  
  168.   sendRequest();
  169.   }
  170.  
  171.  
  172.  
  173. }
  174. void handleBody() { //Handler for the body path
  175.  
  176.       if (server.hasArg("timestamp")== false){ //Check if body received
  177.  
  178.             server.send(200, "text/plain", "nie doszło mordo");
  179.             return;
  180.  
  181.       }
  182.  
  183.       int timestamp = server.arg("timestamp").toInt();
  184.  
  185. rtc.adjust(timestamp);
  186.       server.send(200, "text/plain", "doszlo mordo");
  187.       Serial.print("CHANGED TIME TO: ");
  188.       Serial.println(timestamp);
  189. }
  190.  
  191. void sendRequest() {
  192.   WiFiClient client;
  193.  
  194.   if (!client.connect(serverAddress, httpPort)) {
  195.     Serial.println("connection failed");
  196.     return;
  197.   }
  198.   String data = "{ \"userName\": \"Leszek\", \"sensorName\": \"arduino kitchen\", \"timeStamp\": \"" + (String) now.unixtime() + "\" }";
  199.  
  200.   char c[140];
  201.  
  202.   data.toCharArray(c, 140);
  203.  
  204.   // Serial.println((String )now.unixtime());
  205.   // Serial.println(c);
  206.  
  207.   Serial.print("Requesting POST: ");
  208.   // Send request to the server:
  209.   client.println("POST /lunchtime HTTP/1.1");
  210.   client.println("Host: 192.168.1.103:8080");
  211.   client.println("Content-Type: application/json");
  212.   client.print("Content-Length: ");
  213.   client.println(strlen(c));// number of bytes in the payload
  214.   client.println();
  215.   client.println(c);
  216.  
  217.   delay(500); // Can be changed
  218.  Serial.println("closing connection");
  219.  delay(5000);
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement