#include #include #include #include #include #define esp8266 Serial2 // use esp8266 to talk to esp8266 #define SSID "Jayam-jio" // put here the name of your wifi network #define PASS "Vishnu@127" // put here the password of your wifi network #define IP "184.106.153.149" // thingspeak.com's IP #define VOLT_MIN 300 #define VOLT_MAX 700 #define OVERLOAD_HIGH 8 #define OVERLOAD_LOW 1 #define RPM_HIGH 9000 #define RPM_OK 8000 #define TEMP_OFFSET_HIGH 20 #define TEMP_OFFSET_LOW 10 String GET = "GET /update?key=MSWAP5DJFQ9JSJ0B&"; // put here your thingspeak key instead of [THINGSPEAK_KEY] String FIELD1 = "field1="; // Motor Temperature String FIELD2 = "field2="; // AmbientTemperature String FIELD3 = "field3="; // R-Phase String FIELD4 = "field4="; // Y-Phase String FIELD5 = "field5="; // B-Phase String FIELD6 = "field6="; // current String FIELD7 = "field7="; // rpm String INIT1 = "Motor Management Sys" ; String INIT2 = "for Hoist Appln. IoT" ; String LINE1 = "RY= YB= BR= " ; String LINE2 = "M.I= A SPED rpm" ; String LINE3 = "A.TMP= c M.TMP= c" ; String LINE4 = "MOTOR STATUS= " ; // We are using uno timer 1 #define TMR1_CNT 1000 #define VT_MUL_FACT 0.034714 //change this value to adjust voltage reading3.48==312V 3.57=343.04=3572.58=3812.45=443,2.55=426 #define CT_MUL_FACT 0.034714 //change this value to adjust current reading #define AVG_COUNT 16 // Data wire is plugged into pin 4 & 7 on the Arduino #define ONE_WIRE_BUS1 22 #define ONE_WIRE_BUS2 25 #define PUSH_FWD 39 #define PUSH_REV 46 #define RELAY_FLT 11 #define RELAY_FWD 51 #define RELAY_REV 47 #define BUZZER 13 LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // Setup a oneWire Dallas temperature ICs OneWire oneWire1(ONE_WIRE_BUS1); OneWire oneWire2(ONE_WIRE_BUS2); // Pass our oneWire reference to Dallas Temperature. DallasTemperature tempSensor1(&oneWire1); DallasTemperature tempSensor2(&oneWire2); int decis(); unsigned int adChannel = 0 ; unsigned int adcVAccPhase1 ; unsigned int adcVAccPhase2 ; unsigned int adcVAccPhase3 ; unsigned int adcAccCurrent ; unsigned int adcVPhase1 ; unsigned int adcVPhase2 ; unsigned int adcVPhase3 ; unsigned int adcCurrent ; unsigned int rPhase ; unsigned int yPhase ; unsigned int bPhase ; float amp ; float vPhase1 ; float vPhase2 ; float vPhase3 ; float current ; bool bADCReady ; String strValue ; bool bStatusFWD = FALSE ; bool bStatusREV = FALSE ; unsigned int nCycleCount ; unsigned int nAvgCount ; float temperature1 ; float temperature2 ; const unsigned int RPM_BUFFER_SIZE = 8 ; unsigned int rpmBuffer[RPM_BUFFER_SIZE]; unsigned int rpmIndex = 0 ; unsigned long rpmAcc = 0 ; volatile int rpmcount = 0; unsigned long rpmAverage = 0; unsigned long lastmillis = 0; unsigned int motorStatus ; unsigned int motorDir ; void loop() { // put your main code here, to run repeatedly: char dispLine[256] ; motorDir = 0 ; if( digitalRead(PUSH_FWD) == HIGH ) { if( motorDir != 1 ) { lcd.setCursor(14, 3); lcd.print("FWD"); digitalWrite( RELAY_REV , LOW ); digitalWrite( RELAY_FWD , HIGH ); motorDir = 1 ; } } else if( digitalRead(PUSH_REV) == HIGH ) { if( motorDir != 2 ) { lcd.setCursor(14, 3); lcd.print("REV"); digitalWrite( RELAY_FWD , LOW ); digitalWrite( RELAY_REV , HIGH ); motorDir = 2 ; } } else { if( motorDir == 0 ) { lcd.setCursor(14, 3); lcd.print("OFF"); digitalWrite( RELAY_FWD , LOW ); digitalWrite( RELAY_REV , LOW ); motorDir = 0 ; } } Serial.print("rpm"); CheckRPM(); if( rpmAverage > RPM_HIGH ) { OnRpmError(); } Serial.print("t1"); // Motor temperatures sensor 1 tempSensor1.requestTemperatures(); temperature1 = tempSensor1.getTempCByIndex(0); //Ambient temperatures sensor 2 Serial.print("t2"); tempSensor2.requestTemperatures(); temperature2 = tempSensor2.getTempCByIndex(0); if( temperature1 > temperature2 ) { if(( temperature1 - temperature2 ) > TEMP_OFFSET_HIGH ) { OnTemperatureError(); } } if( bADCReady == true ) { Serial.print("an"); //vPhase1 = ((float)vPhase1 / 16.0) / VT_MUL_FACT ; //vPhase2 = ((float)vPhase2 / 16.0) / VT_MUL_FACT; //vPhase3 = ((float)vPhase3 / 16.0) / VT_MUL_FACT ; //current = ((float)current / 16.0) / CT_MUL_FACT ; vPhase1 = (float)vPhase1 * (float)VT_MUL_FACT ; vPhase2 = (float)vPhase2 * (float)VT_MUL_FACT; vPhase3 = (float)vPhase3 * (float)VT_MUL_FACT ; current = (float)current * (float)CT_MUL_FACT ; rPhase = (unsigned int)vPhase1; yPhase = (unsigned int)vPhase2 ; bPhase = (unsigned int)vPhase3; if((rPhase < VOLT_MIN)||(rPhase > VOLT_MAX)) { OnVoltageError(1); } if((yPhase < VOLT_MIN)||(yPhase > VOLT_MAX)) { OnVoltageError(2); } if((bPhase < VOLT_MIN)||(bPhase > VOLT_MAX)) { OnVoltageError(3); } amp = current; if( amp > OVERLOAD_HIGH ) { OnOverLoadError(); } strValue = String("R=")+ String((int)rPhase) + String("-") + String("Y=")+ String((int)yPhase) + String("-") + String("B=")+ String((int)bPhase) ; //sprintf(dispLine,"field1=%d&field2=%d&field3=%d&field4=%d&field5=%d&field6=%d&field7=%d&field8=%d" , (unsigned int)temperature1,(unsigned int)temperature2,(unsigned int)rPhase , (unsigned int)yPhase , (unsigned int)bPhase, (unsigned int)amp, (unsigned int)rpmAverage, (unsigned int)motorDir); sprintf(dispLine,"field1=%d&field2=%d&field3=%d&field4=%d&field5=%d&field6=%f3.1&field7=%d&field8=%d" , (unsigned int)temperature1,(unsigned int)temperature2,(unsigned int)rPhase , (unsigned int)yPhase , (unsigned int)bPhase, amp, (unsigned int)rpmAverage, (unsigned int)motorDir); Serial.println(dispLine); lcd.setCursor(3, 0); lcd.print(String((int)rPhase)); lcd.setCursor(10, 0); lcd.print(String((int)yPhase)); lcd.setCursor(17, 0); lcd.print(String((int)bPhase)); lcd.setCursor(4, 1); lcd.print(String((int)amp)); lcd.setCursor(13, 1); lcd.print(String((int)rpmAverage)); lcd.setCursor(6, 2); lcd.print(String((int)temperature1)); lcd.setCursor(17, 2); lcd.print(String((int)temperature2)); InitVariable(); bADCReady = FALSE ; } updateFunction(dispLine); } boolean connectWiFi() { esp8266.println("AT+CWMODE=1"); delay(2000); String cmd="AT+CWJAP=\""; cmd+=SSID; cmd+="\",\""; cmd+=PASS; cmd+="\""; esp8266.println(cmd); delay(2000); if(esp8266.find("OK")) { Serial.println("OK"); return true; } else { Serial.println("KO"); return false; } } void updateFunction(String valuetosend) { char getString[255]; String cmd = "AT+CIPSTART=\"TCP\",\""; cmd += IP; cmd += "\",80"; esp8266.println(cmd); delay(200); if(esp8266.find("Error")) { Serial.print("Error1"); return; } cmd = GET + valuetosend + "\r\n"; ; Serial.print(1); esp8266.print("AT+CIPSEND="); esp8266.println(cmd.length()); if(esp8266.find(">")) { esp8266.print(cmd); } else { esp8266.println("AT+CIPCLOSE"); } Serial.print(2); } void setup() { // put your setup code here, to run once: cli(); //disable interrupts for( int x = 0 ; x < RPM_BUFFER_SIZE; ++x ) { rpmBuffer[x] = 0 ; } rpmAcc = 0 ; rpmIndex = 0 ; lcd.begin(20, 4); lcd.print(INIT1); lcd.print(INIT2); InitVariable(); sei(); //enable interrupts Serial.begin(9600); Serial.println("1"); esp8266.begin(115200); esp8266.println("AT"); delay(5000); if(esp8266.find("OK")) { connectWiFi(); } Serial.println("2"); //adc adChannel = 0 ; ADMUX = 0xC0 ; ADCSRA = 0x8E ; //analogReference(INTERNAL1V1); analogReference(DEFAULT); Timer1.initialize(157); Timer1.attachInterrupt(Timer1Callback); Timer1.disablePwm(3); Timer1.disablePwm(5); Timer1.disablePwm(6); Timer1.disablePwm(9); Timer1.disablePwm(10); Timer1.disablePwm(11); pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output lastmillis = millis(); // Uptade lasmillis attachInterrupt(0, RPM_Intr , FALLING); sei(); //enable interrupts pinMode(PUSH_FWD, INPUT); pinMode(PUSH_REV, INPUT); pinMode(RELAY_FWD, OUTPUT); pinMode(RELAY_REV, OUTPUT); pinMode(RELAY_FLT, OUTPUT); pinMode(BUZZER, OUTPUT); tempSensor1.begin(); tempSensor2.begin(); lcd.setCursor(0, 0); lcd.print(LINE1); lcd.setCursor(0, 1); lcd.print(LINE2); lcd.setCursor(0, 2); lcd.print(LINE3); lcd.setCursor(0, 3); lcd.print(LINE4); } void Timer1Callback(void) { if( bADCReady == FALSE ) { TCNT1 = TMR1_CNT ; // preload timer StartADC(); } } void StartADC(void) { if( bADCReady == TRUE ) return ; switch(adChannel) { case 0 : __asm__("nop"); ADCSRA = 0x8E ; __asm__("nop"); ADCSRA |= 1<= 32 ) { nCycleCount = 0 ; adcVAccPhase1 = adcVAccPhase1 + adcVPhase1 ; adcVAccPhase2 = adcVAccPhase2 + adcVPhase2 ; adcVAccPhase3 = adcVAccPhase3 + adcVPhase3 ; adcAccCurrent = adcAccCurrent + adcCurrent ; nAvgCount = nAvgCount + 1 ; if( nAvgCount > AVG_COUNT ) { vPhase1 = adcVAccPhase1 ; vPhase2 = adcVAccPhase2 ; vPhase3 = adcVAccPhase3 ; vPhase1 = adcVAccPhase1 ; current = adcAccCurrent ; bADCReady = true ; adcVAccPhase1 = 0 ; adcVAccPhase2 = 0 ; adcVAccPhase3 = 0 ; adcAccCurrent = 0 ; } } adChannel = 0 ; break ; } } } void InitVariable(void) { bADCReady = false ; adChannel = 0 ; adcVAccPhase1 = 0 ; adcVAccPhase2 = 0 ; adcVAccPhase3 = 0 ; adcAccCurrent = 0 ; adcVPhase1 = 0 ; adcVPhase2 = 0 ; adcVPhase3 = 0 ; adcCurrent = 0 ; bADCReady = 0 ; nCycleCount = 0 ; nAvgCount = 0 ; } void CheckRPM() { unsigned int ctRpm ; unsigned long temp ; char getString[255]; if (millis() - lastmillis >= 1000) /*Uptade every one second, this will be equal to reading frecuency (Hz).*/ { detachInterrupt(0); //Disable interrupt when calculating ctRpm = rpmcount ; rpmcount = 0; // Restart the RPM counter lastmillis = millis(); // Uptade lasmilrpm_fanlis attachInterrupt(0, RPM_Intr, FALLING); //enable interrupt temp = ctRpm * 60 ; rpmAcc = rpmAcc - rpmBuffer[rpmIndex] ; rpmAcc = rpmAcc + temp ; rpmBuffer[rpmIndex] = temp ; rpmAverage = rpmAcc / RPM_BUFFER_SIZE ; rpmIndex++; if(rpmIndex >= RPM_BUFFER_SIZE) { rpmIndex=0; } //sprintf( getString , "%d - %d - %d - %d - %d - %d - %d - %d" , rpmBuffer[0] , rpmBuffer[1] , rpmBuffer[2] , rpmBuffer[3] , rpmBuffer[4] , rpmBuffer[5] , rpmBuffer[6] , rpmBuffer[7] ); //Serial.println(getString); //Serial.print(" RPM = "); //Serial.println(rpmAverage); } } void RPM_Intr() { /* this code will be executed every time the interrupt 0 (pin2) gets low.*/ rpmcount++; } void OnVoltageError(int phase) { digitalWrite( RELAY_FWD , LOW ); digitalWrite( RELAY_REV , LOW ); motorDir = 0 ; digitalWrite(RELAY_FLT , HIGH ); digitalWrite(BUZZER , HIGH ); lcd.clear(); if( phase == 1 ) { lcd.setCursor(0,0); lcd.print("Error in R Phase"); } else if( phase == 2 ) { lcd.setCursor(0,0); lcd.print("Error in Y Phase"); } else if( phase == 3 ) { lcd.setCursor(0,0); lcd.print("Error in B Phase"); } while(1) { if( bADCReady == true ) { vPhase1 = (float)vPhase1 * (float)VT_MUL_FACT ; vPhase2 = (float)vPhase2 * (float)VT_MUL_FACT; vPhase3 = (float)vPhase3 * (float)VT_MUL_FACT ; rPhase = (unsigned int)vPhase1; yPhase = (unsigned int)vPhase2 ; bPhase = (unsigned int)vPhase3; if(((rPhase >= VOLT_MIN)&&(rPhase <= VOLT_MAX))&&((yPhase >= VOLT_MIN)&&(yPhase <= VOLT_MAX))&&((bPhase >= VOLT_MIN)&&(bPhase <= VOLT_MAX))) { lcd.clear(); lcd.setCursor(0, 0); lcd.print(LINE1); lcd.setCursor(0, 1); lcd.print(LINE2); lcd.setCursor(0, 2); lcd.print(LINE3); lcd.setCursor(0, 3); lcd.print(LINE4); digitalWrite(RELAY_FLT , LOW ); digitalWrite(BUZZER , LOW ); break ; } } } } void OnOverLoadError(void) { digitalWrite( RELAY_FWD , LOW ); digitalWrite( RELAY_REV , LOW ); motorDir = 0 ; digitalWrite(RELAY_FLT , HIGH ); digitalWrite(BUZZER , HIGH ); lcd.clear(); lcd.setCursor(0,0); lcd.print("overload Error"); while(1) { if( bADCReady == true ) { amp = (unsigned int)current; if(amp <= OVERLOAD_LOW ) { lcd.clear(); lcd.setCursor(0, 0); lcd.print(LINE1); lcd.setCursor(0, 1); lcd.print(LINE2); lcd.setCursor(0, 2); lcd.print(LINE3); lcd.setCursor(0, 3); lcd.print(LINE4); digitalWrite(RELAY_FLT , LOW ); digitalWrite(BUZZER , LOW ); break ; } } } } void OnRpmError(void) { digitalWrite( RELAY_FWD , LOW ); digitalWrite( RELAY_REV , LOW ); motorDir = 0 ; digitalWrite(RELAY_FLT , HIGH ); digitalWrite(BUZZER , HIGH ); lcd.clear(); lcd.setCursor(0,0); lcd.print("Error High RPM"); while(1) { CheckRPM(); if( rpmAverage <= RPM_OK ) { lcd.clear(); lcd.setCursor(0, 0); lcd.print(LINE1); lcd.setCursor(0, 1); lcd.print(LINE2); lcd.setCursor(0, 2); lcd.print(LINE3); lcd.setCursor(0, 3); lcd.print(LINE4); digitalWrite(RELAY_FLT , LOW ); digitalWrite(BUZZER , LOW ); break ; } } } void OnTemperatureError(void) { digitalWrite( RELAY_FWD , LOW ); digitalWrite( RELAY_REV , LOW ); motorDir = 0 ; digitalWrite(RELAY_FLT , HIGH ); digitalWrite(BUZZER , HIGH ); lcd.clear(); lcd.setCursor(0,0); lcd.print("Error High RPM"); while(1) { // Motor temperatures sensor 1 tempSensor1.requestTemperatures(); temperature1 = tempSensor1.getTempCByIndex(0); //Ambient temperatures sensor 2 tempSensor2.requestTemperatures(); temperature2 = tempSensor2.getTempCByIndex(0); if( temperature1 > temperature2 ) { if(( temperature1 - temperature2 ) < TEMP_OFFSET_LOW ) { lcd.clear(); lcd.setCursor(0, 0); lcd.print(LINE1); lcd.setCursor(0, 1); lcd.print(LINE2); lcd.setCursor(0, 2); lcd.print(LINE3); lcd.setCursor(0, 3); lcd.print(LINE4); digitalWrite(RELAY_FLT , LOW ); digitalWrite(BUZZER , LOW ); break ; } } } }