Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.71 KB | None | 0 0
  1. #define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
  2. #include <ESP8266WiFi.h>
  3. #include <ESP8266mDNS.h>
  4. #include <WiFiUdp.h>
  5. #include <ArduinoOTA.h>
  6. #include <SPI.h>
  7. #include <BlynkSimpleEsp8266.h>
  8. #include <Wire.h>
  9. #include <Adafruit_ADS1015.h>
  10. // Blynk auth code
  11. char auth[] = "INSERT AUTH CODE HERE";
  12. // Instantiating ADC ADS1115
  13. Adafruit_ADS1115 ads;  /* Use this for the 16-bit version */
  14. // Configure WiFi information
  15. const char* ssid = "SSID";
  16. const char* password = "PASSWORD";
  17. unsigned int localPort = 12345;      // local port to listen for UDP packets
  18. int status = WL_IDLE_STATUS;
  19. byte packetBuffer[512]; //buffer to hold incoming and outgoing packets
  20. WiFiUDP Udp;
  21. // Start telnet server for debugging
  22. const uint16_t aport = 23; // standard port
  23. WiFiServer TelnetServer(aport);
  24. WiFiClient TelnetClient;
  25. // OTA Parameters
  26. const char * OTAhostname = "Plants";
  27. const char * OTApassword = "ledstrip";
  28. int output1 = 0;
  29. int output2 = 0;
  30. float outputA = 0;
  31. int altPin1 = 12;
  32. int altPin2 = 13;
  33. int baseAdc = 5000.0; // Approximate value adc sits at with nothing connected
  34. int peakAdc = 7300.0; // Approximate peak value for your sensor
  35. void blynkVirtualWrite(){
  36.     Blynk.virtualWrite(V5, output1);
  37.     delay(100);
  38.     Blynk.virtualWrite(V6, output2);  
  39.     delay(100);
  40.     Blynk.virtualWrite(V7, (int)outputA);
  41. }
  42. void capture(){
  43.  
  44.   digitalWrite(altPin1, LOW);
  45.   digitalWrite(altPin2, HIGH);
  46.   delay(250);
  47.   output1 = ads.readADC_SingleEnded(0);
  48.   delay(250);
  49.   digitalWrite(altPin2, LOW);
  50.   digitalWrite(altPin1, HIGH);
  51.  
  52.   delay(250);
  53.   output2 = ads.readADC_SingleEnded(0);
  54.   delay(250);
  55.   outputA = (output1+output2)/2.0;
  56. }
  57. void setup() {
  58.   // UDP Code
  59.   Udp.begin(80);
  60.   Blynk.begin(auth, "SSID", "PASSWORD");
  61.   // Telnet Code
  62.   TelnetServer.begin();
  63.   TelnetServer.setNoDelay(true);
  64.   // Begin serial
  65.   Serial.begin(9600);
  66.   Serial.println("Booting");
  67.   // Arduino OTA Code
  68.   // Port defaults to 8266
  69.   ArduinoOTA.setPort(8266);
  70.   // Set hostname, upload password
  71.   ArduinoOTA.setHostname(OTAhostname);
  72.   ArduinoOTA.setPassword(OTApassword);
  73.   ArduinoOTA.onStart([]() {
  74.     Serial.println("Start");
  75.   });
  76.   ArduinoOTA.onEnd([]() {
  77.     Serial.println("\nEnd");
  78.   });
  79.   ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  80.     Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  81.   });
  82.   ArduinoOTA.onError([](ota_error_t error) {
  83.     Serial.printf("Error[%u]: ", error);
  84.     if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  85.     else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  86.     else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  87.     else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  88.     else if (error == OTA_END_ERROR) Serial.println("End Failed");
  89.   });
  90.   ArduinoOTA.begin();
  91.  
  92.   Serial.println("Ready");
  93.   Serial.print("IP address: ");
  94.   Serial.println(WiFi.localIP());
  95.   // ADS1115 Code
  96.     Serial.println("Getting single-ended readings from AIN0..3");
  97.   Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");
  98.  
  99.   // The ADC input range (or gain) can be changed via the following
  100.   // functions, but be careful never to exceed VDD +0.3V max, or to
  101.   // exceed the upper and lower limits if you adjust the input range!
  102.   // Setting these values incorrectly may destroy your ADC!
  103.   //                                                                ADS1015  ADS1115
  104.   //                                                                -------  -------
  105.   // ads.setGain(GAIN_TWOTHIRDS);  // 2/3x gain +/- 6.144V  1 bit = 3mV      0.1875mV (default)
  106.    ads.setGain(GAIN_ONE);        // 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV
  107.   // ads.setGain(GAIN_TWO);        // 2x gain   +/- 2.048V  1 bit = 1mV      0.0625mV
  108.   // ads.setGain(GAIN_FOUR);       // 4x gain   +/- 1.024V  1 bit = 0.5mV    0.03125mV
  109.   // ads.setGain(GAIN_EIGHT);      // 8x gain   +/- 0.512V  1 bit = 0.25mV   0.015625mV
  110.   // ads.setGain(GAIN_SIXTEEN);    // 16x gain  +/- 0.256V  1 bit = 0.125mV  0.0078125mV
  111.  
  112.   ads.begin();
  113.  
  114.   // Pins to alternate current
  115.   pinMode(altPin1, OUTPUT);
  116.   pinMode(altPin2, OUTPUT);
  117. }
  118. void loop()
  119. {
  120.   // Telnet Code
  121.   if (!TelnetClient) {  // otherwise it works only once
  122.     TelnetClient = TelnetServer.available();
  123.     if (TelnetClient.connected()) {
  124.       TelnetClient.println("Telnet is active");
  125.     }
  126.   }
  127.   // Blynk Code
  128.   Blynk.run();
  129.   // OTA Code
  130.   ArduinoOTA.handle();
  131.   capture();
  132.   Serial.print("Output1: ");
  133.   Serial.println(output1);
  134.   Serial.print("Output2: ");
  135.   Serial.println(output2);
  136.   Serial.print("Output Avg: ");
  137.   Serial.println(outputA);
  138.   blynkVirtualWrite();
  139. delay(2000);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement