Tak starej wersji oprogramowania używam ponieważ na nowszej nie chciały mi działać niektóre rzeczy.
Powiedzmy taki kod.
Kod:
#include <EtherCard.h>
#include <OneWire.h>
int DS18S20_Pin = 5;
OneWire ds(DS18S20_Pin);
static uint8_t mymac[6] = {
0x54, 0x55, 0x58, 0x10, 0x00, 0x24};
static uint8_t ip[4] = {
192, 168, 0, 185};
static uint16_t port = 80;
byte Ethernet::buffer[700];
static uint32_t timer;
const char website[] PROGMEM = "api.thingspeak.com";
// called when the client request is complete
static void my_callback (byte status, word off, word len) {
Serial.println(">>>");
Ethernet::buffer[off+300] = 0;
Serial.print((const char*) Ethernet::buffer + off);
Serial.println("...");
}
void setup () {
Serial.begin(57600);
Serial.println(F("\n[webClient]"));
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println(F("Failed to access Ethernet controller"));
if (!ether.dhcpSetup())
Serial.println(F("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 () {
float temperature = getTemp();
ether.packetLoop(ether.packetReceive());
if (millis() > timer) {
timer = millis() + 5000;
Serial.println();
Serial.print("<<< REQ ");
Serial.print(temperature);
int myInt = temperature;
char myIntAsString[7];
itoa(myInt, myIntAsString, 10);
ether.browseUrl(PSTR("/update?key=1234678&field1="), myIntAsString, website, my_callback);
}
}
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 5 ) != addr[5]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
Powiedzmy taki kod.