• header.categories
    • header.recent
    • header.tags
    • header.popular
    • register
    • login

    Unique index or primary key violation: "CONSTRAINT_4B_INDEX_4 ON PUBLIC.METRICS_DOUBLE_TYPE_DEVICE

    scheduled pinned locked moved Troubleshooting
    12 posts 2 posters 1.8k views 1 watching
    loading-more-posts
    • oldest-to-newest
    • newest-to-oldest
    • most-votes
    reply
    • reply-as-topic
    guest-login-reply
    deleted-message
    • jkandasaJ offline
      jkandasa @Daniele
      global:last-edited-by,

      @Daniele How often are you sending the temperature value from your node? Looks like receive multiple messages in a single milliseconds?

      one-reply-to-this-post last-reply-time reply quote 0
      • D offline
        Daniele
        global:last-edited-by,

        As a max once per second, but usually every some minute

        one-reply-to-this-post last-reply-time reply quote 0
        • D offline
          Daniele
          global:last-edited-by,

          By the way, looking at the previous errors I can see many different sensor ids, so it seems like something general, not related to a specific sensor.

          one-reply-to-this-post last-reply-time reply quote 0
          • D offline
            Daniele
            global:last-edited-by,

            This morning I was checking the resource logs , and I noticed some odd behaviour I cannot explain:

            aae78055-695c-4606-b1f2-ca855e29ff39-image.png

            The same temperature is transmitted twice at the same timestamp, but my sketch reads the temperature once per second and sends it only when the absolute difference from the previous value is greater than 0.1°C.
            This setup is in place from more than 1 year, and I never saw this beahaviour...
            Any idea?

            jkandasaJ one-reply-to-this-post last-reply-time reply quote 0
            • jkandasaJ offline
              jkandasa @Daniele
              global:last-edited-by,

              @Daniele Can you post your sketch?

              one-reply-to-this-post last-reply-time reply quote 0
              • D offline
                Daniele
                global:last-edited-by,

                /*
                 * MySBootloader 1.3.0 16 MHz
                 * 
                 * 9-13 NRF24 (standard mysensors)
                 * 1 PIR
                 * 2 temperatura (DS18B20)
                 */
                
                #define MY_DEBUG
                #define MY_DEBUG_DETAIL
                
                #define MY_NODE_ID 3
                
                #define CHILD_ID_PIR 1
                #define CHILD_ID_TEMP 2
                
                #define pirPin 8
                #define TempPin 2
                
                #include <MyRF24_P0_GW2.h>
                #include <MySensors.h>
                
                #include <MyCircularBuffer.h>
                #include <OneWire.h>
                #include <DallasTemperature.h>
                
                #define wdt_check_min 10;
                #define wdt_check_max 150;
                #include <MyWDT.h>
                
                OneWire oneWire(TempPin);
                DallasTemperature sensors(&oneWire);
                DeviceAddress ds18Addr;
                
                MyMessage msgPIR(CHILD_ID_PIR, V_TRIPPED);
                MyMessage msgTEMP(CHILD_ID_TEMP, V_TEMP);
                
                int nNoUpdatesPIR = 0, nNoUpdatesTEMP = 0;
                unsigned const int FORCE_UPDATE_N_READS = 300;
                
                
                
                
                unsigned int calibrationTime = 10;
                unsigned int p;
                unsigned int p_tot;
                
                float temp, last_temp = 0, tempTot = 0;
                
                unsigned int msg;
                unsigned int last_msg = 9; //per forzare primo invio
                
                unsigned const int loop_cycles = 60;
                unsigned const int loop_wait = 1; //secondi tra ogni rilevazione del PIR
                
                /*
                unsigned int wdt_failures = 0;
                unsigned int wdt_check_interval = 30;
                unsigned int wdt_loops = 0;
                unsigned int wdt_uplink = 0;
                */
                
                MyCircularBuffer<byte,loop_cycles> pirBuffer;
                MyCircularBuffer<float,10> TempBuffer;
                //CircularBuffer<byte,3> wdtBuffer;
                
                
                void setup() {
                
                  wdt_disable();
                  
                  pinMode(pirPin, INPUT);
                  digitalWrite(pirPin, LOW);
                
                  //Fase di calibrazione
                  Serial.print("Calibrating sensor ");
                  for(int i = 0; i < calibrationTime; i++){
                    Serial.print(".");
                    p = digitalRead(pirPin);
                    pirBuffer.push(p);
                    delay(1000);
                  }
                  Serial.println(" done");
                  Serial.println("SENSOR ACTIVE");
                  delay(50);
                
                  sensors.begin();
                  if (!sensors.getAddress(ds18Addr, 0)) 
                    Serial.println("Unable to find address for Device 0"); 
                
                  sensors.setResolution(ds18Addr, 11);
                
                  wdt_enable(WDTO_4S);
                
                }
                
                
                
                void presentation()
                {
                  // Send the sketch version information to the gateway and Controller
                  sendSketchInfo("P0 - PIR Studio", "1.1");
                
                  present(CHILD_ID_PIR, S_MOTION, "P0 PIR Studio");
                  present(CHILD_ID_TEMP, S_TEMP, "P0 Temperatura Studio");
                }
                
                
                
                
                void loop() {
                
                  p = digitalRead(pirPin);
                  pirBuffer.push(p);
                
                  p_tot = 0;
                  for(int i=0; i<pirBuffer.size(); i++){
                    p_tot += pirBuffer[i];  
                  }
                
                  if(p_tot > loop_cycles/4){
                    msg = 1;
                  }
                  else {
                    msg = 0;
                  }
                
                
                  if (msg != last_msg || nNoUpdatesPIR == FORCE_UPDATE_N_READS) {
                    last_msg = msg;
                    nNoUpdatesPIR = 0;
                    send(msgPIR.set(msg), true);
                
                    #ifdef MY_DEBUG
                    Serial.print("P: ");
                    Serial.println(msg);
                    #endif
                  } else {
                    nNoUpdatesPIR++;
                  }
                
                
                  //check external temp
                  sensors.requestTemperatures();
                  wait(1000);
                  //temp = sensors.getTempCByIndex(0);
                  temp = sensors.getTempC(ds18Addr);
                  if(temp != DEVICE_DISCONNECTED_C)
                    TempBuffer.push(temp);
                
                
                
                  tempTot = 0;
                  for(int i=0; i<TempBuffer.size(); i++){
                    tempTot += TempBuffer[i];  
                  }
                  tempTot = tempTot / TempBuffer.size();
                
                  if (abs(tempTot - last_temp) > 0.1 || nNoUpdatesTEMP == FORCE_UPDATE_N_READS) {
                    last_temp = tempTot;
                    nNoUpdatesTEMP = 0;
                    send(msgTEMP.set(tempTot,1), true);
                
                    #ifdef MY_DEBUG
                    Serial.print("T avg: ");
                    Serial.println(tempTot);
                    #endif
                  } else {
                    nNoUpdatesTEMP++;
                  }
                
                
                  #ifdef MY_DEBUG_DETAIL
                  Serial.print("T: ");
                  Serial.print(temp);
                  Serial.print(" - P:");
                  Serial.println(p);
                  #endif
                
                  wait(loop_wait * 1000);
                
                
                  //wdt uplink check
                  wdt_check();
                
                }
                
                jkandasaJ one-reply-to-this-post last-reply-time reply quote 0
                • jkandasaJ offline
                  jkandasa @Daniele
                  global:last-edited-by,

                  @Daniele Hi, Sorry for the delayed response.
                  Your sketch looks ok. I do not see any issue to repeat a message

                  Possible causes:

                  • Your gateway response might not reach easily to your temperature node, hence your temperature node keeps repeating the same message and gateway sends to MyController.
                  • Restarting MyController operating system might fix this
                  one-reply-to-this-post last-reply-time reply quote 0
                  • D offline
                    Daniele
                    global:last-edited-by,

                    @jkandasa it's probably related to some gateway issue, probably connected to the issue I asked here

                    I made a hard restart of the gateway and now the error disappeared.
                    Thank you!

                    one-reply-to-this-post last-reply-time reply quote 1
                    • D offline
                      Daniele
                      global:last-edited-by,

                      @jkandasa sorry to re-open an hold discussion.
                      the same error appeared again, and this time the gateway hard reset did not solve it.
                      do you have any idea on how to better isolate the issue?
                      thanks
                      Daniele

                      jkandasaJ one-reply-to-this-post last-reply-time reply quote 0
                      • jkandasaJ offline
                        jkandasa @Daniele
                        global:last-edited-by,

                        @Daniele This error happens when we receive multiple messages in the same timeframe. The first one will be inserted into the database and other messages will be dropped with this error.
                        If you do not see impact on your data, just ignore [or] find a duplication situation and avoid it.

                        one-reply-to-this-post last-reply-time reply quote 0
                        • D offline
                          Daniele
                          global:last-edited-by,

                          If others will arrive here searching for a similar issue: in my case the final solution has been changing the Arduino Uno I was using as ethernet gateway.

                          one-reply-to-this-post last-reply-time reply quote 0
                          • first-post
                            last-post

                          0

                          online

                          644

                          users

                          532

                          topics

                          3.4k

                          posts
                          Copyright © 2015-2025 MyController.org | Contributors | Localization