Jump to content
Sign in to follow this  
Guest bret lanius

redback send data to a server running php

Recommended Posts

Guest bret lanius

I need to send sensor data to my php code hosted on my server. None of the examples seem to show how to do this.

Share this post


Link to post
Share on other sites
Guest Guest

anyone? this can't be that difficult. is there at least some documentation of what commands the library supports?

Share this post


Link to post
Share on other sites

Ok so despite the lack of documentation I did finally get something working but now I need to go to the next thing and make it useful.

 

(Example code is not the same a documentation)

 

I was successful getting the Redback to call a PHP page on my server and get the response with this code (edited out secure info)

/*
 * A simple sketch that uses WiServer to get the hourly weather data from LAX and prints
 * it via the Serial API
 */

#include <WiServer.h>

#define WIRELESS_MODE_INFRA	1
#define WIRELESS_MODE_ADHOC	2

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,35};	// IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,254};	// router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0};	// subnet mask for the local network
const prog_char ssid[] PROGMEM = {"XXXXXXX"};		// max 32 bytes

unsigned char security_type = 3;	// 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"XXXXXXX"};	// max 64 characters

// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,	// Key 0
				  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	// Key 1
				  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	// Key 2
				  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00	// Key 3
				};

// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------


// Function that prints data from the server
void printData(char* data, int len) {
  
  // Print the data returned by the server
  // Note that the data is not null-terminated, may be broken up into smaller packets, and 
  // includes the HTTP header. 
  while (len-- > 0) {
    Serial.print(*(data++));
  } 
}


// IP Address for www.weather.gov  
uint8 ip[] = {198,00,00,000};

// A request that gets the latest METAR weather data for LAX
GETrequest getAlert(ip, 80, "www.myserver.com", "/wateralert.php?message=I%20see%20water%20everywhere");


void setup() {
    // Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages) 
  WiServer.init(NULL);
  
  // Enable Serial output and ask WiServer to generate log messages (optional)
  Serial.begin(57600);
  WiServer.enableVerboseMode(true);

  // Have the processData function called when data is returned by the server
  getAlert.setReturnFunc(printData);
}


// Time (in millis) when the data should be retrieved 
long updateTime = 0;

void loop(){

  // Check if it's time to get an update
  if (millis() >= updateTime) {
    getAlert.submit();    
    // Get another update one hour from now
    updateTime += 1000 * 60 * 60;
  }
  
  // Run WiServer
  WiServer.server_task();
 
  delay(10);
}

So using that as my starting point I have the following which does everything but I get either no response or I get a 404 page from the server

/*
Email not sending (could be reset is happening so quickly that it can't ))
*/

#include <WiServer.h>

#define WIRELESS_MODE_INFRA	1
#define WIRELESS_MODE_ADHOC	2

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,2};	// IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,254};	// router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0};	// subnet mask for the local network
const prog_char ssid[] PROGMEM = {"XXXXX"};		// max 32 bytes

unsigned char security_type = 3;	// 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"XXXXXXX"};	// max 64 characters

// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = {	0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,	// Key 0
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	0x00,	// Key 1
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	0x00,	// Key 2
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	0x00	// Key 3
								};

// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

unsigned char ssid_len;
unsigned char security_passphrase_len;
//---------------------------------------------------------------------------

const byte waterPin=7;
const byte redPin=A2;
const byte waterAnalogPin=0;
const byte resetPin=5;
int beenhere=false;

void printData(char* data, int len) {
  // Print the data returned by the server
  // Note that the data is not null-terminated, may be broken up into smaller packets, and 
  // includes the HTTP header. 
  while (len-- > 0) {
    Serial.print(*(data++));
  } 
}

uint8 bret_ip[]={198,00,00,000};

GETrequest getAlert(bret_ip, 80, "www.myserver.com", "/wateralert.php?&message=I%20see%20water%20everywhere");



void setup() {
  // put your setup code here, to run once:
  Serial.begin(57600);
  Serial.println("StartUp");
  WiServer.init(NULL);
  WiServer.enableVerboseMode(true);
  Serial.println("Wifi enabled");
  pinMode(waterPin,INPUT);
  pinMode(redPin,OUTPUT);

  pinMode(resetPin,INPUT_PULLUP);
  digitalWrite(redPin,LOW);
  getAlert.setReturnFunc(printData);
 
}





void loop() {
  if (beenhere==true){
    Serial.println("Back in main loop");
    beenhere=false;
  }
  int water=digitalRead(waterPin);  
  
    if (water==LOW){
      Serial.println("ALERT");
     
      alert();
 
    }

 
  delay(10);
}

void noAlert(){
  noTone(6);
  digitalWrite(redPin,LOW);
  beenhere=true;
}

/*
void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers
{
asm volatile ("  jmp 0");  
} 
*/
void reset(){
  Serial.println("Silenced");
  noTone(6);
  delay(500);
 
  while(digitalRead(resetPin)==HIGH){
    digitalWrite(redPin,HIGH);
    delay(500);
    digitalWrite(redPin,LOW);
    delay(500);
  }
  Serial.println("Clearing alarm");
  delay(100);
  //software_Reset();
  noAlert();
}

void alert(){
      tone(6,3000);
      delay(500);
      Serial.println("sending email");
      getAlert.submit();
          // Run WiServer
      WiServer.server_task();
        while(digitalRead(resetPin)==HIGH){
          digitalWrite(redPin,HIGH);
          delay(50);
          digitalWrite(redPin,LOW);
          delay(50);
        }
       reset();
   }

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×