Dormant Lab pH module with Spark core

I am pretty new to spark/arduino. I am having difficulty connecting a dormant lab pH module with spark core. This is the hardware I am using:

I was able to connect and use it successfully with an arduino board as per developer’s tutorial: http://rezaalihussain.blogspot.com/2014/07/measuring-ph-with-dormant-labs-ph.html

However, when I try to connect it to my spark core, I can’t get it to work. Can anyone help me in modifying the code so that it works with Spark. The original arduino code uses serial monitor. I know spark cloud doesn’t have a serial monitor. What are my options in this case? I tried using ubidot so that instead of serial print, the spark core would http send the ph values to ubidot. But I can’t get it to work. The code doesn’t give any compilation error, but it doesn’t send any value to ubidot. Here’s my setup:

GND and VCC of pH module connected to GND and 3.3V on Spark core.
SDA and SCL of pH module connected to D0 and D1 on Spark core.

Here’s the code I am running through the Spark cloud API:

// This #include statement was automatically added by the Spark IDE.
#include "HttpClient/HttpClient.h"

/**
* Declaring the variables.
*/
HttpClient http;
#define VARIABLE_ID "--hidden--"
#define TOKEN "--hidden--"
int lightLevel = 0;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
      { "Content-Type", "application/json" },
      { "X-Auth-Token" , TOKEN },
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};

http_request_t request;
http_response_t response;

#define PHADDRESS 0x4D

float volt7 = 0.6939;
float volt10= 0.3846;

void setup()
{
    request.hostname = "things.ubidots.com";
    request.port = 80;
}

void loop()
{
  Wire.begin(); //connects I2C
  Serial.begin(9600);

  int sampleSize = 500;
  
  float avgMeasuredPH = 0;
  float avgPHVolts = 0;

  int x;
  for(x=0;x< sampleSize;x++)
  {
  
  float phVolt = getPHVolts();    
  float voltsPerPH = abs(volt10-volt7);
  
  float realPHVolt = (volt7 - phVolt);
  float phUnits = realPHVolt / voltsPerPH;
  float measuredPH = 7 + phUnits;

  avgMeasuredPH+=measuredPH;
  avgPHVolts += phVolt;
    
  
  }
  
  avgMeasuredPH/=sampleSize;
  avgPHVolts/=sampleSize;
    
    request.path = "/api/v1.6/variables/"VARIABLE_ID"/values";
    request.body = "{\"value\":" + String(avgMeasuredPH) + "}";

    http.post(request, response, headers);

  delay(6000000);  
  
}


float getPHVolts()
{
  byte ad_high;
  byte ad_low;
  
  Wire.requestFrom(PHADDRESS, 2);        //requests 2 bytes
  while(Wire.available() < 2);         //while two bytes to receive
    
  ad_high = Wire.read();           
  ad_low = Wire.read();
  float units = (ad_high * 256) + ad_low;
  
  float volts =  (units /4096)*3; 
  return volts;  
}

Any help will be sincerely appreciated.

Does it work without the ubidots/http part? Although the Spark Cloud doesn’t have a serial monitor, you can still use that when you connect your Spark device over USB with your computer. It’d be useful if you could try that, so we know whether or not the code itself is working properly. After that, we can get to the “get it on the web”-part.
Also, I noticed a 6000s (1:40h) delay, which certainly won’t help in the debugging/development phase. Is it required?

@furam28, the Wire.begin() should only be made once in setup() instead of in loop(). You may want to comment out the httpClient code and use the serial monitor output (using Serial.print()) to see if the pH module is working first. One thing to check is that pull-up resistors exist on the board, otherwise 4.7K ohm resistors are fine. How are you powering the pH board? Once the I2C stuff is working, the rest will be much easier. :smile:

2 Likes

Thanks for the reply guys. Sorry I was not getting email notifications from Spark Community. I tried your suggestions: Connected Spark core to computer via usb. First its in listening mode (blinking blue). I went through “spark setup” in cmd and it started breathing cyan. I was able to do spark list, and spark flash tinker successfully. After that, as soon as I tried doing a spark flash ph.ino it started flashing, and then as soon as it finished flashing and started breathing cyan, the wierdest thing happened. My computer keyboard mousepad stopped being responsive! As soon as I unplugged spark from usb they came back again! Here’s the code in ph.ino:

    #define PHADDRESS 0x4D
    
    float volt7 = 0.6939;
    float volt10= 0.3846;
    
    void setup()
    {
        Wire.begin(); //connects I2C
        Serial.begin(9600);
    }
    
    void loop()
    {
    
      int sampleSize = 500;
      
      double avgMeasuredPH = 0;
      double avgPHVolts = 0;
    
      int x;
      for(x=0;x< sampleSize;x++)
      {
      
      double phVolt = getPHVolts();    
      double voltsPerPH = abs(volt10-volt7);
      
      double realPHVolt = (volt7 - phVolt);
      double phUnits = realPHVolt / voltsPerPH;
      double measuredPH = 7 + phUnits;
    
      avgMeasuredPH+=measuredPH;
      avgPHVolts += phVolt;
        
      
      }
      
      avgMeasuredPH/=sampleSize;
      avgPHVolts/=sampleSize;
      
      Serial.print("avgMeasuredPH-");
      Serial.print(avgMeasuredPH,4);
      Serial.print(" avgPhVolts-");
      Serial.print(avgPHVolts,4);
      
      Serial.print(" 7CalVolts-");
      Serial.print(volt7,4);
      Serial.print(" 10CalVolts-");
      Serial.print(volt10,4);    
    
      delay(1000);  
      
    }
    
    double getPHVolts()
    {
      byte ad_high;
      byte ad_low;
      
      Wire.requestFrom(PHADDRESS, 2);        //requests 2 bytes
      while(Wire.available() < 2);         //while two bytes to receive
        
      ad_high = Wire.read();           
      ad_low = Wire.read();
      double units = (ad_high * 256) + ad_low;
      
      double volts =  (units /4096)*3; 
      return volts;  
    }

I master reset the spark core, and connected again. I was able to flash tinker again, but every time I try to flash with ph.ino it does the same thing. So its definitely a problem with the code. Thanks in advance for the helps.

You should take a look at the following topic for the whole “my mouse is going crazy” thing: https://community.spark.io/t/issues-using-serial-debugging-mouse-moving/1200/8


I noticed that you’ve reduced the delay as I suggested. I’m not sure, but it could be that the sensor requires a longer time to make a measurement. The DTH22 for example needs 2 seconds. Having no familiarity with pH sensors, I’d say set the delay to around 10s, just to be sure. That should be fast enough to test, but not too fast to measure.

1 Like

@Moors7. Thanks a lot man. Disabling serial mouse totally fixed the issue. I also changed the delay to 10sec and I am getting nice output on my serial monitor. I will try to see if I can get http send (to ubidots or Google doc) working now. I’ll post updates here. Thanks.

1 Like

hi @furam28. your PH peoject is working now?