Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include <RH_ASK.h>
- #include <SPI.h>
- #include <Arduino.h>
- #include <U8x8lib.h>
- #ifdef U8X8_HAVE_HW_SPI
- #endif
- U8X8_SH1106_128X64_NONAME_4W_HW_SPI u8x8(/* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
- long lastMsg;
- int value;
- char* msg;
- uint8_t buf[16];
- uint8_t buflen = sizeof(buf);
- String str_temp;
- String str_humid;
- char *hum;
- char *temp;
- RH_ASK driver(2000, 12, 4, 0);
- const char *ssid = "AndroidAP";
- const char *password = "52105210";
- //const char* ssid = "Nackademin-IOT";
- //const char* password = "Nack!@pr";
- const char* mqtt_server = "192.168.43.252";
- String clientId = "ESP8266Client-feather";
- const char subscribeTopic[] = "iot/subscribe";
- const char publishTopic[] = "iot/publish";
- WiFiClient espClient;
- PubSubClient client(espClient);
- void initWifi() {
- delay(10);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- randomSeed(micros());
- Serial.println(WiFi.localIP());
- }
- void reconnect() {
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- if (client.connect(clientId.c_str())) {
- Serial.println("connected to MQTT Broker");
- client.subscribe(subscribeTopic);
- } else {
- delay(5000);
- }
- }
- }
- void callback(char* topic, byte* payload, unsigned int length) {
- for (int i = 0; i < length; i++) {
- Serial.print((char)payload[i]);
- }
- Serial.println();
- }
- void setup() {
- Serial.begin(115200);
- driver.init();
- initWifi();
- client.setServer(mqtt_server, 1883);
- client.setCallback(callback);
- u8x8.begin();
- u8x8.setPowerSave(0);
- }
- void loop() {
- if (driver.recv(buf, &buflen)) {
- String str_out = String((char*)buf);
- for (int i = 0; i < str_out.length(); i++) {
- if (str_out.substring(i, i + 1) == ",") {
- str_humid = str_out.substring(i + 1);
- str_temp = str_out.substring(0, i);
- break;
- }
- }
- Serial.print("Temperature: ");
- Serial.println(str_temp);
- Serial.print("Humidity: ");
- Serial.println(str_humid);
- }
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- str_humid.toCharArray(hum,str_humid.length());
- str_temp.toCharArray(temp,str_temp.length());
- long now = millis();
- if (now - lastMsg > 2000) {
- lastMsg = now;
- ++value;
- snprintf (msg, 50, "Hej hej #%ld", value);
- client.publish(publishTopic, msg);
- client.publish(publishTopic, hum);
- client.publish(publishTopic, temp);
- }
- u8x8.setFont(u8x8_font_chroma48medium8_r);
- u8x8.drawString(0,0,"Hello World!");
- //u8x8.refreshDisplay(); // only required for SSD1606/7
- delay(2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement