fork download
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <PubSubClient.h>
  4.  
  5. // Update these with values suitable for your network.
  6. byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
  7. byte ip[] = { 192, 168, 100, 2 };
  8. byte dns[] = {8,8,8,8};
  9. byte gateway[] = { 192, 168, 100, 1 };
  10.  
  11. const int analogInPin = A0;
  12. int sensorValue = 0; // value read from the pot
  13. int sendMQTT = 0;
  14. bool conn_ok;
  15. char message_buff[200];
  16. char* geeknesia_deviceid = "DEVICE-ID";
  17. char* geeknesia_credential = "USERNAME:PASSWORD";
  18.  
  19. EthernetClient ethClient;
  20. void callback(char* topic, byte* payload, unsigned int length) {}
  21. PubSubClient client("geeknesia.com", 1883, callback, ethClient); //Address geeknesia
  22.  
  23. void setup() {
  24. // reset network
  25. system("ifdown eth0");
  26. system("ifup eth0");
  27.  
  28. // wait for accessing sensor
  29. delay(2000);
  30. Serial.begin(9600);
  31.  
  32. // Koneksi DHCP
  33. if (Ethernet.begin(mac) != 1) {
  34. // Jika gagal, maka koneksi manual
  35. Ethernet.begin(mac, ip, dns, gateway);
  36. }
  37.  
  38. Serial.print("Running on: ");
  39. Serial.println(Ethernet.localIP());
  40.  
  41. Serial.println("connecting...");
  42. while (client.connect(geeknesia_deviceid,"iot/live",0,0,geeknesia_deviceid) != 1)
  43. {
  44. Serial.println("Error connecting to MQTT");
  45. delay(2000);
  46. }
  47. }
  48.  
  49. void senddata(char* topic,char* credential, char *var1, int nilai1 ){
  50. String pubString = "{\"code\":\"";
  51. pubString += credential;
  52. pubString +="\",";
  53. pubString +="\"attributes\":{\"";
  54. pubString +=String(var1)+"\":\""+nilai1+"\"";
  55. pubString +="}}";
  56. char message_buff[pubString.length()+1];
  57. pubString.toCharArray(message_buff, pubString.length()+1);
  58. Serial.println(message_buff);
  59. client.publish("iot/data",message_buff);
  60. }
  61.  
  62. void loop() {
  63. if (!client.connected()){
  64. client.connect(geeknesia_deviceid,NULL,NULL,"iot/live",2,64,geeknesia_deviceid);
  65. }
  66. // read the analog in value:
  67. sensorValue = analogRead(analogInPin);
  68.  
  69. // Sending data to Geeknesia
  70. senddata(geeknesia_deviceid, geeknesia_credential,"Analog",sensorValue);
  71.  
  72. // print the results to the serial monitor:
  73. Serial.print("sensor = ");
  74. Serial.print(sensorValue);
  75.  
  76. if(sendMQTT == 1){
  77. conn_ok = client.connected();
  78. }
  79. if (conn_ok == 0){ //no connection, reconnect
  80. client.disconnect();
  81. }
  82.  
  83. // wait 2 milliseconds before the next loop
  84. // for the analog-to-digital converter to settle
  85. // after the last reading:
  86. delay(2);
  87. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:10: fatal error: 'SPI.h' file not found
#include <SPI.h>
         ^
1 error generated.
stdout
Standard output is empty