Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

#include <OneWire.h> //http://playground.arduino.

cc/Learning/OneWire
#include <DallasTemperature.h>
#include <looper.h> //https://github.com/leomil72/looper

// Data wire is plugged into port 2 on the Arduino


#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9

looper myScheduler;
OneWire oneWire(ONE_WIRE_BUS); //PIN data 1 wire
DallasTemperature sensors(&oneWire);

int cnt = 0;
int x = 1;
int viteza = 0;
int a0, a1, a2;
float temperatura[3];
DeviceAddress termo[3];

void setup()
{
Serial.begin(9600);
Serial.println("SCADA vs IoT");

// Start up the library


sensors.begin();
// locate devices on the bus
Serial.print("Localizam senzorii...");
Serial.print("Am gasit ");
Serial.print(sensors.getDeviceCount(), DEC);
Serial.println(" senzori.");
// identificam senzorii
if (!sensors.getAddress(termo[0], 0)) Serial.println("Nu am gasit adresa
senzorului 0");
if (!sensors.getAddress(termo[1], 1)) Serial.println("Nu am gasit adresa
senzorului 1");
if (!sensors.getAddress(termo[2], 2)) Serial.println("Nu am gasit adresa
senzorului 2");

// set the resolution to 9 bit per device


for(int i = 0; i < 3; i++){
sensors.setResolution(termo[i], TEMPERATURE_PRECISION);
}
for(int i = 0; i < 3; i++){
Serial.print("Senzorul ");
Serial.print(i);
Serial.print("are rezolutia: ");
Serial.print(sensors.getResolution(termo[i]), DEC);
Serial.println();
}
myScheduler.addJob(readTemp,1000);
myScheduler.addJob(readSwitch,500);
myScheduler.addJob(sendUbidots,20000);
pinMode(3, OUTPUT);
pinMode(5, INPUT);

}
void loop()
{
myScheduler.scheduler();

void readSwitch(){
// Mb.R[0]=digitalRead(5);

if(!digitalRead(5))
viteza = 0;
}
void readTemp(void) {

// call sensors.requestTemperatures() to issue a global temperature


// request to all devices on the bus
// Serial.print("Citim temperaturile...");
sensors.requestTemperatures();
// Serial.println("Realizat");

// print the device information


// for(int i = 0; i < 3; i++){
// printData(termo[i]);
//}

a0 = analogRead(A0);
a1 = analogRead(A1);
a2 = analogRead(A2);
//Serial.print("Tensiuni: ");
//Serial.print(a0);
//Mb.R[2] = a0;
//Serial.print("; ");
//Serial.print(a1);
//Mb.R[3] = a1;
//Serial.print("; ");
//Serial.print(a2);
//Mb.R[4] = a2;
//Serial.println("; ");

analogWrite(3,viteza);
if(cnt == 5){
if(viteza == 255){
x = -1;
}
if(viteza == 0){
x = 1;
}

viteza = viteza + x;

cnt = 0;

}
cnt++;
// Mb.R[1] = viteza;
for(int i = 0; i < 3; i++){
temperatura[i]=sensors.getTempC(termo[i]);
}
}

void sendUbidots(void){
String s = "'{\"senzor-0\":";
s.concat(String(temperatura[0],2));
s.concat(",\"senzor-1\":");
s.concat(String(temperatura[1],2));
s.concat(",\"senzor-2\":");
s.concat(String(temperatura[2],2));
s.concat(",\"tensiune-r1\":");
s.concat(String(5.0*a0/1024.0, 3));
s.concat(",\"tensiune-r2\":");
s.concat(String(5.0*a1/1024.0, 3));
s.concat(",\"tensiune-fotorezistenta\":");
s.concat(String(5.0*a2/1024.0, 3));
s.concat("}'");
Serial.println(s);

You might also like