Easiest way to directly communicate to core on same network with pc or android

I wrote a simple tcp server core program. However, it is very limited. It seems it can only handle a single device connected to it like a phone, but not two phones. Also I was only able to display the core values but did not know how to set them using the same browser interface. Here’s a screen shot and my code. So I need help on how to get thisto work or do I need to do this totally different.
The core is using semi-automatic mode.
Thanks again folks

/* Includes ------------------------------------------------------------------*/
#include "application.h"
#include "spark-dallas-temperature.h"
#include "OneWire.h"
#define ONE_WIRE_BUS 2
#define BUTTON7 6
#define MISTER1_PIN 7
   

/* Function prototypes -------------------------------------------------------*/
int temperatureRead(int probe);


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

float temperature = 1.0;
char temperStr[460];
char temperStr24[460];
char currentStr[200];
char timeStr[ ] = "00:00:00";
char dayStr[ ] = "00/00/0000";
unsigned xx = 0;

char curTemper = 0;
int temper1Index = 0;

 
// Tracks the last time event fired
unsigned long previousMillis1 = 0;
unsigned long currentMillis;
unsigned long prevTempMills1 = 0;
unsigned long prevTmpMillHr = 0;

// Interval is how long we wait
unsigned long interval1;

boolean button7State = HIGH;  //storage for current button state

SYSTEM_MODE(SEMI_AUTOMATIC);

TCPServer server = TCPServer(80);
TCPClient client;

/* This function is called once at start up ----------------------------------*/
void setup()
{
	WiFi.on();
	WiFi.connect();
	WiFi.ready();
	while(!WiFi.ready()) SPARK_WLAN_Loop();



	// Time.zone(-8); // PST time zone

 sensor.begin();
 sensor.setResolution(12);

 pinMode(MISTER1_PIN, OUTPUT);
 digitalWrite(MISTER1_PIN, LOW);
 interval1 = mist1Ontime;

 pinMode(BUTTON7, INPUT);


 // TCP start listening for clients
 server.begin();

 // Make sure your Serial Terminal app is closed before powering your Core
 Serial.begin(9600);
 // Now open your Serial Terminal, and hit any key to continue!
 //while(!Serial.available()) SPARK_WLAN_Loop();

 Serial.println(WiFi.localIP());
 Serial.println(WiFi.subnetMask());
 Serial.println(WiFi.gatewayIP());
 Serial.println(WiFi.SSID());

}

/* This function loops forever --------------------------------------------*/
void loop() {
	currentMillis = millis(); // capture current mills for mist timers

	//check every 5 seconds
	if ((unsigned long)(currentMillis - prevTempMills1) >= (unsigned long)5000) {
	  prevTempMills1 = currentMillis;

    if (button7State != digitalRead(BUTTON7)) {
			button7State = digitalRead(BUTTON7);
			if (button7State == LOW) Spark.connect();
			SPARK_WLAN_Loop();
			if (Serial.available()) {
				Serial.println(WiFi.localIP());
				Serial.println(WiFi.subnetMask());
				Serial.println(WiFi.gatewayIP());
				Serial.println(WiFi.SSID());
			}
		}
		button7State = digitalRead(BUTTON7);
    digitalWrite(MISTER1_PIN, button7State); // Set nozzle1 to state -LED

		// listen for incoming clients
    client = server.available();
    if (client) {
        Serial.println("new client");
        // an http request ends with a blank line
        boolean currentLineIsBlank = true;
        while (client.connected()) {
				    SPARK_WLAN_Loop();
            if (client.available()) {
                char c = client.read();
                Serial.write(c);
                // if you've gotten to the end of the line (received a newline
                // character) and the line is blank, the http request has ended,
                // so you can send a reply
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");  // the connection will be closed after completion of the response
                    client.println("Refresh: 15");  // refresh the page automatically every 10 sec
                    client.println();
                    client.println("<!DOCTYPE HTML><html><head><title>Aeroponics Dashboard</title><style> .sensor-blk {width:47\%; font-family: Arial; color: \#ffffff; font-size: 20px; background:\#3498db; padding: 10px 10px 10px 10px; text-decoration: none;-moz-border-radius: 10px;-webkit-border-radius:10px;border-radius: 10px;} </style></head><body><button class=\"sensor-blk\" style=\"background:\#FF0000;\">Temperature<br><h2>");
										client.print(temperatureRead(1));
										client.println("&deg;F</h2></button><button class=\"sensor-blk\">Humidity<br><h2>97\%</h2></button><button class=\"sensor-blk\" style=\"background:\#CCCC00;\">Light<br><h2>1099</h2></button><button class=\"sensor-blk\" style=\"background:\#CC6600;\">Root Temp<br><h2>68&deg;F</h2></button>");
                  //  client.print("The current time is: <B>");
                  //  client.print(Time.timeStr());
                  //  client.println("</B>");

                    // output the value of each analog input pin
                    /*for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
                    int sensorReading = analogRead(analogChannel);
                    client.print("analog input ");
                    client.print(analogChannel);
                    client.print(" is ");
                    client.print(sensorReading);
                    client.println("<br />");
                    }*/
                    client.println("</body></html>");
                    client.println();
                    delay(10);
                    break;
                }
                if (c == '\n') {
                    // you're starting a new line
                    currentLineIsBlank = true;
                }
                else if (c != '\r') {
                    // you've gotten a character on the current line
                    currentLineIsBlank = false;
                }
            }
        }
        // give the web browser time to receive the data
        delay(10);
        // close the connection:
        client.stop();
        Serial.println("client disconnected");
    }
	}
}

/*******************************************************************************
* Function Name  : temperatureRead
* Description    : Reads temperature
* Return         :
*******************************************************************************/
int temperatureRead(int probe)
{
	int retTemper;
	sensor.requestTemperatures();
  retTemper = sensor.getTempFByIndex(0);
  //sprintf(myStr,"%.3f",temperature);
	return retTemper;
}
1 Like

@applefarm, nice work. TCPServer opens a single socket on the cc3000 so it will only support a single connection at a time. Perhaps the only way to do what you want is to run a local server which could subscribe to events published on the core, fetch it from Spark.variables or via your TCPServer. The server would then republish the aggregated data on a web page. The phones/devices would connect to that server instead of the cores directly. @bko would really know this! :stuck_out_tongue:

3 Likes

ok lets say i can do just a single connection, how do write data to core with this method?

@applefarm, you really need to read the documentation. There are many ways:

TCPServer
Spark Local Cloud Server

Also, use the Community search function and discover!

1 Like