Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define BLYNK_PRINT Serial // Comment this out to disable prints and save space
- #include <ESP8266WiFi.h>
- #include <ESP8266mDNS.h>
- #include <WiFiUdp.h>
- #include <ArduinoOTA.h>
- #include <SPI.h>
- #include <BlynkSimpleEsp8266.h>
- #include <Wire.h>
- #include <Adafruit_ADS1015.h>
- // Blynk auth code
- char auth[] = "INSERT AUTH CODE HERE";
- // Instantiating ADC ADS1115
- Adafruit_ADS1115 ads; /* Use this for the 16-bit version */
- // Configure WiFi information
- const char* ssid = "SSID";
- const char* password = "PASSWORD";
- unsigned int localPort = 12345; // local port to listen for UDP packets
- int status = WL_IDLE_STATUS;
- byte packetBuffer[512]; //buffer to hold incoming and outgoing packets
- WiFiUDP Udp;
- // Start telnet server for debugging
- const uint16_t aport = 23; // standard port
- WiFiServer TelnetServer(aport);
- WiFiClient TelnetClient;
- // OTA Parameters
- const char * OTAhostname = "Plants";
- const char * OTApassword = "ledstrip";
- int output1 = 0;
- int output2 = 0;
- float outputA = 0;
- int altPin1 = 12;
- int altPin2 = 13;
- int baseAdc = 5000.0; // Approximate value adc sits at with nothing connected
- int peakAdc = 7300.0; // Approximate peak value for your sensor
- void blynkVirtualWrite(){
- Blynk.virtualWrite(V5, output1);
- delay(100);
- Blynk.virtualWrite(V6, output2);
- delay(100);
- Blynk.virtualWrite(V7, (int)outputA);
- }
- void capture(){
- digitalWrite(altPin1, LOW);
- digitalWrite(altPin2, HIGH);
- delay(250);
- output1 = ads.readADC_SingleEnded(0);
- delay(250);
- digitalWrite(altPin2, LOW);
- digitalWrite(altPin1, HIGH);
- delay(250);
- output2 = ads.readADC_SingleEnded(0);
- delay(250);
- outputA = (output1+output2)/2.0;
- }
- void setup() {
- // UDP Code
- Udp.begin(80);
- Blynk.begin(auth, "SSID", "PASSWORD");
- // Telnet Code
- TelnetServer.begin();
- TelnetServer.setNoDelay(true);
- // Begin serial
- Serial.begin(9600);
- Serial.println("Booting");
- // Arduino OTA Code
- // Port defaults to 8266
- ArduinoOTA.setPort(8266);
- // Set hostname, upload password
- ArduinoOTA.setHostname(OTAhostname);
- ArduinoOTA.setPassword(OTApassword);
- ArduinoOTA.onStart([]() {
- Serial.println("Start");
- });
- ArduinoOTA.onEnd([]() {
- Serial.println("\nEnd");
- });
- ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
- Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
- });
- ArduinoOTA.onError([](ota_error_t error) {
- Serial.printf("Error[%u]: ", error);
- if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
- else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
- else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
- else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
- else if (error == OTA_END_ERROR) Serial.println("End Failed");
- });
- ArduinoOTA.begin();
- Serial.println("Ready");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- // ADS1115 Code
- Serial.println("Getting single-ended readings from AIN0..3");
- Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");
- // The ADC input range (or gain) can be changed via the following
- // functions, but be careful never to exceed VDD +0.3V max, or to
- // exceed the upper and lower limits if you adjust the input range!
- // Setting these values incorrectly may destroy your ADC!
- // ADS1015 ADS1115
- // ------- -------
- // ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit = 3mV 0.1875mV (default)
- ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
- // ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 1mV 0.0625mV
- // ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.5mV 0.03125mV
- // ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit = 0.25mV 0.015625mV
- // ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit = 0.125mV 0.0078125mV
- ads.begin();
- // Pins to alternate current
- pinMode(altPin1, OUTPUT);
- pinMode(altPin2, OUTPUT);
- }
- void loop()
- {
- // Telnet Code
- if (!TelnetClient) { // otherwise it works only once
- TelnetClient = TelnetServer.available();
- if (TelnetClient.connected()) {
- TelnetClient.println("Telnet is active");
- }
- }
- // Blynk Code
- Blynk.run();
- // OTA Code
- ArduinoOTA.handle();
- capture();
- Serial.print("Output1: ");
- Serial.println(output1);
- Serial.print("Output2: ");
- Serial.println(output2);
- Serial.print("Output Avg: ");
- Serial.println(outputA);
- blynkVirtualWrite();
- delay(2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement