Hello !
Im struggling to understand how i would go forward to be able to controll the photon from a website. No, not locally. But from a website, anywhere in the world. Im able to do it on the local network. I dont think theres many more steps to it. But how? I keep searching but i can not find anything. Im not very good at servers and stuff like that. So im probably just using wrong words in the search field. I followed a guide. Anyway, here is “my” code so far:
// This #include statement was automatically added by the Particle IDE.
#include <MDNS.h>
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_ST7735.h>
MDNS mdns;
//tft pins
#define cs A2
#define dc A1
#define rst A0
Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst); // hardware spi
#define WEBDUINO_FAVICON_DATA ""
#define WEBDUINO_FAIL_MESSAGE ""
#include "WebServer.h"
WebServer webserver("", 80);
#define POST_NAME_LENGTH 32
#define POST_VALUE_LENGTH 32
// Our "settings"
String myValA = "B";
String myValB = "hello world";
bool myValC = true;
// All pages
P(Page_start) = "<!DOCTYPE html><html><head><title>My Device</title><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\"><meta charset=\"UTF-8\"></head><body>";
P(Page_css) = "<style type=\"text/css\">html,body{font-family:sans-serif;}fieldset{margin-left:auto;margin-right:auto;max-width:480px;border-radius:8px;}</style>";
P(Page_end) = "</body></html>";
// Form-specific
P(Form_css) = "<style type=\"text/css\">p{clear:both;width:100%;line-height:1.5em;}label{width:49%;text-align:right;display:inline-block;line-height:1.5em;float:left;margin-left:-1em;}select,input[type=\"text\"]{width:49%;text-align:left;float:right;}#c{text-align:left;float:left;margin-left:2em;}</style>";
P(Form_settings) = "<form method=\"POST\" action=\"save.html\"><fieldset><legend>MyDevice Config</legend><p><label for=\"a\">First Setting</label><select id=\"a\" name=\"a\"><option value=\"r\">Red</option><option value=\"g\">Green</option><option value=\"B\">Blue</option></select></p><p><label for=\"b\">Second Setting</label><input type=\"text\" id=\"b\" name=\"b\"></p><p><label for=\"c\">Third Setting</label><input type=\"checkbox\" id=\"c\" name=\"c\"></p><p> </p><p><label for=\"s\"> </label><input type=\"submit\" id=\"s\" value=\"Save\"></p></fieldset>";
P(Form_javascript1) = "<script type=\"text/javascript\">";
P(Form_javascript2) = "window.onload=function(){$a=document.querySelector('#a');$b=document.querySelector('#b');$c=document.querySelector('#c');$a.value=a;$b.value=b;if(c==1)$c.checked=\"checked\";}";
P(Form_javascript3) = "</script>";
// Save page
P(Save_fieldset) = "<fieldset><legend>MyDevice Config</legend><p>Your settings have been saved. You will be redirected in 5 seconds, or click <a href=\"/\">here</a> to continue.</p></fieldset>";
P(Save_redirect) = "<meta http-equiv=\"refresh\" content=\"5;URL=/\">";
// Fail page
P(Fail_message) = "<p>Not here. Go away.</p><p>ಠ_ಠ</p>";
// index.html
void web_index(WebServer &server, WebServer::ConnectionType type, char *, bool) {
server.httpSuccess();
server.printP(Page_start);
server.printP(Form_settings);
server.printP(Page_css);
server.printP(Form_css);
server.printP(Form_javascript1);
server.printP("var a=\""+String(myValA)+"\";");
server.printP("var b=\""+myValB+"\";");
server.printP("var c="+String(myValC ? 1 : 0)+";");
server.printP(Form_javascript2);
server.printP(Form_javascript3);
server.printP(Page_end);
}
// save.html
void web_save(WebServer &server, WebServer::ConnectionType type, char *, bool) {
URLPARAM_RESULT rc;
char name[POST_NAME_LENGTH];
char value[POST_VALUE_LENGTH];
server.httpSuccess();
server.printP(Page_start);
server.printP(Save_fieldset);
server.printP(Save_redirect);
server.printP(Page_css);
server.printP(Page_end);
bool c = 0;
// Loop through POSTed data
while(server.readPOSTparam(name, POST_NAME_LENGTH, value, POST_VALUE_LENGTH)) {
// Because strings are easier to test/manipulate
String _name = String(name);
String _value = String(value);
if(_name.equals("a"))
myValA = _value;
else if(_name.equals("b"))
myValB = _value;
else if(_name.equals("c"))
c = 1;
}
myValC = c;
}
// Bad requests
void web_fail(WebServer &server, WebServer::ConnectionType type, char *, bool) {
server.httpFail();
server.printP(Page_start);
server.printP(Fail_message);
server.printP(Page_end);
}
void setup() {
bool mdns_success = mdns.setHostname("mydevice");
if(mdns_success) {
mdns.addService("tcp", "http", 80, "MyDevice Config");
mdns.begin();
}
webserver.setDefaultCommand(&web_index);
webserver.setFailureCommand(&web_fail);
webserver.addCommand("index.html", &web_index);
webserver.addCommand("save.html", &web_save);
webserver.begin();
//TFT-skjerm
tft.initR( INITR_GREENTAB );
tft.fillScreen(ST7735_BLACK);
tft.setCursor(0, 0);
tft.setTextColor(ST7735_WHITE);
tft.setTextWrap(true);
tft.print("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur adipiscing ante sed nibh tincidunt feugiat. Maecenas enim massa, fringilla");
tft.drawLine(0, 0, tft.width()-1, tft.height()-1, ST7735_YELLOW);
tft.drawLine(tft.width()-1, 0, 0, tft.height()-1, ST7735_YELLOW);
tft.drawPixel(0, tft.height()/2, ST7735_GREEN);
}
void loop() {
mdns.processQueries();
char web_buff[64];
int web_len = 64;
webserver.processConnection(web_buff, &web_len);
updateText();
}
void updateText() {
delay(100);
int wut = 1;
tft.setCursor(80, wut);
tft.fillRect( 78, 14, 28, 22, ST7735_BLACK);
tft.print(myValB);
wut++;
}
Ill say thank you in advance for any answer Please help.