Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- const char* ssid = "WISP";
- const char* password = "17215541";
- WiFiClient espClient;
- #include <PubSubClient.h>
- char mqtt_server[] = "52.57.119.78";
- PubSubClient client(espClient);
- void setup() {
- Serial.begin(115200);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- while (WiFi.waitForConnectResult() != WL_CONNECTED) {
- delay(1000);
- }
- client.connect("1","","");
- client.setServer(mqtt_server, 1883);
- client.setCallback(callback);
- while (!client.connected()) {
- client.connect("1","","");
- client.setServer(mqtt_server, 1883);
- client.setCallback(callback);
- if (client.connect("ksl")) { }
- else {
- delay(5000);
- }
- }
- }
- void loop() {
- String tmpValue = "";
- String msg = "";
- while(Serial.available() > 0){
- char mychar = Serial.read();
- tmpValue += mychar;
- }
- if(tmpValue != ""){
- for(int i=0;i<tmpValue.length();i++){
- msg += tmpValue.charAt(i);
- }
- Serial.println(msg);
- client.publish("hcix/tes1", String(msg).c_str());
- }
- client.subscribe("hcix/tes1");
- if(!client.connected()){
- recon();
- }
- client.loop();
- delay(50);
- }
- void recon(){
- while(!client.connected()){
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- while (WiFi.waitForConnectResult() != WL_CONNECTED) {
- delay(1000);
- }
- if(client.connect("hcix")){
- client.subscribe("hcix/tes1");
- }
- else{
- delay(1000);
- }
- }
- }
- void callback(char* topic, byte* payload, unsigned int length){
- for(int i=0;i<length;i++){
- Serial.print((char)payload[i]); //@500
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement