Guest User

Untitled

a guest
May 10th, 2018
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. /*
  2.  * V_TEXT example with Domoticz (c) 2018 GizMoCuz
  3.  *
  4.  * Note this is some quick code to demonstrate the workings
  5.  *
  6.  * When the node has started and correctly been found by the controller,
  7.  * you should have two new text sensors in Domoticz (if not, enable accept new hardware!)
  8.  * Those two sensors have an IDX, for example 1063 and 1063
  9.  * To update the first line call something line http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=1063&nvalue=0&svalue=Hello%20Line%201
  10.  */
  11.  
  12. // Enable debug prints to serial monitor
  13. //#define MY_DEBUG
  14.  
  15. // Enable and select radio type attached
  16. #define MY_RADIO_NRF24
  17. //#define MY_RADIO_NRF5_ESB
  18. //#define MY_RADIO_RFM69
  19. //#define MY_RADIO_RFM95
  20.  
  21. #define MY_NODE_ID 253
  22.  
  23. #include <SPI.h>
  24. #include <MySensors.h>
  25. #include <LiquidCrystal_I2C.h>
  26. #include <Wire.h>
  27.  
  28. #define UV_SENSOR_ANALOG_PIN 0
  29.  
  30.  
  31. const byte LCD1_CHILD_ID = 8 ;         // LCD line 1
  32. const byte LCD2_CHILD_ID = 9 ;         // LCD line 2
  33.  
  34. char lastLCD1[21] = "Line1 - first";   // define & init before first receive
  35. char lastLCD2[21] = "Line2 - second";
  36.  
  37. uint32_t ActTime = 0;
  38. // timers for loop delays
  39. unsigned long lastUpdate=0, lastRequest=0;
  40.  
  41. // Initialize messages for sensor network
  42. MyMessage textMsg(0, V_TEXT);
  43.  
  44. #define I2C_ADDR 0x27
  45. //---(Following are the PCF8574 pin assignments to LCD connections )----
  46. // This are different than earlier/different I2C LCD displays
  47. #define LCD_WIDTH 20
  48. #define LCD_HEIGHT 4
  49.  
  50. LiquidCrystal_I2C lcd(I2C_ADDR, LCD_WIDTH, LCD_HEIGHT);
  51.  
  52. void setup(void){
  53.   // ** LCD display **
  54.   Wire.begin();                   // I2C
  55.   lcd.begin();                   // LCD 2 lines * 16 char.
  56.   lcd.setBacklight(HIGH);
  57.   lcd.setCursor(0, 0);
  58.  
  59.   Serial.begin(115200);
  60.   Serial.println("Sensor Started");
  61. }
  62.  
  63. void presentation()
  64. {
  65.     // Send the sketch version information to the gateway and Controller
  66.     sendSketchInfo("V_TEXT LCD Sensor", "1.1");
  67.  
  68.   present(LCD1_CHILD_ID, S_INFO, "Text Line 1");
  69.   present(LCD2_CHILD_ID, S_INFO, "Text Line 2");
  70.  
  71.   // Initializations
  72.   requestTime();            // get the time from controller (handled by receiveTime)
  73.  
  74.   request(LCD1_CHILD_ID, V_TEXT, 0);           // request latest value from controller
  75.   request(LCD2_CHILD_ID, V_TEXT, 0);          // request latest value from controller
  76. }
  77.  
  78. void loop()
  79. {
  80.   // timer for loop delays
  81.   unsigned long now = millis();
  82.  
  83.   // request time every 10 seconds from controller
  84.   if (now-lastRequest > 10*1000)
  85.   {
  86.     requestTime();  
  87.     lastRequest = now;
  88.   }
  89.   // Update display every second
  90.   if (now-lastUpdate > 1000) {
  91.     LCD_local_display();
  92.     lastUpdate = now;
  93.   }
  94. }
  95.  
  96. // This is called when a new time value was received
  97. void receiveTime(uint32_t controllerTime)
  98. {
  99.   Serial.print("Time value received: ");
  100.   Serial.println(controllerTime);
  101.  
  102.   ActTime = controllerTime;
  103. }
  104.  
  105. void LCD_local_display()
  106. {
  107.   // take care of LCD display information
  108.  
  109.   char buf[LCD_WIDTH]; // temp buffer for max LCD_WIDTH+1 char display
  110.  
  111.   if (ActTime!=0) {
  112.     // start with location & time on first line
  113.     //snprintf(buf, sizeof buf, "%02d:%02d:%02d %02d-%02d", hour(ActTime), minute(), second(), day(), month());
  114.     snprintf(buf, sizeof buf, "%ld,%02d:%02d:%02d %02d-%02d", ActTime, 16, 32, 59, 10, 5);
  115.     lcd.setCursor(0, 0);
  116.     lcd.print(buf);
  117.   }
  118.   lcd.setCursor(0, 2);
  119.   lcd.print(lastLCD1);               // second line is text value from controller
  120.   lcd.setCursor(0, 3);
  121.   lcd.print(lastLCD2);
  122. }
  123.  
  124. void receive(const MyMessage &message)
  125. {
  126.   if (message.type!=V_TEXT) // Text messages only
  127.     return;
  128.  
  129.   // Write some debug info
  130.   Serial.print("Message: ");
  131.   Serial.print(message.sensor);
  132.   Serial.print(", Message: ");
  133.   Serial.println(message.getString());
  134.  
  135.   if (message.sensor == LCD1_CHILD_ID ) {
  136.     snprintf(lastLCD1, sizeof(lastLCD1), "%20s", message.getString());  // load text into LCD string
  137.   }
  138.   else if (message.sensor == LCD2_CHILD_ID) {
  139.     snprintf(lastLCD2, sizeof(lastLCD2), "%20s", message.getString());
  140.   }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment