#include <OPC.h>
#include <Bridge.h>
#include <Ethernet.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
OPCSerial aOPCSerial;
int DS = 12;
OneWire oneWire(DS);
DallasTemperature sensors(&oneWire);
int item_bool(const char *itemID, const opcOperation opcOP, const int value){
sensors.requestTemperatures();
return sensors.getTempCByIndex(0);
}
void setup() {
Serial.begin(9600);
aOPCSerial.setup();
aOPCSerial.addItem("T",opc_read, opc_bool, item_bool);
}
void loop() {
aOPCSerial.processOPCCommands();
}
#include <OPC.h>
#include <Bridge.h>
#include <Ethernet.h>
#include <SPI.h>
// это переменная для работы с OPC
OPCSerial aOPCSerial;
// сигнальный диод
int diod = 13;
// Цифровые
int D0 = 0;
int D1 = 1;
int D2 = 2;
int D4 = 4;
int D7 = 7;
int D8 = 8;
int D12 = 12;
// Цифровые ШИМ (или PWM)
int D3 = 3;
int D5 = 5;
int D6 = 6;
int D9 = 9;
int D10 = 10;
int D11 = 11;
// Аналоговые входа
int AI0 = 0;
int AI1 = 1;
int AI2 = 2;
int AI3 = 3;
int AI4 = 4;
int AI5 = 5;
// Это пример объявления виртуальной переменной, которая будет видна в
// Simple-Scada и её значение будет равно 99
byte MyVirtualItem(const char *itemID, const opcOperation opcOP, const byte value){
return 99;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Далее описаны процедуры которые описывают работу с Цифровыми ШИМ (PWM) входами
int D3_Item(const char *itemID, const opcOperation opcOP, const int value){
// если Simple-Scada хочет записать новое значение
if (opcOP == opc_opwrite) {
analogWrite(D3, value);
}
return analogRead(D3);
}
int D5_Item(const char *itemID, const opcOperation opcOP, const int value){
// если Simple-Scada хочет записать новое значение
if (opcOP == opc_opwrite) {
analogWrite(D5, value);
}
return analogRead(D5);
}
int D6_Item(const char *itemID, const opcOperation opcOP, const int value){
// если Simple-Scada хочет записать новое значение
if (opcOP == opc_opwrite) {
analogWrite(D6, value);
}
return analogRead(D6);
}
int D9_Item(const char *itemID, const opcOperation opcOP, const int value){
// если Simple-Scada хочет записать новое значение
if (opcOP == opc_opwrite) {
analogWrite(D9, value);
}
return analogRead(D9);
}
int D10_Item(const char *itemID, const opcOperation opcOP, const int value){
// если Simple-Scada хочет записать новое значение
if (opcOP == opc_opwrite) {
analogWrite(D10, value);
}
return analogRead(D10);
}
int D11_Item(const char *itemID, const opcOperation opcOP, const int value){
// если Simple-Scada хочет записать новое значение
if (opcOP == opc_opwrite) {
analogWrite(D11, value);
}
return analogRead(D11);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Далее описываются процедуры для чтения значений Аналоговых входов
int AI0_Item(const char *itemID, const opcOperation opcOP, const int value){
return analogRead(AI0);
}
int AI1_Item(const char *itemID, const opcOperation opcOP, const int value){
return analogRead(AI1);
}
int AI2_Item(const char *itemID, const opcOperation opcOP, const int value){
return analogRead(AI2);
}
int AI3_Item(const char *itemID, const opcOperation opcOP, const int value){
return analogRead(AI3);
}
int AI4_Item(const char *itemID, const opcOperation opcOP, const int value){
return analogRead(AI4);
}
int AI5_Item(const char *itemID, const opcOperation opcOP, const int value){
return analogRead(AI5);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void setup()
{
Serial.begin(9600);
// ОБЯЗАТЕЛЬНО!
aOPCSerial.setup();
// для примера объявляем виртуальный тег, который всегда = 99
aOPCSerial.addItem("MyVirtualItem", opc_readwrite, opc_byte, MyVirtualItem);
//регистрируем цифровые ШИМ (PWM) для Simple-Scada, и даем им имена D3, D5... и так далее
// эти имена нужно указывать в Simple-Scada в качестве Адреса
aOPCSerial.addItem("D3", opc_readwrite, opc_int, D3_Item);
aOPCSerial.addItem("D5", opc_readwrite, opc_int, D5_Item);
aOPCSerial.addItem("D6", opc_readwrite, opc_int, D6_Item);
aOPCSerial.addItem("D9", opc_readwrite, opc_int, D9_Item);
aOPCSerial.addItem("D10", opc_readwrite, opc_int, D10_Item);
aOPCSerial.addItem("D11", opc_readwrite, opc_int, D11_Item);
// регистрируем аналоговые входа для Simple-Scada, называя их AI0, AI1... и так далее
// эти имена нужно указывать в Simple-Scada в качестве Адреса
aOPCSerial.addItem("AI0", opc_read, opc_int, AI0_Item);
aOPCSerial.addItem("AI1", opc_read, opc_int, AI1_Item);
aOPCSerial.addItem("AI2", opc_read, opc_int, AI2_Item);
aOPCSerial.addItem("AI3", opc_read, opc_int, AI3_Item);
aOPCSerial.addItem("AI4", opc_read, opc_int, AI4_Item);
aOPCSerial.addItem("AI5", opc_read, opc_int, AI5_Item);
pinMode(diod, OUTPUT);
}
void loop()
{
// ОБЯЗАТЕЛЬНО! Выполняем OPC-команды в цикле
aOPCSerial.processOPCCommands();
}
У меня так и не заработало с даласом, библиотеки не совместимы, допиливать нужно.Почему Вы говорите о несовместимости библиотек? Как мы поняли температура все таки передается в скаду. Но проблема в том, что передается только один раз. Если это так, значит дело может быть не в скаде или датчике, а в коде, который используется для чтения температуры.
#include <OPC.h>
#include <Bridge.h>
#include <Ethernet.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
OPCSerial aOPCSerial;
int DS = 12;
OneWire oneWire(DS);
DallasTemperature sensors(&oneWire);
float item_temperature(const char *itemID, const opcOperation opcOP, const float value){
return sensors.getTempCByIndex(0);
}
void setup() {
Serial.begin(9600);
aOPCSerial.setup();
aOPCSerial.addItem("T", opc_read, opc_float, item_temperature);
}
void loop() {
sensors.requestTemperatures();
aOPCSerial.processOPCCommands();
}
#include <OPC.h>
#include <Bridge.h>
#include <Ethernet.h>
#include <SPI.h>
#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT21
DHT dht(DHTPIN, DHTTYPE);
OPCSerial aOPCSerial;
float callbackh(const char *itemID, const opcOperation opcOP, const float value){
return dht.readHumidity();
}
float callbackt(const char *itemID, const opcOperation opcOP, const float value){
return dht.readTemperature();
}
void setup() {
Serial.begin(9600);
dht.begin();
aOPCSerial.setup();
aOPCSerial.addItem("h",opc_read, opc_int, callbackh);
aOPCSerial.addItem("t",opc_read, opc_int, callbackt);
}
void loop() {
//delay (2000); //для экспериментов, если раскомментить идут нули
aOPCSerial.processOPCCommands();
}
#include <OPC.h>
#include <Bridge.h>
#include <Ethernet.h>
#include <SPI.h>
#include "DHT.h"
OPCSerial aOPCSerial;
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float TCallback(const char *itemID, const opcOperation opcOP, const float value){
return dht.readTemperature();
}
float HCallback(const char *itemID, const opcOperation opcOP, const float value){
return dht.readHumidity();
}
void setup()
{
Serial.begin(9600);
dht.begin();
aOPCSerial.setup();
aOPCSerial.addItem("t", opc_read, opc_float, TCallback);
aOPCSerial.addItem("h", opc_read, opc_float, HCallback);
}
void loop()
{
aOPCSerial.processOPCCommands();
}
#include <OPC.h>
#include <Bridge.h>
#include <Ethernet.h>
#include <SPI.h>
#include "DHT.h"
OPCSerial aOPCSerial;
#define DHTPIN 2
#define DHTTYPE DHT21
DHT dht(DHTPIN, DHTTYPE);
float D2_t = -1;
float D2_h = -1;
float D2_TItem(const char *itemID, const opcOperation opcOP, const float value){
float t = dht.readTemperature();
if (isnan(t)) {
return D2_t;
} else
{
D2_t = t;
return t;
}
}
float D2_HItem(const char *itemID, const opcOperation opcOP, const float value){
float h = dht.readHumidity();
if (isnan(h)) {
return D2_h;
} else
{
D2_h = h;
return h;
}
}
void setup()
{
Serial.begin(9600);
dht.begin();
aOPCSerial.setup();
aOPCSerial.addItem("D2_t", opc_read, opc_float, D2_TItem);
aOPCSerial.addItem("D2_h", opc_read, opc_float, D2_HItem);
}
void loop()
{
aOPCSerial.processOPCCommands();
}
07.01.2016 10:12:46 > Running OPC Server from C:\ArduinoOPCServer_1_6\ArduinoOPCServer.exe
07.01.2016 10:12:46 > Waiting for OPC clients to connect....
07.01.2016 10:12:46 > Connected client
07.01.2016 10:12:46 > Trying to open COM3 port
07.01.2016 10:12:46 > COM3 port open
07.01.2016 10:12:46 > Asking for available OPCItems from ArduinoSerial0
07.01.2016 10:12:46 > Frame: <0,D2_t,0,3,D2_h,0,3>
2
07.01.2016 10:12:46 > Returned OPC Items ms:16
07.01.2016 10:12:57 > Client asks for: D2_h
07.01.2016 10:12:57 > Not frame received for D2_h
07.01.2016 10:13:38 > Client asks for: D2_h
07.01.2016 10:13:38 > Not frame received for D2_h
07.01.2016 10:13:40 > Client asks for: D2_t
07.01.2016 10:13:40 > Not frame received for D2_t
07.01.2016 10:14:31 > Connected client
07.01.2016 10:14:31 > Asking for available OPCItems from ArduinoSerial0
07.01.2016 10:14:31 > Frame: <0,D2_t,0,3,D2_h,0,3>
2
07.01.2016 10:14:31 > Returned OPC Items ms:31
07.01.2016 10:14:31 > Client asks for: D2_h
07.01.2016 10:14:31 > Not frame received for D2_h
07.01.2016 10:14:31 > Client asks for: D2_h
07.01.2016 10:14:31 > Not frame received for D2_h
07.01.2016 10:14:31 > Client asks for: D2_t
07.01.2016 10:14:31 > Received D2_t value OK. Returned frame:20.00 length:5 Frame: 32302E3030
07.01.2016 10:14:31 > Client asks for: D2_t
07.01.2016 10:14:31 > Not frame received for D2_t
07.01.2016 10:14:35 > Client asks for: D2_h
07.01.2016 10:14:35 > Not frame received for D2_h
07.01.2016 10:14:35 > Client asks for: D2_t
07.01.2016 10:14:35 > Not frame received for D2_t
07.01.2016 10:14:37 > Client asks for: D2_h
07.01.2016 10:14:37 > Not frame received for D2_h
07.01.2016 10:14:37 > Client asks for: D2_t
07.01.2016 10:14:37 > Not frame received for D2_t
07.01.2016 10:14:39 > Client asks for: D2_h
07.01.2016 10:14:39 > Not frame received for D2_h
07.01.2016 10:14:39 > Client asks for: D2_t
07.01.2016 10:14:39 > Not frame received for D2_t
07.01.2016 10:14:41 > Client asks for: D2_h
07.01.2016 10:14:41 > Not frame received for D2_h
07.01.2016 10:14:41 > Client asks for: D2_t
07.01.2016 10:14:41 > Not frame received for D2_t
07.01.2016 10:14:43 > Client asks for: D2_h
07.01.2016 10:14:43 > Not frame received for D2_h
07.01.2016 10:14:43 > Client asks for: D2_t
07.01.2016 10:14:43 > Not frame received for D2_t
07.01.2016 10:14:45 > Client asks for: D2_h
07.01.2016 10:14:45 > Not frame received for D2_h
07.01.2016 10:14:45 > Client asks for: D2_t
07.01.2016 10:14:45 > Not frame received for D2_t
07.01.2016 10:14:47 > Client asks for: D2_h
07.01.2016 10:14:47 > Not frame received for D2_h
07.01.2016 10:14:47 > Client asks for: D2_t
07.01.2016 10:14:47 > Not frame received for D2_t
07.01.2016 10:14:49 > Client asks for: D2_h
07.01.2016 10:14:49 > Not frame received for D2_h
07.01.2016 10:14:49 > Client asks for: D2_t
07.01.2016 10:14:49 > Not frame received for D2_t
07.01.2016 10:14:51 > Client asks for: D2_h
07.01.2016 10:14:51 > Not frame received for D2_h
07.01.2016 10:14:51 > Client asks for: D2_t
07.01.2016 10:14:51 > Not frame received for D2_t
07.01.2016 10:14:53 > Client asks for: D2_h
07.01.2016 10:14:53 > Not frame received for D2_h
07.01.2016 10:14:53 > Client asks for: D2_t
07.01.2016 10:14:53 > Not frame received for D2_t
07.01.2016 10:14:55 > Client asks for: D2_h
07.01.2016 10:14:55 > Not frame received for D2_h
07.01.2016 10:14:55 > Client asks for: D2_t
07.01.2016 10:14:55 > Not frame received for D2_t
07.01.2016 10:14:57 > Client asks for: D2_h
07.01.2016 10:14:57 > Not frame received for D2_h
07.01.2016 10:14:57 > Client asks for: D2_t
07.01.2016 10:14:57 > Not frame received for D2_t
07.01.2016 10:14:59 > Client asks for: D2_h
07.01.2016 10:14:59 > Not frame received for D2_h
07.01.2016 10:14:59 > Client asks for: D2_t
07.01.2016 10:14:59 > Not frame received for D2_t
07.01.2016 10:15:01 > Client asks for: D2_h
07.01.2016 10:15:01 > Not frame received for D2_h
07.01.2016 10:15:01 > Client asks for: D2_t
07.01.2016 10:15:01 > Not frame received for D2_t
07.01.2016 10:15:03 > Client asks for: D2_h
07.01.2016 10:15:03 > Not frame received for D2_h
07.01.2016 10:15:03 > Client asks for: D2_t
07.01.2016 10:15:03 > Not frame received for D2_t
07.01.2016 10:15:05 > Client asks for: D2_h
07.01.2016 10:15:05 > Not frame received for D2_h
07.01.2016 10:15:05 > Client asks for: D2_t
07.01.2016 10:15:05 > Not frame received for D2_t
07.01.2016 10:15:07 > Client asks for: D2_h
07.01.2016 10:15:07 > Not frame received for D2_h
07.01.2016 10:15:07 > Client asks for: D2_t
07.01.2016 10:15:07 > Not frame received for D2_t
07.01.2016 10:15:07 > Disconnected client Simple-Scada OPC client
#include <OPC.h>
#include <Bridge.h>
#include <Ethernet.h>
#include <SPI.h>
#include "DHT.h"
OPCSerial aOPCSerial;
#define DHTPIN 2
#define DHTTYPE DHT21
DHT dht(DHTPIN, DHTTYPE);
float D2_t = -1;
float D2_h = -1;
float D2_TItem(const char *itemID, const opcOperation opcOP, const float value){
noInterrupts();
float t = dht.readTemperature();
interrupts();
if (isnan(t)) {
return D2_t;
} else
{
D2_t = t;
return t;
}
}
float D2_HItem(const char *itemID, const opcOperation opcOP, const float value){
noInterrupts();
float h = dht.readHumidity();
interrupts();
if (isnan(h)) {
return D2_h;
} else
{
D2_h = h;
return h;
}
}
void setup()
{
Serial.begin(9600);
dht.begin();
aOPCSerial.setup();
aOPCSerial.addItem("D2_t", opc_read, opc_float, D2_TItem);
aOPCSerial.addItem("D2_h", opc_read, opc_float, D2_HItem);
}
void loop()
{
aOPCSerial.processOPCCommands();
}
int _gtv1 = 15;
int _gtv2 = 75;
int _gtv1_Item(const char *itemID, const opcOperation opcOP, const int value){
return analogRead(_gtv1);
}
int _gtv2_Item(const char *itemID, const opcOperation opcOP, const int value){
return analogRead(_gtv2);
}
aOPCSerial.addItem("_gtv1", opc_read, opc_int, _gtv1_Item);
aOPCSerial.addItem("_gtv2", opc_read, opc_int, _gtv2_Item);
int _gtv1_Item(const char *itemID, const opcOperation opcOP, const int value){
return _gtv1;
}
int _gtv1_Item(const char *itemID, const opcOperation opcOP, const int value){
// Если Simple-Scada пытается записать новое значение в _gtv1
if (opcOP == opc_opwrite) {
_gtv1 = value;
}
return _gtv1;
}
26.01.2016 20:29:55 > Running OPC Server from G:\opc\ArduinoOPCServer.exe
26.01.2016 20:29:55 > Waiting for OPC clients to connect....
26.01.2016 20:30:10 > Connected client
26.01.2016 20:30:10 > Trying to open COM3 port
26.01.2016 20:30:10 > COM3 port open
26.01.2016 20:30:10 > Asking for available OPCItems from ArduinoSerial0
26.01.2016 20:30:10 > Frame: <0,MyVirtualItem,2,1,D3,2,2,D5,2,2,D6,2,2,D9,2,2,D10,2,2,D11,2,2,AI0,0,2,AI1,0,2,AI2,0,2,AI3,0,2,AI4,0,2,AI5,0,2>
13
26.01.2016 20:30:10 > Returned OPC Items ms:140
#include <OPC.h>
#include <Bridge.h>
#include <Ethernet.h>
#include <SPI.h>
#include "DHT.h"
OPCSerial aOPCSerial;
#define DHTPIN 2
#define DHTTYPE DHT21
DHT dht(DHTPIN, DHTTYPE);
float TempBuffer = -1;
float TempItem(const char *itemID, const opcOperation opcOP, const float value){
float t = dht.readTemperature();
// Если температуру считать не удалось
if ((isnan(t)) || (t < 1)) {
return TempBuffer;
} else
// Если температура была успешно считана
{
TempBuffer = t;
return t;
}
}
void setup()
{
Serial.begin(9600);
dht.begin();
aOPCSerial.setup();
aOPCSerial.addItem("Temperature", opc_read, opc_float, TempItem);
}
void loop()
{
aOPCSerial.processOPCCommands();
}
#include <OPC.h>
#include <Bridge.h>
#include <Ethernet.h>
#include <SPI.h>
#include "DHT.h"
OPCSerial aOPCSerial;
#define DHTPIN 2
#define DHTTYPE DHT21
DHT dht(DHTPIN, DHTTYPE);
float TempBuffer = -1;
float TempItem(const char *itemID, const opcOperation opcOP, const float value){
return TempBuffer;
}
void setup()
{
Serial.begin(9600);
dht.begin();
aOPCSerial.setup();
aOPCSerial.addItem("Temperature", opc_read, opc_float, TempItem);
}
void loop()
{
float t = dht.readTemperature();
// Если температуру считать не удалось
if ((isnan(t)) || (t < 1)) {
} else
// Если температура была успешно считана
{
TempBuffer = t;
}
// Здесь ещё можно сделать тесты с delay
// delay (2000);
aOPCSerial.processOPCCommands();
}
Ваш код не компилируется(А подробнее, какие ошибки?
Без изменений, с делеем все понятно, если ставить задержку в скетче то получается нулевое значение как раз на время задержки.Среда разработки 1.6.7 ?
среда 1.7.7 , но думаю роли это не играет, сдвинулось с места, сейчас при запуске не моргает температура меняется в зависимости от нагрева/остывания, но периодически опять начинает меняться и через некоторое время температура снова опускается/поднимается и так по кругу.Попробуйте мой скетч с 18-го поста в этой теме. Сначала noInterrupts();
С noInterrupts(); отказывается вообще считывать показания.
Ваш код пробовал неоднократно, значения вообще не читает, скомпилировал в 1.6.5. вот лог с опсВесьма странно, попробуйте мой код, в среде 1.6.7 как у меня. Не подумайте ничего плохого, а в меняете в моем скетче строки
e: 2D312E3030
27.01.2016 22:11:12 > Client asks for: D2_t
27.01.2016 22:11:12 > Received D2_t value OK. Returned frame:-1.00 length:5 Frame: 2D312E3030
27.01.2016 22:11:12 > Client asks for: D2_h
27.01.2016 22:11:12 > Received D2_h value OK. Returned frame:-1.00 length:5 Frame: 2D312E3030
да тут и нечего думать плохо, по запаре можно и ошибиться в простом:) , но к сожалению это не тот случай, единственная разница у меня дтх11, а подключен так же к 2 пину. уже не знаю что и делать......моя библиотека DHT
#include <OPC.h>
#include <Bridge.h>
#include <Ethernet.h>
#include <SPI.h>
#include "DHT.h"
OPCSerial aOPCSerial;
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float D2_t = -1;
float D2_h = -1;
float D2_TItem(const char *itemID, const opcOperation opcOP, const float value){
noInterrupts();
float t = dht.readTemperature();
interrupts();
if (isnan(t)) {
return D2_t;
} else
{
D2_t = t;
return t;
}
}
float D2_HItem(const char *itemID, const opcOperation opcOP, const float value){
noInterrupts();
float h = dht.readHumidity();
interrupts();
if (isnan(h)) {
return D2_h;
} else
{
D2_h = h;
return h;
}
}
void setup()
{
Serial.begin(9600);
dht.begin();
aOPCSerial.setup();
aOPCSerial.addItem("D2_t", opc_read, opc_float, D2_TItem);
aOPCSerial.addItem("D2_h", opc_read, opc_float, D2_HItem);
}
void loop()
{
aOPCSerial.processOPCCommands();
}
#include <OPC.h>
#include <Bridge.h>
#include <Ethernet.h>
#include <SPI.h>
#include <dht11.h>
dht11 DHT; // Объявление переменной класса dht11
#define DHT11_PIN 2
OPCSerial aOPCSerial;
float TempItem(const char *itemID, const opcOperation opcOP, const float value){
DHT.read(DHT11_PIN);
return(DHT.temperature);
}
float HumItem(const char *itemID, const opcOperation opcOP, const float value){
DHT.read(DHT11_PIN);
return(DHT.humidity);
}
void setup()
{
Serial.begin(9600);
aOPCSerial.setup();
aOPCSerial.addItem("Temperature", opc_read, opc_float, TempItem);
aOPCSerial.addItem("Humidity", opc_read, opc_float, HumItem);
}
void loop()
{
aOPCSerial.processOPCCommands();
}
// Цифровые
int D0 = 0;
int D1 = 1;
int D2 = 2;
int D4 = 4;
int D7 = 7;
int D8 = 8;
int D12 = 12;
#include <OPC.h>
#include <Bridge.h>
#include <Ethernet.h>
#include <SPI.h>
/* Declaring the OPC object */
OPCSerial aOPCSerial;
/* set led status from OPC Client */
int ledPin = 13;
/* create a callback function for the OPCItem */
bool callback(const char *itemID, const opcOperation opcOP, const bool value){
static bool ledValue = false;
/* if operation is a write command from OPC Client */
if (opcOP == opc_opwrite) {
ledValue = value;
if (ledValue)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
}
else
/* read the led status */
return ledValue;
}
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
/* OPC Object initialization */
aOPCSerial.setup();
/* led OPCItem declaration */
aOPCSerial.addItem("led",opc_readwrite, opc_bool, callback);
}
void loop() {
/* OPC process commands */
aOPCSerial.processOPCCommands();
}
#include <OPC.h>
#include <Bridge.h>
#include <Ethernet.h>
#include <SPI.h>
#include <OneWire.h>
OPCSerial aOPCSerial;
OneWire ds (4);
byte data[12];
byte addr1[8] = {0x28, 0xFF, 0xE0, 0x5A, 0x71, 0x15, 0x01, 0x58}; // ID индивидуально для каждого датчика
byte addr2[8] = {0x28, 0xFF, 0x83, 0x4E, 0x71, 0x15, 0x02, 0xD3};
unsigned int raw;
float temp1, temp2;
//OneWire oneWire(DS);
//DallasTemperature sensors(&oneWire);
float item_temperature1(const char *itemID, const opcOperation opcOP, const float value){
temp1 = DS18B20(addr1);
return temp1;
}
float item_temperature2(const char *itemID, const opcOperation opcOP, const float value){
temp2 = DS18B20(addr2);
return temp2;
}
void setup() {
Serial.begin(9600);
aOPCSerial.setup();
aOPCSerial.addItem("T1", opc_read, opc_float, item_temperature1);
aOPCSerial.addItem("T2", opc_read, opc_float, item_temperature2);
}
void loop() {
//sensors.requestTemperatures();
aOPCSerial.processOPCCommands();
}
float DS18B20(byte *adres)
{
ds.reset();
ds.select(adres);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
ds.reset();
ds.select(adres);
ds.write(0xBE); // Read Scratchpad
for (byte i = 0; i < 9; i++) // можно увеличить точность измерения до 0.0625 *С (от 9 до 12 бит)
{ // we need 9 bytes
data[i] = ds.read ();
}
raw = (data[1] << 8) | data[0];//=======Пересчитываем в температуру
float celsius = (float)raw / 16.0;
return celsius;
}
Неужели никто не подскажет? В примере админа регистрируются цифровые пины, но примера работы с ними нет(((Пример (переписал местный для теста) для цифровых выходов (НЕ ШИМ) + проект для СКАДы, может потом кому понадобится...
#include <OPC.h>
#include <Bridge.h>
#include <Ethernet.h>
#include <SPI.h>
// это переменная для работы с OPC
OPCSerial aOPCSerial;
// Цифровые
int D0 = 0;
int D1 = 1;
int D2 = 2;
int D4 = 4;
int D7 = 7;
int D8 = 8;
int D12 = 12;
int D13 = 13;
// Цифровые ШИМ (или PWM)
int D3 = 3;
int D5 = 5;
int D6 = 6;
int D9 = 9;
int D10 = 10;
int D11 = 11;
// Аналоговые входа
int AI0 = 0;
int AI1 = 1;
int AI2 = 2;
int AI3 = 3;
int AI4 = 4;
int AI5 = 5;
// Это пример объявления виртуальной переменной, которая будет видна в
// Simple-Scada и её значение будет равно 99
byte MyVirtualItem(const char *itemID, const opcOperation opcOP, const byte value){
return 99;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Далее описаны процедуры которые описывают работу с Цифровыми выходами
int D7_Item(const char *itemID, const opcOperation opcOP, const int value){
// если Simple-Scada хочет записать новое значение
if (opcOP == opc_opwrite) {
digitalWrite(D7, value);
}
return digitalRead(D7);
}
int D8_Item(const char *itemID, const opcOperation opcOP, const int value){
// если Simple-Scada хочет записать новое значение
if (opcOP == opc_opwrite) {
digitalWrite(D8, value);
}
return digitalRead(D8);
}
int D12_Item(const char *itemID, const opcOperation opcOP, const int value){
// если Simple-Scada хочет записать новое значение
if (opcOP == opc_opwrite) {
digitalWrite(D12, value);
}
return digitalRead(D12);
}
int D13_Item(const char *itemID, const opcOperation opcOP, const int value){
// если Simple-Scada хочет записать новое значение
if (opcOP == opc_opwrite) {
digitalWrite(D13, value);
}
return digitalRead(D13);
}
int D4_Item(const char *itemID, const opcOperation opcOP, const int value){
// если Simple-Scada хочет записать новое значение
if (opcOP == opc_opwrite) {
digitalWrite(D4, value);
}
return digitalRead(D4);
}
int D2_Item(const char *itemID, const opcOperation opcOP, const int value){
// если Simple-Scada хочет записать новое значение
if (opcOP == opc_opwrite) {
digitalWrite(D2, value);
}
return digitalRead(D2);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Далее описываются процедуры для чтения значений Аналоговых входов
int AI0_Item(const char *itemID, const opcOperation opcOP, const int value){
return analogRead(AI0);
}
int AI1_Item(const char *itemID, const opcOperation opcOP, const int value){
return analogRead(AI1);
}
int AI2_Item(const char *itemID, const opcOperation opcOP, const int value){
return analogRead(AI2);
}
int AI3_Item(const char *itemID, const opcOperation opcOP, const int value){
return analogRead(AI3);
}
int AI4_Item(const char *itemID, const opcOperation opcOP, const int value){
return analogRead(AI4);
}
int AI5_Item(const char *itemID, const opcOperation opcOP, const int value){
return analogRead(AI5);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void setup()
{
Serial.begin(57600);
// ОБЯЗАТЕЛЬНО!
aOPCSerial.setup();
// для примера объявляем виртуальный тег, который всегда = 99
aOPCSerial.addItem("MyVirtualItem", opc_readwrite, opc_byte, MyVirtualItem);
//регистрируем цифровые для Simple-Scada, и даем им имена D2, D4... и так далее
// эти имена нужно указывать в Simple-Scada в качестве Адреса
aOPCSerial.addItem("D2", opc_readwrite, opc_int, D2_Item);
aOPCSerial.addItem("D4", opc_readwrite, opc_int, D4_Item);
aOPCSerial.addItem("D7", opc_readwrite, opc_int, D7_Item);
aOPCSerial.addItem("D8", opc_readwrite, opc_int, D8_Item);
aOPCSerial.addItem("D12", opc_readwrite, opc_int, D12_Item);
aOPCSerial.addItem("D13", opc_readwrite, opc_int, D13_Item);
// регистрируем аналоговые входа для Simple-Scada, называя их AI0, AI1... и так далее
// эти имена нужно указывать в Simple-Scada в качестве Адреса
aOPCSerial.addItem("AI0", opc_read, opc_int, AI0_Item);
aOPCSerial.addItem("AI1", opc_read, opc_int, AI1_Item);
aOPCSerial.addItem("AI2", opc_read, opc_int, AI2_Item);
aOPCSerial.addItem("AI3", opc_read, opc_int, AI3_Item);
aOPCSerial.addItem("AI4", opc_read, opc_int, AI4_Item);
aOPCSerial.addItem("AI5", opc_read, opc_int, AI5_Item);
pinMode(D2, OUTPUT);
pinMode(D4, OUTPUT);
pinMode(D7, OUTPUT);
pinMode(D8, OUTPUT);
pinMode(D12, OUTPUT);
pinMode(D13, OUTPUT);
}
void loop()
{
// ОБЯЗАТЕЛЬНО! Выполняем OPC-команды в цикле
aOPCSerial.processOPCCommands();
}
#include <OPC.h>
#include <OneWire.h>
OPCSerial aOPCSerial;
OneWire ds (7);
byte data[2];
unsigned int raw;
float item_temperature1(const char *itemID, const opcOperation opcOP, const float value){
return Read_Temp();
}
void setup() {
Serial.begin(9600);
aOPCSerial.setup();
aOPCSerial.addItem("T1", opc_read, opc_float, item_temperature1);
}
void loop() {
aOPCSerial.processOPCCommands();
}
float Read_Temp (void)
{
ds.reset();
ds.write(0xCC);
ds.write(0x44); // start conversion, with parasite power on at the end
ds.reset();
ds.write(0xCC);
ds.write(0xBE); // Read Scratchpad
data[0] = ds.read ();
data[1] = ds.read ();
raw = (data[1] << 8) | data[0];//=======Пересчитываем в температуру
return (float)raw / 16.0;
}
unsigned int raw;
return (float)raw / 16.0;
20.03.2016 21:34:25 > Asking for available OPCItems from ArduinoEthernet0Хотя если делать тоже самое но с ArduinoSerial все нормально получается тип данных определяется коректно и графические шкалы работают, через езернет любой канал определяется стрингом и графические шкалы не работают, код в ардуинке не меняется... мб нужно как то в ручную указывать тип данных в случае с aOPCEthernet ?
20.03.2016 21:34:26 > Frame: [{"ItemId":"Temperature","AccessRight":"0","ItemType":"3"},{"ItemId":"Humidity","AccessRight":"0","ItemType":"3"},{"ItemId":"RELE01","AccessRight":"2","ItemType":"0"},{"ItemId":"RELE02","AccessRight":"2","ItemType":"0"},{"ItemId":"RELE03","AccessRight":"2","ItemType":"0"},{"ItemId":"RELE04","AccessRight":"2","ItemType":"0"},{"ItemId":"RELE05","AccessRight":"2","ItemType":"0"},
bool callback(const char *itemID, const opcOperation opcOP, const bool value){
static bool ledValue = false;
/* if operation is a write command from OPC Client */
if (opcOP == opc_opwrite) {
ledValue = value;
if (ledValue)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
}
else
/* read the led status */
return ledValue;
}
А когда Simple-Scada 2 будет доступен для скачивания ?Точно сказать очень трудно - постараемся закончить к концу апреля. Тестовую версию отправили Вам на почту.
И будет ли когда клиентская часть под Андроид планшеты... ?Да, такое в планах есть, но будет это не скоро.
#include <OPC.h>
#include <Bridge.h>
#include <Ethernet.h>
#include <SPI.h>
// Declaring the OPC object
OPCSerial aOPCSerial;
// set led status from OPC Client
int ledPin = 13;
// create a callback function for the OPCItem
bool callback(const char *itemID, const opcOperation opcOP, const bool value){
static bool ledValue = false;
// if operation is a write command from OPC Client
if (opcOP == opc_opwrite) {
ledValue = value;
if (ledValue)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
}
else
// read the led status
return ledValue;
}
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
// OPC Object initialization
aOPCSerial.setup();
// led OPCItem declaration
aOPCSerial.addItem("led",opc_readwrite, opc_bool, callback);
}
void loop() {
// OPC process commands
aOPCSerial.processOPCCommands();
}
#include <OPC.h>
#include <Bridge.h>
#include <Ethernet.h>
#include <SPI.h>
// Declaring the OPC object
OPCSerial aOPCSerial;
// set led status from OPC Client
int ledPin = 13;
// create a callback function for the OPCItem
int callback(const char *itemID, const opcOperation opcOP, const int value){
static int ledValue = 0;
// if operation is a write command from OPC Client
if (opcOP == opc_opwrite) {
ledValue = value;
if (ledValue == 1)
digitalWrite(ledPin, HIGH);
else
digitalWrite(ledPin, LOW);
}
else
// read the led status
return ledValue;
}
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
// OPC Object initialization
aOPCSerial.setup();
// led OPCItem declaration
aOPCSerial.addItem("led", opc_readwrite, opc_int, callback);
}
void loop() {
// OPC process commands
aOPCSerial.processOPCCommands();
}
Read file function failed
sYstem error code 6
controlador no valido
Read file function failed
System erro code 996
El ebento superpuesto de entrada/salida no es un estado señalado
Clearcommerror function failed
system error code 6
controlodar no valido
port not open
Читайте тему сначала, я мучался с подключением DHT21 - суть то-же самое. Там все описано.
В общем, решение могу подсказать только через Simple-Scada 2.0 которая как раз недавно вышла)
Мой пример с датчиком DHT11, но я думаю с остальными датчиками можно сделать как в этом примере.
http://simple-scada.com/download - Скачиваем "Демо версия Simple-Scada 2.0 на 64 тега"
в Ардуино уно названия библиотек в коде подсвечены желтым, а OPC библитека нет.Так и должно быть?Да, всё нормально.
#include <OPC.h>
#include <Bridge.h>
#include <Ethernet.h>
#include <SPI.h>
// это переменная для работы с OPC
OPCSerial aOPCSerial;
// сигнальный диод
int diod = 13;
// Цифровые
int D0 = 0;
int D1 = 1;
int D2 = 2;
int D4 = 4;
int D7 = 7;
int D8 = 8;
int D12 = 12;
// Цифровые ШИМ (или PWM)
int D3 = 3;
int D5 = 5;
int D6 = 6;
int D9 = 9;
int D10 = 10;
int D11 = 11;
// Аналоговые входа
int AI0 = 0;
int AI1 = 1;
int AI2 = 2;
int AI3 = 3;
int AI4 = 4;
int AI5 = 5;
// Это пример объявления виртуальной переменной, которая будет видна в
// Simple-Scada и её значение будет равно 99
byte MyVirtualItem(const char *itemID, const opcOperation opcOP, const byte value){
return 99;
}
bool D0_item(const char *ItemID, const opcOperation opcOP, const bool value){
if (opcOP == opc_opwrite){
digitalWrite(D0, value);
}
return digitalRead(D0);
}
bool D1_item(const char *ItemID, const opcOperation opcOP, const bool value){
if (opcOP == opc_opwrite){
digitalWrite(D1, value);
}
return digitalRead(D1);
}
bool D2_item(const char *ItemID, const opcOperation opcOP, const bool value){
if (opcOP == opc_opwrite){
digitalWrite(D2, value);
}
return digitalRead(D2);
}
bool D4_item(const char *ItemID, const opcOperation opcOP, const bool value){
if (opcOP == opc_opwrite){
digitalWrite(D4, value);
}
return digitalRead(D4);
}
bool D7_item(const char *ItemID, const opcOperation opcOP, const bool value){
if (opcOP == opc_opwrite){
digitalWrite(D7, value);
}
return digitalRead(D7);
}
bool D8_item(const char *ItemID, const opcOperation opcOP, const bool value){
if (opcOP == opc_opwrite){
digitalWrite(D8, value);
}
return digitalRead(D8);
}
bool D12_item(const char *ItemID, const opcOperation opcOP, const bool value){
if (opcOP == opc_opwrite){
digitalWrite(D12, value);
}
return digitalRead(D12);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Далее описаны процедуры которые описывают работу с Цифровыми ШИМ (PWM) входами
int D3_Item(const char *itemID, const opcOperation opcOP, const int value){
// если Simple-Scada хочет записать новое значение
if (opcOP == opc_opwrite) {
analogWrite(D3, value);
}
return analogRead(D3);
}
int D5_Item(const char *itemID, const opcOperation opcOP, const int value){
// если Simple-Scada хочет записать новое значение
if (opcOP == opc_opwrite) {
analogWrite(D5, value);
}
return analogRead(D5);
}
int D6_Item(const char *itemID, const opcOperation opcOP, const int value){
// если Simple-Scada хочет записать новое значение
if (opcOP == opc_opwrite) {
analogWrite(D6, value);
}
return analogRead(D6);
}
int D9_Item(const char *itemID, const opcOperation opcOP, const int value){
// если Simple-Scada хочет записать новое значение
if (opcOP == opc_opwrite) {
analogWrite(D9, value);
}
return analogRead(D9);
}
int D10_Item(const char *itemID, const opcOperation opcOP, const int value){
// если Simple-Scada хочет записать новое значение
if (opcOP == opc_opwrite) {
analogWrite(D10, value);
}
return analogRead(D10);
}
int D11_Item(const char *itemID, const opcOperation opcOP, const int value){
// если Simple-Scada хочет записать новое значение
if (opcOP == opc_opwrite) {
analogWrite(D11, value);
}
return analogRead(D11);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Далее описываются процедуры для чтения значений Аналоговых входов
int AI0_Item(const char *itemID, const opcOperation opcOP, const int value){
if (opcOP == opc_opwrite) {
analogWrite(AI0, value);
}
return analogRead(AI0);
}
int AI1_Item(const char *itemID, const opcOperation opcOP, const int value){
if (opcOP == opc_opwrite) {
analogWrite(AI1, value);
}
return analogRead(AI1);
}
int AI2_Item(const char *itemID, const opcOperation opcOP, const int value){
if (opcOP == opc_opwrite) {
analogWrite(AI2, value);
}
return analogRead(AI2);
}
int AI3_Item(const char *itemID, const opcOperation opcOP, const int value){
if (opcOP == opc_opwrite) {
analogWrite(AI3, value);
}
return analogRead(AI3);
}
int AI4_Item(const char *itemID, const opcOperation opcOP, const int value){
if (opcOP == opc_opwrite) {
analogWrite(AI4, value);
}
return analogRead(AI4);
}
int AI5_Item(const char *itemID, const opcOperation opcOP, const int value){
if (opcOP == opc_opwrite) {
analogWrite(AI5, value);
}
return analogRead(AI5);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void setup()
{
Serial.begin(9600);
// ОБЯЗАТЕЛЬНО!
aOPCSerial.setup();
// для примера объявляем виртуальный тег, который всегда = 99
aOPCSerial.addItem("MyVirtualItem", opc_readwrite, opc_byte, MyVirtualItem);
aOPCSerial.addItem("D0", opc_readwrite, opc_int, D0_Item);
aOPCSerial.addItem("D1", opc_readwrite, opc_int, D1_Item);
aOPCSerial.addItem("D2", opc_readwrite, opc_int, D2_Item);
aOPCSerial.addItem("D4", opc_readwrite, opc_int, D4_Item);
aOPCSerial.addItem("D7", opc_readwrite, opc_int, D7_Item);
aOPCSerial.addItem("D8", opc_readwrite, opc_int, D8_Item);
aOPCSerial.addItem("D12", opc_readwrite, opc_int, D12_Item);
//регистрируем цифровые ШИМ (PWM) для Simple-Scada, и даем им имена D3, D5... и так далее
// эти имена нужно указывать в Simple-Scada в качестве Адреса
aOPCSerial.addItem("D3", opc_readwrite, opc_int, D3_Item);
aOPCSerial.addItem("D5", opc_readwrite, opc_int, D5_Item);
aOPCSerial.addItem("D6", opc_readwrite, opc_int, D6_Item);
aOPCSerial.addItem("D9", opc_readwrite, opc_int, D9_Item);
aOPCSerial.addItem("D10", opc_readwrite, opc_int, D10_Item);
aOPCSerial.addItem("D11", opc_readwrite, opc_int, D11_Item);
// регистрируем аналоговые входа для Simple-Scada, называя их AI0, AI1... и так далее
// эти имена нужно указывать в Simple-Scada в качестве Адреса
aOPCSerial.addItem("AI0", opc_read, opc_int, AI0_Item);
aOPCSerial.addItem("AI1", opc_read, opc_int, AI1_Item);
aOPCSerial.addItem("AI2", opc_read, opc_int, AI2_Item);
aOPCSerial.addItem("AI3", opc_read, opc_int, AI3_Item);
aOPCSerial.addItem("AI4", opc_read, opc_int, AI4_Item);
aOPCSerial.addItem("AI5", opc_read, opc_int, AI5_Item);
pinMode(diod, OUTPUT);
}
void loop()
{
// ОБЯЗАТЕЛЬНО! Выполняем OPC-команды в цикле
aOPCSerial.processOPCCommands();
}
OPC:213: error: 'D0_Item' was not declared in this scope
OPC:214: error: 'D1_Item' was not declared in this scope
OPC:215: error: 'D2_Item' was not declared in this scope
OPC:216: error: 'D4_Item' was not declared in this scope
OPC:217: error: 'D7_Item' was not declared in this scope
OPC:218: error: 'D8_Item' was not declared in this scope
OPC:219: error: 'D12_Item' was not declared in this scope
exit status 1
'D0_Item' was not declared in this scope
bool D0_item(const char *ItemID, const opcOperation opcOP, const bool value){
if (opcOP == opc_opwrite){
digitalWrite(D0, value);
}
return digitalRead(D0);
}
aOPCSerial.addItem("D0", opc_readwrite, opc_int, D0_Item);
Нет ли один помочь ?Советую Вам бросить ArduinoOPCserver, это сыроватая и нестабильная штука, тем более что ардуина с ней будет лишь устройством ввода-вывода, а это слишком жирное решение. При попытке внедрить код для каких-то действий кроме работы OPC-сервера начинают лезть всякие глюки. Перейдите на модбас и будет Вам счастье. Есть среда разработки FLProg - замечательный инструмент для визуального программирования. Все требуемые Вам решения реализуются за несколько кликов.
Скажите кода Arduino. очень трудно? Почему разработчик не желает, чтобы помочь ?Ние не можем да разберем точно същността на въпроса. Кодът, който сте записали трябва да работи. Най-малко той работи за Arduino UNO.
begin
Button2.OnClickEvent;
Button3.OnClickEvent;
Button4.OnClickEvent;
end.
Для этого можно использовать следующий код на событие Button1.OnClick:Кодbegin
Button2.OnClickEvent;
Button3.OnClickEvent;
Button4.OnClickEvent;
end.
begin
my_variable1.Value := 1;
my_variable2.Value := 1;
my_variable3.Value := 1;
end.
begin
if VarAuto = TRUE then
begin
my_variable1.Value := 1;
my_variable2.Value := 1;
my_variable3.Value := 1;
end else
begin
my_variable1.Value := 0;
my_variable2.Value := 0;
my_variable3.Value := 0;
end;
end.
Simple-Scada у меня есть к вам вопрос.А как ты хочешь соединить 2 Arduino по UART если USB уже его использует? Или у тебя Mega2560 c 4 UART?
Можете ли вы работать два соединенных доски Arduino связанных с TX - RX ?
Что следует добавить к коду из двух досок, чтобы работать как один ?
#include <OPC.h>
#include <Bridge.h>
#include <Ethernet.h>
#include <SPI.h>
#include <OneWire.h>
OPCSerial aOPCSerial;
OneWire ds (4);
byte data[12];
byte addr1[8] = {0x28, 0xFF, 0x4F, 0x81, 0xA6, 0x16, 0x3, 0x1B}; // ID индивидуально для каждого датчика
unsigned int raw;
float temp1;
//OneWire oneWire(DS);
//DallasTemperature sensors(&oneWire);
float item_temperature1(const char *itemID, const opcOperation opcOP, const float value){
temp1 = DS18B20(addr1);
return temp1;
}
void setup() {
Serial.begin(9600);
aOPCSerial.setup();
aOPCSerial.addItem("T1", opc_read, opc_float, item_temperature1);
}
void loop() {
//sensors.requestTemperatures();
aOPCSerial.processOPCCommands();
}
float DS18B20(byte *adres)
{
ds.reset();
ds.select(adres);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
ds.reset();
ds.select(adres);
ds.write(0xBE); // Read Scratchpad
for (byte i = 0; i < 9; i++) // можно увеличить точность измерения до 0.0625 *С (от 9 до 12 бит)
{ // we need 9 bytes
data[i] = ds.read ();
}
raw = (data[1] << 8) | data[0];//=======Пересчитываем в температуру
float celsius = (float)raw / 16.0;
return celsius;
}
Во-первых, смущает то что в Simole Scada и в ItemProperty у сервера номер 2 в конце, а ни как у всех 1: "ArduinoOPCServer.2".2 это номер версии OPC-сервера. Раньше все устанавливали первую версию.
Во-вторых, ItemProperty не видит адреса тегов, ровно как и Simple Scada:Здесь всё зависит от OPC-сервера. Тег точно регистрируется в программе через "aOPCSerial.addItem("T1", opc_read, opc_float, item_temperature1);", но OPC-сервер по каким-то причинам не отдаёт его OPC-клиентам (скаде и ItemProperty).