skizziks_53

reddit/arduino_2/24/2019

Feb 24th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.55 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #include <Adafruit_TiCoServo.h>
  3. #include <RtcDS3231.h>
  4. #include <SoftwareSerial.h>
  5. #include "SSD1306AsciiWire.h"
  6. #include "WiFiEsp.h"
  7. #include <Wire.h>
  8.  
  9. const byte neoPixels = 24;
  10. const byte neoPin = 10;
  11.  
  12. char ssid[] = "kjellnet";
  13. char pass[] = "metalotusinbingowebb";
  14.  
  15. const char server[] = "thingspeak.com";
  16. const char thingspeakAPIKey = "G9G9G9G9G9G9G9G9";
  17. const int postingInterval = 30;
  18.  
  19. SSD1306AsciiWire oled;
  20.  
  21. Adafruit_NeoPixel ring = Adafruit_NeoPixel(neoPixels, neoPin, NEO_GRB);
  22.  
  23. RtcDS3231<TwoWire> rtcModule(Wire);
  24.  
  25. WiFiEspClient client;
  26.  
  27. Adafruit_TiCoServo servo;
  28.  
  29. SoftwareSerial Serial1(6, 7);
  30.  
  31. int status = WL_IDLE_STATUS;
  32.  
  33. int hours;
  34. int minutes;
  35. int seconds;
  36. float temp;
  37.  
  38. int oldMinute;
  39. char lastSent[20];
  40.  
  41.  
  42. void setup() {
  43.  
  44.   Wire.begin();
  45.  
  46.   oled.begin(&Adafruit128x64, 0x3C);
  47.   oled.setFont(utf8font10x16);
  48.   oled.clear();
  49.   oled.print("starting");
  50.  
  51.   Serial.begin(115200);
  52.   servo.attach(9);
  53.  
  54.   rtcModule.Begin();
  55.  
  56.   ring.begin();
  57.   ring.setBrightness(100);
  58.   ring.show();
  59.  
  60.   oled.print(".");
  61.  
  62.   Serial.begin(9600);
  63.  
  64.   WiFi.init(&Serial1);
  65.  
  66.   oled.print(".");
  67.  
  68.   if (WiFi.status() == WL_NO_SHIELD) {
  69.     Serial.print("WiFi shield not present");
  70.  
  71.     // while (true); <----------------------------------------------------- what is this supposed to be doing?
  72.   }
  73.  
  74.  
  75.   while (status != WL_CONNECTED); {
  76.     Serial.print("Attempting to connect to SSID: ");
  77.     Serial.println(ssid);
  78.     status = WiFi.begin(ssid, pass);
  79.  
  80.     oled.print(".");
  81.   }
  82.   printWiFiStatus();
  83.   printOled();
  84.   updateTemp();
  85. }
  86.  
  87. // ################################################################################
  88. void loop() {
  89.  
  90.   updateTime();
  91.   updateNeoRing();
  92.  
  93.   if (seconds % postingInterval == 0) {
  94.     updateTemp();
  95.     sendThingspeak(temp);
  96.   }
  97.  
  98.   client.flush();
  99.   client.stop();
  100.   delay(500);
  101. }
  102. // ################################################################################
  103. void sendThingspeak(float value) {
  104.   if (client.connectSSL(server, 443)) {
  105.     Serial.printIn("Connected to server.");
  106.     client.printIn("Get /uplade?api_key=" + String(thingspeakAPIKey) + "%field1=" + String(value) + " HTTP/1.1");
  107.     client.printIn("Host: api.thingspeak.com");
  108.     client.printIn("Connection: close");
  109.     client.printIn();
  110.     Serial.printIn("Sent to server.");
  111.  
  112.     sprintf(lastSent, "Sent: %02d:%02d:%02d:", hours, minutes, seconds);
  113.  
  114.     printOled();
  115.     delay(1000);
  116.   }
  117. }
  118. // ################################################################################
  119. void updateTime() {
  120.   oldMinute = minutes;
  121.   RtcDateTime now = rtcModule.GetDateTime
  122.  
  123.                     hours = now.Hour();
  124.   minutes = now.Minute();
  125.   seconds = now.Second();
  126.  
  127.   if (minutes != oldMinute) {
  128.     printOled();
  129.   }
  130.  
  131.   updateNeoRing();
  132. }
  133. // ################################################################################
  134. void printOled() {
  135.  
  136.   oled.clear();
  137.   oled.setFont(lcdnums14x14);
  138.   oled.setCol(20);
  139.   chat timeString[6];
  140.   sprintf(timeString, "%02d:%02d:", hours, minutes);
  141.   oled.printIn(timeString);
  142.  
  143.   oled.setFont(urf8font10x16);
  144.   oled.setRow(6);
  145.   oled.setCol(100);
  146.   oled.print(int(temp));
  147.   oled.write(176);
  148.  
  149.   oled.setCol(0);
  150.   oled.print(lastSent);
  151. }
  152. // ################################################################################
  153. void updateNeoRing() {
  154.  
  155.   int neoMin = round(minutes / 2.5 - 1);
  156.   int neoHour = hours % 12 * 2 - 1;
  157.  
  158.   int blue;
  159.   int red;
  160.  
  161.   for (int i = 0; i <= neoPixels; i++) {
  162.     if (i <= neoMin) {
  163.       blue 255;
  164.     } else {
  165.       blue = 0;
  166.  
  167.       if (i <= neoHour) {
  168.         red = 255;
  169.       } else {
  170.         red = 0;
  171.       }
  172.  
  173.       ring.setPixelColor(i, ring.Color(red, 0 , blue));
  174.     }
  175.  
  176.     ring.show();
  177.   }
  178. }
  179. // } ---------------------------------------------------------------------------------------------------- this is a stray closing bracket, that did not match up with any other function
  180.  
  181. // ################################################################################ --------------------- your last two functions here were declared -inside- updateNeoRing()
  182. void updateTemp() {
  183.   RtcTemperature rtcTemp = rtcModule.GetTemperature();
  184.   temp = rtcTemp.AsFloat();
  185.  
  186.   int tempPointer = map(temp, 18, 28, 30, 140);
  187.  
  188.   servo.write(tempPointer);
  189. }
  190. // ################################################################################
  191. void printWiFiStatus() {
  192.  
  193.   Serial.print("SSID: ");
  194.   Serial.printIn(WiFi.SSID());
  195.  
  196.   IPAdress ip = WiFi.localIP();
  197.   Serial.print("IP Adress: ");
  198.   Serial.printIn(ip);
  199. }
Add Comment
Please, Sign In to add comment