Skip to content

Instantly share code, notes, and snippets.

@torntrousers
Created May 2, 2016 08:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torntrousers/030a82a628c41811a3120bc4d4b87e8b to your computer and use it in GitHub Desktop.
Save torntrousers/030a82a628c41811a3120bc4d4b87e8b to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <PubSubClient.h>
//-------- Customise these values -----------
const char* homeSSID = "BTHub5-72W5";
const char* homePswd = "<homePswd>";
const char* publicSSID = "<openSSID>";
const char* publicUser = "<openUserId>";
const char* publicPswd = "<openPswd>";
#define ORG "quickstart" // your organization or "quickstart"
#define DEVICE_TYPE "ESP8266" // use this default for quickstart or customize to your registered device type
#define DEVICE_ID "Test1" // use this default for quickstart or customize to your registered device id
#define TOKEN "<yourDeviceToken>" // not used with "quickstart"
#define EVENT "myEvent" // use this default or customize to your event type
//-------- Customise the above values --------
char server[] = ORG ".messaging.internetofthings.ibmcloud.com";
char topic[] = "iot-2/evt/status/fmt/json";
char authMethod[] = "use-token-auth";
char token[] = TOKEN;
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;
ESP8266WiFiMulti wifiMulti;
WiFiClient wifiClient;
PubSubClient client(server, 1883, NULL, wifiClient);
void mqttConnect() {
if (client.connected()) return;
Serial.print("Reconnecting client to "); Serial.println(server);
while (!!!client.connect(clientId, authMethod, token)) {
Serial.print(".");
delay(500);
}
Serial.println();
}
String getTimeTag() {
HTTPClient http;
http.begin("http://9.149.245.238/redir.html");
int httpCode = http.GET();
Serial.print("HTTP GET Response: "); Serial.println(httpCode); // HTTP code 200 means ok
String s = http.getString();
http.end();
Serial.print("s:"); Serial.println(s);
int x = s.indexOf("au_pxytimetag value=");
Serial.print("x:"); Serial.println(x);
if (x != -1) {
x += 21;
int y = s.indexOf("\">", x);
String timetag = s.substring(x, y);
Serial.print("timetag:"); Serial.println(timetag);
return timetag;
}
return "";
}
void doPublicLogon() {
String timetag = getTimeTag();
if (timetag == "") return;
HTTPClient http;
http.begin("http://9.149.245.238/");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String postData = "au_pxytimetag=" + timetag + "&consent_status=accept&uname=" + publicUser + "&pwd=" + publicPswd + "&ok=OK";
int httpCode = http.POST(postData);
Serial.print("HTTP POST Response: "); Serial.println(httpCode); // HTTP code 200 means ok
http.writeToStream(&Serial);
http.end();
}
boolean wifiConnect() {
int timeout = 4 * 10; // 10 secs
while ((wifiMulti.run() != WL_CONNECTED) && timeout-- > 0) {
delay(250);
}
if (timeout == 0) {
return false;
}
if (strcmp (WiFi.SSID().c_str(), publicSSID) == 0) {
doPublicLogon();
}
Serial.print("Connected to: "); Serial.print(WiFi.SSID());
Serial.print(", IP address: "); Serial.println(WiFi.localIP());
return true;
}
void setup() {
Serial.begin(115200); Serial.println();
Serial.println("Connecting to WiFi...");
wifiMulti.addAP(homeSSID, homePswd);
wifiMulti.addAP(publicSSID);
if (!!!wifiConnect()) {;
Serial.println("*** Failed to connect to WiFi");
return;
}
Serial.println("Press the button to publish, view the data at:");
Serial.println("https://quickstart.internetofthings.ibmcloud.com/#/device/Test1/sensor/");
}
void loop() {
if (digitalRead(4) != LOW) return;
mqttConnect();
String payload = "{\"d\":{\"myName\":\"ESP8266.Test1\",\"counter\":";
payload += analogRead(A0);
payload += "}}";
if (client.publish(topic, (char*) payload.c_str())) {
// Serial.print("Publish ok: "); Serial.println(payload);
} else {
Serial.println("Publish failed");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment