sketch_jul29a.ino 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <DS18B20.h>
  2. #include <UIPEthernet.h>
  3. byte mac[] = { 0xDE, 0x05, 0xB6, 0x27, 0x39, 0x19 }; // random MAC
  4. IPAddress ip(172, 16, 0, 70);
  5. IPAddress dnServer(172, 16, 0, 11);
  6. IPAddress gateway(172, 16, 0, 1);
  7. IPAddress subnet(255, 255, 255, 128);
  8. DS18B20 ds(4); // DS18B20 at pin 4
  9. EthernetServer server(10050); // Zabbix port
  10. EthernetClient client;
  11. void setup()
  12. {
  13. Serial.begin(9600);
  14. Ethernet.begin(mac, ip, gateway, subnet);
  15. server.begin();
  16. }
  17. void loop()
  18. {
  19. // if an incoming client connects, there will be bytes available to read:
  20. EthernetClient client = server.available();
  21. if (client == true) {
  22. //Serial.println(ds.getTempC());
  23. String c = client.readString();
  24. client.print("ZBXD\x01");
  25. if(c.endsWith("agent.ping") == 1){
  26. byte responseBytes [] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, '1'};
  27. client.write(responseBytes, 9);
  28. }else if(c.endsWith("env.temp") == 1){
  29. byte responseBytes [] = {(byte) String(ds.getTempC()).length(), 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  30. client.write(responseBytes, 8);
  31. client.print(ds.getTempC());
  32. Serial.println(ds.getTempC());
  33. }else {
  34. byte responseBytes [] = {0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  35. client.write(responseBytes, 8);
  36. client.print("ZBX_NOTSUPPORTED");
  37. }
  38. }
  39. delay(10);
  40. }