Obsługa 2xDS18B20 i wysyłka na Thingspeak - zamber - 12-02-2017
Witam
Długo szukałem kodu do obsługi DS18B20 i wstawiania pomiarów z wykorzystaniem modułu ENC28J60 na Thingspeak. Jest jeden dostępny na Majsterkowo, ale nie działa jak należy.
Kod w końcu znalazłem, działa poprawnie, ale obsługuje jeden czujnik. Kombinuję na różne sposoby, jak zmusić Arduino do wysłania pomiarów z dwóch czujników. No na razie jestem za cienki. Program nie działa jak należy, wrzuca dane z jednej albo drugiej czujki, w zależności co gdzie skopiuję w sekcji void loop.
Poniżej działający kod dla jednego czujnika.
Kod: #include <OneWire.h>
/*
| Post temp. values from a DS18B20 to ThingSpeak using the Ethercard interface based on the
| ENC28J60 chip.
| Based on the Ethercard example from www.jeelabs.org
| Phil Grant Jan 2014
*/
#include <EtherCard.h>
// change these settings to match your own setup
#define APIKEY "*****************"
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h> //DS18B20 support
// =========== Setup the Temperature sensor details =================
#define ONE_WIRE_BUS 7 // DS18B20 Temperature chip i/o on pin D7
#define TEMPERATURE_PRECISION 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Insert the ID of your temp sensor here, for the sketch, visit here
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
DeviceAddress Thermometer = {0x28, 0xEE, 0x1B, 0xAC, 0x21, 0x16, 0x2, 0x56 }; // probe
// ethernet interface mac address, must be unique on the LAN
byte mymac[] = { 0x54, 0x55, 0x58, 0x10, 0x00, 0x24 };
const char website[] PROGMEM = "api.thingspeak.com";
byte Ethernet::buffer[700];
uint32_t timer;
Stash stash;
void setup () {
Serial.begin(57600);
Serial.println("\n[webClient]");
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println( "Failed to access Ethernet controller");
if (!ether.dhcpSetup())
Serial.println("DHCP failed");
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
if (!ether.dnsLookup(website))
Serial.println("DNS failed");
ether.printIp("SRV: ", ether.hisip);
}
void loop () {
ether.packetLoop(ether.packetReceive());
if (millis() > timer) {
timer = millis() + 60000; //Transmit every minute
sensors.requestTemperatures();
delay(200);
// generate two fake values as payload - by using a separate stash,
// we can determine the size of the generated message ahead of time
byte sd = stash.create();
stash.print("field1=");
stash.println(String(sensors.getTempC(Thermometer),DEC));
stash.save();
// generate the header with payload - note that the stash size is used,
// and that a "stash descriptor" is passed in as argument using "$H"
Stash::prepare(PSTR("POST /update HTTP/1.1" "\r\n"
"Host: $F" "\r\n"
"Connection: close" "\r\n"
"X-THINGSPEAKAPIKEY: $F" "\r\n"
"Content-Type: application/x-www-form-urlencoded" "\r\n"
"Content-Length: $D" "\r\n"
"\r\n"
"$H"),
website, PSTR(APIKEY), stash.size(), sd);
// send the packet - this also releases all stash buffers once done
ether.tcpSend();
}
}
Odczytałem adres drugiej czujki i dołożyłem do kodu:
Kod: DeviceAddress Thermometer_1 = {0x28, 0xEE, 0x1B, 0xAC, 0x21, 0x16, 0x2, 0x56 };
DeviceAddress Thermometer_2 = {0x28, 0xC7, 0x3F, 0xE7, 0x2, 0x0, 0x0, 0x6F };
W związku ze zmianą nazwy z Thermometer na Thermometer_1 dokonałem odpowiednich zmian w void loop, dane z czujki 1 są poprawnie wrzucane do field1.
Poniżej sekcja void loop po zmianach
Kod: void loop () {
ether.packetLoop(ether.packetReceive());
if (millis() > timer) {
timer = millis() + 60000; //Transmit every minute
sensors.requestTemperatures();
delay(200);
// generate two fake values as payload - by using a separate stash,
// we can determine the size of the generated message ahead of time
byte sd = stash.create();
stash.print("field1=");
stash.println(String(sensors.getTempC(Thermometer_1),DEC));
stash.save();
// generate the header with payload - note that the stash size is used,
// and that a "stash descriptor" is passed in as argument using "$H"
Stash::prepare(PSTR("POST /update HTTP/1.1" "\r\n"
"Host: $F" "\r\n"
"Connection: close" "\r\n"
"X-THINGSPEAKAPIKEY: $F" "\r\n"
"Content-Type: application/x-www-form-urlencoded" "\r\n"
"Content-Length: $D" "\r\n"
"\r\n"
"$H"),
website, PSTR(APIKEY), stash.size(), sd);
// send the packet - this also releases all stash buffers once done
ether.tcpSend();
}
Jak rozbudować program (sekcję void loop), żeby czytał 2 czujkę i wrzucał do field2, co i gdzie skopiować?
Z góry dzięki z podpowiedź.
RE: Obsługa 2xDS18B20 i wysyłka na Thingspeak - namok - 12-02-2017
Przeszukaj forum, temat był poruszany.
RE: Obsługa 2xDS18B20 i wysyłka na Thingspeak - zamber - 12-02-2017
Jest post "thingspeak - przesyłanie 2 wartości na raz", ale kod w nim opiera się na bibliotece ThingSpeak.h i komendach ThingSpeak.setField, a w tym jest to rozwiązane troszkę inaczej. No nic, będę kombinował, ale gdyby ktoś pomógł naprowadzić.
RE: Obsługa 2xDS18B20 i wysyłka na Thingspeak - namok - 12-02-2017
(12-02-2017, 14:20)zamber napisał(a): Jest post "thingspeak - przesyłanie 2 wartości na raz", ale kod w nim opiera się na bibliotece ThingSpeak.h i komendach ThingSpeak.setField, a w tym jest to rozwiązane troszkę inaczej. No nic, będę kombinował, ale gdyby ktoś pomógł naprowadzić. Jedni narzekają że nie ma gotowca a inni że nie działa tak jakby chieli. Jak Wam dogodzić?
RE: Obsługa 2xDS18B20 i wysyłka na Thingspeak - zamber - 12-02-2017
No trochę trzeba pomarudzić, ale potem wziąć się w garść, poszperać w Sieci i naprawić problem
Już działa, pomógł znaczek & (end) przed field2
stash.print("field1=");
stash.println(String(sensors.getTempC(Thermometer_1),DEC));
stash.print("&field2=");
Wklejam pełen kod, może komuś się przyda.
Kod: /*
| Post temp. values from a DS18B20 to ThingSpeak using the Ethercard interface based on the
| ENC28J60 chip.
| Based on the Ethercard example from www.jeelabs.org
| Phil Grant Jan 2014
*/
#include <EtherCard.h>
// change these settings to match your own setup
#define APIKEY "xxx" // zamiast xxx wpisac API KEY
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h> //DS18B20 support
// =========== Setup the Temperature sensor details =================
#define ONE_WIRE_BUS 7 // DS18B20 Temperature chip i/o on pin D7
#define TEMPERATURE_PRECISION 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Insert the ID of your temp sensor here, for the sketch, visit here
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
DeviceAddress Thermometer_1 = {0x28, 0xEE, 0x1B, 0xAC, 0x21, 0x16, 0x2, 0x56 }; // adres 1 czujki
DeviceAddress Thermometer_2 = {0x28, 0xC7, 0x3F, 0xE7, 0x2, 0x0, 0x0, 0x6F }; // adres 2 czujki
// ethernet interface mac address, must be unique on the LAN
byte mymac[] = { 0x54, 0x55, 0x58, 0x10, 0x00, 0x24 };
const char website[] PROGMEM = "api.thingspeak.com";
byte Ethernet::buffer[700];
uint32_t timer;
Stash stash;
void setup () {
Serial.begin(57600);
Serial.println("\n[webClient]");
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println( "Failed to access Ethernet controller");
if (!ether.dhcpSetup())
Serial.println("DHCP failed");
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
if (!ether.dnsLookup(website))
Serial.println("DNS failed");
ether.printIp("SRV: ", ether.hisip);
}
void loop () {
ether.packetLoop(ether.packetReceive());
if (millis() > timer) {
timer = millis() + 60000; //Transmit every minute
sensors.requestTemperatures();
delay(200);
// generate two fake values as payload - by using a separate stash,
// we can determine the size of the generated message ahead of time
byte sd = stash.create();
stash.print("field1=");
stash.println(String(sensors.getTempC(Thermometer_1),DEC));
stash.print("&field2=");
stash.println(String(sensors.getTempC(Thermometer_2),DEC));
stash.save();
// generate the header with payload - note that the stash size is used,
// and that a "stash descriptor" is passed in as argument using "$H"
Stash::prepare(PSTR("POST /update HTTP/1.1" "\r\n"
"Host: $F" "\r\n"
"Connection: close" "\r\n"
"X-THINGSPEAKAPIKEY: $F" "\r\n"
"Content-Type: application/x-www-form-urlencoded" "\r\n"
"Content-Length: $D" "\r\n"
"\r\n"
"$H"),
website, PSTR(APIKEY), stash.size(), sd);
// send the packet - this also releases all stash buffers once done
ether.tcpSend();
}
}
|