My server dont get my json command

Hi

I want to send the output from my DS18B20 device . I see the temp on the serial port but the
my server don’t get it .

This is my code please help TNX :slight_smile:

#include <DallasTemperature.h>
#include <OneWire.h>
#include <Wire.h>
#define DS18S20_ID 0x10
#define DS18B20_ID 0x28

#define HTTP_TIMEOUT 10000
#define ONE_WIRE_BUS 8

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

MilliTimer tReply;
static boolean httpHaveReply;
//---------------------------------------------------------------------
// The PacketBuffer class is used to generate the json string that is send via ethernet - JeeLabs
//---------------------------------------------------------------------
class PacketBuffer :
public Print {
public:
PacketBuffer () :
fill (0) {
}
const char* buffer() {
return buf;
}
byte length() {
return fill;
}
void reset()
{
memset(buf,NULL,sizeof(buf));
fill = 0;
}
virtual size_t write (uint8_t ch)
{
if (fill < sizeof buf) buf[fill++] = ch;
}
byte fill;
char buf[150]; // Might need to be set higher if long strings need to be sent (Be carefull to stay within ram limits)
private:
};
PacketBuffer str;

// ethernet interface mac address, must be unique on the LAN
static byte mymac = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[700];
unsigned long timer;

char website PROGMEM = “poolmanager.cloudapp.net”;

void setup ()
{
Serial.begin(9600);
// Serial.println(“03 - Basic Web Client”);

if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println( “Failed to access Ethernet controller”);

// Set to true and enter IP to give the NanodeRF a static IP address - default is DHCP (NB: must be used with static server IP (hisip)

// DHCP Setup
if (!ether.dhcpSetup())
//Serial.println(“DHCP failed”);

ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);

// DNS Setup
if (!ether.dnsLookup(website))
// Serial.println(“DNS failed”);

ether.printIp("SRV: ", ether.hisip);

}

int DS18B20_Pin = 8; //DS18S20 Signal pin on digital 2

//Temperature chip i/o
OneWire ds(DS18B20_Pin); // on digital pin 8

void loop () {

ether.packetLoop(ether.packetReceive());

int error=0;
float temperature = getTemp();

byte b1 = temperature; // Get the integer part (678).
float e4 = temperature - b1; // Get fractional part (678.0123 - 678 = 0.0123).
byte b2 = trunc(e4 * 1000); // Turn into integer (123).

str.reset();
str.print("{Temp:");
str.print(b1);
str.print(".");
str.print(b2);
str.print("}");

Serial.println(“Request sent TEMP”);
Serial.println( str.buf);
// Send some test data to the server:
ether.browseUrl(PSTR("/input/post.json?apikey=9d08ab824d1ee7248e82ef2d00454b8b&json="),str.buf ,website, 0);
// Wait for reply
tReply.set(HTTP_TIMEOUT);
while (!httpHaveReply) {
ether.packetLoop(ether.packetReceive());
if (tReply.poll()) {
error=1; // network timeout
break;
}
}

}

float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius

byte data[12];
byte addr[8];

if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}

if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println(“CRC is not valid!”);
return -1000;
}

if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print(“Device is not recognized”);
return -1000;
}

ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end

delay(1000);

byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

for (int i = 0; i < 9; i++) { // we need 9 bytes
data = ds.read();

  • }*

  • Serial.println(data[1]);*

  • Serial.println(data[0]);*

  • ds.reset_search();*

  • byte MSB = data[1];*

  • byte LSB = data[0];*

  • float tempRead = ((MSB << 8) | LSB); //using two’s compliment*

  • float TemperatureSum = tempRead / 16;*

  • return TemperatureSum;*

}

you see the smiley ?

that's why we ask for [code]...[/code] tags around source code

what do you want to send over the line?
what is actually send over the line?
do you have other sketches for which your server does get it?

robtillaart:
you see the smiley ?

that's why we ask for [code]...[/code] tags around source code

And the italics (instead of [i] )

But I notice this is using poolmanager.cloudapp.net So it's going to be anyone's guess what format that particular site expects.

I want to send {TEMP:"theTemperature"} .

eitans:
I want to send {TEMP:“theTemperature”} .

Edit your first post.

Just before “#include <DallasTemperature.h>” ADD [code]  Then at the end of your post add [/code]

Nobody is going to be interested in trying to pick the bones out of until you do it.

Sorry this is my first post in this forum

#include <DallasTemperature.h>
#include <OneWire.h>
#include <Wire.h>
#define DS18S20_ID 0x10
#define DS18B20_ID 0x28

#define HTTP_TIMEOUT 10000
#define ONE_WIRE_BUS 8

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);



MilliTimer tReply;
static boolean httpHaveReply;   
//---------------------------------------------------------------------
// The PacketBuffer class is used to generate the json string that is send via ethernet - JeeLabs
//---------------------------------------------------------------------
class PacketBuffer : 
public Print {
public:
  PacketBuffer () : 
  fill (0) {
  }
  const char* buffer() { 
    return buf; 
  }
  byte length() { 
    return fill; 
  }
  void reset()
  { 
    memset(buf,NULL,sizeof(buf));
    fill = 0; 
  }
  virtual size_t write (uint8_t ch)
  { 
    if (fill < sizeof buf) buf[fill++] = ch; 
  }
  byte fill;
  char buf[150];      // Might need to be set higher if long strings need to be sent (Be carefull to stay within ram limits)
private:
};
PacketBuffer str;


// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[700];
unsigned long timer;



char website[] PROGMEM = "poolmanager.cloudapp.net";

void setup () 
{
  Serial.begin(9600);
//  Serial.println("03 - Basic Web Client");

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
    Serial.println( "Failed to access Ethernet controller");
    
    // Set to true and enter IP to give the NanodeRF a static IP address - default is DHCP (NB: must be used with static server IP (hisip)

  // DHCP Setup
 if (!ether.dhcpSetup())
    //Serial.println("DHCP failed");

 ether.printIp("IP:  ", ether.myip);
 ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);  

  // DNS Setup
  if (!ether.dnsLookup(website))
   // Serial.println("DNS failed");
    
  ether.printIp("SRV: ", ether.hisip);

}

int DS18B20_Pin = 8; //DS18S20 Signal pin on digital 2

//Temperature chip i/o
OneWire ds(DS18B20_Pin);  // on digital pin 8

void loop () {

 ether.packetLoop(ether.packetReceive());
  
  int error=0;
float  temperature = getTemp();

byte b1 = temperature;   // Get the integer part (678).
float e4 = temperature - b1;     // Get fractional part (678.0123 - 678 = 0.0123).
byte b2 = trunc(e4 * 1000);   // Turn into integer (123).
  
    str.reset();      
    str.print("{Temp:");
    str.print(b1);
    str.print(".");
    str.print(b2);
    str.print("}");
      
   
    Serial.println("Request sent TEMP");
       Serial.println( str.buf);
// Send some test data to the server:
    ether.browseUrl(PSTR("/input/post.json?apikey=9d08ab824d1ee7248e82ef2d00454b8b&json="),str.buf ,website, 0);
       // Wait for reply
    tReply.set(HTTP_TIMEOUT);
    while (!httpHaveReply) {
      ether.packetLoop(ether.packetReceive());
      if (tReply.poll()) {
        error=1;        // network timeout
        break;
      }
    }
 

}

float getTemp(){
  //returns the temperature from one DS18S20 in DEG Celsius

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
      //no more sensors on chain, reset search
      ds.reset_search();
      return -1000;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return -1000;
  }

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
      Serial.print("Device is not recognized");
      return -1000;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end
  
  delay(1000);     


 byte  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE); // Read Scratchpad

  
  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data = ds.read();
  }
  
  
  Serial.println(data[1]);
  Serial.println(data[0]);
  ds.reset_search();
  
  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;
  
   
  return TemperatureSum;
  
}

Your class code arrangement is awful. Typically, the class that your derives from is on the same line as your class:

class PacketBuffer : public Print

That way, at a glance you can see the relationship between the classes.

The curly brace belongs on the next line.

When you have a list of things that need to be done to initialize class members when the constructor is called, they go on the same line as the constructor name:

  PacketBuffer () :   fill (0)
  {
  }

The curly brace does NOT go on the same line.

The whole class appears to be stuffing binary data into a string, so it seems pointless.

sprintf() would do the same thing, without hiding details.

have you verified you cann connect to an external server? If not, below is some client test code you can try to see if you possibly have lan/network issues.

//zoomkat 3-1-13
//simple client checkip test
//for use with IDE 1.0.1 or later
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "checkip.dyndns.com"; // test web page server
EthernetClient client;

//////////////////////

void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600); 
  Serial.println("Better client ip test 3/1/13"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET / HTTP/1.0"); //download text
    client.println("Host: checkip.dyndns.com");
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

TNX for your help .

I change the code but still the json command don’t send the string .
when Im sending string that the board getting from analog port everything works .

There is no IP issue the board connection working properly.

this is the new code :

#include <EtherCard.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <Wire.h>
#define DS18S20_ID 0x10
#define DS18B20_ID 0x28
#include <JeeLib.h>	     // https://github.com/jcw/jeelib
#include <avr/wdt.h>
#define DEBUG     //comment out to disable serial printing to increase long term stability 
#define UNO       //anti crash wachdog reset only works with Uno (optiboot) bootloader, comment out the line if using delianuova
#define HTTP_TIMEOUT 10000
#define ONE_WIRE_BUS 8
#include <stdio.h>

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);



MilliTimer tReply;
static boolean httpHaveReply;	

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[700];
unsigned long timer;



char website[] PROGMEM = "poolmanager.cloudapp.net";

void setup () 
{
  Serial.begin(9600);
//  Serial.println("03 - Basic Web Client");

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
    Serial.println( "Failed to access Ethernet controller");
    
    // Set to true and enter IP to give the NanodeRF a static IP address - default is DHCP (NB: must be used with static server IP (hisip)

  // DHCP Setup
 if (!ether.dhcpSetup())
    //Serial.println("DHCP failed");

 ether.printIp("IP:  ", ether.myip);
 ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);  

  // DNS Setup
  if (!ether.dnsLookup(website))
   // Serial.println("DNS failed");
    
  ether.printIp("SRV: ", ether.hisip);

}


void loop () {

 ether.packetLoop(ether.packetReceive());
  
  int error=0;
  sensors.requestTemperatures(); // Send the command to get temperatures

  //Serial.println(temp);
  delay (10000);
 float temperature = sensors.getTempCByIndex(0);
 int b1 = temperature;   // Get the integer part (678).
float e4 = temperature - b1;     // Get fractional part (678.0123 - 678 = 0.0123).
int  b2 = trunc(e4 * 100);   // Turn into integer (123).
 char buf [50];
 char n;
  sprintf (buf, "{TEMP:%d.%d}", b1, b2);
   
    Serial.println("Request sent TEMP");
       Serial.println(buf);
// Send some test data to the server:
    ether.browseUrl(PSTR("/input/post.json?apikey=9d08ab824d1ee7248e82ef2d00454b8b&json="),buf,website, 0);
       // Wait for reply
    tReply.set(HTTP_TIMEOUT);
    while (!httpHaveReply) {
      ether.packetLoop(ether.packetReceive());
      if (tReply.poll()) {
        error=1;        // network timeout
        break;
      }
    }
}

Importent input , the sensors.requestTemperatures(); and sensors.getTempCByIndex(0) causes the problem
if im disables the two functions and send basic string "{TEMP:21.21}" the json command works .

What does the output from this:

    Serial.println("Request sent TEMP");
       Serial.println(buf);

look like?

{TEMP:21.21} this is the serial.println(buf); output .

That looks like the static version. What do you get when you actually use the temperature and the sprintf?

Im getting the temperature from the sensor everything looks fine in the sprintf command
Request sent TEMP
{TEMP:21.56}

You have quite a few libraries there. I wonder if you're running out of RAM. Try measuring how much free memory you have: Available Memory